blob: dad66de50aa0c8f76b517880dcd4064dcfe12f99 [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
1662 if (status == 0)
1663 hwi_free_async_msg(phba, cri);
1664 return 0;
1665}
1666
1667static unsigned int
1668hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1669 struct beiscsi_hba *phba,
1670 struct async_pdu_handle *pasync_handle)
1671{
1672 struct hwi_async_pdu_context *pasync_ctx;
1673 struct hwi_controller *phwi_ctrlr;
1674 unsigned int bytes_needed = 0, status = 0;
1675 unsigned short cri = pasync_handle->cri;
1676 struct pdu_base *ppdu;
1677
1678 phwi_ctrlr = phba->phwi_ctrlr;
1679 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1680
1681 list_del(&pasync_handle->link);
1682 if (pasync_handle->is_header) {
1683 pasync_ctx->async_header.busy_entries--;
1684 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1685 hwi_free_async_msg(phba, cri);
1686 BUG();
1687 }
1688
1689 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1690 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1691 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1692 (unsigned short)pasync_handle->buffer_len;
1693 list_add_tail(&pasync_handle->link,
1694 &pasync_ctx->async_entry[cri].wait_queue.list);
1695
1696 ppdu = pasync_handle->pbuffer;
1697 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1698 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1699 0xFFFF0000) | ((be16_to_cpu((ppdu->
1700 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1701 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1702
1703 if (status == 0) {
1704 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1705 bytes_needed;
1706
1707 if (bytes_needed == 0)
1708 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1709 pasync_ctx, cri);
1710 }
1711 } else {
1712 pasync_ctx->async_data.busy_entries--;
1713 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1714 list_add_tail(&pasync_handle->link,
1715 &pasync_ctx->async_entry[cri].wait_queue.
1716 list);
1717 pasync_ctx->async_entry[cri].wait_queue.
1718 bytes_received +=
1719 (unsigned short)pasync_handle->buffer_len;
1720
1721 if (pasync_ctx->async_entry[cri].wait_queue.
1722 bytes_received >=
1723 pasync_ctx->async_entry[cri].wait_queue.
1724 bytes_needed)
1725 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1726 pasync_ctx, cri);
1727 }
1728 }
1729 return status;
1730}
1731
1732static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1733 struct beiscsi_hba *phba,
1734 struct i_t_dpdu_cqe *pdpdu_cqe)
1735{
1736 struct hwi_controller *phwi_ctrlr;
1737 struct hwi_async_pdu_context *pasync_ctx;
1738 struct async_pdu_handle *pasync_handle = NULL;
1739 unsigned int cq_index = -1;
1740
1741 phwi_ctrlr = phba->phwi_ctrlr;
1742 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1743 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1744 pdpdu_cqe, &cq_index);
1745
1746 if (pasync_handle->consumed == 0)
1747 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1748 cq_index);
1749 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1750 hwi_post_async_buffers(phba, pasync_handle->is_header);
1751}
1752
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301753static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1754{
1755 struct be_queue_info *mcc_cq;
1756 struct be_mcc_compl *mcc_compl;
1757 unsigned int num_processed = 0;
1758
1759 mcc_cq = &phba->ctrl.mcc_obj.cq;
1760 mcc_compl = queue_tail_node(mcc_cq);
1761 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1762 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1763
1764 if (num_processed >= 32) {
1765 hwi_ring_cq_db(phba, mcc_cq->id,
1766 num_processed, 0, 0);
1767 num_processed = 0;
1768 }
1769 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1770 /* Interpret flags as an async trailer */
1771 if (is_link_state_evt(mcc_compl->flags))
1772 /* Interpret compl as a async link evt */
1773 beiscsi_async_link_state_process(phba,
1774 (struct be_async_event_link_state *) mcc_compl);
1775 else
1776 SE_DEBUG(DBG_LVL_1,
1777 " Unsupported Async Event, flags"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301778 " = 0x%08x\n", mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301779 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1780 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1781 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1782 }
1783
1784 mcc_compl->flags = 0;
1785 queue_tail_inc(mcc_cq);
1786 mcc_compl = queue_tail_node(mcc_cq);
1787 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1788 num_processed++;
1789 }
1790
1791 if (num_processed > 0)
1792 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1793
1794}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301795
1796static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301797{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301798 struct be_queue_info *cq;
1799 struct sol_cqe *sol;
1800 struct dmsg_cqe *dmsg;
1801 unsigned int num_processed = 0;
1802 unsigned int tot_nump = 0;
1803 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301804 struct beiscsi_endpoint *beiscsi_ep;
1805 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301806 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301807
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301808 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301809 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301810 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301811
1812 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1813 CQE_VALID_MASK) {
1814 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1815
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301816 ep = phba->ep_array[(u32) ((sol->
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301817 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
1818 SOL_CID_MASK) >> 6) -
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301819 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301820
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301821 beiscsi_ep = ep->dd_data;
1822 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301823
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301824 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301825 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301826 num_processed, 0, 0);
1827 tot_nump += num_processed;
1828 num_processed = 0;
1829 }
1830
1831 switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
1832 32] & CQE_CODE_MASK) {
1833 case SOL_CMD_COMPLETE:
1834 hwi_complete_cmd(beiscsi_conn, phba, sol);
1835 break;
1836 case DRIVERMSG_NOTIFY:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05301837 SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301838 dmsg = (struct dmsg_cqe *)sol;
1839 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1840 break;
1841 case UNSOL_HDR_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301842 SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
1843 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1844 (struct i_t_dpdu_cqe *)sol);
1845 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301846 case UNSOL_DATA_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301847 SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301848 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1849 (struct i_t_dpdu_cqe *)sol);
1850 break;
1851 case CXN_INVALIDATE_INDEX_NOTIFY:
1852 case CMD_INVALIDATED_NOTIFY:
1853 case CXN_INVALIDATE_NOTIFY:
1854 SE_DEBUG(DBG_LVL_1,
1855 "Ignoring CQ Error notification for cmd/cxn"
1856 "invalidate\n");
1857 break;
1858 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1859 case CMD_KILLED_INVALID_STATSN_RCVD:
1860 case CMD_KILLED_INVALID_R2T_RCVD:
1861 case CMD_CXN_KILLED_LUN_INVALID:
1862 case CMD_CXN_KILLED_ICD_INVALID:
1863 case CMD_CXN_KILLED_ITT_INVALID:
1864 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1865 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301866 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867 "CQ Error notification for cmd.. "
1868 "code %d cid 0x%x\n",
1869 sol->dw[offsetof(struct amap_sol_cqe, code) /
1870 32] & CQE_CODE_MASK,
1871 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1872 32] & SOL_CID_MASK));
1873 break;
1874 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1875 SE_DEBUG(DBG_LVL_1,
1876 "Digest error on def pdu ring, dropping..\n");
1877 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
1878 (struct i_t_dpdu_cqe *) sol);
1879 break;
1880 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1881 case CXN_KILLED_BURST_LEN_MISMATCH:
1882 case CXN_KILLED_AHS_RCVD:
1883 case CXN_KILLED_HDR_DIGEST_ERR:
1884 case CXN_KILLED_UNKNOWN_HDR:
1885 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1886 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1887 case CXN_KILLED_TIMED_OUT:
1888 case CXN_KILLED_FIN_RCVD:
1889 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1890 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1891 case CXN_KILLED_OVER_RUN_RESIDUAL:
1892 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1893 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301894 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301895 "0x%x...\n",
1896 sol->dw[offsetof(struct amap_sol_cqe, code) /
1897 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301898 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1899 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301900 iscsi_conn_failure(beiscsi_conn->conn,
1901 ISCSI_ERR_CONN_FAILED);
1902 break;
1903 case CXN_KILLED_RST_SENT:
1904 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301905 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301906 "received/sent on CID 0x%x...\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301907 sol->dw[offsetof(struct amap_sol_cqe, code) /
1908 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301909 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1910 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301911 iscsi_conn_failure(beiscsi_conn->conn,
1912 ISCSI_ERR_CONN_FAILED);
1913 break;
1914 default:
1915 SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
1916 "received on CID 0x%x...\n",
1917 sol->dw[offsetof(struct amap_sol_cqe, code) /
1918 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301919 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1920 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301921 break;
1922 }
1923
1924 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
1925 queue_tail_inc(cq);
1926 sol = queue_tail_node(cq);
1927 num_processed++;
1928 }
1929
1930 if (num_processed > 0) {
1931 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301932 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301933 }
1934 return tot_nump;
1935}
1936
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301937void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301938{
1939 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301940 struct hwi_controller *phwi_ctrlr;
1941 struct hwi_context_memory *phwi_context;
1942 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301943 struct beiscsi_hba *phba =
1944 container_of(work, struct beiscsi_hba, work_cqs);
1945
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301946 phwi_ctrlr = phba->phwi_ctrlr;
1947 phwi_context = phwi_ctrlr->phwi_ctxt;
1948 if (phba->msix_enabled)
1949 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
1950 else
1951 pbe_eq = &phwi_context->be_eq[0];
1952
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301953 if (phba->todo_mcc_cq) {
1954 spin_lock_irqsave(&phba->isr_lock, flags);
1955 phba->todo_mcc_cq = 0;
1956 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301957 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301958 }
1959
1960 if (phba->todo_cq) {
1961 spin_lock_irqsave(&phba->isr_lock, flags);
1962 phba->todo_cq = 0;
1963 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301964 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301965 }
1966}
1967
1968static int be_iopoll(struct blk_iopoll *iop, int budget)
1969{
1970 static unsigned int ret;
1971 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301972 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301973
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301974 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
1975 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301976 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301977 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301978 blk_iopoll_complete(iop);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301979 SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
1980 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301981 }
1982 return ret;
1983}
1984
1985static void
1986hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
1987 unsigned int num_sg, struct beiscsi_io_task *io_task)
1988{
1989 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05301990 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301991 unsigned int sge_len = 0;
1992 unsigned long long addr;
1993 struct scatterlist *l_sg;
1994 unsigned int offset;
1995
1996 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
1997 io_task->bhs_pa.u.a32.address_lo);
1998 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
1999 io_task->bhs_pa.u.a32.address_hi);
2000
2001 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302002 for (index = 0; (index < num_sg) && (index < 2); index++,
2003 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302004 if (index == 0) {
2005 sg_len = sg_dma_len(sg);
2006 addr = (u64) sg_dma_address(sg);
2007 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302008 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302009 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302010 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302011 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2012 sg_len);
2013 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302014 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302015 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2016 pwrb, sge_len);
2017 sg_len = sg_dma_len(sg);
2018 addr = (u64) sg_dma_address(sg);
2019 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302020 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302021 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302022 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302023 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2024 sg_len);
2025 }
2026 }
2027 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2028 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2029
2030 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2031
2032 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2033 io_task->bhs_pa.u.a32.address_hi);
2034 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2035 io_task->bhs_pa.u.a32.address_lo);
2036
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302037 if (num_sg == 1) {
2038 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2039 1);
2040 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2041 0);
2042 } else if (num_sg == 2) {
2043 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2044 0);
2045 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2046 1);
2047 } else {
2048 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2049 0);
2050 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2051 0);
2052 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302053 sg = l_sg;
2054 psgl++;
2055 psgl++;
2056 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302057 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302058 sg_len = sg_dma_len(sg);
2059 addr = (u64) sg_dma_address(sg);
2060 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2061 (addr & 0xFFFFFFFF));
2062 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2063 (addr >> 32));
2064 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2065 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2066 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2067 offset += sg_len;
2068 }
2069 psgl--;
2070 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2071}
2072
2073static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2074{
2075 struct iscsi_sge *psgl;
2076 unsigned long long addr;
2077 struct beiscsi_io_task *io_task = task->dd_data;
2078 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2079 struct beiscsi_hba *phba = beiscsi_conn->phba;
2080
2081 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2082 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2083 io_task->bhs_pa.u.a32.address_lo);
2084 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2085 io_task->bhs_pa.u.a32.address_hi);
2086
2087 if (task->data) {
2088 if (task->data_count) {
2089 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2090 addr = (u64) pci_map_single(phba->pcidev,
2091 task->data,
2092 task->data_count, 1);
2093 } else {
2094 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2095 addr = 0;
2096 }
2097 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302098 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302099 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302100 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302101 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2102 task->data_count);
2103
2104 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2105 } else {
2106 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2107 addr = 0;
2108 }
2109
2110 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2111
2112 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2113
2114 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2115 io_task->bhs_pa.u.a32.address_hi);
2116 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2117 io_task->bhs_pa.u.a32.address_lo);
2118 if (task->data) {
2119 psgl++;
2120 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2121 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2122 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2123 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2124 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2125 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2126
2127 psgl++;
2128 if (task->data) {
2129 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302130 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302131 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302132 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302133 }
2134 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2135 }
2136 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2137}
2138
2139static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2140{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302141 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302142 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2143 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2144
2145 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2146 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302147 num_async_pdu_buf_pages =
2148 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2149 phba->params.defpdu_hdr_sz);
2150 num_async_pdu_buf_sgl_pages =
2151 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2152 sizeof(struct phys_addr));
2153 num_async_pdu_data_pages =
2154 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2155 phba->params.defpdu_data_sz);
2156 num_async_pdu_data_sgl_pages =
2157 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2158 sizeof(struct phys_addr));
2159
2160 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2161
2162 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2163 BE_ISCSI_PDU_HEADER_SIZE;
2164 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2165 sizeof(struct hwi_context_memory);
2166
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302167
2168 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2169 * (phba->params.wrbs_per_cxn)
2170 * phba->params.cxns_per_ctrl;
2171 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2172 (phba->params.wrbs_per_cxn);
2173 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2174 phba->params.cxns_per_ctrl);
2175
2176 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2177 phba->params.icds_per_ctrl;
2178 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2179 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2180
2181 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2182 num_async_pdu_buf_pages * PAGE_SIZE;
2183 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2184 num_async_pdu_data_pages * PAGE_SIZE;
2185 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2186 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2187 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2188 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2189 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2190 phba->params.asyncpdus_per_ctrl *
2191 sizeof(struct async_pdu_handle);
2192 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2193 phba->params.asyncpdus_per_ctrl *
2194 sizeof(struct async_pdu_handle);
2195 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2196 sizeof(struct hwi_async_pdu_context) +
2197 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2198}
2199
2200static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2201{
2202 struct be_mem_descriptor *mem_descr;
2203 dma_addr_t bus_add;
2204 struct mem_array *mem_arr, *mem_arr_orig;
2205 unsigned int i, j, alloc_size, curr_alloc_size;
2206
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002207 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302208 if (!phba->phwi_ctrlr)
2209 return -ENOMEM;
2210
2211 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2212 GFP_KERNEL);
2213 if (!phba->init_mem) {
2214 kfree(phba->phwi_ctrlr);
2215 return -ENOMEM;
2216 }
2217
2218 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2219 GFP_KERNEL);
2220 if (!mem_arr_orig) {
2221 kfree(phba->init_mem);
2222 kfree(phba->phwi_ctrlr);
2223 return -ENOMEM;
2224 }
2225
2226 mem_descr = phba->init_mem;
2227 for (i = 0; i < SE_MEM_MAX; i++) {
2228 j = 0;
2229 mem_arr = mem_arr_orig;
2230 alloc_size = phba->mem_req[i];
2231 memset(mem_arr, 0, sizeof(struct mem_array) *
2232 BEISCSI_MAX_FRAGS_INIT);
2233 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2234 do {
2235 mem_arr->virtual_address = pci_alloc_consistent(
2236 phba->pcidev,
2237 curr_alloc_size,
2238 &bus_add);
2239 if (!mem_arr->virtual_address) {
2240 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2241 goto free_mem;
2242 if (curr_alloc_size -
2243 rounddown_pow_of_two(curr_alloc_size))
2244 curr_alloc_size = rounddown_pow_of_two
2245 (curr_alloc_size);
2246 else
2247 curr_alloc_size = curr_alloc_size / 2;
2248 } else {
2249 mem_arr->bus_address.u.
2250 a64.address = (__u64) bus_add;
2251 mem_arr->size = curr_alloc_size;
2252 alloc_size -= curr_alloc_size;
2253 curr_alloc_size = min(be_max_phys_size *
2254 1024, alloc_size);
2255 j++;
2256 mem_arr++;
2257 }
2258 } while (alloc_size);
2259 mem_descr->num_elements = j;
2260 mem_descr->size_in_bytes = phba->mem_req[i];
2261 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2262 GFP_KERNEL);
2263 if (!mem_descr->mem_array)
2264 goto free_mem;
2265
2266 memcpy(mem_descr->mem_array, mem_arr_orig,
2267 sizeof(struct mem_array) * j);
2268 mem_descr++;
2269 }
2270 kfree(mem_arr_orig);
2271 return 0;
2272free_mem:
2273 mem_descr->num_elements = j;
2274 while ((i) || (j)) {
2275 for (j = mem_descr->num_elements; j > 0; j--) {
2276 pci_free_consistent(phba->pcidev,
2277 mem_descr->mem_array[j - 1].size,
2278 mem_descr->mem_array[j - 1].
2279 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302280 (unsigned long)mem_descr->
2281 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302282 bus_address.u.a64.address);
2283 }
2284 if (i) {
2285 i--;
2286 kfree(mem_descr->mem_array);
2287 mem_descr--;
2288 }
2289 }
2290 kfree(mem_arr_orig);
2291 kfree(phba->init_mem);
2292 kfree(phba->phwi_ctrlr);
2293 return -ENOMEM;
2294}
2295
2296static int beiscsi_get_memory(struct beiscsi_hba *phba)
2297{
2298 beiscsi_find_mem_req(phba);
2299 return beiscsi_alloc_mem(phba);
2300}
2301
2302static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2303{
2304 struct pdu_data_out *pdata_out;
2305 struct pdu_nop_out *pnop_out;
2306 struct be_mem_descriptor *mem_descr;
2307
2308 mem_descr = phba->init_mem;
2309 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2310 pdata_out =
2311 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2312 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2313
2314 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2315 IIOC_SCSI_DATA);
2316
2317 pnop_out =
2318 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2319 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2320
2321 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2322 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2323 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2324 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2325}
2326
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002327static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302328{
2329 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002330 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302331 struct hwi_controller *phwi_ctrlr;
2332 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002333 struct iscsi_wrb *pwrb = NULL;
2334 unsigned int num_cxn_wrbh = 0;
2335 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302336
2337 mem_descr_wrbh = phba->init_mem;
2338 mem_descr_wrbh += HWI_MEM_WRBH;
2339
2340 mem_descr_wrb = phba->init_mem;
2341 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302342 phwi_ctrlr = phba->phwi_ctrlr;
2343
2344 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2345 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302346 pwrb_context->pwrb_handle_base =
2347 kzalloc(sizeof(struct wrb_handle *) *
2348 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002349 if (!pwrb_context->pwrb_handle_base) {
2350 shost_printk(KERN_ERR, phba->shost,
2351 "Mem Alloc Failed. Failing to load\n");
2352 goto init_wrb_hndl_failed;
2353 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302354 pwrb_context->pwrb_handle_basestd =
2355 kzalloc(sizeof(struct wrb_handle *) *
2356 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002357 if (!pwrb_context->pwrb_handle_basestd) {
2358 shost_printk(KERN_ERR, phba->shost,
2359 "Mem Alloc Failed. Failing to load\n");
2360 goto init_wrb_hndl_failed;
2361 }
2362 if (!num_cxn_wrbh) {
2363 pwrb_handle =
2364 mem_descr_wrbh->mem_array[idx].virtual_address;
2365 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2366 ((sizeof(struct wrb_handle)) *
2367 phba->params.wrbs_per_cxn));
2368 idx++;
2369 }
2370 pwrb_context->alloc_index = 0;
2371 pwrb_context->wrb_handles_available = 0;
2372 pwrb_context->free_index = 0;
2373
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302374 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302375 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2376 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2377 pwrb_context->pwrb_handle_basestd[j] =
2378 pwrb_handle;
2379 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302380 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302381 pwrb_handle++;
2382 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302383 num_cxn_wrbh--;
2384 }
2385 }
2386 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302387 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302388 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002389 if (!num_cxn_wrb) {
2390 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2391 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2392 ((sizeof(struct iscsi_wrb) *
2393 phba->params.wrbs_per_cxn));
2394 idx++;
2395 }
2396
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302397 if (num_cxn_wrb) {
2398 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2399 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2400 pwrb_handle->pwrb = pwrb;
2401 pwrb++;
2402 }
2403 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302404 }
2405 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002406 return 0;
2407init_wrb_hndl_failed:
2408 for (j = index; j > 0; j--) {
2409 pwrb_context = &phwi_ctrlr->wrb_context[j];
2410 kfree(pwrb_context->pwrb_handle_base);
2411 kfree(pwrb_context->pwrb_handle_basestd);
2412 }
2413 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302414}
2415
2416static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2417{
2418 struct hwi_controller *phwi_ctrlr;
2419 struct hba_parameters *p = &phba->params;
2420 struct hwi_async_pdu_context *pasync_ctx;
2421 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002422 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302423 struct be_mem_descriptor *mem_descr;
2424
2425 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2426 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2427
2428 phwi_ctrlr = phba->phwi_ctrlr;
2429 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2430 mem_descr->mem_array[0].virtual_address;
2431 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2432 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2433
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002434 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2435 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302436
2437 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2438 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2439 if (mem_descr->mem_array[0].virtual_address) {
2440 SE_DEBUG(DBG_LVL_8,
2441 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302442 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302443 } else
2444 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302445 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302446
2447 pasync_ctx->async_header.va_base =
2448 mem_descr->mem_array[0].virtual_address;
2449
2450 pasync_ctx->async_header.pa_base.u.a64.address =
2451 mem_descr->mem_array[0].bus_address.u.a64.address;
2452
2453 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2454 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2455 if (mem_descr->mem_array[0].virtual_address) {
2456 SE_DEBUG(DBG_LVL_8,
2457 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302458 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302459 } else
2460 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302461 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302462 pasync_ctx->async_header.ring_base =
2463 mem_descr->mem_array[0].virtual_address;
2464
2465 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2466 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2467 if (mem_descr->mem_array[0].virtual_address) {
2468 SE_DEBUG(DBG_LVL_8,
2469 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302470 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302471 } else
2472 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302473 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302474
2475 pasync_ctx->async_header.handle_base =
2476 mem_descr->mem_array[0].virtual_address;
2477 pasync_ctx->async_header.writables = 0;
2478 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2479
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302480
2481 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2482 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2483 if (mem_descr->mem_array[0].virtual_address) {
2484 SE_DEBUG(DBG_LVL_8,
2485 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302486 "va=%p\n", mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302487 } else
2488 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302489 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302490
2491 pasync_ctx->async_data.ring_base =
2492 mem_descr->mem_array[0].virtual_address;
2493
2494 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2495 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2496 if (!mem_descr->mem_array[0].virtual_address)
2497 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302498 "No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302499
2500 pasync_ctx->async_data.handle_base =
2501 mem_descr->mem_array[0].virtual_address;
2502 pasync_ctx->async_data.writables = 0;
2503 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2504
2505 pasync_header_h =
2506 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2507 pasync_data_h =
2508 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2509
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002510 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2511 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2512 if (mem_descr->mem_array[0].virtual_address) {
2513 SE_DEBUG(DBG_LVL_8,
2514 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
2515 "va=%p\n", mem_descr->mem_array[0].virtual_address);
2516 } else
2517 shost_printk(KERN_WARNING, phba->shost,
2518 "No Virtual address\n");
2519 idx = 0;
2520 pasync_ctx->async_data.va_base =
2521 mem_descr->mem_array[idx].virtual_address;
2522 pasync_ctx->async_data.pa_base.u.a64.address =
2523 mem_descr->mem_array[idx].bus_address.u.a64.address;
2524
2525 num_async_data = ((mem_descr->mem_array[idx].size) /
2526 phba->params.defpdu_data_sz);
2527 num_per_mem = 0;
2528
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302529 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2530 pasync_header_h->cri = -1;
2531 pasync_header_h->index = (char)index;
2532 INIT_LIST_HEAD(&pasync_header_h->link);
2533 pasync_header_h->pbuffer =
2534 (void *)((unsigned long)
2535 (pasync_ctx->async_header.va_base) +
2536 (p->defpdu_hdr_sz * index));
2537
2538 pasync_header_h->pa.u.a64.address =
2539 pasync_ctx->async_header.pa_base.u.a64.address +
2540 (p->defpdu_hdr_sz * index);
2541
2542 list_add_tail(&pasync_header_h->link,
2543 &pasync_ctx->async_header.free_list);
2544 pasync_header_h++;
2545 pasync_ctx->async_header.free_entries++;
2546 pasync_ctx->async_header.writables++;
2547
2548 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2549 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2550 header_busy_list);
2551 pasync_data_h->cri = -1;
2552 pasync_data_h->index = (char)index;
2553 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002554
2555 if (!num_async_data) {
2556 num_per_mem = 0;
2557 idx++;
2558 pasync_ctx->async_data.va_base =
2559 mem_descr->mem_array[idx].virtual_address;
2560 pasync_ctx->async_data.pa_base.u.a64.address =
2561 mem_descr->mem_array[idx].
2562 bus_address.u.a64.address;
2563
2564 num_async_data = ((mem_descr->mem_array[idx].size) /
2565 phba->params.defpdu_data_sz);
2566 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302567 pasync_data_h->pbuffer =
2568 (void *)((unsigned long)
2569 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002570 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302571
2572 pasync_data_h->pa.u.a64.address =
2573 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002574 (p->defpdu_data_sz * num_per_mem);
2575 num_per_mem++;
2576 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302577
2578 list_add_tail(&pasync_data_h->link,
2579 &pasync_ctx->async_data.free_list);
2580 pasync_data_h++;
2581 pasync_ctx->async_data.free_entries++;
2582 pasync_ctx->async_data.writables++;
2583
2584 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2585 }
2586
2587 pasync_ctx->async_header.host_write_ptr = 0;
2588 pasync_ctx->async_header.ep_read_ptr = -1;
2589 pasync_ctx->async_data.host_write_ptr = 0;
2590 pasync_ctx->async_data.ep_read_ptr = -1;
2591}
2592
2593static int
2594be_sgl_create_contiguous(void *virtual_address,
2595 u64 physical_address, u32 length,
2596 struct be_dma_mem *sgl)
2597{
2598 WARN_ON(!virtual_address);
2599 WARN_ON(!physical_address);
2600 WARN_ON(!length > 0);
2601 WARN_ON(!sgl);
2602
2603 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302604 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302605 sgl->size = length;
2606
2607 return 0;
2608}
2609
2610static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2611{
2612 memset(sgl, 0, sizeof(*sgl));
2613}
2614
2615static void
2616hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2617 struct mem_array *pmem, struct be_dma_mem *sgl)
2618{
2619 if (sgl->va)
2620 be_sgl_destroy_contiguous(sgl);
2621
2622 be_sgl_create_contiguous(pmem->virtual_address,
2623 pmem->bus_address.u.a64.address,
2624 pmem->size, sgl);
2625}
2626
2627static void
2628hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2629 struct mem_array *pmem, struct be_dma_mem *sgl)
2630{
2631 if (sgl->va)
2632 be_sgl_destroy_contiguous(sgl);
2633
2634 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2635 pmem->bus_address.u.a64.address,
2636 pmem->size, sgl);
2637}
2638
2639static int be_fill_queue(struct be_queue_info *q,
2640 u16 len, u16 entry_size, void *vaddress)
2641{
2642 struct be_dma_mem *mem = &q->dma_mem;
2643
2644 memset(q, 0, sizeof(*q));
2645 q->len = len;
2646 q->entry_size = entry_size;
2647 mem->size = len * entry_size;
2648 mem->va = vaddress;
2649 if (!mem->va)
2650 return -ENOMEM;
2651 memset(mem->va, 0, mem->size);
2652 return 0;
2653}
2654
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302655static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302656 struct hwi_context_memory *phwi_context)
2657{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302658 unsigned int i, num_eq_pages;
2659 int ret, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302660 struct be_queue_info *eq;
2661 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302662 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302663 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302664
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302665 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2666 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302667
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302668 if (phba->msix_enabled)
2669 eq_for_mcc = 1;
2670 else
2671 eq_for_mcc = 0;
2672 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2673 eq = &phwi_context->be_eq[i].q;
2674 mem = &eq->dma_mem;
2675 phwi_context->be_eq[i].phba = phba;
2676 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2677 num_eq_pages * PAGE_SIZE,
2678 &paddr);
2679 if (!eq_vaddress)
2680 goto create_eq_error;
2681
2682 mem->va = eq_vaddress;
2683 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2684 sizeof(struct be_eq_entry), eq_vaddress);
2685 if (ret) {
2686 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302687 "be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302688 goto create_eq_error;
2689 }
2690
2691 mem->dma = paddr;
2692 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2693 phwi_context->cur_eqd);
2694 if (ret) {
2695 shost_printk(KERN_ERR, phba->shost,
2696 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302697 "Failedfor EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302698 goto create_eq_error;
2699 }
2700 SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302701 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302702 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302703create_eq_error:
2704 for (i = 0; i < (phba->num_cpus + 1); i++) {
2705 eq = &phwi_context->be_eq[i].q;
2706 mem = &eq->dma_mem;
2707 if (mem->va)
2708 pci_free_consistent(phba->pcidev, num_eq_pages
2709 * PAGE_SIZE,
2710 mem->va, mem->dma);
2711 }
2712 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302713}
2714
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302715static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716 struct hwi_context_memory *phwi_context)
2717{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302718 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302719 int ret;
2720 struct be_queue_info *cq, *eq;
2721 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302722 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302723 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302724 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302725
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302726 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2727 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302728
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302729 for (i = 0; i < phba->num_cpus; i++) {
2730 cq = &phwi_context->be_cq[i];
2731 eq = &phwi_context->be_eq[i].q;
2732 pbe_eq = &phwi_context->be_eq[i];
2733 pbe_eq->cq = cq;
2734 pbe_eq->phba = phba;
2735 mem = &cq->dma_mem;
2736 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2737 num_cq_pages * PAGE_SIZE,
2738 &paddr);
2739 if (!cq_vaddress)
2740 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302741 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302742 sizeof(struct sol_cqe), cq_vaddress);
2743 if (ret) {
2744 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302745 "be_fill_queue Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302746 goto create_cq_error;
2747 }
2748
2749 mem->dma = paddr;
2750 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2751 false, 0);
2752 if (ret) {
2753 shost_printk(KERN_ERR, phba->shost,
2754 "beiscsi_cmd_eq_create"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302755 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302756 goto create_cq_error;
2757 }
2758 SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
2759 cq->id, eq->id);
2760 SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302761 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302762 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302763
2764create_cq_error:
2765 for (i = 0; i < phba->num_cpus; i++) {
2766 cq = &phwi_context->be_cq[i];
2767 mem = &cq->dma_mem;
2768 if (mem->va)
2769 pci_free_consistent(phba->pcidev, num_cq_pages
2770 * PAGE_SIZE,
2771 mem->va, mem->dma);
2772 }
2773 return ret;
2774
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302775}
2776
2777static int
2778beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2779 struct hwi_context_memory *phwi_context,
2780 struct hwi_controller *phwi_ctrlr,
2781 unsigned int def_pdu_ring_sz)
2782{
2783 unsigned int idx;
2784 int ret;
2785 struct be_queue_info *dq, *cq;
2786 struct be_dma_mem *mem;
2787 struct be_mem_descriptor *mem_descr;
2788 void *dq_vaddress;
2789
2790 idx = 0;
2791 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302792 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302793 mem = &dq->dma_mem;
2794 mem_descr = phba->init_mem;
2795 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2796 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2797 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2798 sizeof(struct phys_addr),
2799 sizeof(struct phys_addr), dq_vaddress);
2800 if (ret) {
2801 shost_printk(KERN_ERR, phba->shost,
2802 "be_fill_queue Failed for DEF PDU HDR\n");
2803 return ret;
2804 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302805 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2806 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302807 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2808 def_pdu_ring_sz,
2809 phba->params.defpdu_hdr_sz);
2810 if (ret) {
2811 shost_printk(KERN_ERR, phba->shost,
2812 "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
2813 return ret;
2814 }
2815 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
2816 SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
2817 phwi_context->be_def_hdrq.id);
2818 hwi_post_async_buffers(phba, 1);
2819 return 0;
2820}
2821
2822static int
2823beiscsi_create_def_data(struct beiscsi_hba *phba,
2824 struct hwi_context_memory *phwi_context,
2825 struct hwi_controller *phwi_ctrlr,
2826 unsigned int def_pdu_ring_sz)
2827{
2828 unsigned int idx;
2829 int ret;
2830 struct be_queue_info *dataq, *cq;
2831 struct be_dma_mem *mem;
2832 struct be_mem_descriptor *mem_descr;
2833 void *dq_vaddress;
2834
2835 idx = 0;
2836 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302837 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302838 mem = &dataq->dma_mem;
2839 mem_descr = phba->init_mem;
2840 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2841 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2842 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2843 sizeof(struct phys_addr),
2844 sizeof(struct phys_addr), dq_vaddress);
2845 if (ret) {
2846 shost_printk(KERN_ERR, phba->shost,
2847 "be_fill_queue Failed for DEF PDU DATA\n");
2848 return ret;
2849 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302850 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2851 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302852 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2853 def_pdu_ring_sz,
2854 phba->params.defpdu_data_sz);
2855 if (ret) {
2856 shost_printk(KERN_ERR, phba->shost,
2857 "be_cmd_create_default_pdu_queue Failed"
2858 " for DEF PDU DATA\n");
2859 return ret;
2860 }
2861 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
2862 SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
2863 phwi_context->be_def_dataq.id);
2864 hwi_post_async_buffers(phba, 0);
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302865 SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302866 return 0;
2867}
2868
2869static int
2870beiscsi_post_pages(struct beiscsi_hba *phba)
2871{
2872 struct be_mem_descriptor *mem_descr;
2873 struct mem_array *pm_arr;
2874 unsigned int page_offset, i;
2875 struct be_dma_mem sgl;
2876 int status;
2877
2878 mem_descr = phba->init_mem;
2879 mem_descr += HWI_MEM_SGE;
2880 pm_arr = mem_descr->mem_array;
2881
2882 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
2883 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
2884 for (i = 0; i < mem_descr->num_elements; i++) {
2885 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
2886 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
2887 page_offset,
2888 (pm_arr->size / PAGE_SIZE));
2889 page_offset += pm_arr->size / PAGE_SIZE;
2890 if (status != 0) {
2891 shost_printk(KERN_ERR, phba->shost,
2892 "post sgl failed.\n");
2893 return status;
2894 }
2895 pm_arr++;
2896 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302897 SE_DEBUG(DBG_LVL_8, "POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302898 return 0;
2899}
2900
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302901static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
2902{
2903 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002904 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302905 pci_free_consistent(phba->pcidev, mem->size,
2906 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05002907 mem->va = NULL;
2908 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302909}
2910
2911static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
2912 u16 len, u16 entry_size)
2913{
2914 struct be_dma_mem *mem = &q->dma_mem;
2915
2916 memset(q, 0, sizeof(*q));
2917 q->len = len;
2918 q->entry_size = entry_size;
2919 mem->size = len * entry_size;
2920 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
2921 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05302922 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302923 memset(mem->va, 0, mem->size);
2924 return 0;
2925}
2926
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302927static int
2928beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
2929 struct hwi_context_memory *phwi_context,
2930 struct hwi_controller *phwi_ctrlr)
2931{
2932 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
2933 u64 pa_addr_lo;
2934 unsigned int idx, num, i;
2935 struct mem_array *pwrb_arr;
2936 void *wrb_vaddr;
2937 struct be_dma_mem sgl;
2938 struct be_mem_descriptor *mem_descr;
2939 int status;
2940
2941 idx = 0;
2942 mem_descr = phba->init_mem;
2943 mem_descr += HWI_MEM_WRB;
2944 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
2945 GFP_KERNEL);
2946 if (!pwrb_arr) {
2947 shost_printk(KERN_ERR, phba->shost,
2948 "Memory alloc failed in create wrb ring.\n");
2949 return -ENOMEM;
2950 }
2951 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2952 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
2953 num_wrb_rings = mem_descr->mem_array[idx].size /
2954 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
2955
2956 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
2957 if (num_wrb_rings) {
2958 pwrb_arr[num].virtual_address = wrb_vaddr;
2959 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
2960 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2961 sizeof(struct iscsi_wrb);
2962 wrb_vaddr += pwrb_arr[num].size;
2963 pa_addr_lo += pwrb_arr[num].size;
2964 num_wrb_rings--;
2965 } else {
2966 idx++;
2967 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2968 pa_addr_lo = mem_descr->mem_array[idx].\
2969 bus_address.u.a64.address;
2970 num_wrb_rings = mem_descr->mem_array[idx].size /
2971 (phba->params.wrbs_per_cxn *
2972 sizeof(struct iscsi_wrb));
2973 pwrb_arr[num].virtual_address = wrb_vaddr;
2974 pwrb_arr[num].bus_address.u.a64.address\
2975 = pa_addr_lo;
2976 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2977 sizeof(struct iscsi_wrb);
2978 wrb_vaddr += pwrb_arr[num].size;
2979 pa_addr_lo += pwrb_arr[num].size;
2980 num_wrb_rings--;
2981 }
2982 }
2983 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2984 wrb_mem_index = 0;
2985 offset = 0;
2986 size = 0;
2987
2988 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
2989 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
2990 &phwi_context->be_wrbq[i]);
2991 if (status != 0) {
2992 shost_printk(KERN_ERR, phba->shost,
2993 "wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02002994 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302995 return status;
2996 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302997 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
2998 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302999 }
3000 kfree(pwrb_arr);
3001 return 0;
3002}
3003
3004static void free_wrb_handles(struct beiscsi_hba *phba)
3005{
3006 unsigned int index;
3007 struct hwi_controller *phwi_ctrlr;
3008 struct hwi_wrb_context *pwrb_context;
3009
3010 phwi_ctrlr = phba->phwi_ctrlr;
3011 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3012 pwrb_context = &phwi_ctrlr->wrb_context[index];
3013 kfree(pwrb_context->pwrb_handle_base);
3014 kfree(pwrb_context->pwrb_handle_basestd);
3015 }
3016}
3017
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303018static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3019{
3020 struct be_queue_info *q;
3021 struct be_ctrl_info *ctrl = &phba->ctrl;
3022
3023 q = &phba->ctrl.mcc_obj.q;
3024 if (q->created)
3025 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3026 be_queue_free(phba, q);
3027
3028 q = &phba->ctrl.mcc_obj.cq;
3029 if (q->created)
3030 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3031 be_queue_free(phba, q);
3032}
3033
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303034static void hwi_cleanup(struct beiscsi_hba *phba)
3035{
3036 struct be_queue_info *q;
3037 struct be_ctrl_info *ctrl = &phba->ctrl;
3038 struct hwi_controller *phwi_ctrlr;
3039 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303040 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303041
3042 phwi_ctrlr = phba->phwi_ctrlr;
3043 phwi_context = phwi_ctrlr->phwi_ctxt;
3044 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3045 q = &phwi_context->be_wrbq[i];
3046 if (q->created)
3047 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3048 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303049 free_wrb_handles(phba);
3050
3051 q = &phwi_context->be_def_hdrq;
3052 if (q->created)
3053 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3054
3055 q = &phwi_context->be_def_dataq;
3056 if (q->created)
3057 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3058
3059 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3060
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303061 for (i = 0; i < (phba->num_cpus); i++) {
3062 q = &phwi_context->be_cq[i];
3063 if (q->created)
3064 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3065 }
3066 if (phba->msix_enabled)
3067 eq_num = 1;
3068 else
3069 eq_num = 0;
3070 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3071 q = &phwi_context->be_eq[i].q;
3072 if (q->created)
3073 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3074 }
3075 be_mcc_queues_destroy(phba);
3076}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303078static int be_mcc_queues_create(struct beiscsi_hba *phba,
3079 struct hwi_context_memory *phwi_context)
3080{
3081 struct be_queue_info *q, *cq;
3082 struct be_ctrl_info *ctrl = &phba->ctrl;
3083
3084 /* Alloc MCC compl queue */
3085 cq = &phba->ctrl.mcc_obj.cq;
3086 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3087 sizeof(struct be_mcc_compl)))
3088 goto err;
3089 /* Ask BE to create MCC compl queue; */
3090 if (phba->msix_enabled) {
3091 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3092 [phba->num_cpus].q, false, true, 0))
3093 goto mcc_cq_free;
3094 } else {
3095 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3096 false, true, 0))
3097 goto mcc_cq_free;
3098 }
3099
3100 /* Alloc MCC queue */
3101 q = &phba->ctrl.mcc_obj.q;
3102 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3103 goto mcc_cq_destroy;
3104
3105 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303106 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303107 goto mcc_q_free;
3108
3109 return 0;
3110
3111mcc_q_free:
3112 be_queue_free(phba, q);
3113mcc_cq_destroy:
3114 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3115mcc_cq_free:
3116 be_queue_free(phba, cq);
3117err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303118 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303119}
3120
3121static int find_num_cpus(void)
3122{
3123 int num_cpus = 0;
3124
3125 num_cpus = num_online_cpus();
3126 if (num_cpus >= MAX_CPUS)
3127 num_cpus = MAX_CPUS - 1;
3128
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303129 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303130 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303131}
3132
3133static int hwi_init_port(struct beiscsi_hba *phba)
3134{
3135 struct hwi_controller *phwi_ctrlr;
3136 struct hwi_context_memory *phwi_context;
3137 unsigned int def_pdu_ring_sz;
3138 struct be_ctrl_info *ctrl = &phba->ctrl;
3139 int status;
3140
3141 def_pdu_ring_sz =
3142 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3143 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303144 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303145 phwi_context->max_eqd = 0;
3146 phwi_context->min_eqd = 0;
3147 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303148 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303149
3150 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303151 if (status != 0) {
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303152 shost_printk(KERN_ERR, phba->shost, "EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303153 goto error;
3154 }
3155
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303156 status = be_mcc_queues_create(phba, phwi_context);
3157 if (status != 0)
3158 goto error;
3159
3160 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303161 if (status != 0) {
3162 shost_printk(KERN_ERR, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303163 "Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303164 goto error;
3165 }
3166
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303167 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303168 if (status != 0) {
3169 shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
3170 goto error;
3171 }
3172
3173 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3174 def_pdu_ring_sz);
3175 if (status != 0) {
3176 shost_printk(KERN_ERR, phba->shost,
3177 "Default Header not created\n");
3178 goto error;
3179 }
3180
3181 status = beiscsi_create_def_data(phba, phwi_context,
3182 phwi_ctrlr, def_pdu_ring_sz);
3183 if (status != 0) {
3184 shost_printk(KERN_ERR, phba->shost,
3185 "Default Data not created\n");
3186 goto error;
3187 }
3188
3189 status = beiscsi_post_pages(phba);
3190 if (status != 0) {
3191 shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
3192 goto error;
3193 }
3194
3195 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3196 if (status != 0) {
3197 shost_printk(KERN_ERR, phba->shost,
3198 "WRB Rings not created\n");
3199 goto error;
3200 }
3201
3202 SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
3203 return 0;
3204
3205error:
3206 shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
3207 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003208 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303209}
3210
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303211static int hwi_init_controller(struct beiscsi_hba *phba)
3212{
3213 struct hwi_controller *phwi_ctrlr;
3214
3215 phwi_ctrlr = phba->phwi_ctrlr;
3216 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3217 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3218 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303219 SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303220 phwi_ctrlr->phwi_ctxt);
3221 } else {
3222 shost_printk(KERN_ERR, phba->shost,
3223 "HWI_MEM_ADDN_CONTEXT is more than one element."
3224 "Failing to load\n");
3225 return -ENOMEM;
3226 }
3227
3228 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003229 if (beiscsi_init_wrb_handle(phba))
3230 return -ENOMEM;
3231
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303232 hwi_init_async_pdu_ctx(phba);
3233 if (hwi_init_port(phba) != 0) {
3234 shost_printk(KERN_ERR, phba->shost,
3235 "hwi_init_controller failed\n");
3236 return -ENOMEM;
3237 }
3238 return 0;
3239}
3240
3241static void beiscsi_free_mem(struct beiscsi_hba *phba)
3242{
3243 struct be_mem_descriptor *mem_descr;
3244 int i, j;
3245
3246 mem_descr = phba->init_mem;
3247 i = 0;
3248 j = 0;
3249 for (i = 0; i < SE_MEM_MAX; i++) {
3250 for (j = mem_descr->num_elements; j > 0; j--) {
3251 pci_free_consistent(phba->pcidev,
3252 mem_descr->mem_array[j - 1].size,
3253 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303254 (unsigned long)mem_descr->mem_array[j - 1].
3255 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303256 }
3257 kfree(mem_descr->mem_array);
3258 mem_descr++;
3259 }
3260 kfree(phba->init_mem);
3261 kfree(phba->phwi_ctrlr);
3262}
3263
3264static int beiscsi_init_controller(struct beiscsi_hba *phba)
3265{
3266 int ret = -ENOMEM;
3267
3268 ret = beiscsi_get_memory(phba);
3269 if (ret < 0) {
3270 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303271 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303272 return ret;
3273 }
3274
3275 ret = hwi_init_controller(phba);
3276 if (ret)
3277 goto free_init;
3278 SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
3279 return 0;
3280
3281free_init:
3282 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003283 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303284}
3285
3286static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3287{
3288 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3289 struct sgl_handle *psgl_handle;
3290 struct iscsi_sge *pfrag;
3291 unsigned int arr_index, i, idx;
3292
3293 phba->io_sgl_hndl_avbl = 0;
3294 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303295
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303296 mem_descr_sglh = phba->init_mem;
3297 mem_descr_sglh += HWI_MEM_SGLH;
3298 if (1 == mem_descr_sglh->num_elements) {
3299 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3300 phba->params.ios_per_ctrl,
3301 GFP_KERNEL);
3302 if (!phba->io_sgl_hndl_base) {
3303 shost_printk(KERN_ERR, phba->shost,
3304 "Mem Alloc Failed. Failing to load\n");
3305 return -ENOMEM;
3306 }
3307 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3308 (phba->params.icds_per_ctrl -
3309 phba->params.ios_per_ctrl),
3310 GFP_KERNEL);
3311 if (!phba->eh_sgl_hndl_base) {
3312 kfree(phba->io_sgl_hndl_base);
3313 shost_printk(KERN_ERR, phba->shost,
3314 "Mem Alloc Failed. Failing to load\n");
3315 return -ENOMEM;
3316 }
3317 } else {
3318 shost_printk(KERN_ERR, phba->shost,
3319 "HWI_MEM_SGLH is more than one element."
3320 "Failing to load\n");
3321 return -ENOMEM;
3322 }
3323
3324 arr_index = 0;
3325 idx = 0;
3326 while (idx < mem_descr_sglh->num_elements) {
3327 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3328
3329 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3330 sizeof(struct sgl_handle)); i++) {
3331 if (arr_index < phba->params.ios_per_ctrl) {
3332 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3333 phba->io_sgl_hndl_avbl++;
3334 arr_index++;
3335 } else {
3336 phba->eh_sgl_hndl_base[arr_index -
3337 phba->params.ios_per_ctrl] =
3338 psgl_handle;
3339 arr_index++;
3340 phba->eh_sgl_hndl_avbl++;
3341 }
3342 psgl_handle++;
3343 }
3344 idx++;
3345 }
3346 SE_DEBUG(DBG_LVL_8,
3347 "phba->io_sgl_hndl_avbl=%d"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303348 "phba->eh_sgl_hndl_avbl=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303349 phba->io_sgl_hndl_avbl,
3350 phba->eh_sgl_hndl_avbl);
3351 mem_descr_sg = phba->init_mem;
3352 mem_descr_sg += HWI_MEM_SGE;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303353 SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303354 mem_descr_sg->num_elements);
3355 arr_index = 0;
3356 idx = 0;
3357 while (idx < mem_descr_sg->num_elements) {
3358 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3359
3360 for (i = 0;
3361 i < (mem_descr_sg->mem_array[idx].size) /
3362 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3363 i++) {
3364 if (arr_index < phba->params.ios_per_ctrl)
3365 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3366 else
3367 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3368 phba->params.ios_per_ctrl];
3369 psgl_handle->pfrag = pfrag;
3370 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3371 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3372 pfrag += phba->params.num_sge_per_io;
3373 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303374 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303375 }
3376 idx++;
3377 }
3378 phba->io_sgl_free_index = 0;
3379 phba->io_sgl_alloc_index = 0;
3380 phba->eh_sgl_free_index = 0;
3381 phba->eh_sgl_alloc_index = 0;
3382 return 0;
3383}
3384
3385static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3386{
3387 int i, new_cid;
3388
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303389 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303390 GFP_KERNEL);
3391 if (!phba->cid_array) {
3392 shost_printk(KERN_ERR, phba->shost,
3393 "Failed to allocate memory in "
3394 "hba_setup_cid_tbls\n");
3395 return -ENOMEM;
3396 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303397 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303398 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3399 if (!phba->ep_array) {
3400 shost_printk(KERN_ERR, phba->shost,
3401 "Failed to allocate memory in "
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303402 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303403 kfree(phba->cid_array);
3404 return -ENOMEM;
3405 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303406 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303407 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3408 phba->cid_array[i] = new_cid;
3409 new_cid += 2;
3410 }
3411 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3412 return 0;
3413}
3414
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303415static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303416{
3417 struct be_ctrl_info *ctrl = &phba->ctrl;
3418 struct hwi_controller *phwi_ctrlr;
3419 struct hwi_context_memory *phwi_context;
3420 struct be_queue_info *eq;
3421 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303422 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303423 u32 enabled;
3424
3425 phwi_ctrlr = phba->phwi_ctrlr;
3426 phwi_context = phwi_ctrlr->phwi_ctxt;
3427
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303428 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3429 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3430 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303431
3432 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3433 if (!enabled) {
3434 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303435 SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303436 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003437 }
3438
3439 if (!phba->msix_enabled) {
3440 eq = &phwi_context->be_eq[0].q;
3441 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
3442 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3443 } else {
3444 for (i = 0; i <= phba->num_cpus; i++) {
3445 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303446 SE_DEBUG(DBG_LVL_8, "eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303447 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3448 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303449 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303450}
3451
3452static void hwi_disable_intr(struct beiscsi_hba *phba)
3453{
3454 struct be_ctrl_info *ctrl = &phba->ctrl;
3455
3456 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3457 u32 reg = ioread32(addr);
3458
3459 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3460 if (enabled) {
3461 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3462 iowrite32(reg, addr);
3463 } else
3464 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303465 "In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303466}
3467
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303468static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3469{
3470 struct be_cmd_resp_get_boot_target *boot_resp;
3471 struct be_cmd_resp_get_session *session_resp;
3472 struct be_mcc_wrb *wrb;
3473 struct be_dma_mem nonemb_cmd;
3474 unsigned int tag, wrb_num;
3475 unsigned short status, extd_status;
3476 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003477 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303478
3479 tag = beiscsi_get_boot_target(phba);
3480 if (!tag) {
3481 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed\n");
3482 return -EAGAIN;
3483 } else
3484 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3485 phba->ctrl.mcc_numtag[tag]);
3486
3487 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3488 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3489 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3490 if (status || extd_status) {
3491 SE_DEBUG(DBG_LVL_1, "be_cmd_get_mac_addr Failed"
3492 " status = %d extd_status = %d\n",
3493 status, extd_status);
3494 free_mcc_tag(&phba->ctrl, tag);
3495 return -EBUSY;
3496 }
3497 wrb = queue_get_wrb(mccq, wrb_num);
3498 free_mcc_tag(&phba->ctrl, tag);
3499 boot_resp = embedded_payload(wrb);
3500
3501 if (boot_resp->boot_session_handle < 0) {
Mike Christief457a462011-06-24 15:11:53 -05003502 shost_printk(KERN_INFO, phba->shost, "No Boot Session.\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303503 return -ENXIO;
3504 }
3505
3506 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3507 sizeof(*session_resp),
3508 &nonemb_cmd.dma);
3509 if (nonemb_cmd.va == NULL) {
3510 SE_DEBUG(DBG_LVL_1,
3511 "Failed to allocate memory for"
3512 "beiscsi_get_session_info\n");
3513 return -ENOMEM;
3514 }
3515
3516 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
3517 tag = beiscsi_get_session_info(phba,
3518 boot_resp->boot_session_handle, &nonemb_cmd);
3519 if (!tag) {
3520 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info"
3521 " Failed\n");
3522 goto boot_freemem;
3523 } else
3524 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3525 phba->ctrl.mcc_numtag[tag]);
3526
3527 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3528 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3529 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3530 if (status || extd_status) {
3531 SE_DEBUG(DBG_LVL_1, "beiscsi_get_session_info Failed"
3532 " status = %d extd_status = %d\n",
3533 status, extd_status);
3534 free_mcc_tag(&phba->ctrl, tag);
3535 goto boot_freemem;
3536 }
3537 wrb = queue_get_wrb(mccq, wrb_num);
3538 free_mcc_tag(&phba->ctrl, tag);
3539 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003540
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303541 memcpy(&phba->boot_sess, &session_resp->session_info,
3542 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003543 ret = 0;
3544
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303545boot_freemem:
3546 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3547 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003548 return ret;
3549}
3550
3551static void beiscsi_boot_release(void *data)
3552{
3553 struct beiscsi_hba *phba = data;
3554
3555 scsi_host_put(phba->shost);
3556}
3557
3558static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3559{
3560 struct iscsi_boot_kobj *boot_kobj;
3561
3562 /* get boot info using mgmt cmd */
3563 if (beiscsi_get_boot_info(phba))
3564 /* Try to see if we can carry on without this */
3565 return 0;
3566
3567 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3568 if (!phba->boot_kset)
3569 return -ENOMEM;
3570
3571 /* get a ref because the show function will ref the phba */
3572 if (!scsi_host_get(phba->shost))
3573 goto free_kset;
3574 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3575 beiscsi_show_boot_tgt_info,
3576 beiscsi_tgt_get_attr_visibility,
3577 beiscsi_boot_release);
3578 if (!boot_kobj)
3579 goto put_shost;
3580
3581 if (!scsi_host_get(phba->shost))
3582 goto free_kset;
3583 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3584 beiscsi_show_boot_ini_info,
3585 beiscsi_ini_get_attr_visibility,
3586 beiscsi_boot_release);
3587 if (!boot_kobj)
3588 goto put_shost;
3589
3590 if (!scsi_host_get(phba->shost))
3591 goto free_kset;
3592 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3593 beiscsi_show_boot_eth_info,
3594 beiscsi_eth_get_attr_visibility,
3595 beiscsi_boot_release);
3596 if (!boot_kobj)
3597 goto put_shost;
3598 return 0;
3599
3600put_shost:
3601 scsi_host_put(phba->shost);
3602free_kset:
3603 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303604 return -ENOMEM;
3605}
3606
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303607static int beiscsi_init_port(struct beiscsi_hba *phba)
3608{
3609 int ret;
3610
3611 ret = beiscsi_init_controller(phba);
3612 if (ret < 0) {
3613 shost_printk(KERN_ERR, phba->shost,
3614 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303615 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303616 return ret;
3617 }
3618 ret = beiscsi_init_sgl_handle(phba);
3619 if (ret < 0) {
3620 shost_printk(KERN_ERR, phba->shost,
3621 "beiscsi_dev_probe - Failed in"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303622 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303623 goto do_cleanup_ctrlr;
3624 }
3625
3626 if (hba_setup_cid_tbls(phba)) {
3627 shost_printk(KERN_ERR, phba->shost,
3628 "Failed in hba_setup_cid_tbls\n");
3629 kfree(phba->io_sgl_hndl_base);
3630 kfree(phba->eh_sgl_hndl_base);
3631 goto do_cleanup_ctrlr;
3632 }
3633
3634 return ret;
3635
3636do_cleanup_ctrlr:
3637 hwi_cleanup(phba);
3638 return ret;
3639}
3640
3641static void hwi_purge_eq(struct beiscsi_hba *phba)
3642{
3643 struct hwi_controller *phwi_ctrlr;
3644 struct hwi_context_memory *phwi_context;
3645 struct be_queue_info *eq;
3646 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303647 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303648 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303649
3650 phwi_ctrlr = phba->phwi_ctrlr;
3651 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303652 if (phba->msix_enabled)
3653 eq_msix = 1;
3654 else
3655 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303656
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303657 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3658 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303659 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303660 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303661 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3662 & EQE_VALID_MASK) {
3663 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3664 queue_tail_inc(eq);
3665 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303666 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303667 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303668
3669 if (num_processed)
3670 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303671 }
3672}
3673
3674static void beiscsi_clean_port(struct beiscsi_hba *phba)
3675{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303676 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303677
3678 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3679 if (mgmt_status)
3680 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303681 "mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303682
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303683 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303684 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303685 kfree(phba->io_sgl_hndl_base);
3686 kfree(phba->eh_sgl_hndl_base);
3687 kfree(phba->cid_array);
3688 kfree(phba->ep_array);
3689}
3690
Mike Christie1282ab72012-04-18 03:06:00 -05003691static void beiscsi_cleanup_task(struct iscsi_task *task)
3692{
3693 struct beiscsi_io_task *io_task = task->dd_data;
3694 struct iscsi_conn *conn = task->conn;
3695 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3696 struct beiscsi_hba *phba = beiscsi_conn->phba;
3697 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3698 struct hwi_wrb_context *pwrb_context;
3699 struct hwi_controller *phwi_ctrlr;
3700
3701 phwi_ctrlr = phba->phwi_ctrlr;
3702 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3703 - phba->fw_config.iscsi_cid_start];
3704
3705 if (io_task->cmd_bhs) {
3706 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3707 io_task->bhs_pa.u.a64.address);
3708 io_task->cmd_bhs = NULL;
3709 }
3710
3711 if (task->sc) {
3712 if (io_task->pwrb_handle) {
3713 free_wrb_handle(phba, pwrb_context,
3714 io_task->pwrb_handle);
3715 io_task->pwrb_handle = NULL;
3716 }
3717
3718 if (io_task->psgl_handle) {
3719 spin_lock(&phba->io_sgl_lock);
3720 free_io_sgl_handle(phba, io_task->psgl_handle);
3721 spin_unlock(&phba->io_sgl_lock);
3722 io_task->psgl_handle = NULL;
3723 }
3724 } else {
3725 if (!beiscsi_conn->login_in_progress) {
3726 if (io_task->pwrb_handle) {
3727 free_wrb_handle(phba, pwrb_context,
3728 io_task->pwrb_handle);
3729 io_task->pwrb_handle = NULL;
3730 }
3731 if (io_task->psgl_handle) {
3732 spin_lock(&phba->mgmt_sgl_lock);
3733 free_mgmt_sgl_handle(phba,
3734 io_task->psgl_handle);
3735 spin_unlock(&phba->mgmt_sgl_lock);
3736 io_task->psgl_handle = NULL;
3737 }
3738 }
3739 }
3740}
3741
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303742void
3743beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3744 struct beiscsi_offload_params *params)
3745{
3746 struct wrb_handle *pwrb_handle;
3747 struct iscsi_target_context_update_wrb *pwrb = NULL;
3748 struct be_mem_descriptor *mem_descr;
3749 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003750 struct iscsi_task *task = beiscsi_conn->task;
3751 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303752 u32 doorbell = 0;
3753
3754 /*
3755 * We can always use 0 here because it is reserved by libiscsi for
3756 * login/startup related tasks.
3757 */
Mike Christie1282ab72012-04-18 03:06:00 -05003758 beiscsi_conn->login_in_progress = 0;
3759 spin_lock_bh(&session->lock);
3760 beiscsi_cleanup_task(task);
3761 spin_unlock_bh(&session->lock);
3762
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303763 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303764 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303765 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3766 memset(pwrb, 0, sizeof(*pwrb));
3767 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3768 max_burst_length, pwrb, params->dw[offsetof
3769 (struct amap_beiscsi_offload_params,
3770 max_burst_length) / 32]);
3771 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3772 max_send_data_segment_length, pwrb,
3773 params->dw[offsetof(struct amap_beiscsi_offload_params,
3774 max_send_data_segment_length) / 32]);
3775 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3776 first_burst_length,
3777 pwrb,
3778 params->dw[offsetof(struct amap_beiscsi_offload_params,
3779 first_burst_length) / 32]);
3780
3781 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3782 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3783 erl) / 32] & OFFLD_PARAMS_ERL));
3784 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3785 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3786 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3787 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3788 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3789 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3790 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3791 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3792 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3793 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3794 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3795 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3796 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3797 pwrb,
3798 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3799 exp_statsn) / 32] + 1));
3800 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3801 0x7);
3802 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3803 pwrb, pwrb_handle->wrb_index);
3804 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3805 pwrb, pwrb_handle->nxt_wrb_index);
3806 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3807 session_state, pwrb, 0);
3808 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3809 pwrb, 1);
3810 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3811 pwrb, 0);
3812 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3813 0);
3814
3815 mem_descr = phba->init_mem;
3816 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3817
3818 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3819 pad_buffer_addr_hi, pwrb,
3820 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3821 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3822 pad_buffer_addr_lo, pwrb,
3823 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3824
3825 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3826
3827 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303828 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303829 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303830 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3831
3832 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3833}
3834
3835static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
3836 int *index, int *age)
3837{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303838 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303839 if (age)
3840 *age = conn->session->age;
3841}
3842
3843/**
3844 * beiscsi_alloc_pdu - allocates pdu and related resources
3845 * @task: libiscsi task
3846 * @opcode: opcode of pdu for task
3847 *
3848 * This is called with the session lock held. It will allocate
3849 * the wrb and sgl if needed for the command. And it will prep
3850 * the pdu's itt. beiscsi_parse_pdu will later translate
3851 * the pdu itt to the libiscsi task itt.
3852 */
3853static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3854{
3855 struct beiscsi_io_task *io_task = task->dd_data;
3856 struct iscsi_conn *conn = task->conn;
3857 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3858 struct beiscsi_hba *phba = beiscsi_conn->phba;
3859 struct hwi_wrb_context *pwrb_context;
3860 struct hwi_controller *phwi_ctrlr;
3861 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303862 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3863 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303864
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303865 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06003866 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303867 if (!io_task->cmd_bhs)
3868 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303869 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303870 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303871 io_task->conn = beiscsi_conn;
3872
3873 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
3874 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303875 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003876 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303877
3878 if (task->sc) {
3879 spin_lock(&phba->io_sgl_lock);
3880 io_task->psgl_handle = alloc_io_sgl_handle(phba);
3881 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303882 if (!io_task->psgl_handle)
3883 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303884 io_task->pwrb_handle = alloc_wrb_handle(phba,
3885 beiscsi_conn->beiscsi_conn_cid -
3886 phba->fw_config.iscsi_cid_start);
3887 if (!io_task->pwrb_handle)
3888 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303889 } else {
3890 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05303891 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303892 if (!beiscsi_conn->login_in_progress) {
3893 spin_lock(&phba->mgmt_sgl_lock);
3894 io_task->psgl_handle = (struct sgl_handle *)
3895 alloc_mgmt_sgl_handle(phba);
3896 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303897 if (!io_task->psgl_handle)
3898 goto free_hndls;
3899
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303900 beiscsi_conn->login_in_progress = 1;
3901 beiscsi_conn->plogin_sgl_handle =
3902 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303903 io_task->pwrb_handle =
3904 alloc_wrb_handle(phba,
3905 beiscsi_conn->beiscsi_conn_cid -
3906 phba->fw_config.iscsi_cid_start);
3907 if (!io_task->pwrb_handle)
3908 goto free_io_hndls;
3909 beiscsi_conn->plogin_wrb_handle =
3910 io_task->pwrb_handle;
3911
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303912 } else {
3913 io_task->psgl_handle =
3914 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303915 io_task->pwrb_handle =
3916 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303917 }
Mike Christie1282ab72012-04-18 03:06:00 -05003918 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303919 } else {
3920 spin_lock(&phba->mgmt_sgl_lock);
3921 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
3922 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303923 if (!io_task->psgl_handle)
3924 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303925 io_task->pwrb_handle =
3926 alloc_wrb_handle(phba,
3927 beiscsi_conn->beiscsi_conn_cid -
3928 phba->fw_config.iscsi_cid_start);
3929 if (!io_task->pwrb_handle)
3930 goto free_mgmt_hndls;
3931
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303932 }
3933 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303934 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
3935 wrb_index << 16) | (unsigned int)
3936 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303937 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303938
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303939 io_task->cmd_bhs->iscsi_hdr.itt = itt;
3940 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303941
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303942free_io_hndls:
3943 spin_lock(&phba->io_sgl_lock);
3944 free_io_sgl_handle(phba, io_task->psgl_handle);
3945 spin_unlock(&phba->io_sgl_lock);
3946 goto free_hndls;
3947free_mgmt_hndls:
3948 spin_lock(&phba->mgmt_sgl_lock);
3949 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
3950 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303951free_hndls:
3952 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303953 pwrb_context = &phwi_ctrlr->wrb_context[
3954 beiscsi_conn->beiscsi_conn_cid -
3955 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05303956 if (io_task->pwrb_handle)
3957 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303958 io_task->pwrb_handle = NULL;
3959 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3960 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05003961 io_task->cmd_bhs = NULL;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303962 SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303963 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303964}
3965
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303966static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
3967 unsigned int num_sg, unsigned int xferlen,
3968 unsigned int writedir)
3969{
3970
3971 struct beiscsi_io_task *io_task = task->dd_data;
3972 struct iscsi_conn *conn = task->conn;
3973 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3974 struct beiscsi_hba *phba = beiscsi_conn->phba;
3975 struct iscsi_wrb *pwrb = NULL;
3976 unsigned int doorbell = 0;
3977
3978 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303979 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
3980 io_task->bhs_len = sizeof(struct be_cmd_bhs);
3981
3982 if (writedir) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303983 memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
3984 AMAP_SET_BITS(struct amap_pdu_data_out, itt,
3985 &io_task->cmd_bhs->iscsi_data_pdu,
3986 (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
3987 AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
3988 &io_task->cmd_bhs->iscsi_data_pdu,
3989 ISCSI_OPCODE_SCSI_DATA_OUT);
3990 AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
3991 &io_task->cmd_bhs->iscsi_data_pdu, 1);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303992 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3993 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303994 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303995 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303996 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3997 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303998 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
3999 }
4000 memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
4001 dw[offsetof(struct amap_pdu_data_out, lun) / 32],
Andy Grover516f43a2011-06-16 15:57:09 -07004002 &io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304003
4004 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004005 cpu_to_be16(*(unsigned short *)
4006 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304007 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4008 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4009 io_task->pwrb_handle->wrb_index);
4010 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4011 be32_to_cpu(task->cmdsn));
4012 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4013 io_task->psgl_handle->sgl_index);
4014
4015 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4016
4017 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4018 io_task->pwrb_handle->nxt_wrb_index);
4019 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4020
4021 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304022 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304023 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4024 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4025
4026 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4027 return 0;
4028}
4029
4030static int beiscsi_mtask(struct iscsi_task *task)
4031{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304032 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304033 struct iscsi_conn *conn = task->conn;
4034 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4035 struct beiscsi_hba *phba = beiscsi_conn->phba;
4036 struct iscsi_wrb *pwrb = NULL;
4037 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304038 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304039
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304040 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304041 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304042 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304043 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4044 be32_to_cpu(task->cmdsn));
4045 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4046 io_task->pwrb_handle->wrb_index);
4047 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4048 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304049
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304050 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4051 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304052 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4053 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304054 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4055 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4056 hwi_write_buffer(pwrb, task);
4057 break;
4058 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004059 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4060 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4061 TGT_DM_CMD);
4062 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4063 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004064 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004065 } else {
4066 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4067 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004068 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004069 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304070 hwi_write_buffer(pwrb, task);
4071 break;
4072 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304073 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304074 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304075 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304076 hwi_write_buffer(pwrb, task);
4077 break;
4078 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304079 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4080 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304081 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4082 hwi_write_buffer(pwrb, task);
4083 break;
4084 case ISCSI_OP_LOGOUT:
4085 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4086 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304087 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304088 hwi_write_buffer(pwrb, task);
4089 break;
4090
4091 default:
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304092 SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304093 task->hdr->opcode & ISCSI_OPCODE_MASK);
4094 return -EINVAL;
4095 }
4096
4097 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304098 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304099 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4100 io_task->pwrb_handle->nxt_wrb_index);
4101 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4102
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304103 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304104 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304105 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4106 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4107 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4108 return 0;
4109}
4110
4111static int beiscsi_task_xmit(struct iscsi_task *task)
4112{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304113 struct beiscsi_io_task *io_task = task->dd_data;
4114 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304115 struct scatterlist *sg;
4116 int num_sg;
4117 unsigned int writedir = 0, xferlen = 0;
4118
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304119 if (!sc)
4120 return beiscsi_mtask(task);
4121
4122 io_task->scsi_cmnd = sc;
4123 num_sg = scsi_dma_map(sc);
4124 if (num_sg < 0) {
4125 SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
4126 return num_sg;
4127 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304128 xferlen = scsi_bufflen(sc);
4129 sg = scsi_sglist(sc);
4130 if (sc->sc_data_direction == DMA_TO_DEVICE) {
4131 writedir = 1;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304132 SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304133 task->imm_count);
4134 } else
4135 writedir = 0;
4136 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4137}
4138
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004139static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304140{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304141 struct hwi_controller *phwi_ctrlr;
4142 struct hwi_context_memory *phwi_context;
4143 struct be_eq_obj *pbe_eq;
4144 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304145 u8 *real_offset = 0;
4146 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304147
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304148 phwi_ctrlr = phba->phwi_ctrlr;
4149 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304150 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304151 if (phba->msix_enabled) {
4152 for (i = 0; i <= phba->num_cpus; i++) {
4153 msix_vec = phba->msix_entries[i].vector;
4154 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004155 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304156 }
4157 } else
4158 if (phba->pcidev->irq)
4159 free_irq(phba->pcidev->irq, phba);
4160 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304161 destroy_workqueue(phba->wq);
4162 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304163 for (i = 0; i < phba->num_cpus; i++) {
4164 pbe_eq = &phwi_context->be_eq[i];
4165 blk_iopoll_disable(&pbe_eq->iopoll);
4166 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304167
4168 beiscsi_clean_port(phba);
4169 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304170 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4171
4172 value = readl((void *)real_offset);
4173
4174 if (value & 0x00010000) {
4175 value &= 0xfffeffff;
4176 writel(value, (void *)real_offset);
4177 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304178 beiscsi_unmap_pci_function(phba);
4179 pci_free_consistent(phba->pcidev,
4180 phba->ctrl.mbox_mem_alloced.size,
4181 phba->ctrl.mbox_mem_alloced.va,
4182 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004183}
4184
4185static void beiscsi_remove(struct pci_dev *pcidev)
4186{
4187
4188 struct beiscsi_hba *phba = NULL;
4189
4190 phba = pci_get_drvdata(pcidev);
4191 if (!phba) {
4192 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4193 return;
4194 }
4195
4196 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004197 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304198 iscsi_host_remove(phba->shost);
4199 pci_dev_put(phba->pcidev);
4200 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004201 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304202}
4203
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004204static void beiscsi_shutdown(struct pci_dev *pcidev)
4205{
4206
4207 struct beiscsi_hba *phba = NULL;
4208
4209 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4210 if (!phba) {
4211 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4212 return;
4213 }
4214
4215 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004216 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004217}
4218
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304219static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4220{
4221 int i, status;
4222
4223 for (i = 0; i <= phba->num_cpus; i++)
4224 phba->msix_entries[i].entry = i;
4225
4226 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4227 (phba->num_cpus + 1));
4228 if (!status)
4229 phba->msix_enabled = true;
4230
4231 return;
4232}
4233
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304234static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4235 const struct pci_device_id *id)
4236{
4237 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304238 struct hwi_controller *phwi_ctrlr;
4239 struct hwi_context_memory *phwi_context;
4240 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304241 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304242 u8 *real_offset = 0;
4243 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304244
4245 ret = beiscsi_enable_pci(pcidev);
4246 if (ret < 0) {
Dan Carpenter82284c02010-06-10 09:53:05 +02004247 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
4248 " Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304249 return ret;
4250 }
4251
4252 phba = beiscsi_hba_alloc(pcidev);
4253 if (!phba) {
4254 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304255 " Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304256 goto disable_pci;
4257 }
4258
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304259 switch (pcidev->device) {
4260 case BE_DEVICE_ID1:
4261 case OC_DEVICE_ID1:
4262 case OC_DEVICE_ID2:
4263 phba->generation = BE_GEN2;
4264 break;
4265 case BE_DEVICE_ID2:
4266 case OC_DEVICE_ID3:
4267 phba->generation = BE_GEN3;
4268 break;
4269 default:
4270 phba->generation = 0;
4271 }
4272
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304273 if (enable_msix)
4274 num_cpus = find_num_cpus();
4275 else
4276 num_cpus = 1;
4277 phba->num_cpus = num_cpus;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304278 SE_DEBUG(DBG_LVL_8, "num_cpus = %d\n", phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304279
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004280 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304281 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004282 if (!phba->msix_enabled)
4283 phba->num_cpus = 1;
4284 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304285 ret = be_ctrl_init(phba, pcidev);
4286 if (ret) {
4287 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4288 "Failed in be_ctrl_init\n");
4289 goto hba_free;
4290 }
4291
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304292 if (!num_hba) {
4293 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4294 value = readl((void *)real_offset);
4295 if (value & 0x00010000) {
4296 gcrashmode++;
4297 shost_printk(KERN_ERR, phba->shost,
4298 "Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004299 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304300 if (ret) {
4301 shost_printk(KERN_ERR, phba->shost,
4302 "Reset Failed. Aborting Crashdump\n");
4303 goto hba_free;
4304 }
4305 ret = be_chk_reset_complete(phba);
4306 if (ret) {
4307 shost_printk(KERN_ERR, phba->shost,
4308 "Failed to get out of reset."
4309 "Aborting Crashdump\n");
4310 goto hba_free;
4311 }
4312 } else {
4313 value |= 0x00010000;
4314 writel(value, (void *)real_offset);
4315 num_hba++;
4316 }
4317 }
4318
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304319 spin_lock_init(&phba->io_sgl_lock);
4320 spin_lock_init(&phba->mgmt_sgl_lock);
4321 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304322 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4323 if (ret != 0) {
4324 shost_printk(KERN_ERR, phba->shost,
4325 "Error getting fw config\n");
4326 goto free_port;
4327 }
4328 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304329 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304330 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304331 ret = beiscsi_init_port(phba);
4332 if (ret < 0) {
4333 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4334 "Failed in beiscsi_init_port\n");
4335 goto free_port;
4336 }
4337
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304338 for (i = 0; i < MAX_MCC_CMD ; i++) {
4339 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4340 phba->ctrl.mcc_tag[i] = i + 1;
4341 phba->ctrl.mcc_numtag[i + 1] = 0;
4342 phba->ctrl.mcc_tag_available++;
4343 }
4344
4345 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4346
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304347 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4348 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004349 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304350 if (!phba->wq) {
4351 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4352 "Failed to allocate work queue\n");
4353 goto free_twq;
4354 }
4355
4356 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4357
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304358 phwi_ctrlr = phba->phwi_ctrlr;
4359 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304360 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304361 for (i = 0; i < phba->num_cpus; i++) {
4362 pbe_eq = &phwi_context->be_eq[i];
4363 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4364 be_iopoll);
4365 blk_iopoll_enable(&pbe_eq->iopoll);
4366 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304367 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304368 ret = beiscsi_init_irqs(phba);
4369 if (ret < 0) {
4370 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
4371 "Failed to beiscsi_init_irqs\n");
4372 goto free_blkenbld;
4373 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304374 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004375
4376 if (beiscsi_setup_boot_info(phba))
4377 /*
4378 * log error but continue, because we may not be using
4379 * iscsi boot.
4380 */
4381 shost_printk(KERN_ERR, phba->shost, "Could not set up "
4382 "iSCSI boot info.");
4383
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304384 SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304385 return 0;
4386
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304387free_blkenbld:
4388 destroy_workqueue(phba->wq);
4389 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304390 for (i = 0; i < phba->num_cpus; i++) {
4391 pbe_eq = &phwi_context->be_eq[i];
4392 blk_iopoll_disable(&pbe_eq->iopoll);
4393 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304394free_twq:
4395 beiscsi_clean_port(phba);
4396 beiscsi_free_mem(phba);
4397free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304398 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4399
4400 value = readl((void *)real_offset);
4401
4402 if (value & 0x00010000) {
4403 value &= 0xfffeffff;
4404 writel(value, (void *)real_offset);
4405 }
4406
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304407 pci_free_consistent(phba->pcidev,
4408 phba->ctrl.mbox_mem_alloced.size,
4409 phba->ctrl.mbox_mem_alloced.va,
4410 phba->ctrl.mbox_mem_alloced.dma);
4411 beiscsi_unmap_pci_function(phba);
4412hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304413 if (phba->msix_enabled)
4414 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304415 iscsi_host_remove(phba->shost);
4416 pci_dev_put(phba->pcidev);
4417 iscsi_host_free(phba->shost);
4418disable_pci:
4419 pci_disable_device(pcidev);
4420 return ret;
4421}
4422
4423struct iscsi_transport beiscsi_iscsi_transport = {
4424 .owner = THIS_MODULE,
4425 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304426 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304427 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304428 .create_session = beiscsi_session_create,
4429 .destroy_session = beiscsi_session_destroy,
4430 .create_conn = beiscsi_conn_create,
4431 .bind_conn = beiscsi_conn_bind,
4432 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004433 .attr_is_visible = be2iscsi_attr_is_visible,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304434 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004435 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304436 .get_session_param = iscsi_session_get_param,
4437 .get_host_param = beiscsi_get_host_param,
4438 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004439 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304440 .send_pdu = iscsi_conn_send_pdu,
4441 .xmit_task = beiscsi_task_xmit,
4442 .cleanup_task = beiscsi_cleanup_task,
4443 .alloc_pdu = beiscsi_alloc_pdu,
4444 .parse_pdu_itt = beiscsi_parse_pdu,
4445 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004446 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304447 .ep_connect = beiscsi_ep_connect,
4448 .ep_poll = beiscsi_ep_poll,
4449 .ep_disconnect = beiscsi_ep_disconnect,
4450 .session_recovery_timedout = iscsi_session_recovery_timedout,
4451};
4452
4453static struct pci_driver beiscsi_pci_driver = {
4454 .name = DRV_NAME,
4455 .probe = beiscsi_dev_probe,
4456 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004457 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304458 .id_table = beiscsi_pci_id_table
4459};
4460
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304461
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304462static int __init beiscsi_module_init(void)
4463{
4464 int ret;
4465
4466 beiscsi_scsi_transport =
4467 iscsi_register_transport(&beiscsi_iscsi_transport);
4468 if (!beiscsi_scsi_transport) {
4469 SE_DEBUG(DBG_LVL_1,
4470 "beiscsi_module_init - Unable to register beiscsi"
4471 "transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304472 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304473 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304474 SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304475 &beiscsi_iscsi_transport);
4476
4477 ret = pci_register_driver(&beiscsi_pci_driver);
4478 if (ret) {
4479 SE_DEBUG(DBG_LVL_1,
4480 "beiscsi_module_init - Unable to register"
4481 "beiscsi pci driver.\n");
4482 goto unregister_iscsi_transport;
4483 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304484 return 0;
4485
4486unregister_iscsi_transport:
4487 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4488 return ret;
4489}
4490
4491static void __exit beiscsi_module_exit(void)
4492{
4493 pci_unregister_driver(&beiscsi_pci_driver);
4494 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4495}
4496
4497module_init(beiscsi_module_init);
4498module_exit(beiscsi_module_exit);