blob: d0c7d97769c36070b081d04d236529ae03f7d7ee [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -07002 * Copyright (C) 2005 - 2011 Emulex
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070013 * linux-drivers@emulex.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070015 * Emulex
16 * 3333 Susan Street
17 * Costa Mesa, CA 92626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018 */
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070019
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053020#include <linux/reboot.h>
21#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053023#include <linux/interrupt.h>
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/semaphore.h>
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +053029#include <linux/iscsi_boot_sysfs.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040030#include <linux/module.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053031
32#include <scsi/libiscsi.h>
33#include <scsi/scsi_transport_iscsi.h>
34#include <scsi/scsi_transport.h>
35#include <scsi/scsi_cmnd.h>
36#include <scsi/scsi_device.h>
37#include <scsi/scsi_host.h>
38#include <scsi/scsi.h>
39#include "be_main.h"
40#include "be_iscsi.h"
41#include "be_mgmt.h"
42
43static unsigned int be_iopoll_budget = 10;
44static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053045static unsigned int enable_msix = 1;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +053046static unsigned int gcrashmode = 0;
47static unsigned int num_hba = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053048
49MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
50MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050051MODULE_VERSION(BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050052MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053053MODULE_LICENSE("GPL");
54module_param(be_iopoll_budget, int, 0);
55module_param(enable_msix, int, 0);
56module_param(be_max_phys_size, uint, S_IRUGO);
57MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
58 "contiguous memory that can be allocated."
59 "Range is 16 - 128");
60
61static int beiscsi_slave_configure(struct scsi_device *sdev)
62{
63 blk_queue_max_segment_size(sdev->request_queue, 65536);
64 return 0;
65}
66
Jayamohan Kallickal41831222010-02-20 08:02:39 +053067static int beiscsi_eh_abort(struct scsi_cmnd *sc)
68{
69 struct iscsi_cls_session *cls_session;
70 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
71 struct beiscsi_io_task *aborted_io_task;
72 struct iscsi_conn *conn;
73 struct beiscsi_conn *beiscsi_conn;
74 struct beiscsi_hba *phba;
75 struct iscsi_session *session;
76 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +053077 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +053078 unsigned int cid, tag, num_invalidate;
79
80 cls_session = starget_to_session(scsi_target(sc->device));
81 session = cls_session->dd_data;
82
83 spin_lock_bh(&session->lock);
84 if (!aborted_task || !aborted_task->sc) {
85 /* we raced */
86 spin_unlock_bh(&session->lock);
87 return SUCCESS;
88 }
89
90 aborted_io_task = aborted_task->dd_data;
91 if (!aborted_io_task->scsi_cmnd) {
92 /* raced or invalid command */
93 spin_unlock_bh(&session->lock);
94 return SUCCESS;
95 }
96 spin_unlock_bh(&session->lock);
97 conn = aborted_task->conn;
98 beiscsi_conn = conn->dd_data;
99 phba = beiscsi_conn->phba;
100
101 /* invalidate iocb */
102 cid = beiscsi_conn->beiscsi_conn_cid;
103 inv_tbl = phba->inv_tbl;
104 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
105 inv_tbl->cid = cid;
106 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
107 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530108 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
109 sizeof(struct invalidate_commands_params_in),
110 &nonemb_cmd.dma);
111 if (nonemb_cmd.va == NULL) {
112 SE_DEBUG(DBG_LVL_1,
113 "Failed to allocate memory for"
114 "mgmt_invalidate_icds\n");
115 return FAILED;
116 }
117 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
118
119 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
120 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530121 if (!tag) {
122 shost_printk(KERN_WARNING, phba->shost,
123 "mgmt_invalidate_icds could not be"
124 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530125 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
126 nonemb_cmd.va, nonemb_cmd.dma);
127
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530128 return FAILED;
129 } else {
130 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
131 phba->ctrl.mcc_numtag[tag]);
132 free_mcc_tag(&phba->ctrl, tag);
133 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530134 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
135 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530136 return iscsi_eh_abort(sc);
137}
138
139static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
140{
141 struct iscsi_task *abrt_task;
142 struct beiscsi_io_task *abrt_io_task;
143 struct iscsi_conn *conn;
144 struct beiscsi_conn *beiscsi_conn;
145 struct beiscsi_hba *phba;
146 struct iscsi_session *session;
147 struct iscsi_cls_session *cls_session;
148 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530149 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530150 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530151
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);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500156 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
157 spin_unlock_bh(&session->lock);
158 return FAILED;
159 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530160 conn = session->leadconn;
161 beiscsi_conn = conn->dd_data;
162 phba = beiscsi_conn->phba;
163 cid = beiscsi_conn->beiscsi_conn_cid;
164 inv_tbl = phba->inv_tbl;
165 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
166 num_invalidate = 0;
167 for (i = 0; i < conn->session->cmds_max; i++) {
168 abrt_task = conn->session->cmds[i];
169 abrt_io_task = abrt_task->dd_data;
170 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
171 continue;
172
173 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
174 continue;
175
176 inv_tbl->cid = cid;
177 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
178 num_invalidate++;
179 inv_tbl++;
180 }
181 spin_unlock_bh(&session->lock);
182 inv_tbl = phba->inv_tbl;
183
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530184 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
185 sizeof(struct invalidate_commands_params_in),
186 &nonemb_cmd.dma);
187 if (nonemb_cmd.va == NULL) {
188 SE_DEBUG(DBG_LVL_1,
189 "Failed to allocate memory for"
190 "mgmt_invalidate_icds\n");
191 return FAILED;
192 }
193 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
194 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
195 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
196 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530197 if (!tag) {
198 shost_printk(KERN_WARNING, phba->shost,
199 "mgmt_invalidate_icds could not be"
200 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530201 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
202 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530203 return FAILED;
204 } else {
205 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
206 phba->ctrl.mcc_numtag[tag]);
207 free_mcc_tag(&phba->ctrl, tag);
208 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530209 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
210 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530211 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530212}
213
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530214static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
215{
216 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500217 struct mgmt_session_info *boot_sess = &phba->boot_sess;
218 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530219 char *str = buf;
220 int rc;
221
222 switch (type) {
223 case ISCSI_BOOT_TGT_NAME:
224 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500225 (int)strlen(boot_sess->target_name),
226 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530227 break;
228 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500229 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530230 rc = sprintf(buf, "%pI4\n",
Mike Christief457a462011-06-24 15:11:53 -0500231 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530232 else
233 rc = sprintf(str, "%pI6\n",
Mike Christief457a462011-06-24 15:11:53 -0500234 (char *)&boot_conn->dest_ipaddr.ip_address);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530235 break;
236 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500237 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530238 break;
239
240 case ISCSI_BOOT_TGT_CHAP_NAME:
241 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500242 boot_conn->negotiated_login_options.auth_data.chap.
243 target_chap_name_length,
244 (char *)&boot_conn->negotiated_login_options.
245 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530246 break;
247 case ISCSI_BOOT_TGT_CHAP_SECRET:
248 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500249 boot_conn->negotiated_login_options.auth_data.chap.
250 target_secret_length,
251 (char *)&boot_conn->negotiated_login_options.
252 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530253 break;
254 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
255 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500256 boot_conn->negotiated_login_options.auth_data.chap.
257 intr_chap_name_length,
258 (char *)&boot_conn->negotiated_login_options.
259 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530260 break;
261 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500262 rc = sprintf(str, "%.*s\n",
263 boot_conn->negotiated_login_options.auth_data.chap.
264 intr_secret_length,
265 (char *)&boot_conn->negotiated_login_options.
266 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530267 break;
268 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500269 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530270 break;
271 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500272 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530273 break;
274 default:
275 rc = -ENOSYS;
276 break;
277 }
278 return rc;
279}
280
281static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
282{
283 struct beiscsi_hba *phba = data;
284 char *str = buf;
285 int rc;
286
287 switch (type) {
288 case ISCSI_BOOT_INI_INITIATOR_NAME:
289 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
290 break;
291 default:
292 rc = -ENOSYS;
293 break;
294 }
295 return rc;
296}
297
298static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
299{
300 struct beiscsi_hba *phba = data;
301 char *str = buf;
302 int rc;
303
304 switch (type) {
305 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500306 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530307 break;
308 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500309 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530310 break;
311 case ISCSI_BOOT_ETH_MAC:
312 rc = beiscsi_get_macaddr(buf, phba);
313 if (rc < 0) {
314 SE_DEBUG(DBG_LVL_1, "beiscsi_get_macaddr Failed\n");
315 return rc;
316 }
317 break;
318 default:
319 rc = -ENOSYS;
320 break;
321 }
322 return rc;
323}
324
325
Al Viro587a1f12011-07-23 23:11:19 -0400326static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530327{
Al Viro587a1f12011-07-23 23:11:19 -0400328 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530329
330 switch (type) {
331 case ISCSI_BOOT_TGT_NAME:
332 case ISCSI_BOOT_TGT_IP_ADDR:
333 case ISCSI_BOOT_TGT_PORT:
334 case ISCSI_BOOT_TGT_CHAP_NAME:
335 case ISCSI_BOOT_TGT_CHAP_SECRET:
336 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
337 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
338 case ISCSI_BOOT_TGT_NIC_ASSOC:
339 case ISCSI_BOOT_TGT_FLAGS:
340 rc = S_IRUGO;
341 break;
342 default:
343 rc = 0;
344 break;
345 }
346 return rc;
347}
348
Al Viro587a1f12011-07-23 23:11:19 -0400349static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530350{
Al Viro587a1f12011-07-23 23:11:19 -0400351 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530352
353 switch (type) {
354 case ISCSI_BOOT_INI_INITIATOR_NAME:
355 rc = S_IRUGO;
356 break;
357 default:
358 rc = 0;
359 break;
360 }
361 return rc;
362}
363
364
Al Viro587a1f12011-07-23 23:11:19 -0400365static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530366{
Al Viro587a1f12011-07-23 23:11:19 -0400367 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530368
369 switch (type) {
370 case ISCSI_BOOT_ETH_FLAGS:
371 case ISCSI_BOOT_ETH_MAC:
372 case ISCSI_BOOT_ETH_INDEX:
373 rc = S_IRUGO;
374 break;
375 default:
376 rc = 0;
377 break;
378 }
379 return rc;
380}
381
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530382/*------------------- PCI Driver operations and data ----------------- */
383static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
384 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530385 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530386 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
387 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
388 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530389 { 0 }
390};
391MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
392
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530393static struct scsi_host_template beiscsi_sht = {
394 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500395 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530396 .proc_name = DRV_NAME,
397 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530398 .change_queue_depth = iscsi_change_queue_depth,
399 .slave_configure = beiscsi_slave_configure,
400 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530401 .eh_abort_handler = beiscsi_eh_abort,
402 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530403 .eh_target_reset_handler = iscsi_eh_session_reset,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530404 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
405 .can_queue = BE2_IO_DEPTH,
406 .this_id = -1,
407 .max_sectors = BEISCSI_MAX_SECTORS,
408 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
409 .use_clustering = ENABLE_CLUSTERING,
410};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530411
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530412static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530413
414static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
415{
416 struct beiscsi_hba *phba;
417 struct Scsi_Host *shost;
418
419 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
420 if (!shost) {
421 dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530422 "iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530423 return NULL;
424 }
425 shost->dma_boundary = pcidev->dma_mask;
426 shost->max_id = BE2_MAX_SESSIONS;
427 shost->max_channel = 0;
428 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
429 shost->max_lun = BEISCSI_NUM_MAX_LUN;
430 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530431 phba = iscsi_host_priv(shost);
432 memset(phba, 0, sizeof(*phba));
433 phba->shost = shost;
434 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530435 pci_set_drvdata(pcidev, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530436
437 if (iscsi_host_add(shost, &phba->pcidev->dev))
438 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530439
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530440 return phba;
441
442free_devices:
443 pci_dev_put(phba->pcidev);
444 iscsi_host_free(phba->shost);
445 return NULL;
446}
447
448static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
449{
450 if (phba->csr_va) {
451 iounmap(phba->csr_va);
452 phba->csr_va = NULL;
453 }
454 if (phba->db_va) {
455 iounmap(phba->db_va);
456 phba->db_va = NULL;
457 }
458 if (phba->pci_va) {
459 iounmap(phba->pci_va);
460 phba->pci_va = NULL;
461 }
462}
463
464static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
465 struct pci_dev *pcidev)
466{
467 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530468 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530469
470 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
471 pci_resource_len(pcidev, 2));
472 if (addr == NULL)
473 return -ENOMEM;
474 phba->ctrl.csr = addr;
475 phba->csr_va = addr;
476 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
477
478 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
479 if (addr == NULL)
480 goto pci_map_err;
481 phba->ctrl.db = addr;
482 phba->db_va = addr;
483 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
484
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530485 if (phba->generation == BE_GEN2)
486 pcicfg_reg = 1;
487 else
488 pcicfg_reg = 0;
489
490 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
491 pci_resource_len(pcidev, pcicfg_reg));
492
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530493 if (addr == NULL)
494 goto pci_map_err;
495 phba->ctrl.pcicfg = addr;
496 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530497 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530498 return 0;
499
500pci_map_err:
501 beiscsi_unmap_pci_function(phba);
502 return -ENOMEM;
503}
504
505static int beiscsi_enable_pci(struct pci_dev *pcidev)
506{
507 int ret;
508
509 ret = pci_enable_device(pcidev);
510 if (ret) {
511 dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
512 "failed. Returning -ENODEV\n");
513 return ret;
514 }
515
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530516 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530517 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
518 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
519 if (ret) {
520 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
521 pci_disable_device(pcidev);
522 return ret;
523 }
524 }
525 return 0;
526}
527
528static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
529{
530 struct be_ctrl_info *ctrl = &phba->ctrl;
531 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
532 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
533 int status = 0;
534
535 ctrl->pdev = pdev;
536 status = beiscsi_map_pci_bars(phba, pdev);
537 if (status)
538 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530539 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
540 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
541 mbox_mem_alloc->size,
542 &mbox_mem_alloc->dma);
543 if (!mbox_mem_alloc->va) {
544 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500545 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530546 }
547
548 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
549 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
550 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
551 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
552 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530553 spin_lock_init(&phba->ctrl.mcc_lock);
554 spin_lock_init(&phba->ctrl.mcc_cq_lock);
555
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530556 return status;
557}
558
559static void beiscsi_get_params(struct beiscsi_hba *phba)
560{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530561 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
562 - (phba->fw_config.iscsi_cid_count
563 + BE2_TMFS
564 + BE2_NOPOUT_REQ));
565 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530566 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700567 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530568 phba->params.num_sge_per_io = BE2_SGE;
569 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
570 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
571 phba->params.eq_timer = 64;
572 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530573 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
574 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530575 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
576 ? 1024 : phba->params.num_eq_entries;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530577 SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d\n",
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530578 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530579 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530580 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
581 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530582 phba->params.wrbs_per_cxn = 256;
583}
584
585static void hwi_ring_eq_db(struct beiscsi_hba *phba,
586 unsigned int id, unsigned int clr_interrupt,
587 unsigned int num_processed,
588 unsigned char rearm, unsigned char event)
589{
590 u32 val = 0;
591 val |= id & DB_EQ_RING_ID_MASK;
592 if (rearm)
593 val |= 1 << DB_EQ_REARM_SHIFT;
594 if (clr_interrupt)
595 val |= 1 << DB_EQ_CLR_SHIFT;
596 if (event)
597 val |= 1 << DB_EQ_EVNT_SHIFT;
598 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
599 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
600}
601
602/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530603 * be_isr_mcc - The isr routine of the driver.
604 * @irq: Not used
605 * @dev_id: Pointer to host adapter structure
606 */
607static irqreturn_t be_isr_mcc(int irq, void *dev_id)
608{
609 struct beiscsi_hba *phba;
610 struct be_eq_entry *eqe = NULL;
611 struct be_queue_info *eq;
612 struct be_queue_info *mcc;
613 unsigned int num_eq_processed;
614 struct be_eq_obj *pbe_eq;
615 unsigned long flags;
616
617 pbe_eq = dev_id;
618 eq = &pbe_eq->q;
619 phba = pbe_eq->phba;
620 mcc = &phba->ctrl.mcc_obj.cq;
621 eqe = queue_tail_node(eq);
622 if (!eqe)
623 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
624
625 num_eq_processed = 0;
626
627 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
628 & EQE_VALID_MASK) {
629 if (((eqe->dw[offsetof(struct amap_eq_entry,
630 resource_id) / 32] &
631 EQE_RESID_MASK) >> 16) == mcc->id) {
632 spin_lock_irqsave(&phba->isr_lock, flags);
633 phba->todo_mcc_cq = 1;
634 spin_unlock_irqrestore(&phba->isr_lock, flags);
635 }
636 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
637 queue_tail_inc(eq);
638 eqe = queue_tail_node(eq);
639 num_eq_processed++;
640 }
641 if (phba->todo_mcc_cq)
642 queue_work(phba->wq, &phba->work_cqs);
643 if (num_eq_processed)
644 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
645
646 return IRQ_HANDLED;
647}
648
649/**
650 * be_isr_msix - The isr routine of the driver.
651 * @irq: Not used
652 * @dev_id: Pointer to host adapter structure
653 */
654static irqreturn_t be_isr_msix(int irq, void *dev_id)
655{
656 struct beiscsi_hba *phba;
657 struct be_eq_entry *eqe = NULL;
658 struct be_queue_info *eq;
659 struct be_queue_info *cq;
660 unsigned int num_eq_processed;
661 struct be_eq_obj *pbe_eq;
662 unsigned long flags;
663
664 pbe_eq = dev_id;
665 eq = &pbe_eq->q;
666 cq = pbe_eq->cq;
667 eqe = queue_tail_node(eq);
668 if (!eqe)
669 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
670
671 phba = pbe_eq->phba;
672 num_eq_processed = 0;
673 if (blk_iopoll_enabled) {
674 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
675 & EQE_VALID_MASK) {
676 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
677 blk_iopoll_sched(&pbe_eq->iopoll);
678
679 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
680 queue_tail_inc(eq);
681 eqe = queue_tail_node(eq);
682 num_eq_processed++;
683 }
684 if (num_eq_processed)
685 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
686
687 return IRQ_HANDLED;
688 } else {
689 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
690 & EQE_VALID_MASK) {
691 spin_lock_irqsave(&phba->isr_lock, flags);
692 phba->todo_cq = 1;
693 spin_unlock_irqrestore(&phba->isr_lock, flags);
694 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
695 queue_tail_inc(eq);
696 eqe = queue_tail_node(eq);
697 num_eq_processed++;
698 }
699 if (phba->todo_cq)
700 queue_work(phba->wq, &phba->work_cqs);
701
702 if (num_eq_processed)
703 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
704
705 return IRQ_HANDLED;
706 }
707}
708
709/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530710 * be_isr - The isr routine of the driver.
711 * @irq: Not used
712 * @dev_id: Pointer to host adapter structure
713 */
714static irqreturn_t be_isr(int irq, void *dev_id)
715{
716 struct beiscsi_hba *phba;
717 struct hwi_controller *phwi_ctrlr;
718 struct hwi_context_memory *phwi_context;
719 struct be_eq_entry *eqe = NULL;
720 struct be_queue_info *eq;
721 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530722 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530723 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530724 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530725 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530726 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530727 int isr;
728
729 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700730 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530731 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
732 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
733 if (!isr)
734 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530735
736 phwi_ctrlr = phba->phwi_ctrlr;
737 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530738 pbe_eq = &phwi_context->be_eq[0];
739
740 eq = &phwi_context->be_eq[0].q;
741 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530742 index = 0;
743 eqe = queue_tail_node(eq);
744 if (!eqe)
745 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
746
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530747 num_ioeq_processed = 0;
748 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530749 if (blk_iopoll_enabled) {
750 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
751 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530752 if (((eqe->dw[offsetof(struct amap_eq_entry,
753 resource_id) / 32] &
754 EQE_RESID_MASK) >> 16) == mcc->id) {
755 spin_lock_irqsave(&phba->isr_lock, flags);
756 phba->todo_mcc_cq = 1;
757 spin_unlock_irqrestore(&phba->isr_lock, flags);
758 num_mcceq_processed++;
759 } else {
760 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
761 blk_iopoll_sched(&pbe_eq->iopoll);
762 num_ioeq_processed++;
763 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530764 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
765 queue_tail_inc(eq);
766 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530767 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530768 if (num_ioeq_processed || num_mcceq_processed) {
769 if (phba->todo_mcc_cq)
770 queue_work(phba->wq, &phba->work_cqs);
771
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530772 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530773 hwi_ring_eq_db(phba, eq->id, 0,
774 (num_ioeq_processed +
775 num_mcceq_processed) , 1, 1);
776 else
777 hwi_ring_eq_db(phba, eq->id, 0,
778 (num_ioeq_processed +
779 num_mcceq_processed), 0, 1);
780
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530781 return IRQ_HANDLED;
782 } else
783 return IRQ_NONE;
784 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530785 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530786 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
787 & EQE_VALID_MASK) {
788
789 if (((eqe->dw[offsetof(struct amap_eq_entry,
790 resource_id) / 32] &
791 EQE_RESID_MASK) >> 16) != cq->id) {
792 spin_lock_irqsave(&phba->isr_lock, flags);
793 phba->todo_mcc_cq = 1;
794 spin_unlock_irqrestore(&phba->isr_lock, flags);
795 } else {
796 spin_lock_irqsave(&phba->isr_lock, flags);
797 phba->todo_cq = 1;
798 spin_unlock_irqrestore(&phba->isr_lock, flags);
799 }
800 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
801 queue_tail_inc(eq);
802 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530803 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530804 }
805 if (phba->todo_cq || phba->todo_mcc_cq)
806 queue_work(phba->wq, &phba->work_cqs);
807
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530808 if (num_ioeq_processed) {
809 hwi_ring_eq_db(phba, eq->id, 0,
810 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530811 return IRQ_HANDLED;
812 } else
813 return IRQ_NONE;
814 }
815}
816
817static int beiscsi_init_irqs(struct beiscsi_hba *phba)
818{
819 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530820 struct hwi_controller *phwi_ctrlr;
821 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530822 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530823
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530824 phwi_ctrlr = phba->phwi_ctrlr;
825 phwi_context = phwi_ctrlr->phwi_ctxt;
826
827 if (phba->msix_enabled) {
828 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700829 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
830 GFP_KERNEL);
831 if (!phba->msi_name[i]) {
832 ret = -ENOMEM;
833 goto free_msix_irqs;
834 }
835
836 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
837 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530838 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700839 ret = request_irq(msix_vec, be_isr_msix, 0,
840 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530841 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530842 if (ret) {
843 shost_printk(KERN_ERR, phba->shost,
844 "beiscsi_init_irqs-Failed to"
845 "register msix for i = %d\n", i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700846 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530847 goto free_msix_irqs;
848 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530849 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700850 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
851 if (!phba->msi_name[i]) {
852 ret = -ENOMEM;
853 goto free_msix_irqs;
854 }
855 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
856 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530857 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700858 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530860 if (ret) {
861 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
862 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700863 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530864 goto free_msix_irqs;
865 }
866
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530867 } else {
868 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
869 "beiscsi", phba);
870 if (ret) {
871 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
872 "Failed to register irq\\n");
873 return ret;
874 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530875 }
876 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530877free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700878 for (j = i - 1; j >= 0; j--) {
879 kfree(phba->msi_name[j]);
880 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530881 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700882 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530883 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530884}
885
886static void hwi_ring_cq_db(struct beiscsi_hba *phba,
887 unsigned int id, unsigned int num_processed,
888 unsigned char rearm, unsigned char event)
889{
890 u32 val = 0;
891 val |= id & DB_CQ_RING_ID_MASK;
892 if (rearm)
893 val |= 1 << DB_CQ_REARM_SHIFT;
894 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
895 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
896}
897
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530898static unsigned int
899beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
900 struct beiscsi_hba *phba,
901 unsigned short cid,
902 struct pdu_base *ppdu,
903 unsigned long pdu_len,
904 void *pbuffer, unsigned long buf_len)
905{
906 struct iscsi_conn *conn = beiscsi_conn->conn;
907 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530908 struct iscsi_task *task;
909 struct beiscsi_io_task *io_task;
910 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530911
912 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
913 PDUBASE_OPCODE_MASK) {
914 case ISCSI_OP_NOOP_IN:
915 pbuffer = NULL;
916 buf_len = 0;
917 break;
918 case ISCSI_OP_ASYNC_EVENT:
919 break;
920 case ISCSI_OP_REJECT:
921 WARN_ON(!pbuffer);
922 WARN_ON(!(buf_len == 48));
923 SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
924 break;
925 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530926 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530927 task = conn->login_task;
928 io_task = task->dd_data;
929 login_hdr = (struct iscsi_hdr *)ppdu;
930 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530931 break;
932 default:
933 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530934 "Unrecognized opcode 0x%x in async msg\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530935 (ppdu->
936 dw[offsetof(struct amap_pdu_base, opcode) / 32]
937 & PDUBASE_OPCODE_MASK));
938 return 1;
939 }
940
941 spin_lock_bh(&session->lock);
942 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
943 spin_unlock_bh(&session->lock);
944 return 0;
945}
946
947static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
948{
949 struct sgl_handle *psgl_handle;
950
951 if (phba->io_sgl_hndl_avbl) {
952 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530953 "In alloc_io_sgl_handle,io_sgl_alloc_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530954 phba->io_sgl_alloc_index);
955 psgl_handle = phba->io_sgl_hndl_base[phba->
956 io_sgl_alloc_index];
957 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
958 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530959 if (phba->io_sgl_alloc_index == (phba->params.
960 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530961 phba->io_sgl_alloc_index = 0;
962 else
963 phba->io_sgl_alloc_index++;
964 } else
965 psgl_handle = NULL;
966 return psgl_handle;
967}
968
969static void
970free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
971{
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530972 SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530973 phba->io_sgl_free_index);
974 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
975 /*
976 * this can happen if clean_task is called on a task that
977 * failed in xmit_task or alloc_pdu.
978 */
979 SE_DEBUG(DBG_LVL_8,
980 "Double Free in IO SGL io_sgl_free_index=%d,"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +0530981 "value there=%p\n", phba->io_sgl_free_index,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530982 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
983 return;
984 }
985 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
986 phba->io_sgl_hndl_avbl++;
987 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
988 phba->io_sgl_free_index = 0;
989 else
990 phba->io_sgl_free_index++;
991}
992
993/**
994 * alloc_wrb_handle - To allocate a wrb handle
995 * @phba: The hba pointer
996 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530997 *
998 * This happens under session_lock until submission to chip
999 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301000struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301001{
1002 struct hwi_wrb_context *pwrb_context;
1003 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301004 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301005
1006 phwi_ctrlr = phba->phwi_ctrlr;
1007 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301008 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301009 pwrb_handle = pwrb_context->pwrb_handle_base[
1010 pwrb_context->alloc_index];
1011 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301012 if (pwrb_context->alloc_index ==
1013 (phba->params.wrbs_per_cxn - 1))
1014 pwrb_context->alloc_index = 0;
1015 else
1016 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301017 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1018 pwrb_context->alloc_index];
1019 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301020 } else
1021 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301022 return pwrb_handle;
1023}
1024
1025/**
1026 * free_wrb_handle - To free the wrb handle back to pool
1027 * @phba: The hba pointer
1028 * @pwrb_context: The context to free from
1029 * @pwrb_handle: The wrb_handle to free
1030 *
1031 * This happens under session_lock until submission to chip
1032 */
1033static void
1034free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1035 struct wrb_handle *pwrb_handle)
1036{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301037 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301038 pwrb_context->wrb_handles_available++;
1039 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1040 pwrb_context->free_index = 0;
1041 else
1042 pwrb_context->free_index++;
1043
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301044 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301045 "FREE WRB: pwrb_handle=%p free_index=0x%x"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301046 "wrb_handles_available=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301047 pwrb_handle, pwrb_context->free_index,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301048 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301049}
1050
1051static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1052{
1053 struct sgl_handle *psgl_handle;
1054
1055 if (phba->eh_sgl_hndl_avbl) {
1056 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1057 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301058 SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301059 phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
1060 phba->eh_sgl_hndl_avbl--;
1061 if (phba->eh_sgl_alloc_index ==
1062 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1063 1))
1064 phba->eh_sgl_alloc_index = 0;
1065 else
1066 phba->eh_sgl_alloc_index++;
1067 } else
1068 psgl_handle = NULL;
1069 return psgl_handle;
1070}
1071
1072void
1073free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1074{
1075
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301076 SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d\n",
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301077 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301078 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1079 /*
1080 * this can happen if clean_task is called on a task that
1081 * failed in xmit_task or alloc_pdu.
1082 */
1083 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301084 "Double Free in eh SGL ,eh_sgl_free_index=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301085 phba->eh_sgl_free_index);
1086 return;
1087 }
1088 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1089 phba->eh_sgl_hndl_avbl++;
1090 if (phba->eh_sgl_free_index ==
1091 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1092 phba->eh_sgl_free_index = 0;
1093 else
1094 phba->eh_sgl_free_index++;
1095}
1096
1097static void
1098be_complete_io(struct beiscsi_conn *beiscsi_conn,
1099 struct iscsi_task *task, struct sol_cqe *psol)
1100{
1101 struct beiscsi_io_task *io_task = task->dd_data;
1102 struct be_status_bhs *sts_bhs =
1103 (struct be_status_bhs *)io_task->cmd_bhs;
1104 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301105 unsigned char *sense;
1106 u32 resid = 0, exp_cmdsn, max_cmdsn;
1107 u8 rsp, status, flags;
1108
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301109 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301110 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1111 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301112 max_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) +
1115 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1116 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1117 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
1118 & SOL_RESP_MASK) >> 16);
1119 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
1120 & SOL_STS_MASK) >> 8);
1121 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1122 & SOL_FLAGS_MASK) >> 24) | 0x80;
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001123 if (!task->sc) {
1124 if (io_task->scsi_cmnd)
1125 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301126
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001127 return;
1128 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129 task->sc->result = (DID_OK << 16) | status;
1130 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1131 task->sc->result = DID_ERROR << 16;
1132 goto unmap;
1133 }
1134
1135 /* bidi not initially supported */
1136 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1137 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
1138 32] & SOL_RES_CNT_MASK);
1139
1140 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1141 task->sc->result = DID_ERROR << 16;
1142
1143 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1144 scsi_set_resid(task->sc, resid);
1145 if (!status && (scsi_bufflen(task->sc) - resid <
1146 task->sc->underflow))
1147 task->sc->result = DID_ERROR << 16;
1148 }
1149 }
1150
1151 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001152 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301153 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001154
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301155 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001156 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301157 memcpy(task->sc->sense_buffer, sense,
1158 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1159 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301160
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301161 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
1162 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1163 & SOL_RES_CNT_MASK)
1164 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301165 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1166 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301167 }
1168unmap:
1169 scsi_dma_unmap(io_task->scsi_cmnd);
1170 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1171}
1172
1173static void
1174be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1175 struct iscsi_task *task, struct sol_cqe *psol)
1176{
1177 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301178 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301179 struct iscsi_conn *conn = beiscsi_conn->conn;
1180
1181 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301182 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301183 hdr->t2wait = 5;
1184 hdr->t2retain = 0;
1185 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1186 & SOL_FLAGS_MASK) >> 24) | 0x80;
1187 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1188 32] & SOL_RESP_MASK);
1189 hdr->exp_cmdsn = cpu_to_be32(psol->
1190 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1191 & SOL_EXP_CMD_SN_MASK);
1192 hdr->max_cmdsn = be32_to_cpu((psol->
1193 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1194 & SOL_EXP_CMD_SN_MASK) +
1195 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1196 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301197 hdr->dlength[0] = 0;
1198 hdr->dlength[1] = 0;
1199 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301200 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301201 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301202 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1203}
1204
1205static void
1206be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1207 struct iscsi_task *task, struct sol_cqe *psol)
1208{
1209 struct iscsi_tm_rsp *hdr;
1210 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301211 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301212
1213 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301214 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301215 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1216 & SOL_FLAGS_MASK) >> 24) | 0x80;
1217 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1218 32] & SOL_RESP_MASK);
1219 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301220 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301221 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1222 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1223 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1224 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301225 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301226 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1227}
1228
1229static void
1230hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1231 struct beiscsi_hba *phba, struct sol_cqe *psol)
1232{
1233 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301234 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301235 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301236 struct iscsi_task *task;
1237 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301238 struct iscsi_conn *conn = beiscsi_conn->conn;
1239 struct iscsi_session *session = conn->session;
1240
1241 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301242 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301243 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301244 SOL_CID_MASK) >> 6) -
1245 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301246 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301247 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1248 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301249 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301250
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301251 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001252 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301253 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001254 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301255 spin_lock_bh(&session->lock);
1256 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1257 spin_unlock_bh(&session->lock);
1258}
1259
1260static void
1261be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1262 struct iscsi_task *task, struct sol_cqe *psol)
1263{
1264 struct iscsi_nopin *hdr;
1265 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301266 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301267
1268 hdr = (struct iscsi_nopin *)task->hdr;
1269 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1270 & SOL_FLAGS_MASK) >> 24) | 0x80;
1271 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
1272 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
1273 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1274 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1275 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1276 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1277 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301278 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301279 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1280}
1281
1282static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1283 struct beiscsi_hba *phba, struct sol_cqe *psol)
1284{
1285 struct hwi_wrb_context *pwrb_context;
1286 struct wrb_handle *pwrb_handle;
1287 struct iscsi_wrb *pwrb = NULL;
1288 struct hwi_controller *phwi_ctrlr;
1289 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301290 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301291 struct iscsi_conn *conn = beiscsi_conn->conn;
1292 struct iscsi_session *session = conn->session;
1293
1294 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301295 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301296 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301297 & SOL_CID_MASK) >> 6) -
1298 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301299 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301300 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1301 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301302 task = pwrb_handle->pio_handle;
1303 pwrb = pwrb_handle->pwrb;
1304 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
1305 WRB_TYPE_MASK) >> 28;
1306
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301307 spin_lock_bh(&session->lock);
1308 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301309 case HWH_TYPE_IO:
1310 case HWH_TYPE_IO_RD:
1311 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301312 ISCSI_OP_NOOP_OUT)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301313 be_complete_nopin_resp(beiscsi_conn, task, psol);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301314 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301315 be_complete_io(beiscsi_conn, task, psol);
1316 break;
1317
1318 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301319 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1320 be_complete_logout(beiscsi_conn, task, psol);
1321 else
1322 be_complete_tmf(beiscsi_conn, task, psol);
1323
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301324 break;
1325
1326 case HWH_TYPE_LOGIN:
1327 SE_DEBUG(DBG_LVL_1,
1328 "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301329 "- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301330 break;
1331
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301332 case HWH_TYPE_NOP:
1333 be_complete_nopin_resp(beiscsi_conn, task, psol);
1334 break;
1335
1336 default:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301337 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301338 "In hwi_complete_cmd, unknown type = %d"
1339 "wrb_index 0x%x CID 0x%x\n", type,
1340 ((psol->dw[offsetof(struct amap_iscsi_wrb,
1341 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
1342 ((psol->dw[offsetof(struct amap_sol_cqe,
1343 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301344 break;
1345 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301346
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301347 spin_unlock_bh(&session->lock);
1348}
1349
1350static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1351 *pasync_ctx, unsigned int is_header,
1352 unsigned int host_write_ptr)
1353{
1354 if (is_header)
1355 return &pasync_ctx->async_entry[host_write_ptr].
1356 header_busy_list;
1357 else
1358 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1359}
1360
1361static struct async_pdu_handle *
1362hwi_get_async_handle(struct beiscsi_hba *phba,
1363 struct beiscsi_conn *beiscsi_conn,
1364 struct hwi_async_pdu_context *pasync_ctx,
1365 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1366{
1367 struct be_bus_address phys_addr;
1368 struct list_head *pbusy_list;
1369 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301370 unsigned char is_header = 0;
1371
1372 phys_addr.u.a32.address_lo =
1373 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1374 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1375 & PDUCQE_DPL_MASK) >> 16);
1376 phys_addr.u.a32.address_hi =
1377 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1378
1379 phys_addr.u.a64.address =
1380 *((unsigned long long *)(&phys_addr.u.a64.address));
1381
1382 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1383 & PDUCQE_CODE_MASK) {
1384 case UNSOL_HDR_NOTIFY:
1385 is_header = 1;
1386
1387 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1388 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1389 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301390 break;
1391 case UNSOL_DATA_NOTIFY:
1392 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1393 dw[offsetof(struct amap_i_t_dpdu_cqe,
1394 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301395 break;
1396 default:
1397 pbusy_list = NULL;
1398 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301399 "Unexpected code=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301400 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1401 code) / 32] & PDUCQE_CODE_MASK);
1402 return NULL;
1403 }
1404
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301405 WARN_ON(list_empty(pbusy_list));
1406 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001407 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301408 break;
1409 }
1410
1411 WARN_ON(!pasync_handle);
1412
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301413 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1414 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301415 pasync_handle->is_header = is_header;
1416 pasync_handle->buffer_len = ((pdpdu_cqe->
1417 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1418 & PDUCQE_DPL_MASK) >> 16);
1419
1420 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1421 index) / 32] & PDUCQE_INDEX_MASK);
1422 return pasync_handle;
1423}
1424
1425static unsigned int
1426hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
1427 unsigned int is_header, unsigned int cq_index)
1428{
1429 struct list_head *pbusy_list;
1430 struct async_pdu_handle *pasync_handle;
1431 unsigned int num_entries, writables = 0;
1432 unsigned int *pep_read_ptr, *pwritables;
1433
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001434 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301435 if (is_header) {
1436 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1437 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301438 } else {
1439 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1440 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301441 }
1442
1443 while ((*pep_read_ptr) != cq_index) {
1444 (*pep_read_ptr)++;
1445 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1446
1447 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1448 *pep_read_ptr);
1449 if (writables == 0)
1450 WARN_ON(list_empty(pbusy_list));
1451
1452 if (!list_empty(pbusy_list)) {
1453 pasync_handle = list_entry(pbusy_list->next,
1454 struct async_pdu_handle,
1455 link);
1456 WARN_ON(!pasync_handle);
1457 pasync_handle->consumed = 1;
1458 }
1459
1460 writables++;
1461 }
1462
1463 if (!writables) {
1464 SE_DEBUG(DBG_LVL_1,
1465 "Duplicate notification received - index 0x%x!!\n",
1466 cq_index);
1467 WARN_ON(1);
1468 }
1469
1470 *pwritables = *pwritables + writables;
1471 return 0;
1472}
1473
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001474static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301475 unsigned int cri)
1476{
1477 struct hwi_controller *phwi_ctrlr;
1478 struct hwi_async_pdu_context *pasync_ctx;
1479 struct async_pdu_handle *pasync_handle, *tmp_handle;
1480 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301481
1482 phwi_ctrlr = phba->phwi_ctrlr;
1483 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1484
1485 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1486
1487 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1488 list_del(&pasync_handle->link);
1489
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001490 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301491 list_add_tail(&pasync_handle->link,
1492 &pasync_ctx->async_header.free_list);
1493 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301494 } else {
1495 list_add_tail(&pasync_handle->link,
1496 &pasync_ctx->async_data.free_list);
1497 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301498 }
1499 }
1500
1501 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1502 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1503 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301504}
1505
1506static struct phys_addr *
1507hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1508 unsigned int is_header, unsigned int host_write_ptr)
1509{
1510 struct phys_addr *pasync_sge = NULL;
1511
1512 if (is_header)
1513 pasync_sge = pasync_ctx->async_header.ring_base;
1514 else
1515 pasync_sge = pasync_ctx->async_data.ring_base;
1516
1517 return pasync_sge + host_write_ptr;
1518}
1519
1520static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1521 unsigned int is_header)
1522{
1523 struct hwi_controller *phwi_ctrlr;
1524 struct hwi_async_pdu_context *pasync_ctx;
1525 struct async_pdu_handle *pasync_handle;
1526 struct list_head *pfree_link, *pbusy_list;
1527 struct phys_addr *pasync_sge;
1528 unsigned int ring_id, num_entries;
1529 unsigned int host_write_num;
1530 unsigned int writables;
1531 unsigned int i = 0;
1532 u32 doorbell = 0;
1533
1534 phwi_ctrlr = phba->phwi_ctrlr;
1535 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001536 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301537
1538 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301539 writables = min(pasync_ctx->async_header.writables,
1540 pasync_ctx->async_header.free_entries);
1541 pfree_link = pasync_ctx->async_header.free_list.next;
1542 host_write_num = pasync_ctx->async_header.host_write_ptr;
1543 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1544 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301545 writables = min(pasync_ctx->async_data.writables,
1546 pasync_ctx->async_data.free_entries);
1547 pfree_link = pasync_ctx->async_data.free_list.next;
1548 host_write_num = pasync_ctx->async_data.host_write_ptr;
1549 ring_id = phwi_ctrlr->default_pdu_data.id;
1550 }
1551
1552 writables = (writables / 8) * 8;
1553 if (writables) {
1554 for (i = 0; i < writables; i++) {
1555 pbusy_list =
1556 hwi_get_async_busy_list(pasync_ctx, is_header,
1557 host_write_num);
1558 pasync_handle =
1559 list_entry(pfree_link, struct async_pdu_handle,
1560 link);
1561 WARN_ON(!pasync_handle);
1562 pasync_handle->consumed = 0;
1563
1564 pfree_link = pfree_link->next;
1565
1566 pasync_sge = hwi_get_ring_address(pasync_ctx,
1567 is_header, host_write_num);
1568
1569 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1570 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1571
1572 list_move(&pasync_handle->link, pbusy_list);
1573
1574 host_write_num++;
1575 host_write_num = host_write_num % num_entries;
1576 }
1577
1578 if (is_header) {
1579 pasync_ctx->async_header.host_write_ptr =
1580 host_write_num;
1581 pasync_ctx->async_header.free_entries -= writables;
1582 pasync_ctx->async_header.writables -= writables;
1583 pasync_ctx->async_header.busy_entries += writables;
1584 } else {
1585 pasync_ctx->async_data.host_write_ptr = host_write_num;
1586 pasync_ctx->async_data.free_entries -= writables;
1587 pasync_ctx->async_data.writables -= writables;
1588 pasync_ctx->async_data.busy_entries += writables;
1589 }
1590
1591 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1592 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1593 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1594 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1595 << DB_DEF_PDU_CQPROC_SHIFT;
1596
1597 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1598 }
1599}
1600
1601static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1602 struct beiscsi_conn *beiscsi_conn,
1603 struct i_t_dpdu_cqe *pdpdu_cqe)
1604{
1605 struct hwi_controller *phwi_ctrlr;
1606 struct hwi_async_pdu_context *pasync_ctx;
1607 struct async_pdu_handle *pasync_handle = NULL;
1608 unsigned int cq_index = -1;
1609
1610 phwi_ctrlr = phba->phwi_ctrlr;
1611 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1612
1613 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1614 pdpdu_cqe, &cq_index);
1615 BUG_ON(pasync_handle->is_header != 0);
1616 if (pasync_handle->consumed == 0)
1617 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1618 cq_index);
1619
1620 hwi_free_async_msg(phba, pasync_handle->cri);
1621 hwi_post_async_buffers(phba, pasync_handle->is_header);
1622}
1623
1624static unsigned int
1625hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1626 struct beiscsi_hba *phba,
1627 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1628{
1629 struct list_head *plist;
1630 struct async_pdu_handle *pasync_handle;
1631 void *phdr = NULL;
1632 unsigned int hdr_len = 0, buf_len = 0;
1633 unsigned int status, index = 0, offset = 0;
1634 void *pfirst_buffer = NULL;
1635 unsigned int num_buf = 0;
1636
1637 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1638
1639 list_for_each_entry(pasync_handle, plist, link) {
1640 if (index == 0) {
1641 phdr = pasync_handle->pbuffer;
1642 hdr_len = pasync_handle->buffer_len;
1643 } else {
1644 buf_len = pasync_handle->buffer_len;
1645 if (!num_buf) {
1646 pfirst_buffer = pasync_handle->pbuffer;
1647 num_buf++;
1648 }
1649 memcpy(pfirst_buffer + offset,
1650 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001651 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301652 }
1653 index++;
1654 }
1655
1656 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301657 (beiscsi_conn->beiscsi_conn_cid -
1658 phba->fw_config.iscsi_cid_start),
1659 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001660 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301661
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001662 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301663 return 0;
1664}
1665
1666static unsigned int
1667hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1668 struct beiscsi_hba *phba,
1669 struct async_pdu_handle *pasync_handle)
1670{
1671 struct hwi_async_pdu_context *pasync_ctx;
1672 struct hwi_controller *phwi_ctrlr;
1673 unsigned int bytes_needed = 0, status = 0;
1674 unsigned short cri = pasync_handle->cri;
1675 struct pdu_base *ppdu;
1676
1677 phwi_ctrlr = phba->phwi_ctrlr;
1678 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1679
1680 list_del(&pasync_handle->link);
1681 if (pasync_handle->is_header) {
1682 pasync_ctx->async_header.busy_entries--;
1683 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1684 hwi_free_async_msg(phba, cri);
1685 BUG();
1686 }
1687
1688 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1689 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1690 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1691 (unsigned short)pasync_handle->buffer_len;
1692 list_add_tail(&pasync_handle->link,
1693 &pasync_ctx->async_entry[cri].wait_queue.list);
1694
1695 ppdu = pasync_handle->pbuffer;
1696 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1697 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1698 0xFFFF0000) | ((be16_to_cpu((ppdu->
1699 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1700 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1701
1702 if (status == 0) {
1703 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1704 bytes_needed;
1705
1706 if (bytes_needed == 0)
1707 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1708 pasync_ctx, cri);
1709 }
1710 } else {
1711 pasync_ctx->async_data.busy_entries--;
1712 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1713 list_add_tail(&pasync_handle->link,
1714 &pasync_ctx->async_entry[cri].wait_queue.
1715 list);
1716 pasync_ctx->async_entry[cri].wait_queue.
1717 bytes_received +=
1718 (unsigned short)pasync_handle->buffer_len;
1719
1720 if (pasync_ctx->async_entry[cri].wait_queue.
1721 bytes_received >=
1722 pasync_ctx->async_entry[cri].wait_queue.
1723 bytes_needed)
1724 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1725 pasync_ctx, cri);
1726 }
1727 }
1728 return status;
1729}
1730
1731static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1732 struct beiscsi_hba *phba,
1733 struct i_t_dpdu_cqe *pdpdu_cqe)
1734{
1735 struct hwi_controller *phwi_ctrlr;
1736 struct hwi_async_pdu_context *pasync_ctx;
1737 struct async_pdu_handle *pasync_handle = NULL;
1738 unsigned int cq_index = -1;
1739
1740 phwi_ctrlr = phba->phwi_ctrlr;
1741 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1742 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1743 pdpdu_cqe, &cq_index);
1744
1745 if (pasync_handle->consumed == 0)
1746 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1747 cq_index);
1748 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1749 hwi_post_async_buffers(phba, pasync_handle->is_header);
1750}
1751
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301752static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1753{
1754 struct be_queue_info *mcc_cq;
1755 struct be_mcc_compl *mcc_compl;
1756 unsigned int num_processed = 0;
1757
1758 mcc_cq = &phba->ctrl.mcc_obj.cq;
1759 mcc_compl = queue_tail_node(mcc_cq);
1760 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1761 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1762
1763 if (num_processed >= 32) {
1764 hwi_ring_cq_db(phba, mcc_cq->id,
1765 num_processed, 0, 0);
1766 num_processed = 0;
1767 }
1768 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1769 /* Interpret flags as an async trailer */
1770 if (is_link_state_evt(mcc_compl->flags))
1771 /* Interpret compl as a async link evt */
1772 beiscsi_async_link_state_process(phba,
1773 (struct be_async_event_link_state *) mcc_compl);
1774 else
1775 SE_DEBUG(DBG_LVL_1,
1776 " Unsupported Async Event, flags"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301777 " = 0x%08x\n", mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301778 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1779 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1780 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1781 }
1782
1783 mcc_compl->flags = 0;
1784 queue_tail_inc(mcc_cq);
1785 mcc_compl = queue_tail_node(mcc_cq);
1786 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1787 num_processed++;
1788 }
1789
1790 if (num_processed > 0)
1791 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1792
1793}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301794
1795static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301796{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301797 struct be_queue_info *cq;
1798 struct sol_cqe *sol;
1799 struct dmsg_cqe *dmsg;
1800 unsigned int num_processed = 0;
1801 unsigned int tot_nump = 0;
1802 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301803 struct beiscsi_endpoint *beiscsi_ep;
1804 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301805 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301806
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301807 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301808 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301809 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301810
1811 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1812 CQE_VALID_MASK) {
1813 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1814
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301815 ep = phba->ep_array[(u32) ((sol->
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301816 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
1817 SOL_CID_MASK) >> 6) -
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301818 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301819
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301820 beiscsi_ep = ep->dd_data;
1821 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301822
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301823 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301824 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301825 num_processed, 0, 0);
1826 tot_nump += num_processed;
1827 num_processed = 0;
1828 }
1829
1830 switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
1831 32] & CQE_CODE_MASK) {
1832 case SOL_CMD_COMPLETE:
1833 hwi_complete_cmd(beiscsi_conn, phba, sol);
1834 break;
1835 case DRIVERMSG_NOTIFY:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301836 SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301837 dmsg = (struct dmsg_cqe *)sol;
1838 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1839 break;
1840 case UNSOL_HDR_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301841 SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
1842 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1843 (struct i_t_dpdu_cqe *)sol);
1844 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301845 case UNSOL_DATA_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301846 SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301847 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1848 (struct i_t_dpdu_cqe *)sol);
1849 break;
1850 case CXN_INVALIDATE_INDEX_NOTIFY:
1851 case CMD_INVALIDATED_NOTIFY:
1852 case CXN_INVALIDATE_NOTIFY:
1853 SE_DEBUG(DBG_LVL_1,
1854 "Ignoring CQ Error notification for cmd/cxn"
1855 "invalidate\n");
1856 break;
1857 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1858 case CMD_KILLED_INVALID_STATSN_RCVD:
1859 case CMD_KILLED_INVALID_R2T_RCVD:
1860 case CMD_CXN_KILLED_LUN_INVALID:
1861 case CMD_CXN_KILLED_ICD_INVALID:
1862 case CMD_CXN_KILLED_ITT_INVALID:
1863 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1864 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301865 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301866 "CQ Error notification for cmd.. "
1867 "code %d cid 0x%x\n",
1868 sol->dw[offsetof(struct amap_sol_cqe, code) /
1869 32] & CQE_CODE_MASK,
1870 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1871 32] & SOL_CID_MASK));
1872 break;
1873 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1874 SE_DEBUG(DBG_LVL_1,
1875 "Digest error on def pdu ring, dropping..\n");
1876 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
1877 (struct i_t_dpdu_cqe *) sol);
1878 break;
1879 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1880 case CXN_KILLED_BURST_LEN_MISMATCH:
1881 case CXN_KILLED_AHS_RCVD:
1882 case CXN_KILLED_HDR_DIGEST_ERR:
1883 case CXN_KILLED_UNKNOWN_HDR:
1884 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1885 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1886 case CXN_KILLED_TIMED_OUT:
1887 case CXN_KILLED_FIN_RCVD:
1888 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1889 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1890 case CXN_KILLED_OVER_RUN_RESIDUAL:
1891 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1892 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301893 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301894 "0x%x...\n",
1895 sol->dw[offsetof(struct amap_sol_cqe, code) /
1896 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301897 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1898 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301899 iscsi_conn_failure(beiscsi_conn->conn,
1900 ISCSI_ERR_CONN_FAILED);
1901 break;
1902 case CXN_KILLED_RST_SENT:
1903 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301904 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301905 "received/sent on CID 0x%x...\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301906 sol->dw[offsetof(struct amap_sol_cqe, code) /
1907 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301908 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1909 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301910 iscsi_conn_failure(beiscsi_conn->conn,
1911 ISCSI_ERR_CONN_FAILED);
1912 break;
1913 default:
1914 SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
1915 "received on CID 0x%x...\n",
1916 sol->dw[offsetof(struct amap_sol_cqe, code) /
1917 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301918 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1919 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301920 break;
1921 }
1922
1923 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
1924 queue_tail_inc(cq);
1925 sol = queue_tail_node(cq);
1926 num_processed++;
1927 }
1928
1929 if (num_processed > 0) {
1930 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301931 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301932 }
1933 return tot_nump;
1934}
1935
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301936void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301937{
1938 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301939 struct hwi_controller *phwi_ctrlr;
1940 struct hwi_context_memory *phwi_context;
1941 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301942 struct beiscsi_hba *phba =
1943 container_of(work, struct beiscsi_hba, work_cqs);
1944
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301945 phwi_ctrlr = phba->phwi_ctrlr;
1946 phwi_context = phwi_ctrlr->phwi_ctxt;
1947 if (phba->msix_enabled)
1948 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
1949 else
1950 pbe_eq = &phwi_context->be_eq[0];
1951
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301952 if (phba->todo_mcc_cq) {
1953 spin_lock_irqsave(&phba->isr_lock, flags);
1954 phba->todo_mcc_cq = 0;
1955 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301956 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301957 }
1958
1959 if (phba->todo_cq) {
1960 spin_lock_irqsave(&phba->isr_lock, flags);
1961 phba->todo_cq = 0;
1962 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301963 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301964 }
1965}
1966
1967static int be_iopoll(struct blk_iopoll *iop, int budget)
1968{
1969 static unsigned int ret;
1970 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301971 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301972
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301973 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
1974 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301975 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301976 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301977 blk_iopoll_complete(iop);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301978 SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
1979 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301980 }
1981 return ret;
1982}
1983
1984static void
1985hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
1986 unsigned int num_sg, struct beiscsi_io_task *io_task)
1987{
1988 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05301989 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301990 unsigned int sge_len = 0;
1991 unsigned long long addr;
1992 struct scatterlist *l_sg;
1993 unsigned int offset;
1994
1995 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
1996 io_task->bhs_pa.u.a32.address_lo);
1997 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
1998 io_task->bhs_pa.u.a32.address_hi);
1999
2000 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302001 for (index = 0; (index < num_sg) && (index < 2); index++,
2002 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302003 if (index == 0) {
2004 sg_len = sg_dma_len(sg);
2005 addr = (u64) sg_dma_address(sg);
2006 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302007 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302008 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302009 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302010 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2011 sg_len);
2012 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302013 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302014 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2015 pwrb, sge_len);
2016 sg_len = sg_dma_len(sg);
2017 addr = (u64) sg_dma_address(sg);
2018 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302019 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302020 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302021 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2023 sg_len);
2024 }
2025 }
2026 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2027 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2028
2029 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2030
2031 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2032 io_task->bhs_pa.u.a32.address_hi);
2033 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2034 io_task->bhs_pa.u.a32.address_lo);
2035
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302036 if (num_sg == 1) {
2037 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2038 1);
2039 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2040 0);
2041 } else if (num_sg == 2) {
2042 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2043 0);
2044 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2045 1);
2046 } else {
2047 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2048 0);
2049 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2050 0);
2051 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302052 sg = l_sg;
2053 psgl++;
2054 psgl++;
2055 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302056 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302057 sg_len = sg_dma_len(sg);
2058 addr = (u64) sg_dma_address(sg);
2059 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2060 (addr & 0xFFFFFFFF));
2061 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2062 (addr >> 32));
2063 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2064 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2065 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2066 offset += sg_len;
2067 }
2068 psgl--;
2069 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2070}
2071
2072static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2073{
2074 struct iscsi_sge *psgl;
2075 unsigned long long addr;
2076 struct beiscsi_io_task *io_task = task->dd_data;
2077 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2078 struct beiscsi_hba *phba = beiscsi_conn->phba;
2079
2080 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2081 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2082 io_task->bhs_pa.u.a32.address_lo);
2083 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2084 io_task->bhs_pa.u.a32.address_hi);
2085
2086 if (task->data) {
2087 if (task->data_count) {
2088 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2089 addr = (u64) pci_map_single(phba->pcidev,
2090 task->data,
2091 task->data_count, 1);
2092 } else {
2093 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2094 addr = 0;
2095 }
2096 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302097 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302098 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302099 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302100 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2101 task->data_count);
2102
2103 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2104 } else {
2105 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2106 addr = 0;
2107 }
2108
2109 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2110
2111 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2112
2113 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2114 io_task->bhs_pa.u.a32.address_hi);
2115 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2116 io_task->bhs_pa.u.a32.address_lo);
2117 if (task->data) {
2118 psgl++;
2119 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2120 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2121 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2122 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2123 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2124 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2125
2126 psgl++;
2127 if (task->data) {
2128 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302129 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302130 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302131 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302132 }
2133 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2134 }
2135 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2136}
2137
2138static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2139{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302140 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302141 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2142 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2143
2144 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2145 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302146 num_async_pdu_buf_pages =
2147 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2148 phba->params.defpdu_hdr_sz);
2149 num_async_pdu_buf_sgl_pages =
2150 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2151 sizeof(struct phys_addr));
2152 num_async_pdu_data_pages =
2153 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2154 phba->params.defpdu_data_sz);
2155 num_async_pdu_data_sgl_pages =
2156 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2157 sizeof(struct phys_addr));
2158
2159 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2160
2161 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2162 BE_ISCSI_PDU_HEADER_SIZE;
2163 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2164 sizeof(struct hwi_context_memory);
2165
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302166
2167 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2168 * (phba->params.wrbs_per_cxn)
2169 * phba->params.cxns_per_ctrl;
2170 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2171 (phba->params.wrbs_per_cxn);
2172 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2173 phba->params.cxns_per_ctrl);
2174
2175 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2176 phba->params.icds_per_ctrl;
2177 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2178 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2179
2180 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2181 num_async_pdu_buf_pages * PAGE_SIZE;
2182 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2183 num_async_pdu_data_pages * PAGE_SIZE;
2184 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2185 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2186 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2187 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2188 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2189 phba->params.asyncpdus_per_ctrl *
2190 sizeof(struct async_pdu_handle);
2191 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2192 phba->params.asyncpdus_per_ctrl *
2193 sizeof(struct async_pdu_handle);
2194 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2195 sizeof(struct hwi_async_pdu_context) +
2196 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2197}
2198
2199static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2200{
2201 struct be_mem_descriptor *mem_descr;
2202 dma_addr_t bus_add;
2203 struct mem_array *mem_arr, *mem_arr_orig;
2204 unsigned int i, j, alloc_size, curr_alloc_size;
2205
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002206 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302207 if (!phba->phwi_ctrlr)
2208 return -ENOMEM;
2209
2210 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2211 GFP_KERNEL);
2212 if (!phba->init_mem) {
2213 kfree(phba->phwi_ctrlr);
2214 return -ENOMEM;
2215 }
2216
2217 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2218 GFP_KERNEL);
2219 if (!mem_arr_orig) {
2220 kfree(phba->init_mem);
2221 kfree(phba->phwi_ctrlr);
2222 return -ENOMEM;
2223 }
2224
2225 mem_descr = phba->init_mem;
2226 for (i = 0; i < SE_MEM_MAX; i++) {
2227 j = 0;
2228 mem_arr = mem_arr_orig;
2229 alloc_size = phba->mem_req[i];
2230 memset(mem_arr, 0, sizeof(struct mem_array) *
2231 BEISCSI_MAX_FRAGS_INIT);
2232 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2233 do {
2234 mem_arr->virtual_address = pci_alloc_consistent(
2235 phba->pcidev,
2236 curr_alloc_size,
2237 &bus_add);
2238 if (!mem_arr->virtual_address) {
2239 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2240 goto free_mem;
2241 if (curr_alloc_size -
2242 rounddown_pow_of_two(curr_alloc_size))
2243 curr_alloc_size = rounddown_pow_of_two
2244 (curr_alloc_size);
2245 else
2246 curr_alloc_size = curr_alloc_size / 2;
2247 } else {
2248 mem_arr->bus_address.u.
2249 a64.address = (__u64) bus_add;
2250 mem_arr->size = curr_alloc_size;
2251 alloc_size -= curr_alloc_size;
2252 curr_alloc_size = min(be_max_phys_size *
2253 1024, alloc_size);
2254 j++;
2255 mem_arr++;
2256 }
2257 } while (alloc_size);
2258 mem_descr->num_elements = j;
2259 mem_descr->size_in_bytes = phba->mem_req[i];
2260 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2261 GFP_KERNEL);
2262 if (!mem_descr->mem_array)
2263 goto free_mem;
2264
2265 memcpy(mem_descr->mem_array, mem_arr_orig,
2266 sizeof(struct mem_array) * j);
2267 mem_descr++;
2268 }
2269 kfree(mem_arr_orig);
2270 return 0;
2271free_mem:
2272 mem_descr->num_elements = j;
2273 while ((i) || (j)) {
2274 for (j = mem_descr->num_elements; j > 0; j--) {
2275 pci_free_consistent(phba->pcidev,
2276 mem_descr->mem_array[j - 1].size,
2277 mem_descr->mem_array[j - 1].
2278 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302279 (unsigned long)mem_descr->
2280 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302281 bus_address.u.a64.address);
2282 }
2283 if (i) {
2284 i--;
2285 kfree(mem_descr->mem_array);
2286 mem_descr--;
2287 }
2288 }
2289 kfree(mem_arr_orig);
2290 kfree(phba->init_mem);
2291 kfree(phba->phwi_ctrlr);
2292 return -ENOMEM;
2293}
2294
2295static int beiscsi_get_memory(struct beiscsi_hba *phba)
2296{
2297 beiscsi_find_mem_req(phba);
2298 return beiscsi_alloc_mem(phba);
2299}
2300
2301static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2302{
2303 struct pdu_data_out *pdata_out;
2304 struct pdu_nop_out *pnop_out;
2305 struct be_mem_descriptor *mem_descr;
2306
2307 mem_descr = phba->init_mem;
2308 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2309 pdata_out =
2310 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2311 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2312
2313 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2314 IIOC_SCSI_DATA);
2315
2316 pnop_out =
2317 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2318 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2319
2320 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2321 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2322 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2323 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2324}
2325
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002326static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302327{
2328 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002329 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302330 struct hwi_controller *phwi_ctrlr;
2331 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002332 struct iscsi_wrb *pwrb = NULL;
2333 unsigned int num_cxn_wrbh = 0;
2334 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302335
2336 mem_descr_wrbh = phba->init_mem;
2337 mem_descr_wrbh += HWI_MEM_WRBH;
2338
2339 mem_descr_wrb = phba->init_mem;
2340 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302341 phwi_ctrlr = phba->phwi_ctrlr;
2342
2343 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2344 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302345 pwrb_context->pwrb_handle_base =
2346 kzalloc(sizeof(struct wrb_handle *) *
2347 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002348 if (!pwrb_context->pwrb_handle_base) {
2349 shost_printk(KERN_ERR, phba->shost,
2350 "Mem Alloc Failed. Failing to load\n");
2351 goto init_wrb_hndl_failed;
2352 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302353 pwrb_context->pwrb_handle_basestd =
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_basestd) {
2357 shost_printk(KERN_ERR, phba->shost,
2358 "Mem Alloc Failed. Failing to load\n");
2359 goto init_wrb_hndl_failed;
2360 }
2361 if (!num_cxn_wrbh) {
2362 pwrb_handle =
2363 mem_descr_wrbh->mem_array[idx].virtual_address;
2364 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2365 ((sizeof(struct wrb_handle)) *
2366 phba->params.wrbs_per_cxn));
2367 idx++;
2368 }
2369 pwrb_context->alloc_index = 0;
2370 pwrb_context->wrb_handles_available = 0;
2371 pwrb_context->free_index = 0;
2372
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302373 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302374 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2375 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2376 pwrb_context->pwrb_handle_basestd[j] =
2377 pwrb_handle;
2378 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302379 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302380 pwrb_handle++;
2381 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302382 num_cxn_wrbh--;
2383 }
2384 }
2385 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302386 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302387 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002388 if (!num_cxn_wrb) {
2389 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2390 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2391 ((sizeof(struct iscsi_wrb) *
2392 phba->params.wrbs_per_cxn));
2393 idx++;
2394 }
2395
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302396 if (num_cxn_wrb) {
2397 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2398 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2399 pwrb_handle->pwrb = pwrb;
2400 pwrb++;
2401 }
2402 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302403 }
2404 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002405 return 0;
2406init_wrb_hndl_failed:
2407 for (j = index; j > 0; j--) {
2408 pwrb_context = &phwi_ctrlr->wrb_context[j];
2409 kfree(pwrb_context->pwrb_handle_base);
2410 kfree(pwrb_context->pwrb_handle_basestd);
2411 }
2412 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302413}
2414
2415static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2416{
2417 struct hwi_controller *phwi_ctrlr;
2418 struct hba_parameters *p = &phba->params;
2419 struct hwi_async_pdu_context *pasync_ctx;
2420 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002421 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302422 struct be_mem_descriptor *mem_descr;
2423
2424 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2425 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2426
2427 phwi_ctrlr = phba->phwi_ctrlr;
2428 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2429 mem_descr->mem_array[0].virtual_address;
2430 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2431 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2432
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002433 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2434 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302435
2436 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2437 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2438 if (mem_descr->mem_array[0].virtual_address) {
2439 SE_DEBUG(DBG_LVL_8,
2440 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302441 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302442 } else
2443 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302444 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302445
2446 pasync_ctx->async_header.va_base =
2447 mem_descr->mem_array[0].virtual_address;
2448
2449 pasync_ctx->async_header.pa_base.u.a64.address =
2450 mem_descr->mem_array[0].bus_address.u.a64.address;
2451
2452 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2453 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2454 if (mem_descr->mem_array[0].virtual_address) {
2455 SE_DEBUG(DBG_LVL_8,
2456 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302457 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302458 } else
2459 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302460 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302461 pasync_ctx->async_header.ring_base =
2462 mem_descr->mem_array[0].virtual_address;
2463
2464 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2465 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2466 if (mem_descr->mem_array[0].virtual_address) {
2467 SE_DEBUG(DBG_LVL_8,
2468 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302469 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302470 } else
2471 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302472 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302473
2474 pasync_ctx->async_header.handle_base =
2475 mem_descr->mem_array[0].virtual_address;
2476 pasync_ctx->async_header.writables = 0;
2477 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2478
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302479
2480 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2481 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2482 if (mem_descr->mem_array[0].virtual_address) {
2483 SE_DEBUG(DBG_LVL_8,
2484 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302485 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302486 } else
2487 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302488 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302489
2490 pasync_ctx->async_data.ring_base =
2491 mem_descr->mem_array[0].virtual_address;
2492
2493 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2494 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2495 if (!mem_descr->mem_array[0].virtual_address)
2496 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302497 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302498
2499 pasync_ctx->async_data.handle_base =
2500 mem_descr->mem_array[0].virtual_address;
2501 pasync_ctx->async_data.writables = 0;
2502 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2503
2504 pasync_header_h =
2505 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2506 pasync_data_h =
2507 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2508
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002509 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2510 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2511 if (mem_descr->mem_array[0].virtual_address) {
2512 SE_DEBUG(DBG_LVL_8,
2513 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
2514 "va=%p\n", mem_descr->mem_array[0].virtual_address);
2515 } else
2516 shost_printk(KERN_WARNING, phba->shost,
2517 "No Virtual address\n");
2518 idx = 0;
2519 pasync_ctx->async_data.va_base =
2520 mem_descr->mem_array[idx].virtual_address;
2521 pasync_ctx->async_data.pa_base.u.a64.address =
2522 mem_descr->mem_array[idx].bus_address.u.a64.address;
2523
2524 num_async_data = ((mem_descr->mem_array[idx].size) /
2525 phba->params.defpdu_data_sz);
2526 num_per_mem = 0;
2527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302528 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2529 pasync_header_h->cri = -1;
2530 pasync_header_h->index = (char)index;
2531 INIT_LIST_HEAD(&pasync_header_h->link);
2532 pasync_header_h->pbuffer =
2533 (void *)((unsigned long)
2534 (pasync_ctx->async_header.va_base) +
2535 (p->defpdu_hdr_sz * index));
2536
2537 pasync_header_h->pa.u.a64.address =
2538 pasync_ctx->async_header.pa_base.u.a64.address +
2539 (p->defpdu_hdr_sz * index);
2540
2541 list_add_tail(&pasync_header_h->link,
2542 &pasync_ctx->async_header.free_list);
2543 pasync_header_h++;
2544 pasync_ctx->async_header.free_entries++;
2545 pasync_ctx->async_header.writables++;
2546
2547 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2548 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2549 header_busy_list);
2550 pasync_data_h->cri = -1;
2551 pasync_data_h->index = (char)index;
2552 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002553
2554 if (!num_async_data) {
2555 num_per_mem = 0;
2556 idx++;
2557 pasync_ctx->async_data.va_base =
2558 mem_descr->mem_array[idx].virtual_address;
2559 pasync_ctx->async_data.pa_base.u.a64.address =
2560 mem_descr->mem_array[idx].
2561 bus_address.u.a64.address;
2562
2563 num_async_data = ((mem_descr->mem_array[idx].size) /
2564 phba->params.defpdu_data_sz);
2565 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302566 pasync_data_h->pbuffer =
2567 (void *)((unsigned long)
2568 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002569 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302570
2571 pasync_data_h->pa.u.a64.address =
2572 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002573 (p->defpdu_data_sz * num_per_mem);
2574 num_per_mem++;
2575 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302576
2577 list_add_tail(&pasync_data_h->link,
2578 &pasync_ctx->async_data.free_list);
2579 pasync_data_h++;
2580 pasync_ctx->async_data.free_entries++;
2581 pasync_ctx->async_data.writables++;
2582
2583 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2584 }
2585
2586 pasync_ctx->async_header.host_write_ptr = 0;
2587 pasync_ctx->async_header.ep_read_ptr = -1;
2588 pasync_ctx->async_data.host_write_ptr = 0;
2589 pasync_ctx->async_data.ep_read_ptr = -1;
2590}
2591
2592static int
2593be_sgl_create_contiguous(void *virtual_address,
2594 u64 physical_address, u32 length,
2595 struct be_dma_mem *sgl)
2596{
2597 WARN_ON(!virtual_address);
2598 WARN_ON(!physical_address);
2599 WARN_ON(!length > 0);
2600 WARN_ON(!sgl);
2601
2602 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302603 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302604 sgl->size = length;
2605
2606 return 0;
2607}
2608
2609static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2610{
2611 memset(sgl, 0, sizeof(*sgl));
2612}
2613
2614static void
2615hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2616 struct mem_array *pmem, struct be_dma_mem *sgl)
2617{
2618 if (sgl->va)
2619 be_sgl_destroy_contiguous(sgl);
2620
2621 be_sgl_create_contiguous(pmem->virtual_address,
2622 pmem->bus_address.u.a64.address,
2623 pmem->size, sgl);
2624}
2625
2626static void
2627hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2628 struct mem_array *pmem, struct be_dma_mem *sgl)
2629{
2630 if (sgl->va)
2631 be_sgl_destroy_contiguous(sgl);
2632
2633 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2634 pmem->bus_address.u.a64.address,
2635 pmem->size, sgl);
2636}
2637
2638static int be_fill_queue(struct be_queue_info *q,
2639 u16 len, u16 entry_size, void *vaddress)
2640{
2641 struct be_dma_mem *mem = &q->dma_mem;
2642
2643 memset(q, 0, sizeof(*q));
2644 q->len = len;
2645 q->entry_size = entry_size;
2646 mem->size = len * entry_size;
2647 mem->va = vaddress;
2648 if (!mem->va)
2649 return -ENOMEM;
2650 memset(mem->va, 0, mem->size);
2651 return 0;
2652}
2653
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302654static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302655 struct hwi_context_memory *phwi_context)
2656{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302657 unsigned int i, num_eq_pages;
2658 int ret, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302659 struct be_queue_info *eq;
2660 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302661 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302662 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302663
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302664 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2665 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302666
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302667 if (phba->msix_enabled)
2668 eq_for_mcc = 1;
2669 else
2670 eq_for_mcc = 0;
2671 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2672 eq = &phwi_context->be_eq[i].q;
2673 mem = &eq->dma_mem;
2674 phwi_context->be_eq[i].phba = phba;
2675 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2676 num_eq_pages * PAGE_SIZE,
2677 &paddr);
2678 if (!eq_vaddress)
2679 goto create_eq_error;
2680
2681 mem->va = eq_vaddress;
2682 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2683 sizeof(struct be_eq_entry), eq_vaddress);
2684 if (ret) {
2685 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302686 "be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302687 goto create_eq_error;
2688 }
2689
2690 mem->dma = paddr;
2691 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2692 phwi_context->cur_eqd);
2693 if (ret) {
2694 shost_printk(KERN_ERR, phba->shost,
2695 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302696 "Failedfor EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302697 goto create_eq_error;
2698 }
2699 SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302700 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302701 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302702create_eq_error:
2703 for (i = 0; i < (phba->num_cpus + 1); i++) {
2704 eq = &phwi_context->be_eq[i].q;
2705 mem = &eq->dma_mem;
2706 if (mem->va)
2707 pci_free_consistent(phba->pcidev, num_eq_pages
2708 * PAGE_SIZE,
2709 mem->va, mem->dma);
2710 }
2711 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302712}
2713
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302714static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302715 struct hwi_context_memory *phwi_context)
2716{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302717 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302718 int ret;
2719 struct be_queue_info *cq, *eq;
2720 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302721 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302722 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302723 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302724
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302725 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2726 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302727
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302728 for (i = 0; i < phba->num_cpus; i++) {
2729 cq = &phwi_context->be_cq[i];
2730 eq = &phwi_context->be_eq[i].q;
2731 pbe_eq = &phwi_context->be_eq[i];
2732 pbe_eq->cq = cq;
2733 pbe_eq->phba = phba;
2734 mem = &cq->dma_mem;
2735 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2736 num_cq_pages * PAGE_SIZE,
2737 &paddr);
2738 if (!cq_vaddress)
2739 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302740 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302741 sizeof(struct sol_cqe), cq_vaddress);
2742 if (ret) {
2743 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302744 "be_fill_queue Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302745 goto create_cq_error;
2746 }
2747
2748 mem->dma = paddr;
2749 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2750 false, 0);
2751 if (ret) {
2752 shost_printk(KERN_ERR, phba->shost,
2753 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302754 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302755 goto create_cq_error;
2756 }
2757 SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
2758 cq->id, eq->id);
2759 SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302760 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302761 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302762
2763create_cq_error:
2764 for (i = 0; i < phba->num_cpus; i++) {
2765 cq = &phwi_context->be_cq[i];
2766 mem = &cq->dma_mem;
2767 if (mem->va)
2768 pci_free_consistent(phba->pcidev, num_cq_pages
2769 * PAGE_SIZE,
2770 mem->va, mem->dma);
2771 }
2772 return ret;
2773
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302774}
2775
2776static int
2777beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2778 struct hwi_context_memory *phwi_context,
2779 struct hwi_controller *phwi_ctrlr,
2780 unsigned int def_pdu_ring_sz)
2781{
2782 unsigned int idx;
2783 int ret;
2784 struct be_queue_info *dq, *cq;
2785 struct be_dma_mem *mem;
2786 struct be_mem_descriptor *mem_descr;
2787 void *dq_vaddress;
2788
2789 idx = 0;
2790 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302791 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302792 mem = &dq->dma_mem;
2793 mem_descr = phba->init_mem;
2794 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2795 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2796 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2797 sizeof(struct phys_addr),
2798 sizeof(struct phys_addr), dq_vaddress);
2799 if (ret) {
2800 shost_printk(KERN_ERR, phba->shost,
2801 "be_fill_queue Failed for DEF PDU HDR\n");
2802 return ret;
2803 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302804 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2805 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302806 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2807 def_pdu_ring_sz,
2808 phba->params.defpdu_hdr_sz);
2809 if (ret) {
2810 shost_printk(KERN_ERR, phba->shost,
2811 "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
2812 return ret;
2813 }
2814 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
2815 SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
2816 phwi_context->be_def_hdrq.id);
2817 hwi_post_async_buffers(phba, 1);
2818 return 0;
2819}
2820
2821static int
2822beiscsi_create_def_data(struct beiscsi_hba *phba,
2823 struct hwi_context_memory *phwi_context,
2824 struct hwi_controller *phwi_ctrlr,
2825 unsigned int def_pdu_ring_sz)
2826{
2827 unsigned int idx;
2828 int ret;
2829 struct be_queue_info *dataq, *cq;
2830 struct be_dma_mem *mem;
2831 struct be_mem_descriptor *mem_descr;
2832 void *dq_vaddress;
2833
2834 idx = 0;
2835 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302836 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302837 mem = &dataq->dma_mem;
2838 mem_descr = phba->init_mem;
2839 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2840 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2841 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2842 sizeof(struct phys_addr),
2843 sizeof(struct phys_addr), dq_vaddress);
2844 if (ret) {
2845 shost_printk(KERN_ERR, phba->shost,
2846 "be_fill_queue Failed for DEF PDU DATA\n");
2847 return ret;
2848 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302849 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2850 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302851 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2852 def_pdu_ring_sz,
2853 phba->params.defpdu_data_sz);
2854 if (ret) {
2855 shost_printk(KERN_ERR, phba->shost,
2856 "be_cmd_create_default_pdu_queue Failed"
2857 " for DEF PDU DATA\n");
2858 return ret;
2859 }
2860 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
2861 SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
2862 phwi_context->be_def_dataq.id);
2863 hwi_post_async_buffers(phba, 0);
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302864 SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302865 return 0;
2866}
2867
2868static int
2869beiscsi_post_pages(struct beiscsi_hba *phba)
2870{
2871 struct be_mem_descriptor *mem_descr;
2872 struct mem_array *pm_arr;
2873 unsigned int page_offset, i;
2874 struct be_dma_mem sgl;
2875 int status;
2876
2877 mem_descr = phba->init_mem;
2878 mem_descr += HWI_MEM_SGE;
2879 pm_arr = mem_descr->mem_array;
2880
2881 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
2882 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
2883 for (i = 0; i < mem_descr->num_elements; i++) {
2884 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
2885 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
2886 page_offset,
2887 (pm_arr->size / PAGE_SIZE));
2888 page_offset += pm_arr->size / PAGE_SIZE;
2889 if (status != 0) {
2890 shost_printk(KERN_ERR, phba->shost,
2891 "post sgl failed.\n");
2892 return status;
2893 }
2894 pm_arr++;
2895 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302896 SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302897 return 0;
2898}
2899
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302900static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
2901{
2902 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002903 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302904 pci_free_consistent(phba->pcidev, mem->size,
2905 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002906 mem->va = NULL;
2907 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302908}
2909
2910static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
2911 u16 len, u16 entry_size)
2912{
2913 struct be_dma_mem *mem = &q->dma_mem;
2914
2915 memset(q, 0, sizeof(*q));
2916 q->len = len;
2917 q->entry_size = entry_size;
2918 mem->size = len * entry_size;
2919 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
2920 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05302921 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302922 memset(mem->va, 0, mem->size);
2923 return 0;
2924}
2925
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302926static int
2927beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
2928 struct hwi_context_memory *phwi_context,
2929 struct hwi_controller *phwi_ctrlr)
2930{
2931 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
2932 u64 pa_addr_lo;
2933 unsigned int idx, num, i;
2934 struct mem_array *pwrb_arr;
2935 void *wrb_vaddr;
2936 struct be_dma_mem sgl;
2937 struct be_mem_descriptor *mem_descr;
2938 int status;
2939
2940 idx = 0;
2941 mem_descr = phba->init_mem;
2942 mem_descr += HWI_MEM_WRB;
2943 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
2944 GFP_KERNEL);
2945 if (!pwrb_arr) {
2946 shost_printk(KERN_ERR, phba->shost,
2947 "Memory alloc failed in create wrb ring.\n");
2948 return -ENOMEM;
2949 }
2950 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2951 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
2952 num_wrb_rings = mem_descr->mem_array[idx].size /
2953 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
2954
2955 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
2956 if (num_wrb_rings) {
2957 pwrb_arr[num].virtual_address = wrb_vaddr;
2958 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
2959 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2960 sizeof(struct iscsi_wrb);
2961 wrb_vaddr += pwrb_arr[num].size;
2962 pa_addr_lo += pwrb_arr[num].size;
2963 num_wrb_rings--;
2964 } else {
2965 idx++;
2966 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2967 pa_addr_lo = mem_descr->mem_array[idx].\
2968 bus_address.u.a64.address;
2969 num_wrb_rings = mem_descr->mem_array[idx].size /
2970 (phba->params.wrbs_per_cxn *
2971 sizeof(struct iscsi_wrb));
2972 pwrb_arr[num].virtual_address = wrb_vaddr;
2973 pwrb_arr[num].bus_address.u.a64.address\
2974 = pa_addr_lo;
2975 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2976 sizeof(struct iscsi_wrb);
2977 wrb_vaddr += pwrb_arr[num].size;
2978 pa_addr_lo += pwrb_arr[num].size;
2979 num_wrb_rings--;
2980 }
2981 }
2982 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2983 wrb_mem_index = 0;
2984 offset = 0;
2985 size = 0;
2986
2987 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
2988 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
2989 &phwi_context->be_wrbq[i]);
2990 if (status != 0) {
2991 shost_printk(KERN_ERR, phba->shost,
2992 "wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02002993 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302994 return status;
2995 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302996 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
2997 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302998 }
2999 kfree(pwrb_arr);
3000 return 0;
3001}
3002
3003static void free_wrb_handles(struct beiscsi_hba *phba)
3004{
3005 unsigned int index;
3006 struct hwi_controller *phwi_ctrlr;
3007 struct hwi_wrb_context *pwrb_context;
3008
3009 phwi_ctrlr = phba->phwi_ctrlr;
3010 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3011 pwrb_context = &phwi_ctrlr->wrb_context[index];
3012 kfree(pwrb_context->pwrb_handle_base);
3013 kfree(pwrb_context->pwrb_handle_basestd);
3014 }
3015}
3016
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303017static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3018{
3019 struct be_queue_info *q;
3020 struct be_ctrl_info *ctrl = &phba->ctrl;
3021
3022 q = &phba->ctrl.mcc_obj.q;
3023 if (q->created)
3024 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3025 be_queue_free(phba, q);
3026
3027 q = &phba->ctrl.mcc_obj.cq;
3028 if (q->created)
3029 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3030 be_queue_free(phba, q);
3031}
3032
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303033static void hwi_cleanup(struct beiscsi_hba *phba)
3034{
3035 struct be_queue_info *q;
3036 struct be_ctrl_info *ctrl = &phba->ctrl;
3037 struct hwi_controller *phwi_ctrlr;
3038 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303039 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303040
3041 phwi_ctrlr = phba->phwi_ctrlr;
3042 phwi_context = phwi_ctrlr->phwi_ctxt;
3043 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3044 q = &phwi_context->be_wrbq[i];
3045 if (q->created)
3046 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3047 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303048 free_wrb_handles(phba);
3049
3050 q = &phwi_context->be_def_hdrq;
3051 if (q->created)
3052 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3053
3054 q = &phwi_context->be_def_dataq;
3055 if (q->created)
3056 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3057
3058 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3059
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303060 for (i = 0; i < (phba->num_cpus); i++) {
3061 q = &phwi_context->be_cq[i];
3062 if (q->created)
3063 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3064 }
3065 if (phba->msix_enabled)
3066 eq_num = 1;
3067 else
3068 eq_num = 0;
3069 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3070 q = &phwi_context->be_eq[i].q;
3071 if (q->created)
3072 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3073 }
3074 be_mcc_queues_destroy(phba);
3075}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303076
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303077static int be_mcc_queues_create(struct beiscsi_hba *phba,
3078 struct hwi_context_memory *phwi_context)
3079{
3080 struct be_queue_info *q, *cq;
3081 struct be_ctrl_info *ctrl = &phba->ctrl;
3082
3083 /* Alloc MCC compl queue */
3084 cq = &phba->ctrl.mcc_obj.cq;
3085 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3086 sizeof(struct be_mcc_compl)))
3087 goto err;
3088 /* Ask BE to create MCC compl queue; */
3089 if (phba->msix_enabled) {
3090 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3091 [phba->num_cpus].q, false, true, 0))
3092 goto mcc_cq_free;
3093 } else {
3094 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3095 false, true, 0))
3096 goto mcc_cq_free;
3097 }
3098
3099 /* Alloc MCC queue */
3100 q = &phba->ctrl.mcc_obj.q;
3101 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3102 goto mcc_cq_destroy;
3103
3104 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303105 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303106 goto mcc_q_free;
3107
3108 return 0;
3109
3110mcc_q_free:
3111 be_queue_free(phba, q);
3112mcc_cq_destroy:
3113 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3114mcc_cq_free:
3115 be_queue_free(phba, cq);
3116err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303117 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303118}
3119
3120static int find_num_cpus(void)
3121{
3122 int num_cpus = 0;
3123
3124 num_cpus = num_online_cpus();
3125 if (num_cpus >= MAX_CPUS)
3126 num_cpus = MAX_CPUS - 1;
3127
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303128 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303129 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303130}
3131
3132static int hwi_init_port(struct beiscsi_hba *phba)
3133{
3134 struct hwi_controller *phwi_ctrlr;
3135 struct hwi_context_memory *phwi_context;
3136 unsigned int def_pdu_ring_sz;
3137 struct be_ctrl_info *ctrl = &phba->ctrl;
3138 int status;
3139
3140 def_pdu_ring_sz =
3141 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3142 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303143 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303144 phwi_context->max_eqd = 0;
3145 phwi_context->min_eqd = 0;
3146 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303147 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303148
3149 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303150 if (status != 0) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303151 shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303152 goto error;
3153 }
3154
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303155 status = be_mcc_queues_create(phba, phwi_context);
3156 if (status != 0)
3157 goto error;
3158
3159 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303160 if (status != 0) {
3161 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303162 "Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303163 goto error;
3164 }
3165
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303166 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303167 if (status != 0) {
3168 shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
3169 goto error;
3170 }
3171
3172 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3173 def_pdu_ring_sz);
3174 if (status != 0) {
3175 shost_printk(KERN_ERR, phba->shost,
3176 "Default Header not created\n");
3177 goto error;
3178 }
3179
3180 status = beiscsi_create_def_data(phba, phwi_context,
3181 phwi_ctrlr, def_pdu_ring_sz);
3182 if (status != 0) {
3183 shost_printk(KERN_ERR, phba->shost,
3184 "Default Data not created\n");
3185 goto error;
3186 }
3187
3188 status = beiscsi_post_pages(phba);
3189 if (status != 0) {
3190 shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
3191 goto error;
3192 }
3193
3194 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3195 if (status != 0) {
3196 shost_printk(KERN_ERR, phba->shost,
3197 "WRB Rings not created\n");
3198 goto error;
3199 }
3200
3201 SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
3202 return 0;
3203
3204error:
3205 shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
3206 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003207 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303208}
3209
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303210static int hwi_init_controller(struct beiscsi_hba *phba)
3211{
3212 struct hwi_controller *phwi_ctrlr;
3213
3214 phwi_ctrlr = phba->phwi_ctrlr;
3215 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3216 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3217 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303218 SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303219 phwi_ctrlr->phwi_ctxt);
3220 } else {
3221 shost_printk(KERN_ERR, phba->shost,
3222 "HWI_MEM_ADDN_CONTEXT is more than one element."
3223 "Failing to load\n");
3224 return -ENOMEM;
3225 }
3226
3227 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003228 if (beiscsi_init_wrb_handle(phba))
3229 return -ENOMEM;
3230
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303231 hwi_init_async_pdu_ctx(phba);
3232 if (hwi_init_port(phba) != 0) {
3233 shost_printk(KERN_ERR, phba->shost,
3234 "hwi_init_controller failed\n");
3235 return -ENOMEM;
3236 }
3237 return 0;
3238}
3239
3240static void beiscsi_free_mem(struct beiscsi_hba *phba)
3241{
3242 struct be_mem_descriptor *mem_descr;
3243 int i, j;
3244
3245 mem_descr = phba->init_mem;
3246 i = 0;
3247 j = 0;
3248 for (i = 0; i < SE_MEM_MAX; i++) {
3249 for (j = mem_descr->num_elements; j > 0; j--) {
3250 pci_free_consistent(phba->pcidev,
3251 mem_descr->mem_array[j - 1].size,
3252 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303253 (unsigned long)mem_descr->mem_array[j - 1].
3254 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303255 }
3256 kfree(mem_descr->mem_array);
3257 mem_descr++;
3258 }
3259 kfree(phba->init_mem);
3260 kfree(phba->phwi_ctrlr);
3261}
3262
3263static int beiscsi_init_controller(struct beiscsi_hba *phba)
3264{
3265 int ret = -ENOMEM;
3266
3267 ret = beiscsi_get_memory(phba);
3268 if (ret < 0) {
3269 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303270 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303271 return ret;
3272 }
3273
3274 ret = hwi_init_controller(phba);
3275 if (ret)
3276 goto free_init;
3277 SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
3278 return 0;
3279
3280free_init:
3281 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003282 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303283}
3284
3285static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3286{
3287 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3288 struct sgl_handle *psgl_handle;
3289 struct iscsi_sge *pfrag;
3290 unsigned int arr_index, i, idx;
3291
3292 phba->io_sgl_hndl_avbl = 0;
3293 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303294
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303295 mem_descr_sglh = phba->init_mem;
3296 mem_descr_sglh += HWI_MEM_SGLH;
3297 if (1 == mem_descr_sglh->num_elements) {
3298 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3299 phba->params.ios_per_ctrl,
3300 GFP_KERNEL);
3301 if (!phba->io_sgl_hndl_base) {
3302 shost_printk(KERN_ERR, phba->shost,
3303 "Mem Alloc Failed. Failing to load\n");
3304 return -ENOMEM;
3305 }
3306 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3307 (phba->params.icds_per_ctrl -
3308 phba->params.ios_per_ctrl),
3309 GFP_KERNEL);
3310 if (!phba->eh_sgl_hndl_base) {
3311 kfree(phba->io_sgl_hndl_base);
3312 shost_printk(KERN_ERR, phba->shost,
3313 "Mem Alloc Failed. Failing to load\n");
3314 return -ENOMEM;
3315 }
3316 } else {
3317 shost_printk(KERN_ERR, phba->shost,
3318 "HWI_MEM_SGLH is more than one element."
3319 "Failing to load\n");
3320 return -ENOMEM;
3321 }
3322
3323 arr_index = 0;
3324 idx = 0;
3325 while (idx < mem_descr_sglh->num_elements) {
3326 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3327
3328 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3329 sizeof(struct sgl_handle)); i++) {
3330 if (arr_index < phba->params.ios_per_ctrl) {
3331 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3332 phba->io_sgl_hndl_avbl++;
3333 arr_index++;
3334 } else {
3335 phba->eh_sgl_hndl_base[arr_index -
3336 phba->params.ios_per_ctrl] =
3337 psgl_handle;
3338 arr_index++;
3339 phba->eh_sgl_hndl_avbl++;
3340 }
3341 psgl_handle++;
3342 }
3343 idx++;
3344 }
3345 SE_DEBUG(DBG_LVL_8,
3346 "phba->io_sgl_hndl_avbl=%d"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303347 "phba->eh_sgl_hndl_avbl=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303348 phba->io_sgl_hndl_avbl,
3349 phba->eh_sgl_hndl_avbl);
3350 mem_descr_sg = phba->init_mem;
3351 mem_descr_sg += HWI_MEM_SGE;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303352 SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303353 mem_descr_sg->num_elements);
3354 arr_index = 0;
3355 idx = 0;
3356 while (idx < mem_descr_sg->num_elements) {
3357 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3358
3359 for (i = 0;
3360 i < (mem_descr_sg->mem_array[idx].size) /
3361 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3362 i++) {
3363 if (arr_index < phba->params.ios_per_ctrl)
3364 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3365 else
3366 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3367 phba->params.ios_per_ctrl];
3368 psgl_handle->pfrag = pfrag;
3369 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3370 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3371 pfrag += phba->params.num_sge_per_io;
3372 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303373 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303374 }
3375 idx++;
3376 }
3377 phba->io_sgl_free_index = 0;
3378 phba->io_sgl_alloc_index = 0;
3379 phba->eh_sgl_free_index = 0;
3380 phba->eh_sgl_alloc_index = 0;
3381 return 0;
3382}
3383
3384static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3385{
3386 int i, new_cid;
3387
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303388 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303389 GFP_KERNEL);
3390 if (!phba->cid_array) {
3391 shost_printk(KERN_ERR, phba->shost,
3392 "Failed to allocate memory in "
3393 "hba_setup_cid_tbls\n");
3394 return -ENOMEM;
3395 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303396 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303397 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3398 if (!phba->ep_array) {
3399 shost_printk(KERN_ERR, phba->shost,
3400 "Failed to allocate memory in "
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303401 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303402 kfree(phba->cid_array);
3403 return -ENOMEM;
3404 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303405 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303406 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3407 phba->cid_array[i] = new_cid;
3408 new_cid += 2;
3409 }
3410 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3411 return 0;
3412}
3413
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303414static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303415{
3416 struct be_ctrl_info *ctrl = &phba->ctrl;
3417 struct hwi_controller *phwi_ctrlr;
3418 struct hwi_context_memory *phwi_context;
3419 struct be_queue_info *eq;
3420 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303421 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303422 u32 enabled;
3423
3424 phwi_ctrlr = phba->phwi_ctrlr;
3425 phwi_context = phwi_ctrlr->phwi_ctxt;
3426
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303427 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3428 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3429 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303430
3431 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3432 if (!enabled) {
3433 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303434 SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303435 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003436 }
3437
3438 if (!phba->msix_enabled) {
3439 eq = &phwi_context->be_eq[0].q;
3440 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
3441 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3442 } else {
3443 for (i = 0; i <= phba->num_cpus; i++) {
3444 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303445 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303446 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3447 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303448 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303449}
3450
3451static void hwi_disable_intr(struct beiscsi_hba *phba)
3452{
3453 struct be_ctrl_info *ctrl = &phba->ctrl;
3454
3455 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3456 u32 reg = ioread32(addr);
3457
3458 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3459 if (enabled) {
3460 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3461 iowrite32(reg, addr);
3462 } else
3463 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303464 "In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303465}
3466
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303467static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3468{
3469 struct be_cmd_resp_get_boot_target *boot_resp;
3470 struct be_cmd_resp_get_session *session_resp;
3471 struct be_mcc_wrb *wrb;
3472 struct be_dma_mem nonemb_cmd;
3473 unsigned int tag, wrb_num;
3474 unsigned short status, extd_status;
3475 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003476 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303477
3478 tag = beiscsi_get_boot_target(phba);
3479 if (!tag) {
3480 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
3481 return -EAGAIN;
3482 } else
3483 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3484 phba->ctrl.mcc_numtag[tag]);
3485
3486 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3487 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3488 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3489 if (status || extd_status) {
3490 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
3491 " status = %d extd_status = %d\n",
3492 status, extd_status);
3493 free_mcc_tag(&phba->ctrl, tag);
3494 return -EBUSY;
3495 }
3496 wrb = queue_get_wrb(mccq, wrb_num);
3497 free_mcc_tag(&phba->ctrl, tag);
3498 boot_resp = embedded_payload(wrb);
3499
3500 if (boot_resp->boot_session_handle < 0) {
Mike Christief457a462011-06-24 15:11:53 -05003501 shost_printk(KERN_INFO, phba->shost, "No Boot Session.\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303502 return -ENXIO;
3503 }
3504
3505 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3506 sizeof(*session_resp),
3507 &nonemb_cmd.dma);
3508 if (nonemb_cmd.va == NULL) {
3509 SE_DEBUG(DBG_LVL_1,
3510 "Failed to allocate memory for"
3511 "beiscsi_get_session_info\n");
3512 return -ENOMEM;
3513 }
3514
3515 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
3516 tag = beiscsi_get_session_info(phba,
3517 boot_resp->boot_session_handle, &nonemb_cmd);
3518 if (!tag) {
3519 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info"
3520 " Failed\n");
3521 goto boot_freemem;
3522 } else
3523 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3524 phba->ctrl.mcc_numtag[tag]);
3525
3526 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3527 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3528 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3529 if (status || extd_status) {
3530 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info Failed"
3531 " status = %d extd_status = %d\n",
3532 status, extd_status);
3533 free_mcc_tag(&phba->ctrl, tag);
3534 goto boot_freemem;
3535 }
3536 wrb = queue_get_wrb(mccq, wrb_num);
3537 free_mcc_tag(&phba->ctrl, tag);
3538 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003539
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303540 memcpy(&phba->boot_sess, &session_resp->session_info,
3541 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003542 ret = 0;
3543
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303544boot_freemem:
3545 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3546 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003547 return ret;
3548}
3549
3550static void beiscsi_boot_release(void *data)
3551{
3552 struct beiscsi_hba *phba = data;
3553
3554 scsi_host_put(phba->shost);
3555}
3556
3557static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3558{
3559 struct iscsi_boot_kobj *boot_kobj;
3560
3561 /* get boot info using mgmt cmd */
3562 if (beiscsi_get_boot_info(phba))
3563 /* Try to see if we can carry on without this */
3564 return 0;
3565
3566 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3567 if (!phba->boot_kset)
3568 return -ENOMEM;
3569
3570 /* get a ref because the show function will ref the phba */
3571 if (!scsi_host_get(phba->shost))
3572 goto free_kset;
3573 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3574 beiscsi_show_boot_tgt_info,
3575 beiscsi_tgt_get_attr_visibility,
3576 beiscsi_boot_release);
3577 if (!boot_kobj)
3578 goto put_shost;
3579
3580 if (!scsi_host_get(phba->shost))
3581 goto free_kset;
3582 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3583 beiscsi_show_boot_ini_info,
3584 beiscsi_ini_get_attr_visibility,
3585 beiscsi_boot_release);
3586 if (!boot_kobj)
3587 goto put_shost;
3588
3589 if (!scsi_host_get(phba->shost))
3590 goto free_kset;
3591 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3592 beiscsi_show_boot_eth_info,
3593 beiscsi_eth_get_attr_visibility,
3594 beiscsi_boot_release);
3595 if (!boot_kobj)
3596 goto put_shost;
3597 return 0;
3598
3599put_shost:
3600 scsi_host_put(phba->shost);
3601free_kset:
3602 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303603 return -ENOMEM;
3604}
3605
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303606static int beiscsi_init_port(struct beiscsi_hba *phba)
3607{
3608 int ret;
3609
3610 ret = beiscsi_init_controller(phba);
3611 if (ret < 0) {
3612 shost_printk(KERN_ERR, phba->shost,
3613 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303614 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303615 return ret;
3616 }
3617 ret = beiscsi_init_sgl_handle(phba);
3618 if (ret < 0) {
3619 shost_printk(KERN_ERR, phba->shost,
3620 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303621 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303622 goto do_cleanup_ctrlr;
3623 }
3624
3625 if (hba_setup_cid_tbls(phba)) {
3626 shost_printk(KERN_ERR, phba->shost,
3627 "Failed in hba_setup_cid_tbls\n");
3628 kfree(phba->io_sgl_hndl_base);
3629 kfree(phba->eh_sgl_hndl_base);
3630 goto do_cleanup_ctrlr;
3631 }
3632
3633 return ret;
3634
3635do_cleanup_ctrlr:
3636 hwi_cleanup(phba);
3637 return ret;
3638}
3639
3640static void hwi_purge_eq(struct beiscsi_hba *phba)
3641{
3642 struct hwi_controller *phwi_ctrlr;
3643 struct hwi_context_memory *phwi_context;
3644 struct be_queue_info *eq;
3645 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303646 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303647 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303648
3649 phwi_ctrlr = phba->phwi_ctrlr;
3650 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303651 if (phba->msix_enabled)
3652 eq_msix = 1;
3653 else
3654 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303655
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303656 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3657 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303658 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303659 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303660 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3661 & EQE_VALID_MASK) {
3662 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3663 queue_tail_inc(eq);
3664 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303665 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303666 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303667
3668 if (num_processed)
3669 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303670 }
3671}
3672
3673static void beiscsi_clean_port(struct beiscsi_hba *phba)
3674{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303675 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303676
3677 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3678 if (mgmt_status)
3679 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303680 "mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303681
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303682 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303683 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303684 kfree(phba->io_sgl_hndl_base);
3685 kfree(phba->eh_sgl_hndl_base);
3686 kfree(phba->cid_array);
3687 kfree(phba->ep_array);
3688}
3689
Mike Christie1282ab72012-04-18 03:06:00 -05003690static void beiscsi_cleanup_task(struct iscsi_task *task)
3691{
3692 struct beiscsi_io_task *io_task = task->dd_data;
3693 struct iscsi_conn *conn = task->conn;
3694 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3695 struct beiscsi_hba *phba = beiscsi_conn->phba;
3696 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3697 struct hwi_wrb_context *pwrb_context;
3698 struct hwi_controller *phwi_ctrlr;
3699
3700 phwi_ctrlr = phba->phwi_ctrlr;
3701 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3702 - phba->fw_config.iscsi_cid_start];
3703
3704 if (io_task->cmd_bhs) {
3705 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3706 io_task->bhs_pa.u.a64.address);
3707 io_task->cmd_bhs = NULL;
3708 }
3709
3710 if (task->sc) {
3711 if (io_task->pwrb_handle) {
3712 free_wrb_handle(phba, pwrb_context,
3713 io_task->pwrb_handle);
3714 io_task->pwrb_handle = NULL;
3715 }
3716
3717 if (io_task->psgl_handle) {
3718 spin_lock(&phba->io_sgl_lock);
3719 free_io_sgl_handle(phba, io_task->psgl_handle);
3720 spin_unlock(&phba->io_sgl_lock);
3721 io_task->psgl_handle = NULL;
3722 }
3723 } else {
3724 if (!beiscsi_conn->login_in_progress) {
3725 if (io_task->pwrb_handle) {
3726 free_wrb_handle(phba, pwrb_context,
3727 io_task->pwrb_handle);
3728 io_task->pwrb_handle = NULL;
3729 }
3730 if (io_task->psgl_handle) {
3731 spin_lock(&phba->mgmt_sgl_lock);
3732 free_mgmt_sgl_handle(phba,
3733 io_task->psgl_handle);
3734 spin_unlock(&phba->mgmt_sgl_lock);
3735 io_task->psgl_handle = NULL;
3736 }
3737 }
3738 }
3739}
3740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303741void
3742beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3743 struct beiscsi_offload_params *params)
3744{
3745 struct wrb_handle *pwrb_handle;
3746 struct iscsi_target_context_update_wrb *pwrb = NULL;
3747 struct be_mem_descriptor *mem_descr;
3748 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003749 struct iscsi_task *task = beiscsi_conn->task;
3750 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303751 u32 doorbell = 0;
3752
3753 /*
3754 * We can always use 0 here because it is reserved by libiscsi for
3755 * login/startup related tasks.
3756 */
Mike Christie1282ab72012-04-18 03:06:00 -05003757 beiscsi_conn->login_in_progress = 0;
3758 spin_lock_bh(&session->lock);
3759 beiscsi_cleanup_task(task);
3760 spin_unlock_bh(&session->lock);
3761
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303762 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303763 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303764 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3765 memset(pwrb, 0, sizeof(*pwrb));
3766 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3767 max_burst_length, pwrb, params->dw[offsetof
3768 (struct amap_beiscsi_offload_params,
3769 max_burst_length) / 32]);
3770 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3771 max_send_data_segment_length, pwrb,
3772 params->dw[offsetof(struct amap_beiscsi_offload_params,
3773 max_send_data_segment_length) / 32]);
3774 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3775 first_burst_length,
3776 pwrb,
3777 params->dw[offsetof(struct amap_beiscsi_offload_params,
3778 first_burst_length) / 32]);
3779
3780 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3781 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3782 erl) / 32] & OFFLD_PARAMS_ERL));
3783 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3784 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3785 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3786 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3787 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3788 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3789 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3790 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3791 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3792 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3793 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3794 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3795 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3796 pwrb,
3797 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3798 exp_statsn) / 32] + 1));
3799 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3800 0x7);
3801 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3802 pwrb, pwrb_handle->wrb_index);
3803 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3804 pwrb, pwrb_handle->nxt_wrb_index);
3805 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3806 session_state, pwrb, 0);
3807 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3808 pwrb, 1);
3809 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3810 pwrb, 0);
3811 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3812 0);
3813
3814 mem_descr = phba->init_mem;
3815 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3816
3817 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3818 pad_buffer_addr_hi, pwrb,
3819 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3820 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3821 pad_buffer_addr_lo, pwrb,
3822 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3823
3824 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3825
3826 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303827 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303828 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303829 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3830
3831 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3832}
3833
3834static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
3835 int *index, int *age)
3836{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303837 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303838 if (age)
3839 *age = conn->session->age;
3840}
3841
3842/**
3843 * beiscsi_alloc_pdu - allocates pdu and related resources
3844 * @task: libiscsi task
3845 * @opcode: opcode of pdu for task
3846 *
3847 * This is called with the session lock held. It will allocate
3848 * the wrb and sgl if needed for the command. And it will prep
3849 * the pdu's itt. beiscsi_parse_pdu will later translate
3850 * the pdu itt to the libiscsi task itt.
3851 */
3852static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3853{
3854 struct beiscsi_io_task *io_task = task->dd_data;
3855 struct iscsi_conn *conn = task->conn;
3856 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3857 struct beiscsi_hba *phba = beiscsi_conn->phba;
3858 struct hwi_wrb_context *pwrb_context;
3859 struct hwi_controller *phwi_ctrlr;
3860 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303861 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3862 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303863
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303864 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06003865 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303866 if (!io_task->cmd_bhs)
3867 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303868 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303869 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303870 io_task->conn = beiscsi_conn;
3871
3872 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
3873 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303874 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003875 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303876
3877 if (task->sc) {
3878 spin_lock(&phba->io_sgl_lock);
3879 io_task->psgl_handle = alloc_io_sgl_handle(phba);
3880 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303881 if (!io_task->psgl_handle)
3882 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303883 io_task->pwrb_handle = alloc_wrb_handle(phba,
3884 beiscsi_conn->beiscsi_conn_cid -
3885 phba->fw_config.iscsi_cid_start);
3886 if (!io_task->pwrb_handle)
3887 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303888 } else {
3889 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05303890 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303891 if (!beiscsi_conn->login_in_progress) {
3892 spin_lock(&phba->mgmt_sgl_lock);
3893 io_task->psgl_handle = (struct sgl_handle *)
3894 alloc_mgmt_sgl_handle(phba);
3895 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303896 if (!io_task->psgl_handle)
3897 goto free_hndls;
3898
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899 beiscsi_conn->login_in_progress = 1;
3900 beiscsi_conn->plogin_sgl_handle =
3901 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303902 io_task->pwrb_handle =
3903 alloc_wrb_handle(phba,
3904 beiscsi_conn->beiscsi_conn_cid -
3905 phba->fw_config.iscsi_cid_start);
3906 if (!io_task->pwrb_handle)
3907 goto free_io_hndls;
3908 beiscsi_conn->plogin_wrb_handle =
3909 io_task->pwrb_handle;
3910
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303911 } else {
3912 io_task->psgl_handle =
3913 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303914 io_task->pwrb_handle =
3915 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303916 }
Mike Christie1282ab72012-04-18 03:06:00 -05003917 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303918 } else {
3919 spin_lock(&phba->mgmt_sgl_lock);
3920 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
3921 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303922 if (!io_task->psgl_handle)
3923 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303924 io_task->pwrb_handle =
3925 alloc_wrb_handle(phba,
3926 beiscsi_conn->beiscsi_conn_cid -
3927 phba->fw_config.iscsi_cid_start);
3928 if (!io_task->pwrb_handle)
3929 goto free_mgmt_hndls;
3930
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303931 }
3932 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303933 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
3934 wrb_index << 16) | (unsigned int)
3935 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303936 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303937
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303938 io_task->cmd_bhs->iscsi_hdr.itt = itt;
3939 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303940
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303941free_io_hndls:
3942 spin_lock(&phba->io_sgl_lock);
3943 free_io_sgl_handle(phba, io_task->psgl_handle);
3944 spin_unlock(&phba->io_sgl_lock);
3945 goto free_hndls;
3946free_mgmt_hndls:
3947 spin_lock(&phba->mgmt_sgl_lock);
3948 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
3949 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303950free_hndls:
3951 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303952 pwrb_context = &phwi_ctrlr->wrb_context[
3953 beiscsi_conn->beiscsi_conn_cid -
3954 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303955 if (io_task->pwrb_handle)
3956 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303957 io_task->pwrb_handle = NULL;
3958 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3959 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05003960 io_task->cmd_bhs = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303961 SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303962 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303963}
3964
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303965static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
3966 unsigned int num_sg, unsigned int xferlen,
3967 unsigned int writedir)
3968{
3969
3970 struct beiscsi_io_task *io_task = task->dd_data;
3971 struct iscsi_conn *conn = task->conn;
3972 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3973 struct beiscsi_hba *phba = beiscsi_conn->phba;
3974 struct iscsi_wrb *pwrb = NULL;
3975 unsigned int doorbell = 0;
3976
3977 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303978 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
3979 io_task->bhs_len = sizeof(struct be_cmd_bhs);
3980
3981 if (writedir) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303982 memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
3983 AMAP_SET_BITS(struct amap_pdu_data_out, itt,
3984 &io_task->cmd_bhs->iscsi_data_pdu,
3985 (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
3986 AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
3987 &io_task->cmd_bhs->iscsi_data_pdu,
3988 ISCSI_OPCODE_SCSI_DATA_OUT);
3989 AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
3990 &io_task->cmd_bhs->iscsi_data_pdu, 1);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303991 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3992 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303993 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303994 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303995 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3996 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303997 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
3998 }
3999 memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
4000 dw[offsetof(struct amap_pdu_data_out, lun) / 32],
Andy Grover516f43a2011-06-16 15:57:09 -07004001 &io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304002
4003 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004004 cpu_to_be16(*(unsigned short *)
4005 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304006 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4007 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4008 io_task->pwrb_handle->wrb_index);
4009 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4010 be32_to_cpu(task->cmdsn));
4011 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4012 io_task->psgl_handle->sgl_index);
4013
4014 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4015
4016 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4017 io_task->pwrb_handle->nxt_wrb_index);
4018 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4019
4020 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304021 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304022 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4023 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4024
4025 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4026 return 0;
4027}
4028
4029static int beiscsi_mtask(struct iscsi_task *task)
4030{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304031 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304032 struct iscsi_conn *conn = task->conn;
4033 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4034 struct beiscsi_hba *phba = beiscsi_conn->phba;
4035 struct iscsi_wrb *pwrb = NULL;
4036 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304037 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304038
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304039 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304040 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304041 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304042 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4043 be32_to_cpu(task->cmdsn));
4044 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4045 io_task->pwrb_handle->wrb_index);
4046 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4047 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304048
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304049 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4050 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304051 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4052 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304053 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4054 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4055 hwi_write_buffer(pwrb, task);
4056 break;
4057 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004058 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4059 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4060 TGT_DM_CMD);
4061 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4062 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004063 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004064 } else {
4065 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4066 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004067 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004068 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304069 hwi_write_buffer(pwrb, task);
4070 break;
4071 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304072 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304073 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304074 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304075 hwi_write_buffer(pwrb, task);
4076 break;
4077 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304078 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4079 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304080 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4081 hwi_write_buffer(pwrb, task);
4082 break;
4083 case ISCSI_OP_LOGOUT:
4084 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4085 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304086 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304087 hwi_write_buffer(pwrb, task);
4088 break;
4089
4090 default:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304091 SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304092 task->hdr->opcode & ISCSI_OPCODE_MASK);
4093 return -EINVAL;
4094 }
4095
4096 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304097 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304098 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4099 io_task->pwrb_handle->nxt_wrb_index);
4100 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4101
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304102 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304103 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304104 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4105 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4106 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4107 return 0;
4108}
4109
4110static int beiscsi_task_xmit(struct iscsi_task *task)
4111{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304112 struct beiscsi_io_task *io_task = task->dd_data;
4113 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304114 struct scatterlist *sg;
4115 int num_sg;
4116 unsigned int writedir = 0, xferlen = 0;
4117
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304118 if (!sc)
4119 return beiscsi_mtask(task);
4120
4121 io_task->scsi_cmnd = sc;
4122 num_sg = scsi_dma_map(sc);
4123 if (num_sg < 0) {
4124 SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
4125 return num_sg;
4126 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304127 xferlen = scsi_bufflen(sc);
4128 sg = scsi_sglist(sc);
4129 if (sc->sc_data_direction == DMA_TO_DEVICE) {
4130 writedir = 1;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304131 SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304132 task->imm_count);
4133 } else
4134 writedir = 0;
4135 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4136}
4137
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004138static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304139{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304140 struct hwi_controller *phwi_ctrlr;
4141 struct hwi_context_memory *phwi_context;
4142 struct be_eq_obj *pbe_eq;
4143 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304144 u8 *real_offset = 0;
4145 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304146
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304147 phwi_ctrlr = phba->phwi_ctrlr;
4148 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304149 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304150 if (phba->msix_enabled) {
4151 for (i = 0; i <= phba->num_cpus; i++) {
4152 msix_vec = phba->msix_entries[i].vector;
4153 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004154 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304155 }
4156 } else
4157 if (phba->pcidev->irq)
4158 free_irq(phba->pcidev->irq, phba);
4159 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304160 destroy_workqueue(phba->wq);
4161 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304162 for (i = 0; i < phba->num_cpus; i++) {
4163 pbe_eq = &phwi_context->be_eq[i];
4164 blk_iopoll_disable(&pbe_eq->iopoll);
4165 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304166
4167 beiscsi_clean_port(phba);
4168 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304169 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4170
4171 value = readl((void *)real_offset);
4172
4173 if (value & 0x00010000) {
4174 value &= 0xfffeffff;
4175 writel(value, (void *)real_offset);
4176 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304177 beiscsi_unmap_pci_function(phba);
4178 pci_free_consistent(phba->pcidev,
4179 phba->ctrl.mbox_mem_alloced.size,
4180 phba->ctrl.mbox_mem_alloced.va,
4181 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004182}
4183
4184static void beiscsi_remove(struct pci_dev *pcidev)
4185{
4186
4187 struct beiscsi_hba *phba = NULL;
4188
4189 phba = pci_get_drvdata(pcidev);
4190 if (!phba) {
4191 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4192 return;
4193 }
4194
4195 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004196 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304197 iscsi_host_remove(phba->shost);
4198 pci_dev_put(phba->pcidev);
4199 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004200 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304201}
4202
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004203static void beiscsi_shutdown(struct pci_dev *pcidev)
4204{
4205
4206 struct beiscsi_hba *phba = NULL;
4207
4208 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4209 if (!phba) {
4210 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4211 return;
4212 }
4213
4214 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004215 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004216}
4217
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304218static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4219{
4220 int i, status;
4221
4222 for (i = 0; i <= phba->num_cpus; i++)
4223 phba->msix_entries[i].entry = i;
4224
4225 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4226 (phba->num_cpus + 1));
4227 if (!status)
4228 phba->msix_enabled = true;
4229
4230 return;
4231}
4232
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304233static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4234 const struct pci_device_id *id)
4235{
4236 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304237 struct hwi_controller *phwi_ctrlr;
4238 struct hwi_context_memory *phwi_context;
4239 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304240 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304241 u8 *real_offset = 0;
4242 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304243
4244 ret = beiscsi_enable_pci(pcidev);
4245 if (ret < 0) {
Dan Carpenter82284c02010-06-10 09:53:05 +02004246 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
4247 " Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304248 return ret;
4249 }
4250
4251 phba = beiscsi_hba_alloc(pcidev);
4252 if (!phba) {
4253 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304254 " Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304255 goto disable_pci;
4256 }
4257
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304258 switch (pcidev->device) {
4259 case BE_DEVICE_ID1:
4260 case OC_DEVICE_ID1:
4261 case OC_DEVICE_ID2:
4262 phba->generation = BE_GEN2;
4263 break;
4264 case BE_DEVICE_ID2:
4265 case OC_DEVICE_ID3:
4266 phba->generation = BE_GEN3;
4267 break;
4268 default:
4269 phba->generation = 0;
4270 }
4271
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304272 if (enable_msix)
4273 num_cpus = find_num_cpus();
4274 else
4275 num_cpus = 1;
4276 phba->num_cpus = num_cpus;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304277 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304278
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004279 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304280 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004281 if (!phba->msix_enabled)
4282 phba->num_cpus = 1;
4283 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304284 ret = be_ctrl_init(phba, pcidev);
4285 if (ret) {
4286 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4287 "Failed in be_ctrl_init\n");
4288 goto hba_free;
4289 }
4290
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304291 if (!num_hba) {
4292 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4293 value = readl((void *)real_offset);
4294 if (value & 0x00010000) {
4295 gcrashmode++;
4296 shost_printk(KERN_ERR, phba->shost,
4297 "Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004298 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304299 if (ret) {
4300 shost_printk(KERN_ERR, phba->shost,
4301 "Reset Failed. Aborting Crashdump\n");
4302 goto hba_free;
4303 }
4304 ret = be_chk_reset_complete(phba);
4305 if (ret) {
4306 shost_printk(KERN_ERR, phba->shost,
4307 "Failed to get out of reset."
4308 "Aborting Crashdump\n");
4309 goto hba_free;
4310 }
4311 } else {
4312 value |= 0x00010000;
4313 writel(value, (void *)real_offset);
4314 num_hba++;
4315 }
4316 }
4317
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304318 spin_lock_init(&phba->io_sgl_lock);
4319 spin_lock_init(&phba->mgmt_sgl_lock);
4320 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304321 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4322 if (ret != 0) {
4323 shost_printk(KERN_ERR, phba->shost,
4324 "Error getting fw config\n");
4325 goto free_port;
4326 }
4327 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304328 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304329 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304330 ret = beiscsi_init_port(phba);
4331 if (ret < 0) {
4332 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4333 "Failed in beiscsi_init_port\n");
4334 goto free_port;
4335 }
4336
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304337 for (i = 0; i < MAX_MCC_CMD ; i++) {
4338 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4339 phba->ctrl.mcc_tag[i] = i + 1;
4340 phba->ctrl.mcc_numtag[i + 1] = 0;
4341 phba->ctrl.mcc_tag_available++;
4342 }
4343
4344 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4345
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304346 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4347 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004348 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304349 if (!phba->wq) {
4350 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4351 "Failed to allocate work queue\n");
4352 goto free_twq;
4353 }
4354
4355 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4356
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304357 phwi_ctrlr = phba->phwi_ctrlr;
4358 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304359 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304360 for (i = 0; i < phba->num_cpus; i++) {
4361 pbe_eq = &phwi_context->be_eq[i];
4362 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4363 be_iopoll);
4364 blk_iopoll_enable(&pbe_eq->iopoll);
4365 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304366 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304367 ret = beiscsi_init_irqs(phba);
4368 if (ret < 0) {
4369 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4370 "Failed to beiscsi_init_irqs\n");
4371 goto free_blkenbld;
4372 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304373 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004374
4375 if (beiscsi_setup_boot_info(phba))
4376 /*
4377 * log error but continue, because we may not be using
4378 * iscsi boot.
4379 */
4380 shost_printk(KERN_ERR, phba->shost, "Could not set up "
4381 "iSCSI boot info.");
4382
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304383 SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304384 return 0;
4385
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304386free_blkenbld:
4387 destroy_workqueue(phba->wq);
4388 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304389 for (i = 0; i < phba->num_cpus; i++) {
4390 pbe_eq = &phwi_context->be_eq[i];
4391 blk_iopoll_disable(&pbe_eq->iopoll);
4392 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304393free_twq:
4394 beiscsi_clean_port(phba);
4395 beiscsi_free_mem(phba);
4396free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304397 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4398
4399 value = readl((void *)real_offset);
4400
4401 if (value & 0x00010000) {
4402 value &= 0xfffeffff;
4403 writel(value, (void *)real_offset);
4404 }
4405
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304406 pci_free_consistent(phba->pcidev,
4407 phba->ctrl.mbox_mem_alloced.size,
4408 phba->ctrl.mbox_mem_alloced.va,
4409 phba->ctrl.mbox_mem_alloced.dma);
4410 beiscsi_unmap_pci_function(phba);
4411hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304412 if (phba->msix_enabled)
4413 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304414 iscsi_host_remove(phba->shost);
4415 pci_dev_put(phba->pcidev);
4416 iscsi_host_free(phba->shost);
4417disable_pci:
4418 pci_disable_device(pcidev);
4419 return ret;
4420}
4421
4422struct iscsi_transport beiscsi_iscsi_transport = {
4423 .owner = THIS_MODULE,
4424 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304425 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304426 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304427 .create_session = beiscsi_session_create,
4428 .destroy_session = beiscsi_session_destroy,
4429 .create_conn = beiscsi_conn_create,
4430 .bind_conn = beiscsi_conn_bind,
4431 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004432 .attr_is_visible = be2iscsi_attr_is_visible,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304433 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004434 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304435 .get_session_param = iscsi_session_get_param,
4436 .get_host_param = beiscsi_get_host_param,
4437 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004438 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304439 .send_pdu = iscsi_conn_send_pdu,
4440 .xmit_task = beiscsi_task_xmit,
4441 .cleanup_task = beiscsi_cleanup_task,
4442 .alloc_pdu = beiscsi_alloc_pdu,
4443 .parse_pdu_itt = beiscsi_parse_pdu,
4444 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004445 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304446 .ep_connect = beiscsi_ep_connect,
4447 .ep_poll = beiscsi_ep_poll,
4448 .ep_disconnect = beiscsi_ep_disconnect,
4449 .session_recovery_timedout = iscsi_session_recovery_timedout,
4450};
4451
4452static struct pci_driver beiscsi_pci_driver = {
4453 .name = DRV_NAME,
4454 .probe = beiscsi_dev_probe,
4455 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004456 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304457 .id_table = beiscsi_pci_id_table
4458};
4459
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304460
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304461static int __init beiscsi_module_init(void)
4462{
4463 int ret;
4464
4465 beiscsi_scsi_transport =
4466 iscsi_register_transport(&beiscsi_iscsi_transport);
4467 if (!beiscsi_scsi_transport) {
4468 SE_DEBUG(DBG_LVL_1,
4469 "beiscsi_module_init - Unable to register beiscsi"
4470 "transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304471 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304472 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304473 SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304474 &beiscsi_iscsi_transport);
4475
4476 ret = pci_register_driver(&beiscsi_pci_driver);
4477 if (ret) {
4478 SE_DEBUG(DBG_LVL_1,
4479 "beiscsi_module_init - Unable to register"
4480 "beiscsi pci driver.\n");
4481 goto unregister_iscsi_transport;
4482 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304483 return 0;
4484
4485unregister_iscsi_transport:
4486 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4487 return ret;
4488}
4489
4490static void __exit beiscsi_module_exit(void)
4491{
4492 pci_unregister_driver(&beiscsi_pci_driver);
4493 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4494}
4495
4496module_init(beiscsi_module_init);
4497module_exit(beiscsi_module_exit);