blob: b52c5c0a4fd0744cbcd5c66f76e7e029539c5c5c [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 Kallickalffce3e22012-04-03 23:41:50 -050031#include <linux/bsg-lib.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053032
33#include <scsi/libiscsi.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050034#include <scsi/scsi_bsg_iscsi.h>
35#include <scsi/scsi_netlink.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053036#include <scsi/scsi_transport_iscsi.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_cmnd.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include "be_main.h"
43#include "be_iscsi.h"
44#include "be_mgmt.h"
John Soni Jose0a513dd2012-08-20 23:00:55 +053045#include "be_cmds.h"
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053046
47static unsigned int be_iopoll_budget = 10;
48static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053049static unsigned int enable_msix = 1;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +053050static unsigned int gcrashmode = 0;
51static unsigned int num_hba = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053052
53MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
54MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050055MODULE_VERSION(BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050056MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053057MODULE_LICENSE("GPL");
58module_param(be_iopoll_budget, int, 0);
59module_param(enable_msix, int, 0);
60module_param(be_max_phys_size, uint, S_IRUGO);
John Soni Jose99bc5d52012-08-20 23:00:18 +053061MODULE_PARM_DESC(be_max_phys_size,
62 "Maximum Size (In Kilobytes) of physically contiguous "
63 "memory that can be allocated. Range is 16 - 128");
64
65#define beiscsi_disp_param(_name)\
66ssize_t \
67beiscsi_##_name##_disp(struct device *dev,\
68 struct device_attribute *attrib, char *buf) \
69{ \
70 struct Scsi_Host *shost = class_to_shost(dev);\
71 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
72 uint32_t param_val = 0; \
73 param_val = phba->attr_##_name;\
74 return snprintf(buf, PAGE_SIZE, "%d\n",\
75 phba->attr_##_name);\
76}
77
78#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
79int \
80beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
81{\
82 if (val >= _minval && val <= _maxval) {\
83 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
84 "BA_%d : beiscsi_"#_name" updated "\
85 "from 0x%x ==> 0x%x\n",\
86 phba->attr_##_name, val); \
87 phba->attr_##_name = val;\
88 return 0;\
89 } \
90 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
91 "BA_%d beiscsi_"#_name" attribute "\
92 "cannot be updated to 0x%x, "\
93 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
94 return -EINVAL;\
95}
96
97#define beiscsi_store_param(_name) \
98ssize_t \
99beiscsi_##_name##_store(struct device *dev,\
100 struct device_attribute *attr, const char *buf,\
101 size_t count) \
102{ \
103 struct Scsi_Host *shost = class_to_shost(dev);\
104 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
105 uint32_t param_val = 0;\
106 if (!isdigit(buf[0]))\
107 return -EINVAL;\
108 if (sscanf(buf, "%i", &param_val) != 1)\
109 return -EINVAL;\
110 if (beiscsi_##_name##_change(phba, param_val) == 0) \
111 return strlen(buf);\
112 else \
113 return -EINVAL;\
114}
115
116#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
117int \
118beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
119{ \
120 if (val >= _minval && val <= _maxval) {\
121 phba->attr_##_name = val;\
122 return 0;\
123 } \
124 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
125 "BA_%d beiscsi_"#_name" attribute " \
126 "cannot be updated to 0x%x, "\
127 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
128 phba->attr_##_name = _defval;\
129 return -EINVAL;\
130}
131
132#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
133static uint beiscsi_##_name = _defval;\
134module_param(beiscsi_##_name, uint, S_IRUGO);\
135MODULE_PARM_DESC(beiscsi_##_name, _descp);\
136beiscsi_disp_param(_name)\
137beiscsi_change_param(_name, _minval, _maxval, _defval)\
138beiscsi_store_param(_name)\
139beiscsi_init_param(_name, _minval, _maxval, _defval)\
140DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
141 beiscsi_##_name##_disp, beiscsi_##_name##_store)
142
143/*
144 * When new log level added update the
145 * the MAX allowed value for log_enable
146 */
147BEISCSI_RW_ATTR(log_enable, 0x00,
148 0xFF, 0x00, "Enable logging Bit Mask\n"
149 "\t\t\t\tInitialization Events : 0x01\n"
150 "\t\t\t\tMailbox Events : 0x02\n"
151 "\t\t\t\tMiscellaneous Events : 0x04\n"
152 "\t\t\t\tError Handling : 0x08\n"
153 "\t\t\t\tIO Path Events : 0x10\n"
154 "\t\t\t\tConfiguration Path : 0x20\n");
155
156struct device_attribute *beiscsi_attrs[] = {
157 &dev_attr_beiscsi_log_enable,
158 NULL,
159};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530160
John Soni Jose6763daa2012-10-20 04:41:45 +0530161static char const *cqe_desc[] = {
162 "RESERVED_DESC",
163 "SOL_CMD_COMPLETE",
164 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
165 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
166 "CXN_KILLED_BURST_LEN_MISMATCH",
167 "CXN_KILLED_AHS_RCVD",
168 "CXN_KILLED_HDR_DIGEST_ERR",
169 "CXN_KILLED_UNKNOWN_HDR",
170 "CXN_KILLED_STALE_ITT_TTT_RCVD",
171 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
172 "CXN_KILLED_RST_RCVD",
173 "CXN_KILLED_TIMED_OUT",
174 "CXN_KILLED_RST_SENT",
175 "CXN_KILLED_FIN_RCVD",
176 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
177 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
178 "CXN_KILLED_OVER_RUN_RESIDUAL",
179 "CXN_KILLED_UNDER_RUN_RESIDUAL",
180 "CMD_KILLED_INVALID_STATSN_RCVD",
181 "CMD_KILLED_INVALID_R2T_RCVD",
182 "CMD_CXN_KILLED_LUN_INVALID",
183 "CMD_CXN_KILLED_ICD_INVALID",
184 "CMD_CXN_KILLED_ITT_INVALID",
185 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
186 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
187 "CXN_INVALIDATE_NOTIFY",
188 "CXN_INVALIDATE_INDEX_NOTIFY",
189 "CMD_INVALIDATED_NOTIFY",
190 "UNSOL_HDR_NOTIFY",
191 "UNSOL_DATA_NOTIFY",
192 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
193 "DRIVERMSG_NOTIFY",
194 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
195 "SOL_CMD_KILLED_DIF_ERR",
196 "CXN_KILLED_SYN_RCVD",
197 "CXN_KILLED_IMM_DATA_RCVD"
198};
199
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530200static int beiscsi_slave_configure(struct scsi_device *sdev)
201{
202 blk_queue_max_segment_size(sdev->request_queue, 65536);
203 return 0;
204}
205
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530206static int beiscsi_eh_abort(struct scsi_cmnd *sc)
207{
208 struct iscsi_cls_session *cls_session;
209 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
210 struct beiscsi_io_task *aborted_io_task;
211 struct iscsi_conn *conn;
212 struct beiscsi_conn *beiscsi_conn;
213 struct beiscsi_hba *phba;
214 struct iscsi_session *session;
215 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530216 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530217 unsigned int cid, tag, num_invalidate;
218
219 cls_session = starget_to_session(scsi_target(sc->device));
220 session = cls_session->dd_data;
221
222 spin_lock_bh(&session->lock);
223 if (!aborted_task || !aborted_task->sc) {
224 /* we raced */
225 spin_unlock_bh(&session->lock);
226 return SUCCESS;
227 }
228
229 aborted_io_task = aborted_task->dd_data;
230 if (!aborted_io_task->scsi_cmnd) {
231 /* raced or invalid command */
232 spin_unlock_bh(&session->lock);
233 return SUCCESS;
234 }
235 spin_unlock_bh(&session->lock);
236 conn = aborted_task->conn;
237 beiscsi_conn = conn->dd_data;
238 phba = beiscsi_conn->phba;
239
240 /* invalidate iocb */
241 cid = beiscsi_conn->beiscsi_conn_cid;
242 inv_tbl = phba->inv_tbl;
243 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
244 inv_tbl->cid = cid;
245 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
246 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530247 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
248 sizeof(struct invalidate_commands_params_in),
249 &nonemb_cmd.dma);
250 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530251 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
252 "BM_%d : Failed to allocate memory for"
253 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530254 return FAILED;
255 }
256 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
257
258 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
259 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530260 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530261 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
262 "BM_%d : mgmt_invalidate_icds could not be"
263 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530264 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
265 nonemb_cmd.va, nonemb_cmd.dma);
266
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530267 return FAILED;
268 } else {
269 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
270 phba->ctrl.mcc_numtag[tag]);
271 free_mcc_tag(&phba->ctrl, tag);
272 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530273 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
274 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530275 return iscsi_eh_abort(sc);
276}
277
278static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
279{
280 struct iscsi_task *abrt_task;
281 struct beiscsi_io_task *abrt_io_task;
282 struct iscsi_conn *conn;
283 struct beiscsi_conn *beiscsi_conn;
284 struct beiscsi_hba *phba;
285 struct iscsi_session *session;
286 struct iscsi_cls_session *cls_session;
287 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530288 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530289 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530290
291 /* invalidate iocbs */
292 cls_session = starget_to_session(scsi_target(sc->device));
293 session = cls_session->dd_data;
294 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500295 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
296 spin_unlock_bh(&session->lock);
297 return FAILED;
298 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530299 conn = session->leadconn;
300 beiscsi_conn = conn->dd_data;
301 phba = beiscsi_conn->phba;
302 cid = beiscsi_conn->beiscsi_conn_cid;
303 inv_tbl = phba->inv_tbl;
304 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
305 num_invalidate = 0;
306 for (i = 0; i < conn->session->cmds_max; i++) {
307 abrt_task = conn->session->cmds[i];
308 abrt_io_task = abrt_task->dd_data;
309 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
310 continue;
311
312 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
313 continue;
314
315 inv_tbl->cid = cid;
316 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
317 num_invalidate++;
318 inv_tbl++;
319 }
320 spin_unlock_bh(&session->lock);
321 inv_tbl = phba->inv_tbl;
322
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530323 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
324 sizeof(struct invalidate_commands_params_in),
325 &nonemb_cmd.dma);
326 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530327 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
328 "BM_%d : Failed to allocate memory for"
329 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530330 return FAILED;
331 }
332 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
333 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
334 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
335 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530336 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530337 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
338 "BM_%d : mgmt_invalidate_icds could not be"
339 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530340 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
341 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530342 return FAILED;
343 } else {
344 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
345 phba->ctrl.mcc_numtag[tag]);
346 free_mcc_tag(&phba->ctrl, tag);
347 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530348 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
349 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530350 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530351}
352
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530353static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
354{
355 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500356 struct mgmt_session_info *boot_sess = &phba->boot_sess;
357 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530358 char *str = buf;
359 int rc;
360
361 switch (type) {
362 case ISCSI_BOOT_TGT_NAME:
363 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500364 (int)strlen(boot_sess->target_name),
365 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530366 break;
367 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500368 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530369 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500370 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530371 else
372 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500373 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530374 break;
375 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500376 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530377 break;
378
379 case ISCSI_BOOT_TGT_CHAP_NAME:
380 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500381 boot_conn->negotiated_login_options.auth_data.chap.
382 target_chap_name_length,
383 (char *)&boot_conn->negotiated_login_options.
384 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530385 break;
386 case ISCSI_BOOT_TGT_CHAP_SECRET:
387 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500388 boot_conn->negotiated_login_options.auth_data.chap.
389 target_secret_length,
390 (char *)&boot_conn->negotiated_login_options.
391 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530392 break;
393 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
394 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500395 boot_conn->negotiated_login_options.auth_data.chap.
396 intr_chap_name_length,
397 (char *)&boot_conn->negotiated_login_options.
398 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530399 break;
400 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500401 rc = sprintf(str, "%.*s\n",
402 boot_conn->negotiated_login_options.auth_data.chap.
403 intr_secret_length,
404 (char *)&boot_conn->negotiated_login_options.
405 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530406 break;
407 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500408 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530409 break;
410 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500411 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530412 break;
413 default:
414 rc = -ENOSYS;
415 break;
416 }
417 return rc;
418}
419
420static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
421{
422 struct beiscsi_hba *phba = data;
423 char *str = buf;
424 int rc;
425
426 switch (type) {
427 case ISCSI_BOOT_INI_INITIATOR_NAME:
428 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
429 break;
430 default:
431 rc = -ENOSYS;
432 break;
433 }
434 return rc;
435}
436
437static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
438{
439 struct beiscsi_hba *phba = data;
440 char *str = buf;
441 int rc;
442
443 switch (type) {
444 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500445 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530446 break;
447 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500448 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530449 break;
450 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500451 rc = beiscsi_get_macaddr(str, phba);
452 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530453 default:
454 rc = -ENOSYS;
455 break;
456 }
457 return rc;
458}
459
460
Al Viro587a1f12011-07-23 23:11:19 -0400461static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530462{
Al Viro587a1f12011-07-23 23:11:19 -0400463 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464
465 switch (type) {
466 case ISCSI_BOOT_TGT_NAME:
467 case ISCSI_BOOT_TGT_IP_ADDR:
468 case ISCSI_BOOT_TGT_PORT:
469 case ISCSI_BOOT_TGT_CHAP_NAME:
470 case ISCSI_BOOT_TGT_CHAP_SECRET:
471 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
472 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
473 case ISCSI_BOOT_TGT_NIC_ASSOC:
474 case ISCSI_BOOT_TGT_FLAGS:
475 rc = S_IRUGO;
476 break;
477 default:
478 rc = 0;
479 break;
480 }
481 return rc;
482}
483
Al Viro587a1f12011-07-23 23:11:19 -0400484static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530485{
Al Viro587a1f12011-07-23 23:11:19 -0400486 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530487
488 switch (type) {
489 case ISCSI_BOOT_INI_INITIATOR_NAME:
490 rc = S_IRUGO;
491 break;
492 default:
493 rc = 0;
494 break;
495 }
496 return rc;
497}
498
499
Al Viro587a1f12011-07-23 23:11:19 -0400500static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530501{
Al Viro587a1f12011-07-23 23:11:19 -0400502 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530503
504 switch (type) {
505 case ISCSI_BOOT_ETH_FLAGS:
506 case ISCSI_BOOT_ETH_MAC:
507 case ISCSI_BOOT_ETH_INDEX:
508 rc = S_IRUGO;
509 break;
510 default:
511 rc = 0;
512 break;
513 }
514 return rc;
515}
516
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530517/*------------------- PCI Driver operations and data ----------------- */
518static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
519 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530520 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530521 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
522 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
523 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530524 { 0 }
525};
526MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
527
John Soni Jose99bc5d52012-08-20 23:00:18 +0530528
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530529static struct scsi_host_template beiscsi_sht = {
530 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500531 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530532 .proc_name = DRV_NAME,
533 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530534 .change_queue_depth = iscsi_change_queue_depth,
535 .slave_configure = beiscsi_slave_configure,
536 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530537 .eh_abort_handler = beiscsi_eh_abort,
538 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530539 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530540 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530541 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
542 .can_queue = BE2_IO_DEPTH,
543 .this_id = -1,
544 .max_sectors = BEISCSI_MAX_SECTORS,
545 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
546 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500547 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
548
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530549};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530550
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530551static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530552
553static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
554{
555 struct beiscsi_hba *phba;
556 struct Scsi_Host *shost;
557
558 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
559 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530560 dev_err(&pcidev->dev,
561 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530562 return NULL;
563 }
564 shost->dma_boundary = pcidev->dma_mask;
565 shost->max_id = BE2_MAX_SESSIONS;
566 shost->max_channel = 0;
567 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
568 shost->max_lun = BEISCSI_NUM_MAX_LUN;
569 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530570 phba = iscsi_host_priv(shost);
571 memset(phba, 0, sizeof(*phba));
572 phba->shost = shost;
573 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530574 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500575 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530576
577 if (iscsi_host_add(shost, &phba->pcidev->dev))
578 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530579
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530580 return phba;
581
582free_devices:
583 pci_dev_put(phba->pcidev);
584 iscsi_host_free(phba->shost);
585 return NULL;
586}
587
588static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
589{
590 if (phba->csr_va) {
591 iounmap(phba->csr_va);
592 phba->csr_va = NULL;
593 }
594 if (phba->db_va) {
595 iounmap(phba->db_va);
596 phba->db_va = NULL;
597 }
598 if (phba->pci_va) {
599 iounmap(phba->pci_va);
600 phba->pci_va = NULL;
601 }
602}
603
604static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
605 struct pci_dev *pcidev)
606{
607 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530608 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530609
610 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
611 pci_resource_len(pcidev, 2));
612 if (addr == NULL)
613 return -ENOMEM;
614 phba->ctrl.csr = addr;
615 phba->csr_va = addr;
616 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
617
618 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
619 if (addr == NULL)
620 goto pci_map_err;
621 phba->ctrl.db = addr;
622 phba->db_va = addr;
623 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
624
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530625 if (phba->generation == BE_GEN2)
626 pcicfg_reg = 1;
627 else
628 pcicfg_reg = 0;
629
630 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
631 pci_resource_len(pcidev, pcicfg_reg));
632
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530633 if (addr == NULL)
634 goto pci_map_err;
635 phba->ctrl.pcicfg = addr;
636 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530637 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530638 return 0;
639
640pci_map_err:
641 beiscsi_unmap_pci_function(phba);
642 return -ENOMEM;
643}
644
645static int beiscsi_enable_pci(struct pci_dev *pcidev)
646{
647 int ret;
648
649 ret = pci_enable_device(pcidev);
650 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530651 dev_err(&pcidev->dev,
652 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530653 return ret;
654 }
655
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530656 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530657 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
658 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
659 if (ret) {
660 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
661 pci_disable_device(pcidev);
662 return ret;
663 }
664 }
665 return 0;
666}
667
668static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
669{
670 struct be_ctrl_info *ctrl = &phba->ctrl;
671 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
672 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
673 int status = 0;
674
675 ctrl->pdev = pdev;
676 status = beiscsi_map_pci_bars(phba, pdev);
677 if (status)
678 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530679 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
680 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
681 mbox_mem_alloc->size,
682 &mbox_mem_alloc->dma);
683 if (!mbox_mem_alloc->va) {
684 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500685 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530686 }
687
688 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
689 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
690 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
691 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
692 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530693 spin_lock_init(&phba->ctrl.mcc_lock);
694 spin_lock_init(&phba->ctrl.mcc_cq_lock);
695
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530696 return status;
697}
698
699static void beiscsi_get_params(struct beiscsi_hba *phba)
700{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530701 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
702 - (phba->fw_config.iscsi_cid_count
703 + BE2_TMFS
704 + BE2_NOPOUT_REQ));
705 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530706 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700707 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530708 phba->params.num_sge_per_io = BE2_SGE;
709 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
710 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
711 phba->params.eq_timer = 64;
712 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530713 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
714 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530715 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
716 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530717 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
718 "BM_%d : phba->params.num_eq_entries=%d\n",
719 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530720 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530721 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
722 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530723 phba->params.wrbs_per_cxn = 256;
724}
725
726static void hwi_ring_eq_db(struct beiscsi_hba *phba,
727 unsigned int id, unsigned int clr_interrupt,
728 unsigned int num_processed,
729 unsigned char rearm, unsigned char event)
730{
731 u32 val = 0;
732 val |= id & DB_EQ_RING_ID_MASK;
733 if (rearm)
734 val |= 1 << DB_EQ_REARM_SHIFT;
735 if (clr_interrupt)
736 val |= 1 << DB_EQ_CLR_SHIFT;
737 if (event)
738 val |= 1 << DB_EQ_EVNT_SHIFT;
739 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
740 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
741}
742
743/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530744 * be_isr_mcc - The isr routine of the driver.
745 * @irq: Not used
746 * @dev_id: Pointer to host adapter structure
747 */
748static irqreturn_t be_isr_mcc(int irq, void *dev_id)
749{
750 struct beiscsi_hba *phba;
751 struct be_eq_entry *eqe = NULL;
752 struct be_queue_info *eq;
753 struct be_queue_info *mcc;
754 unsigned int num_eq_processed;
755 struct be_eq_obj *pbe_eq;
756 unsigned long flags;
757
758 pbe_eq = dev_id;
759 eq = &pbe_eq->q;
760 phba = pbe_eq->phba;
761 mcc = &phba->ctrl.mcc_obj.cq;
762 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530763
764 num_eq_processed = 0;
765
766 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
767 & EQE_VALID_MASK) {
768 if (((eqe->dw[offsetof(struct amap_eq_entry,
769 resource_id) / 32] &
770 EQE_RESID_MASK) >> 16) == mcc->id) {
771 spin_lock_irqsave(&phba->isr_lock, flags);
772 phba->todo_mcc_cq = 1;
773 spin_unlock_irqrestore(&phba->isr_lock, flags);
774 }
775 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
776 queue_tail_inc(eq);
777 eqe = queue_tail_node(eq);
778 num_eq_processed++;
779 }
780 if (phba->todo_mcc_cq)
781 queue_work(phba->wq, &phba->work_cqs);
782 if (num_eq_processed)
783 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
784
785 return IRQ_HANDLED;
786}
787
788/**
789 * be_isr_msix - The isr routine of the driver.
790 * @irq: Not used
791 * @dev_id: Pointer to host adapter structure
792 */
793static irqreturn_t be_isr_msix(int irq, void *dev_id)
794{
795 struct beiscsi_hba *phba;
796 struct be_eq_entry *eqe = NULL;
797 struct be_queue_info *eq;
798 struct be_queue_info *cq;
799 unsigned int num_eq_processed;
800 struct be_eq_obj *pbe_eq;
801 unsigned long flags;
802
803 pbe_eq = dev_id;
804 eq = &pbe_eq->q;
805 cq = pbe_eq->cq;
806 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530807
808 phba = pbe_eq->phba;
809 num_eq_processed = 0;
810 if (blk_iopoll_enabled) {
811 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
812 & EQE_VALID_MASK) {
813 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
814 blk_iopoll_sched(&pbe_eq->iopoll);
815
816 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
817 queue_tail_inc(eq);
818 eqe = queue_tail_node(eq);
819 num_eq_processed++;
820 }
821 if (num_eq_processed)
822 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
823
824 return IRQ_HANDLED;
825 } else {
826 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
827 & EQE_VALID_MASK) {
828 spin_lock_irqsave(&phba->isr_lock, flags);
829 phba->todo_cq = 1;
830 spin_unlock_irqrestore(&phba->isr_lock, flags);
831 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
832 queue_tail_inc(eq);
833 eqe = queue_tail_node(eq);
834 num_eq_processed++;
835 }
836 if (phba->todo_cq)
837 queue_work(phba->wq, &phba->work_cqs);
838
839 if (num_eq_processed)
840 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
841
842 return IRQ_HANDLED;
843 }
844}
845
846/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530847 * be_isr - The isr routine of the driver.
848 * @irq: Not used
849 * @dev_id: Pointer to host adapter structure
850 */
851static irqreturn_t be_isr(int irq, void *dev_id)
852{
853 struct beiscsi_hba *phba;
854 struct hwi_controller *phwi_ctrlr;
855 struct hwi_context_memory *phwi_context;
856 struct be_eq_entry *eqe = NULL;
857 struct be_queue_info *eq;
858 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530861 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530862 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530863 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530864 int isr;
865
866 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700867 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530868 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
869 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
870 if (!isr)
871 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530872
873 phwi_ctrlr = phba->phwi_ctrlr;
874 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530875 pbe_eq = &phwi_context->be_eq[0];
876
877 eq = &phwi_context->be_eq[0].q;
878 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530879 index = 0;
880 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530881
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530882 num_ioeq_processed = 0;
883 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530884 if (blk_iopoll_enabled) {
885 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
886 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530887 if (((eqe->dw[offsetof(struct amap_eq_entry,
888 resource_id) / 32] &
889 EQE_RESID_MASK) >> 16) == mcc->id) {
890 spin_lock_irqsave(&phba->isr_lock, flags);
891 phba->todo_mcc_cq = 1;
892 spin_unlock_irqrestore(&phba->isr_lock, flags);
893 num_mcceq_processed++;
894 } else {
895 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
896 blk_iopoll_sched(&pbe_eq->iopoll);
897 num_ioeq_processed++;
898 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530899 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
900 queue_tail_inc(eq);
901 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530902 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530903 if (num_ioeq_processed || num_mcceq_processed) {
904 if (phba->todo_mcc_cq)
905 queue_work(phba->wq, &phba->work_cqs);
906
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530907 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530908 hwi_ring_eq_db(phba, eq->id, 0,
909 (num_ioeq_processed +
910 num_mcceq_processed) , 1, 1);
911 else
912 hwi_ring_eq_db(phba, eq->id, 0,
913 (num_ioeq_processed +
914 num_mcceq_processed), 0, 1);
915
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530916 return IRQ_HANDLED;
917 } else
918 return IRQ_NONE;
919 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530920 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530921 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
922 & EQE_VALID_MASK) {
923
924 if (((eqe->dw[offsetof(struct amap_eq_entry,
925 resource_id) / 32] &
926 EQE_RESID_MASK) >> 16) != cq->id) {
927 spin_lock_irqsave(&phba->isr_lock, flags);
928 phba->todo_mcc_cq = 1;
929 spin_unlock_irqrestore(&phba->isr_lock, flags);
930 } else {
931 spin_lock_irqsave(&phba->isr_lock, flags);
932 phba->todo_cq = 1;
933 spin_unlock_irqrestore(&phba->isr_lock, flags);
934 }
935 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
936 queue_tail_inc(eq);
937 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530938 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530939 }
940 if (phba->todo_cq || phba->todo_mcc_cq)
941 queue_work(phba->wq, &phba->work_cqs);
942
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530943 if (num_ioeq_processed) {
944 hwi_ring_eq_db(phba, eq->id, 0,
945 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530946 return IRQ_HANDLED;
947 } else
948 return IRQ_NONE;
949 }
950}
951
952static int beiscsi_init_irqs(struct beiscsi_hba *phba)
953{
954 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530955 struct hwi_controller *phwi_ctrlr;
956 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530957 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530958
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530959 phwi_ctrlr = phba->phwi_ctrlr;
960 phwi_context = phwi_ctrlr->phwi_ctxt;
961
962 if (phba->msix_enabled) {
963 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700964 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
965 GFP_KERNEL);
966 if (!phba->msi_name[i]) {
967 ret = -ENOMEM;
968 goto free_msix_irqs;
969 }
970
971 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
972 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530973 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700974 ret = request_irq(msix_vec, be_isr_msix, 0,
975 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530976 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530977 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530978 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
979 "BM_%d : beiscsi_init_irqs-Failed to"
980 "register msix for i = %d\n",
981 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700982 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530983 goto free_msix_irqs;
984 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530985 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700986 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
987 if (!phba->msi_name[i]) {
988 ret = -ENOMEM;
989 goto free_msix_irqs;
990 }
991 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
992 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530993 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700994 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530995 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530996 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530997 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
998 "BM_%d : beiscsi_init_irqs-"
999 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001000 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301001 goto free_msix_irqs;
1002 }
1003
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301004 } else {
1005 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1006 "beiscsi", phba);
1007 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301008 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1009 "BM_%d : beiscsi_init_irqs-"
1010 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301011 return ret;
1012 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301013 }
1014 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301015free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001016 for (j = i - 1; j >= 0; j--) {
1017 kfree(phba->msi_name[j]);
1018 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301019 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001020 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301021 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301022}
1023
1024static void hwi_ring_cq_db(struct beiscsi_hba *phba,
1025 unsigned int id, unsigned int num_processed,
1026 unsigned char rearm, unsigned char event)
1027{
1028 u32 val = 0;
1029 val |= id & DB_CQ_RING_ID_MASK;
1030 if (rearm)
1031 val |= 1 << DB_CQ_REARM_SHIFT;
1032 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1033 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1034}
1035
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301036static unsigned int
1037beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1038 struct beiscsi_hba *phba,
1039 unsigned short cid,
1040 struct pdu_base *ppdu,
1041 unsigned long pdu_len,
1042 void *pbuffer, unsigned long buf_len)
1043{
1044 struct iscsi_conn *conn = beiscsi_conn->conn;
1045 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301046 struct iscsi_task *task;
1047 struct beiscsi_io_task *io_task;
1048 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301049
1050 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1051 PDUBASE_OPCODE_MASK) {
1052 case ISCSI_OP_NOOP_IN:
1053 pbuffer = NULL;
1054 buf_len = 0;
1055 break;
1056 case ISCSI_OP_ASYNC_EVENT:
1057 break;
1058 case ISCSI_OP_REJECT:
1059 WARN_ON(!pbuffer);
1060 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301061 beiscsi_log(phba, KERN_ERR,
1062 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1063 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301064 break;
1065 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301066 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301067 task = conn->login_task;
1068 io_task = task->dd_data;
1069 login_hdr = (struct iscsi_hdr *)ppdu;
1070 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301071 break;
1072 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301073 beiscsi_log(phba, KERN_WARNING,
1074 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1075 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1076 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301077 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301078 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301079 return 1;
1080 }
1081
1082 spin_lock_bh(&session->lock);
1083 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1084 spin_unlock_bh(&session->lock);
1085 return 0;
1086}
1087
1088static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1089{
1090 struct sgl_handle *psgl_handle;
1091
1092 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301093 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1094 "BM_%d : In alloc_io_sgl_handle,"
1095 " io_sgl_alloc_index=%d\n",
1096 phba->io_sgl_alloc_index);
1097
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301098 psgl_handle = phba->io_sgl_hndl_base[phba->
1099 io_sgl_alloc_index];
1100 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1101 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301102 if (phba->io_sgl_alloc_index == (phba->params.
1103 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301104 phba->io_sgl_alloc_index = 0;
1105 else
1106 phba->io_sgl_alloc_index++;
1107 } else
1108 psgl_handle = NULL;
1109 return psgl_handle;
1110}
1111
1112static void
1113free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1114{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301115 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1116 "BM_%d : In free_,io_sgl_free_index=%d\n",
1117 phba->io_sgl_free_index);
1118
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301119 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1120 /*
1121 * this can happen if clean_task is called on a task that
1122 * failed in xmit_task or alloc_pdu.
1123 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301124 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1125 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1126 "value there=%p\n", phba->io_sgl_free_index,
1127 phba->io_sgl_hndl_base
1128 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129 return;
1130 }
1131 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1132 phba->io_sgl_hndl_avbl++;
1133 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1134 phba->io_sgl_free_index = 0;
1135 else
1136 phba->io_sgl_free_index++;
1137}
1138
1139/**
1140 * alloc_wrb_handle - To allocate a wrb handle
1141 * @phba: The hba pointer
1142 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301143 *
1144 * This happens under session_lock until submission to chip
1145 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301146struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301147{
1148 struct hwi_wrb_context *pwrb_context;
1149 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301150 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301151
1152 phwi_ctrlr = phba->phwi_ctrlr;
1153 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301154 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301155 pwrb_handle = pwrb_context->pwrb_handle_base[
1156 pwrb_context->alloc_index];
1157 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301158 if (pwrb_context->alloc_index ==
1159 (phba->params.wrbs_per_cxn - 1))
1160 pwrb_context->alloc_index = 0;
1161 else
1162 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301163 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1164 pwrb_context->alloc_index];
1165 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301166 } else
1167 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301168 return pwrb_handle;
1169}
1170
1171/**
1172 * free_wrb_handle - To free the wrb handle back to pool
1173 * @phba: The hba pointer
1174 * @pwrb_context: The context to free from
1175 * @pwrb_handle: The wrb_handle to free
1176 *
1177 * This happens under session_lock until submission to chip
1178 */
1179static void
1180free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1181 struct wrb_handle *pwrb_handle)
1182{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301183 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301184 pwrb_context->wrb_handles_available++;
1185 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1186 pwrb_context->free_index = 0;
1187 else
1188 pwrb_context->free_index++;
1189
John Soni Jose99bc5d52012-08-20 23:00:18 +05301190 beiscsi_log(phba, KERN_INFO,
1191 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1192 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1193 "wrb_handles_available=%d\n",
1194 pwrb_handle, pwrb_context->free_index,
1195 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301196}
1197
1198static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1199{
1200 struct sgl_handle *psgl_handle;
1201
1202 if (phba->eh_sgl_hndl_avbl) {
1203 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1204 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301205 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1206 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1207 phba->eh_sgl_alloc_index,
1208 phba->eh_sgl_alloc_index);
1209
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301210 phba->eh_sgl_hndl_avbl--;
1211 if (phba->eh_sgl_alloc_index ==
1212 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1213 1))
1214 phba->eh_sgl_alloc_index = 0;
1215 else
1216 phba->eh_sgl_alloc_index++;
1217 } else
1218 psgl_handle = NULL;
1219 return psgl_handle;
1220}
1221
1222void
1223free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1224{
1225
John Soni Jose99bc5d52012-08-20 23:00:18 +05301226 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1227 "BM_%d : In free_mgmt_sgl_handle,"
1228 "eh_sgl_free_index=%d\n",
1229 phba->eh_sgl_free_index);
1230
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301231 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1232 /*
1233 * this can happen if clean_task is called on a task that
1234 * failed in xmit_task or alloc_pdu.
1235 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301236 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1237 "BM_%d : Double Free in eh SGL ,"
1238 "eh_sgl_free_index=%d\n",
1239 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301240 return;
1241 }
1242 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1243 phba->eh_sgl_hndl_avbl++;
1244 if (phba->eh_sgl_free_index ==
1245 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1246 phba->eh_sgl_free_index = 0;
1247 else
1248 phba->eh_sgl_free_index++;
1249}
1250
1251static void
1252be_complete_io(struct beiscsi_conn *beiscsi_conn,
1253 struct iscsi_task *task, struct sol_cqe *psol)
1254{
1255 struct beiscsi_io_task *io_task = task->dd_data;
1256 struct be_status_bhs *sts_bhs =
1257 (struct be_status_bhs *)io_task->cmd_bhs;
1258 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301259 unsigned char *sense;
1260 u32 resid = 0, exp_cmdsn, max_cmdsn;
1261 u8 rsp, status, flags;
1262
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301263 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301264 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1265 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301266 max_cmdsn = ((psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301267 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1268 & SOL_EXP_CMD_SN_MASK) +
1269 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1270 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1271 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
1272 & SOL_RESP_MASK) >> 16);
1273 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
1274 & SOL_STS_MASK) >> 8);
1275 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1276 & SOL_FLAGS_MASK) >> 24) | 0x80;
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001277 if (!task->sc) {
1278 if (io_task->scsi_cmnd)
1279 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301280
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001281 return;
1282 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301283 task->sc->result = (DID_OK << 16) | status;
1284 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1285 task->sc->result = DID_ERROR << 16;
1286 goto unmap;
1287 }
1288
1289 /* bidi not initially supported */
1290 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1291 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
1292 32] & SOL_RES_CNT_MASK);
1293
1294 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1295 task->sc->result = DID_ERROR << 16;
1296
1297 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1298 scsi_set_resid(task->sc, resid);
1299 if (!status && (scsi_bufflen(task->sc) - resid <
1300 task->sc->underflow))
1301 task->sc->result = DID_ERROR << 16;
1302 }
1303 }
1304
1305 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001306 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301307 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001308
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301309 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001310 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301311 memcpy(task->sc->sense_buffer, sense,
1312 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1313 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301314
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301315 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
1316 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1317 & SOL_RES_CNT_MASK)
1318 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301319 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1320 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301321 }
1322unmap:
1323 scsi_dma_unmap(io_task->scsi_cmnd);
1324 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1325}
1326
1327static void
1328be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1329 struct iscsi_task *task, struct sol_cqe *psol)
1330{
1331 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301332 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 struct iscsi_conn *conn = beiscsi_conn->conn;
1334
1335 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301336 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301337 hdr->t2wait = 5;
1338 hdr->t2retain = 0;
1339 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1340 & SOL_FLAGS_MASK) >> 24) | 0x80;
1341 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1342 32] & SOL_RESP_MASK);
1343 hdr->exp_cmdsn = cpu_to_be32(psol->
1344 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1345 & SOL_EXP_CMD_SN_MASK);
1346 hdr->max_cmdsn = be32_to_cpu((psol->
1347 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1348 & SOL_EXP_CMD_SN_MASK) +
1349 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1350 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301351 hdr->dlength[0] = 0;
1352 hdr->dlength[1] = 0;
1353 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301354 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301355 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301356 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1357}
1358
1359static void
1360be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1361 struct iscsi_task *task, struct sol_cqe *psol)
1362{
1363 struct iscsi_tm_rsp *hdr;
1364 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301365 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301366
1367 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301368 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301369 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1370 & SOL_FLAGS_MASK) >> 24) | 0x80;
1371 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1372 32] & SOL_RESP_MASK);
1373 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301374 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301375 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1376 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1377 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1378 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301379 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301380 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1381}
1382
1383static void
1384hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1385 struct beiscsi_hba *phba, struct sol_cqe *psol)
1386{
1387 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301388 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301389 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301390 struct iscsi_task *task;
1391 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301392 struct iscsi_conn *conn = beiscsi_conn->conn;
1393 struct iscsi_session *session = conn->session;
1394
1395 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301396 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301397 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301398 SOL_CID_MASK) >> 6) -
1399 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301400 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301401 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1402 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301403 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301404
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301405 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001406 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301407 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001408 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301409 spin_lock_bh(&session->lock);
1410 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1411 spin_unlock_bh(&session->lock);
1412}
1413
1414static void
1415be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1416 struct iscsi_task *task, struct sol_cqe *psol)
1417{
1418 struct iscsi_nopin *hdr;
1419 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301420 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301421
1422 hdr = (struct iscsi_nopin *)task->hdr;
1423 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1424 & SOL_FLAGS_MASK) >> 24) | 0x80;
1425 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
1426 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
1427 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1428 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1429 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1430 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1431 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301432 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301433 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1434}
1435
1436static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1437 struct beiscsi_hba *phba, struct sol_cqe *psol)
1438{
1439 struct hwi_wrb_context *pwrb_context;
1440 struct wrb_handle *pwrb_handle;
1441 struct iscsi_wrb *pwrb = NULL;
1442 struct hwi_controller *phwi_ctrlr;
1443 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301444 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301445 struct iscsi_conn *conn = beiscsi_conn->conn;
1446 struct iscsi_session *session = conn->session;
1447
1448 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301449 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301450 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301451 & SOL_CID_MASK) >> 6) -
1452 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301453 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301454 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1455 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301456 task = pwrb_handle->pio_handle;
1457 pwrb = pwrb_handle->pwrb;
1458 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
1459 WRB_TYPE_MASK) >> 28;
1460
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301461 spin_lock_bh(&session->lock);
1462 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301463 case HWH_TYPE_IO:
1464 case HWH_TYPE_IO_RD:
1465 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301466 ISCSI_OP_NOOP_OUT)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301467 be_complete_nopin_resp(beiscsi_conn, task, psol);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301468 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301469 be_complete_io(beiscsi_conn, task, psol);
1470 break;
1471
1472 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301473 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1474 be_complete_logout(beiscsi_conn, task, psol);
1475 else
1476 be_complete_tmf(beiscsi_conn, task, psol);
1477
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301478 break;
1479
1480 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301481 beiscsi_log(phba, KERN_ERR,
1482 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1483 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1484 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301485 break;
1486
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301487 case HWH_TYPE_NOP:
1488 be_complete_nopin_resp(beiscsi_conn, task, psol);
1489 break;
1490
1491 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301492 beiscsi_log(phba, KERN_WARNING,
1493 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1494 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1495 "wrb_index 0x%x CID 0x%x\n", type,
1496 ((psol->dw[offsetof(struct amap_iscsi_wrb,
1497 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
1498 ((psol->dw[offsetof(struct amap_sol_cqe,
1499 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301500 break;
1501 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301502
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301503 spin_unlock_bh(&session->lock);
1504}
1505
1506static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1507 *pasync_ctx, unsigned int is_header,
1508 unsigned int host_write_ptr)
1509{
1510 if (is_header)
1511 return &pasync_ctx->async_entry[host_write_ptr].
1512 header_busy_list;
1513 else
1514 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1515}
1516
1517static struct async_pdu_handle *
1518hwi_get_async_handle(struct beiscsi_hba *phba,
1519 struct beiscsi_conn *beiscsi_conn,
1520 struct hwi_async_pdu_context *pasync_ctx,
1521 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1522{
1523 struct be_bus_address phys_addr;
1524 struct list_head *pbusy_list;
1525 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 unsigned char is_header = 0;
1527
1528 phys_addr.u.a32.address_lo =
1529 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1530 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1531 & PDUCQE_DPL_MASK) >> 16);
1532 phys_addr.u.a32.address_hi =
1533 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1534
1535 phys_addr.u.a64.address =
1536 *((unsigned long long *)(&phys_addr.u.a64.address));
1537
1538 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1539 & PDUCQE_CODE_MASK) {
1540 case UNSOL_HDR_NOTIFY:
1541 is_header = 1;
1542
1543 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1544 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1545 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301546 break;
1547 case UNSOL_DATA_NOTIFY:
1548 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1549 dw[offsetof(struct amap_i_t_dpdu_cqe,
1550 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301551 break;
1552 default:
1553 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301554 beiscsi_log(phba, KERN_WARNING,
1555 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1556 "BM_%d : Unexpected code=%d\n",
1557 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1558 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301559 return NULL;
1560 }
1561
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301562 WARN_ON(list_empty(pbusy_list));
1563 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001564 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301565 break;
1566 }
1567
1568 WARN_ON(!pasync_handle);
1569
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301570 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1571 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301572 pasync_handle->is_header = is_header;
1573 pasync_handle->buffer_len = ((pdpdu_cqe->
1574 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1575 & PDUCQE_DPL_MASK) >> 16);
1576
1577 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1578 index) / 32] & PDUCQE_INDEX_MASK);
1579 return pasync_handle;
1580}
1581
1582static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301583hwi_update_async_writables(struct beiscsi_hba *phba,
1584 struct hwi_async_pdu_context *pasync_ctx,
1585 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301586{
1587 struct list_head *pbusy_list;
1588 struct async_pdu_handle *pasync_handle;
1589 unsigned int num_entries, writables = 0;
1590 unsigned int *pep_read_ptr, *pwritables;
1591
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001592 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301593 if (is_header) {
1594 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1595 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301596 } else {
1597 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1598 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301599 }
1600
1601 while ((*pep_read_ptr) != cq_index) {
1602 (*pep_read_ptr)++;
1603 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1604
1605 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1606 *pep_read_ptr);
1607 if (writables == 0)
1608 WARN_ON(list_empty(pbusy_list));
1609
1610 if (!list_empty(pbusy_list)) {
1611 pasync_handle = list_entry(pbusy_list->next,
1612 struct async_pdu_handle,
1613 link);
1614 WARN_ON(!pasync_handle);
1615 pasync_handle->consumed = 1;
1616 }
1617
1618 writables++;
1619 }
1620
1621 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301622 beiscsi_log(phba, KERN_ERR,
1623 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1624 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1625 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301626 WARN_ON(1);
1627 }
1628
1629 *pwritables = *pwritables + writables;
1630 return 0;
1631}
1632
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001633static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301634 unsigned int cri)
1635{
1636 struct hwi_controller *phwi_ctrlr;
1637 struct hwi_async_pdu_context *pasync_ctx;
1638 struct async_pdu_handle *pasync_handle, *tmp_handle;
1639 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301640
1641 phwi_ctrlr = phba->phwi_ctrlr;
1642 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1643
1644 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1645
1646 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1647 list_del(&pasync_handle->link);
1648
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001649 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301650 list_add_tail(&pasync_handle->link,
1651 &pasync_ctx->async_header.free_list);
1652 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301653 } else {
1654 list_add_tail(&pasync_handle->link,
1655 &pasync_ctx->async_data.free_list);
1656 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301657 }
1658 }
1659
1660 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1661 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1662 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301663}
1664
1665static struct phys_addr *
1666hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1667 unsigned int is_header, unsigned int host_write_ptr)
1668{
1669 struct phys_addr *pasync_sge = NULL;
1670
1671 if (is_header)
1672 pasync_sge = pasync_ctx->async_header.ring_base;
1673 else
1674 pasync_sge = pasync_ctx->async_data.ring_base;
1675
1676 return pasync_sge + host_write_ptr;
1677}
1678
1679static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1680 unsigned int is_header)
1681{
1682 struct hwi_controller *phwi_ctrlr;
1683 struct hwi_async_pdu_context *pasync_ctx;
1684 struct async_pdu_handle *pasync_handle;
1685 struct list_head *pfree_link, *pbusy_list;
1686 struct phys_addr *pasync_sge;
1687 unsigned int ring_id, num_entries;
1688 unsigned int host_write_num;
1689 unsigned int writables;
1690 unsigned int i = 0;
1691 u32 doorbell = 0;
1692
1693 phwi_ctrlr = phba->phwi_ctrlr;
1694 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001695 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301696
1697 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301698 writables = min(pasync_ctx->async_header.writables,
1699 pasync_ctx->async_header.free_entries);
1700 pfree_link = pasync_ctx->async_header.free_list.next;
1701 host_write_num = pasync_ctx->async_header.host_write_ptr;
1702 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1703 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301704 writables = min(pasync_ctx->async_data.writables,
1705 pasync_ctx->async_data.free_entries);
1706 pfree_link = pasync_ctx->async_data.free_list.next;
1707 host_write_num = pasync_ctx->async_data.host_write_ptr;
1708 ring_id = phwi_ctrlr->default_pdu_data.id;
1709 }
1710
1711 writables = (writables / 8) * 8;
1712 if (writables) {
1713 for (i = 0; i < writables; i++) {
1714 pbusy_list =
1715 hwi_get_async_busy_list(pasync_ctx, is_header,
1716 host_write_num);
1717 pasync_handle =
1718 list_entry(pfree_link, struct async_pdu_handle,
1719 link);
1720 WARN_ON(!pasync_handle);
1721 pasync_handle->consumed = 0;
1722
1723 pfree_link = pfree_link->next;
1724
1725 pasync_sge = hwi_get_ring_address(pasync_ctx,
1726 is_header, host_write_num);
1727
1728 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1729 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1730
1731 list_move(&pasync_handle->link, pbusy_list);
1732
1733 host_write_num++;
1734 host_write_num = host_write_num % num_entries;
1735 }
1736
1737 if (is_header) {
1738 pasync_ctx->async_header.host_write_ptr =
1739 host_write_num;
1740 pasync_ctx->async_header.free_entries -= writables;
1741 pasync_ctx->async_header.writables -= writables;
1742 pasync_ctx->async_header.busy_entries += writables;
1743 } else {
1744 pasync_ctx->async_data.host_write_ptr = host_write_num;
1745 pasync_ctx->async_data.free_entries -= writables;
1746 pasync_ctx->async_data.writables -= writables;
1747 pasync_ctx->async_data.busy_entries += writables;
1748 }
1749
1750 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1751 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1752 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1753 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1754 << DB_DEF_PDU_CQPROC_SHIFT;
1755
1756 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1757 }
1758}
1759
1760static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1761 struct beiscsi_conn *beiscsi_conn,
1762 struct i_t_dpdu_cqe *pdpdu_cqe)
1763{
1764 struct hwi_controller *phwi_ctrlr;
1765 struct hwi_async_pdu_context *pasync_ctx;
1766 struct async_pdu_handle *pasync_handle = NULL;
1767 unsigned int cq_index = -1;
1768
1769 phwi_ctrlr = phba->phwi_ctrlr;
1770 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1771
1772 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1773 pdpdu_cqe, &cq_index);
1774 BUG_ON(pasync_handle->is_header != 0);
1775 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301776 hwi_update_async_writables(phba, pasync_ctx,
1777 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301778
1779 hwi_free_async_msg(phba, pasync_handle->cri);
1780 hwi_post_async_buffers(phba, pasync_handle->is_header);
1781}
1782
1783static unsigned int
1784hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1785 struct beiscsi_hba *phba,
1786 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1787{
1788 struct list_head *plist;
1789 struct async_pdu_handle *pasync_handle;
1790 void *phdr = NULL;
1791 unsigned int hdr_len = 0, buf_len = 0;
1792 unsigned int status, index = 0, offset = 0;
1793 void *pfirst_buffer = NULL;
1794 unsigned int num_buf = 0;
1795
1796 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1797
1798 list_for_each_entry(pasync_handle, plist, link) {
1799 if (index == 0) {
1800 phdr = pasync_handle->pbuffer;
1801 hdr_len = pasync_handle->buffer_len;
1802 } else {
1803 buf_len = pasync_handle->buffer_len;
1804 if (!num_buf) {
1805 pfirst_buffer = pasync_handle->pbuffer;
1806 num_buf++;
1807 }
1808 memcpy(pfirst_buffer + offset,
1809 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001810 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301811 }
1812 index++;
1813 }
1814
1815 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301816 (beiscsi_conn->beiscsi_conn_cid -
1817 phba->fw_config.iscsi_cid_start),
1818 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001819 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301820
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001821 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301822 return 0;
1823}
1824
1825static unsigned int
1826hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1827 struct beiscsi_hba *phba,
1828 struct async_pdu_handle *pasync_handle)
1829{
1830 struct hwi_async_pdu_context *pasync_ctx;
1831 struct hwi_controller *phwi_ctrlr;
1832 unsigned int bytes_needed = 0, status = 0;
1833 unsigned short cri = pasync_handle->cri;
1834 struct pdu_base *ppdu;
1835
1836 phwi_ctrlr = phba->phwi_ctrlr;
1837 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1838
1839 list_del(&pasync_handle->link);
1840 if (pasync_handle->is_header) {
1841 pasync_ctx->async_header.busy_entries--;
1842 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1843 hwi_free_async_msg(phba, cri);
1844 BUG();
1845 }
1846
1847 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1848 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1849 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1850 (unsigned short)pasync_handle->buffer_len;
1851 list_add_tail(&pasync_handle->link,
1852 &pasync_ctx->async_entry[cri].wait_queue.list);
1853
1854 ppdu = pasync_handle->pbuffer;
1855 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1856 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1857 0xFFFF0000) | ((be16_to_cpu((ppdu->
1858 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1859 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1860
1861 if (status == 0) {
1862 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1863 bytes_needed;
1864
1865 if (bytes_needed == 0)
1866 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1867 pasync_ctx, cri);
1868 }
1869 } else {
1870 pasync_ctx->async_data.busy_entries--;
1871 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1872 list_add_tail(&pasync_handle->link,
1873 &pasync_ctx->async_entry[cri].wait_queue.
1874 list);
1875 pasync_ctx->async_entry[cri].wait_queue.
1876 bytes_received +=
1877 (unsigned short)pasync_handle->buffer_len;
1878
1879 if (pasync_ctx->async_entry[cri].wait_queue.
1880 bytes_received >=
1881 pasync_ctx->async_entry[cri].wait_queue.
1882 bytes_needed)
1883 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1884 pasync_ctx, cri);
1885 }
1886 }
1887 return status;
1888}
1889
1890static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1891 struct beiscsi_hba *phba,
1892 struct i_t_dpdu_cqe *pdpdu_cqe)
1893{
1894 struct hwi_controller *phwi_ctrlr;
1895 struct hwi_async_pdu_context *pasync_ctx;
1896 struct async_pdu_handle *pasync_handle = NULL;
1897 unsigned int cq_index = -1;
1898
1899 phwi_ctrlr = phba->phwi_ctrlr;
1900 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1901 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1902 pdpdu_cqe, &cq_index);
1903
1904 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301905 hwi_update_async_writables(phba, pasync_ctx,
1906 pasync_handle->is_header, cq_index);
1907
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301908 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1909 hwi_post_async_buffers(phba, pasync_handle->is_header);
1910}
1911
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301912static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1913{
1914 struct be_queue_info *mcc_cq;
1915 struct be_mcc_compl *mcc_compl;
1916 unsigned int num_processed = 0;
1917
1918 mcc_cq = &phba->ctrl.mcc_obj.cq;
1919 mcc_compl = queue_tail_node(mcc_cq);
1920 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1921 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1922
1923 if (num_processed >= 32) {
1924 hwi_ring_cq_db(phba, mcc_cq->id,
1925 num_processed, 0, 0);
1926 num_processed = 0;
1927 }
1928 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1929 /* Interpret flags as an async trailer */
1930 if (is_link_state_evt(mcc_compl->flags))
1931 /* Interpret compl as a async link evt */
1932 beiscsi_async_link_state_process(phba,
1933 (struct be_async_event_link_state *) mcc_compl);
1934 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301935 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1936 "BM_%d : Unsupported Async Event, flags"
1937 " = 0x%08x\n",
1938 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301939 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1940 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1941 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1942 }
1943
1944 mcc_compl->flags = 0;
1945 queue_tail_inc(mcc_cq);
1946 mcc_compl = queue_tail_node(mcc_cq);
1947 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1948 num_processed++;
1949 }
1950
1951 if (num_processed > 0)
1952 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1953
1954}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301955
John Soni Jose6763daa2012-10-20 04:41:45 +05301956/**
1957 * beiscsi_process_cq()- Process the Completion Queue
1958 * @pbe_eq: Event Q on which the Completion has come
1959 *
1960 * return
1961 * Number of Completion Entries processed.
1962 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301963static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301964{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301965 struct be_queue_info *cq;
1966 struct sol_cqe *sol;
1967 struct dmsg_cqe *dmsg;
1968 unsigned int num_processed = 0;
1969 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301970 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301971 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301972 struct beiscsi_endpoint *beiscsi_ep;
1973 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301974 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301975
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301976 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301977 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301978 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301979
1980 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1981 CQE_VALID_MASK) {
1982 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1983
John Soni Jose0a513dd2012-08-20 23:00:55 +05301984 cid = ((sol->dw[offsetof(struct amap_sol_cqe, cid)/32] &
1985 CQE_CID_MASK) >> 6);
1986 code = (sol->dw[offsetof(struct amap_sol_cqe, code)/32] &
1987 CQE_CODE_MASK);
1988 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301989
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301990 beiscsi_ep = ep->dd_data;
1991 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301992
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301993 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301994 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301995 num_processed, 0, 0);
1996 tot_nump += num_processed;
1997 num_processed = 0;
1998 }
1999
John Soni Jose0a513dd2012-08-20 23:00:55 +05302000 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302001 case SOL_CMD_COMPLETE:
2002 hwi_complete_cmd(beiscsi_conn, phba, sol);
2003 break;
2004 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302005 beiscsi_log(phba, KERN_INFO,
2006 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302007 "BM_%d : Received %s[%d] on CID : %d\n",
2008 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302009
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302010 dmsg = (struct dmsg_cqe *)sol;
2011 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2012 break;
2013 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302014 beiscsi_log(phba, KERN_INFO,
2015 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302016 "BM_%d : Received %s[%d] on CID : %d\n",
2017 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302018
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302019 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2020 (struct i_t_dpdu_cqe *)sol);
2021 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302023 beiscsi_log(phba, KERN_INFO,
2024 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302025 "BM_%d : Received %s[%d] on CID : %d\n",
2026 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302027
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302028 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2029 (struct i_t_dpdu_cqe *)sol);
2030 break;
2031 case CXN_INVALIDATE_INDEX_NOTIFY:
2032 case CMD_INVALIDATED_NOTIFY:
2033 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302034 beiscsi_log(phba, KERN_ERR,
2035 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302036 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2037 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302038 break;
2039 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2040 case CMD_KILLED_INVALID_STATSN_RCVD:
2041 case CMD_KILLED_INVALID_R2T_RCVD:
2042 case CMD_CXN_KILLED_LUN_INVALID:
2043 case CMD_CXN_KILLED_ICD_INVALID:
2044 case CMD_CXN_KILLED_ITT_INVALID:
2045 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2046 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302047 beiscsi_log(phba, KERN_ERR,
2048 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302049 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2050 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302051 break;
2052 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302053 beiscsi_log(phba, KERN_ERR,
2054 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302055 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2056 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302057 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2058 (struct i_t_dpdu_cqe *) sol);
2059 break;
2060 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2061 case CXN_KILLED_BURST_LEN_MISMATCH:
2062 case CXN_KILLED_AHS_RCVD:
2063 case CXN_KILLED_HDR_DIGEST_ERR:
2064 case CXN_KILLED_UNKNOWN_HDR:
2065 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2066 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2067 case CXN_KILLED_TIMED_OUT:
2068 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302069 case CXN_KILLED_RST_SENT:
2070 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302071 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2072 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2073 case CXN_KILLED_OVER_RUN_RESIDUAL:
2074 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2075 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302076 beiscsi_log(phba, KERN_ERR,
2077 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302078 "BM_%d : Event %s[%d] received on CID : %d\n",
2079 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302080 if (beiscsi_conn)
2081 iscsi_conn_failure(beiscsi_conn->conn,
2082 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302083 break;
2084 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302085 beiscsi_log(phba, KERN_ERR,
2086 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302087 "BM_%d : Invalid CQE Event Received Code : %d"
2088 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302089 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302090 break;
2091 }
2092
2093 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2094 queue_tail_inc(cq);
2095 sol = queue_tail_node(cq);
2096 num_processed++;
2097 }
2098
2099 if (num_processed > 0) {
2100 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302101 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302102 }
2103 return tot_nump;
2104}
2105
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302106void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302107{
2108 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302109 struct hwi_controller *phwi_ctrlr;
2110 struct hwi_context_memory *phwi_context;
2111 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302112 struct beiscsi_hba *phba =
2113 container_of(work, struct beiscsi_hba, work_cqs);
2114
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302115 phwi_ctrlr = phba->phwi_ctrlr;
2116 phwi_context = phwi_ctrlr->phwi_ctxt;
2117 if (phba->msix_enabled)
2118 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
2119 else
2120 pbe_eq = &phwi_context->be_eq[0];
2121
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302122 if (phba->todo_mcc_cq) {
2123 spin_lock_irqsave(&phba->isr_lock, flags);
2124 phba->todo_mcc_cq = 0;
2125 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302126 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302127 }
2128
2129 if (phba->todo_cq) {
2130 spin_lock_irqsave(&phba->isr_lock, flags);
2131 phba->todo_cq = 0;
2132 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302133 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302134 }
2135}
2136
2137static int be_iopoll(struct blk_iopoll *iop, int budget)
2138{
2139 static unsigned int ret;
2140 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302141 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302142
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302143 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2144 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302145 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302146 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302147 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302148 beiscsi_log(phba, KERN_INFO,
2149 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2150 "BM_%d : rearm pbe_eq->q.id =%d\n",
2151 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302152 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302153 }
2154 return ret;
2155}
2156
2157static void
2158hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2159 unsigned int num_sg, struct beiscsi_io_task *io_task)
2160{
2161 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302162 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302163 unsigned int sge_len = 0;
2164 unsigned long long addr;
2165 struct scatterlist *l_sg;
2166 unsigned int offset;
2167
2168 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2169 io_task->bhs_pa.u.a32.address_lo);
2170 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2171 io_task->bhs_pa.u.a32.address_hi);
2172
2173 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302174 for (index = 0; (index < num_sg) && (index < 2); index++,
2175 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302176 if (index == 0) {
2177 sg_len = sg_dma_len(sg);
2178 addr = (u64) sg_dma_address(sg);
2179 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302180 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302181 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302182 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302183 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2184 sg_len);
2185 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302186 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302187 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2188 pwrb, sge_len);
2189 sg_len = sg_dma_len(sg);
2190 addr = (u64) sg_dma_address(sg);
2191 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302192 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302193 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302194 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302195 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2196 sg_len);
2197 }
2198 }
2199 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2200 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2201
2202 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2203
2204 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2205 io_task->bhs_pa.u.a32.address_hi);
2206 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2207 io_task->bhs_pa.u.a32.address_lo);
2208
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302209 if (num_sg == 1) {
2210 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2211 1);
2212 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2213 0);
2214 } else if (num_sg == 2) {
2215 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2216 0);
2217 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2218 1);
2219 } else {
2220 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2221 0);
2222 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2223 0);
2224 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302225 sg = l_sg;
2226 psgl++;
2227 psgl++;
2228 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302229 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302230 sg_len = sg_dma_len(sg);
2231 addr = (u64) sg_dma_address(sg);
2232 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2233 (addr & 0xFFFFFFFF));
2234 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2235 (addr >> 32));
2236 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2237 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2238 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2239 offset += sg_len;
2240 }
2241 psgl--;
2242 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2243}
2244
2245static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2246{
2247 struct iscsi_sge *psgl;
2248 unsigned long long addr;
2249 struct beiscsi_io_task *io_task = task->dd_data;
2250 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2251 struct beiscsi_hba *phba = beiscsi_conn->phba;
2252
2253 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2254 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2255 io_task->bhs_pa.u.a32.address_lo);
2256 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2257 io_task->bhs_pa.u.a32.address_hi);
2258
2259 if (task->data) {
2260 if (task->data_count) {
2261 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2262 addr = (u64) pci_map_single(phba->pcidev,
2263 task->data,
2264 task->data_count, 1);
2265 } else {
2266 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2267 addr = 0;
2268 }
2269 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302270 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302271 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302272 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302273 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2274 task->data_count);
2275
2276 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2277 } else {
2278 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2279 addr = 0;
2280 }
2281
2282 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2283
2284 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2285
2286 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2287 io_task->bhs_pa.u.a32.address_hi);
2288 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2289 io_task->bhs_pa.u.a32.address_lo);
2290 if (task->data) {
2291 psgl++;
2292 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2293 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2294 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2295 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2296 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2297 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2298
2299 psgl++;
2300 if (task->data) {
2301 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302302 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302303 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302304 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302305 }
2306 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2307 }
2308 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2309}
2310
2311static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2312{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302313 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302314 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2315 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2316
2317 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2318 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302319 num_async_pdu_buf_pages =
2320 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2321 phba->params.defpdu_hdr_sz);
2322 num_async_pdu_buf_sgl_pages =
2323 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2324 sizeof(struct phys_addr));
2325 num_async_pdu_data_pages =
2326 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2327 phba->params.defpdu_data_sz);
2328 num_async_pdu_data_sgl_pages =
2329 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2330 sizeof(struct phys_addr));
2331
2332 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2333
2334 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2335 BE_ISCSI_PDU_HEADER_SIZE;
2336 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2337 sizeof(struct hwi_context_memory);
2338
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302339
2340 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2341 * (phba->params.wrbs_per_cxn)
2342 * phba->params.cxns_per_ctrl;
2343 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2344 (phba->params.wrbs_per_cxn);
2345 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2346 phba->params.cxns_per_ctrl);
2347
2348 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2349 phba->params.icds_per_ctrl;
2350 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2351 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2352
2353 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2354 num_async_pdu_buf_pages * PAGE_SIZE;
2355 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2356 num_async_pdu_data_pages * PAGE_SIZE;
2357 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2358 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2359 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2360 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2361 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2362 phba->params.asyncpdus_per_ctrl *
2363 sizeof(struct async_pdu_handle);
2364 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2365 phba->params.asyncpdus_per_ctrl *
2366 sizeof(struct async_pdu_handle);
2367 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2368 sizeof(struct hwi_async_pdu_context) +
2369 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2370}
2371
2372static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2373{
2374 struct be_mem_descriptor *mem_descr;
2375 dma_addr_t bus_add;
2376 struct mem_array *mem_arr, *mem_arr_orig;
2377 unsigned int i, j, alloc_size, curr_alloc_size;
2378
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002379 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302380 if (!phba->phwi_ctrlr)
2381 return -ENOMEM;
2382
2383 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2384 GFP_KERNEL);
2385 if (!phba->init_mem) {
2386 kfree(phba->phwi_ctrlr);
2387 return -ENOMEM;
2388 }
2389
2390 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2391 GFP_KERNEL);
2392 if (!mem_arr_orig) {
2393 kfree(phba->init_mem);
2394 kfree(phba->phwi_ctrlr);
2395 return -ENOMEM;
2396 }
2397
2398 mem_descr = phba->init_mem;
2399 for (i = 0; i < SE_MEM_MAX; i++) {
2400 j = 0;
2401 mem_arr = mem_arr_orig;
2402 alloc_size = phba->mem_req[i];
2403 memset(mem_arr, 0, sizeof(struct mem_array) *
2404 BEISCSI_MAX_FRAGS_INIT);
2405 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2406 do {
2407 mem_arr->virtual_address = pci_alloc_consistent(
2408 phba->pcidev,
2409 curr_alloc_size,
2410 &bus_add);
2411 if (!mem_arr->virtual_address) {
2412 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2413 goto free_mem;
2414 if (curr_alloc_size -
2415 rounddown_pow_of_two(curr_alloc_size))
2416 curr_alloc_size = rounddown_pow_of_two
2417 (curr_alloc_size);
2418 else
2419 curr_alloc_size = curr_alloc_size / 2;
2420 } else {
2421 mem_arr->bus_address.u.
2422 a64.address = (__u64) bus_add;
2423 mem_arr->size = curr_alloc_size;
2424 alloc_size -= curr_alloc_size;
2425 curr_alloc_size = min(be_max_phys_size *
2426 1024, alloc_size);
2427 j++;
2428 mem_arr++;
2429 }
2430 } while (alloc_size);
2431 mem_descr->num_elements = j;
2432 mem_descr->size_in_bytes = phba->mem_req[i];
2433 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2434 GFP_KERNEL);
2435 if (!mem_descr->mem_array)
2436 goto free_mem;
2437
2438 memcpy(mem_descr->mem_array, mem_arr_orig,
2439 sizeof(struct mem_array) * j);
2440 mem_descr++;
2441 }
2442 kfree(mem_arr_orig);
2443 return 0;
2444free_mem:
2445 mem_descr->num_elements = j;
2446 while ((i) || (j)) {
2447 for (j = mem_descr->num_elements; j > 0; j--) {
2448 pci_free_consistent(phba->pcidev,
2449 mem_descr->mem_array[j - 1].size,
2450 mem_descr->mem_array[j - 1].
2451 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302452 (unsigned long)mem_descr->
2453 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302454 bus_address.u.a64.address);
2455 }
2456 if (i) {
2457 i--;
2458 kfree(mem_descr->mem_array);
2459 mem_descr--;
2460 }
2461 }
2462 kfree(mem_arr_orig);
2463 kfree(phba->init_mem);
2464 kfree(phba->phwi_ctrlr);
2465 return -ENOMEM;
2466}
2467
2468static int beiscsi_get_memory(struct beiscsi_hba *phba)
2469{
2470 beiscsi_find_mem_req(phba);
2471 return beiscsi_alloc_mem(phba);
2472}
2473
2474static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2475{
2476 struct pdu_data_out *pdata_out;
2477 struct pdu_nop_out *pnop_out;
2478 struct be_mem_descriptor *mem_descr;
2479
2480 mem_descr = phba->init_mem;
2481 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2482 pdata_out =
2483 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2484 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2485
2486 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2487 IIOC_SCSI_DATA);
2488
2489 pnop_out =
2490 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2491 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2492
2493 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2494 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2495 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2496 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2497}
2498
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002499static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302500{
2501 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002502 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302503 struct hwi_controller *phwi_ctrlr;
2504 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002505 struct iscsi_wrb *pwrb = NULL;
2506 unsigned int num_cxn_wrbh = 0;
2507 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302508
2509 mem_descr_wrbh = phba->init_mem;
2510 mem_descr_wrbh += HWI_MEM_WRBH;
2511
2512 mem_descr_wrb = phba->init_mem;
2513 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302514 phwi_ctrlr = phba->phwi_ctrlr;
2515
2516 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2517 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302518 pwrb_context->pwrb_handle_base =
2519 kzalloc(sizeof(struct wrb_handle *) *
2520 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002521 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302522 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2523 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002524 goto init_wrb_hndl_failed;
2525 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302526 pwrb_context->pwrb_handle_basestd =
2527 kzalloc(sizeof(struct wrb_handle *) *
2528 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002529 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302530 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2531 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002532 goto init_wrb_hndl_failed;
2533 }
2534 if (!num_cxn_wrbh) {
2535 pwrb_handle =
2536 mem_descr_wrbh->mem_array[idx].virtual_address;
2537 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2538 ((sizeof(struct wrb_handle)) *
2539 phba->params.wrbs_per_cxn));
2540 idx++;
2541 }
2542 pwrb_context->alloc_index = 0;
2543 pwrb_context->wrb_handles_available = 0;
2544 pwrb_context->free_index = 0;
2545
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302546 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302547 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2548 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2549 pwrb_context->pwrb_handle_basestd[j] =
2550 pwrb_handle;
2551 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302552 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302553 pwrb_handle++;
2554 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302555 num_cxn_wrbh--;
2556 }
2557 }
2558 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302559 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302560 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002561 if (!num_cxn_wrb) {
2562 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2563 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2564 ((sizeof(struct iscsi_wrb) *
2565 phba->params.wrbs_per_cxn));
2566 idx++;
2567 }
2568
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302569 if (num_cxn_wrb) {
2570 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2571 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2572 pwrb_handle->pwrb = pwrb;
2573 pwrb++;
2574 }
2575 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302576 }
2577 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002578 return 0;
2579init_wrb_hndl_failed:
2580 for (j = index; j > 0; j--) {
2581 pwrb_context = &phwi_ctrlr->wrb_context[j];
2582 kfree(pwrb_context->pwrb_handle_base);
2583 kfree(pwrb_context->pwrb_handle_basestd);
2584 }
2585 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302586}
2587
2588static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2589{
2590 struct hwi_controller *phwi_ctrlr;
2591 struct hba_parameters *p = &phba->params;
2592 struct hwi_async_pdu_context *pasync_ctx;
2593 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002594 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302595 struct be_mem_descriptor *mem_descr;
2596
2597 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2598 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2599
2600 phwi_ctrlr = phba->phwi_ctrlr;
2601 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2602 mem_descr->mem_array[0].virtual_address;
2603 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2604 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2605
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002606 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2607 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302608
2609 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2610 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2611 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302612 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2613 "BM_%d : hwi_init_async_pdu_ctx"
2614 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2615 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302616 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302617 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2618 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302619
2620 pasync_ctx->async_header.va_base =
2621 mem_descr->mem_array[0].virtual_address;
2622
2623 pasync_ctx->async_header.pa_base.u.a64.address =
2624 mem_descr->mem_array[0].bus_address.u.a64.address;
2625
2626 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2627 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2628 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302629 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2630 "BM_%d : hwi_init_async_pdu_ctx"
2631 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2632 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302633 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302634 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2635 "BM_%d : No Virtual address\n");
2636
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302637 pasync_ctx->async_header.ring_base =
2638 mem_descr->mem_array[0].virtual_address;
2639
2640 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2641 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2642 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302643 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2644 "BM_%d : hwi_init_async_pdu_ctx"
2645 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2646 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302647 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302648 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2649 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302650
2651 pasync_ctx->async_header.handle_base =
2652 mem_descr->mem_array[0].virtual_address;
2653 pasync_ctx->async_header.writables = 0;
2654 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2655
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302656
2657 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2658 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2659 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302660 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2661 "BM_%d : hwi_init_async_pdu_ctx"
2662 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2663 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302664 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302665 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2666 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302667
2668 pasync_ctx->async_data.ring_base =
2669 mem_descr->mem_array[0].virtual_address;
2670
2671 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2672 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2673 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302674 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2675 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302676
2677 pasync_ctx->async_data.handle_base =
2678 mem_descr->mem_array[0].virtual_address;
2679 pasync_ctx->async_data.writables = 0;
2680 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2681
2682 pasync_header_h =
2683 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2684 pasync_data_h =
2685 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2686
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002687 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2688 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2689 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302690 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2691 "BM_%d : hwi_init_async_pdu_ctx"
2692 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2693 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002694 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302695 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2696 "BM_%d : No Virtual address\n");
2697
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002698 idx = 0;
2699 pasync_ctx->async_data.va_base =
2700 mem_descr->mem_array[idx].virtual_address;
2701 pasync_ctx->async_data.pa_base.u.a64.address =
2702 mem_descr->mem_array[idx].bus_address.u.a64.address;
2703
2704 num_async_data = ((mem_descr->mem_array[idx].size) /
2705 phba->params.defpdu_data_sz);
2706 num_per_mem = 0;
2707
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302708 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2709 pasync_header_h->cri = -1;
2710 pasync_header_h->index = (char)index;
2711 INIT_LIST_HEAD(&pasync_header_h->link);
2712 pasync_header_h->pbuffer =
2713 (void *)((unsigned long)
2714 (pasync_ctx->async_header.va_base) +
2715 (p->defpdu_hdr_sz * index));
2716
2717 pasync_header_h->pa.u.a64.address =
2718 pasync_ctx->async_header.pa_base.u.a64.address +
2719 (p->defpdu_hdr_sz * index);
2720
2721 list_add_tail(&pasync_header_h->link,
2722 &pasync_ctx->async_header.free_list);
2723 pasync_header_h++;
2724 pasync_ctx->async_header.free_entries++;
2725 pasync_ctx->async_header.writables++;
2726
2727 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2728 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2729 header_busy_list);
2730 pasync_data_h->cri = -1;
2731 pasync_data_h->index = (char)index;
2732 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002733
2734 if (!num_async_data) {
2735 num_per_mem = 0;
2736 idx++;
2737 pasync_ctx->async_data.va_base =
2738 mem_descr->mem_array[idx].virtual_address;
2739 pasync_ctx->async_data.pa_base.u.a64.address =
2740 mem_descr->mem_array[idx].
2741 bus_address.u.a64.address;
2742
2743 num_async_data = ((mem_descr->mem_array[idx].size) /
2744 phba->params.defpdu_data_sz);
2745 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302746 pasync_data_h->pbuffer =
2747 (void *)((unsigned long)
2748 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002749 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302750
2751 pasync_data_h->pa.u.a64.address =
2752 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002753 (p->defpdu_data_sz * num_per_mem);
2754 num_per_mem++;
2755 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302756
2757 list_add_tail(&pasync_data_h->link,
2758 &pasync_ctx->async_data.free_list);
2759 pasync_data_h++;
2760 pasync_ctx->async_data.free_entries++;
2761 pasync_ctx->async_data.writables++;
2762
2763 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2764 }
2765
2766 pasync_ctx->async_header.host_write_ptr = 0;
2767 pasync_ctx->async_header.ep_read_ptr = -1;
2768 pasync_ctx->async_data.host_write_ptr = 0;
2769 pasync_ctx->async_data.ep_read_ptr = -1;
2770}
2771
2772static int
2773be_sgl_create_contiguous(void *virtual_address,
2774 u64 physical_address, u32 length,
2775 struct be_dma_mem *sgl)
2776{
2777 WARN_ON(!virtual_address);
2778 WARN_ON(!physical_address);
2779 WARN_ON(!length > 0);
2780 WARN_ON(!sgl);
2781
2782 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302783 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302784 sgl->size = length;
2785
2786 return 0;
2787}
2788
2789static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2790{
2791 memset(sgl, 0, sizeof(*sgl));
2792}
2793
2794static void
2795hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2796 struct mem_array *pmem, struct be_dma_mem *sgl)
2797{
2798 if (sgl->va)
2799 be_sgl_destroy_contiguous(sgl);
2800
2801 be_sgl_create_contiguous(pmem->virtual_address,
2802 pmem->bus_address.u.a64.address,
2803 pmem->size, sgl);
2804}
2805
2806static void
2807hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2808 struct mem_array *pmem, struct be_dma_mem *sgl)
2809{
2810 if (sgl->va)
2811 be_sgl_destroy_contiguous(sgl);
2812
2813 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2814 pmem->bus_address.u.a64.address,
2815 pmem->size, sgl);
2816}
2817
2818static int be_fill_queue(struct be_queue_info *q,
2819 u16 len, u16 entry_size, void *vaddress)
2820{
2821 struct be_dma_mem *mem = &q->dma_mem;
2822
2823 memset(q, 0, sizeof(*q));
2824 q->len = len;
2825 q->entry_size = entry_size;
2826 mem->size = len * entry_size;
2827 mem->va = vaddress;
2828 if (!mem->va)
2829 return -ENOMEM;
2830 memset(mem->va, 0, mem->size);
2831 return 0;
2832}
2833
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302834static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302835 struct hwi_context_memory *phwi_context)
2836{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302837 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302838 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302839 struct be_queue_info *eq;
2840 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302841 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302842 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302843
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302844 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2845 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302846
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302847 if (phba->msix_enabled)
2848 eq_for_mcc = 1;
2849 else
2850 eq_for_mcc = 0;
2851 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2852 eq = &phwi_context->be_eq[i].q;
2853 mem = &eq->dma_mem;
2854 phwi_context->be_eq[i].phba = phba;
2855 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2856 num_eq_pages * PAGE_SIZE,
2857 &paddr);
2858 if (!eq_vaddress)
2859 goto create_eq_error;
2860
2861 mem->va = eq_vaddress;
2862 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2863 sizeof(struct be_eq_entry), eq_vaddress);
2864 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302865 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2866 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302867 goto create_eq_error;
2868 }
2869
2870 mem->dma = paddr;
2871 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2872 phwi_context->cur_eqd);
2873 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302874 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2875 "BM_%d : beiscsi_cmd_eq_create"
2876 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302877 goto create_eq_error;
2878 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05302879
2880 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2881 "BM_%d : eqid = %d\n",
2882 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302883 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302884 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302885create_eq_error:
2886 for (i = 0; i < (phba->num_cpus + 1); i++) {
2887 eq = &phwi_context->be_eq[i].q;
2888 mem = &eq->dma_mem;
2889 if (mem->va)
2890 pci_free_consistent(phba->pcidev, num_eq_pages
2891 * PAGE_SIZE,
2892 mem->va, mem->dma);
2893 }
2894 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302895}
2896
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302897static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302898 struct hwi_context_memory *phwi_context)
2899{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302900 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302901 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302902 struct be_queue_info *cq, *eq;
2903 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302904 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302905 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302906 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302907
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302908 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2909 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302910
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302911 for (i = 0; i < phba->num_cpus; i++) {
2912 cq = &phwi_context->be_cq[i];
2913 eq = &phwi_context->be_eq[i].q;
2914 pbe_eq = &phwi_context->be_eq[i];
2915 pbe_eq->cq = cq;
2916 pbe_eq->phba = phba;
2917 mem = &cq->dma_mem;
2918 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2919 num_cq_pages * PAGE_SIZE,
2920 &paddr);
2921 if (!cq_vaddress)
2922 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302923 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302924 sizeof(struct sol_cqe), cq_vaddress);
2925 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302926 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2927 "BM_%d : be_fill_queue Failed "
2928 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302929 goto create_cq_error;
2930 }
2931
2932 mem->dma = paddr;
2933 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2934 false, 0);
2935 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302936 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2937 "BM_%d : beiscsi_cmd_eq_create"
2938 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302939 goto create_cq_error;
2940 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05302941 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2942 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
2943 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302944 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302945 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302946
2947create_cq_error:
2948 for (i = 0; i < phba->num_cpus; i++) {
2949 cq = &phwi_context->be_cq[i];
2950 mem = &cq->dma_mem;
2951 if (mem->va)
2952 pci_free_consistent(phba->pcidev, num_cq_pages
2953 * PAGE_SIZE,
2954 mem->va, mem->dma);
2955 }
2956 return ret;
2957
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302958}
2959
2960static int
2961beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2962 struct hwi_context_memory *phwi_context,
2963 struct hwi_controller *phwi_ctrlr,
2964 unsigned int def_pdu_ring_sz)
2965{
2966 unsigned int idx;
2967 int ret;
2968 struct be_queue_info *dq, *cq;
2969 struct be_dma_mem *mem;
2970 struct be_mem_descriptor *mem_descr;
2971 void *dq_vaddress;
2972
2973 idx = 0;
2974 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302975 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302976 mem = &dq->dma_mem;
2977 mem_descr = phba->init_mem;
2978 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2979 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2980 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2981 sizeof(struct phys_addr),
2982 sizeof(struct phys_addr), dq_vaddress);
2983 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302984 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2985 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302986 return ret;
2987 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302988 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2989 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302990 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2991 def_pdu_ring_sz,
2992 phba->params.defpdu_hdr_sz);
2993 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302994 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2995 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302996 return ret;
2997 }
2998 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302999 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3000 "BM_%d : iscsi def pdu id is %d\n",
3001 phwi_context->be_def_hdrq.id);
3002
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303003 hwi_post_async_buffers(phba, 1);
3004 return 0;
3005}
3006
3007static int
3008beiscsi_create_def_data(struct beiscsi_hba *phba,
3009 struct hwi_context_memory *phwi_context,
3010 struct hwi_controller *phwi_ctrlr,
3011 unsigned int def_pdu_ring_sz)
3012{
3013 unsigned int idx;
3014 int ret;
3015 struct be_queue_info *dataq, *cq;
3016 struct be_dma_mem *mem;
3017 struct be_mem_descriptor *mem_descr;
3018 void *dq_vaddress;
3019
3020 idx = 0;
3021 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303022 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303023 mem = &dataq->dma_mem;
3024 mem_descr = phba->init_mem;
3025 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3026 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3027 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3028 sizeof(struct phys_addr),
3029 sizeof(struct phys_addr), dq_vaddress);
3030 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303031 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3032 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303033 return ret;
3034 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303035 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3036 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303037 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3038 def_pdu_ring_sz,
3039 phba->params.defpdu_data_sz);
3040 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303041 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3042 "BM_%d be_cmd_create_default_pdu_queue"
3043 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303044 return ret;
3045 }
3046 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303047 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3048 "BM_%d : iscsi def data id is %d\n",
3049 phwi_context->be_def_dataq.id);
3050
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303051 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303052 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3053 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3054
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303055 return 0;
3056}
3057
3058static int
3059beiscsi_post_pages(struct beiscsi_hba *phba)
3060{
3061 struct be_mem_descriptor *mem_descr;
3062 struct mem_array *pm_arr;
3063 unsigned int page_offset, i;
3064 struct be_dma_mem sgl;
3065 int status;
3066
3067 mem_descr = phba->init_mem;
3068 mem_descr += HWI_MEM_SGE;
3069 pm_arr = mem_descr->mem_array;
3070
3071 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3072 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3073 for (i = 0; i < mem_descr->num_elements; i++) {
3074 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3075 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3076 page_offset,
3077 (pm_arr->size / PAGE_SIZE));
3078 page_offset += pm_arr->size / PAGE_SIZE;
3079 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303080 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3081 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303082 return status;
3083 }
3084 pm_arr++;
3085 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303086 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3087 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303088 return 0;
3089}
3090
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303091static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3092{
3093 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003094 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303095 pci_free_consistent(phba->pcidev, mem->size,
3096 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003097 mem->va = NULL;
3098 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303099}
3100
3101static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3102 u16 len, u16 entry_size)
3103{
3104 struct be_dma_mem *mem = &q->dma_mem;
3105
3106 memset(q, 0, sizeof(*q));
3107 q->len = len;
3108 q->entry_size = entry_size;
3109 mem->size = len * entry_size;
3110 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3111 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303112 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303113 memset(mem->va, 0, mem->size);
3114 return 0;
3115}
3116
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303117static int
3118beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3119 struct hwi_context_memory *phwi_context,
3120 struct hwi_controller *phwi_ctrlr)
3121{
3122 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3123 u64 pa_addr_lo;
3124 unsigned int idx, num, i;
3125 struct mem_array *pwrb_arr;
3126 void *wrb_vaddr;
3127 struct be_dma_mem sgl;
3128 struct be_mem_descriptor *mem_descr;
3129 int status;
3130
3131 idx = 0;
3132 mem_descr = phba->init_mem;
3133 mem_descr += HWI_MEM_WRB;
3134 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3135 GFP_KERNEL);
3136 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303137 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3138 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303139 return -ENOMEM;
3140 }
3141 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3142 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3143 num_wrb_rings = mem_descr->mem_array[idx].size /
3144 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3145
3146 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3147 if (num_wrb_rings) {
3148 pwrb_arr[num].virtual_address = wrb_vaddr;
3149 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3150 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3151 sizeof(struct iscsi_wrb);
3152 wrb_vaddr += pwrb_arr[num].size;
3153 pa_addr_lo += pwrb_arr[num].size;
3154 num_wrb_rings--;
3155 } else {
3156 idx++;
3157 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3158 pa_addr_lo = mem_descr->mem_array[idx].\
3159 bus_address.u.a64.address;
3160 num_wrb_rings = mem_descr->mem_array[idx].size /
3161 (phba->params.wrbs_per_cxn *
3162 sizeof(struct iscsi_wrb));
3163 pwrb_arr[num].virtual_address = wrb_vaddr;
3164 pwrb_arr[num].bus_address.u.a64.address\
3165 = pa_addr_lo;
3166 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3167 sizeof(struct iscsi_wrb);
3168 wrb_vaddr += pwrb_arr[num].size;
3169 pa_addr_lo += pwrb_arr[num].size;
3170 num_wrb_rings--;
3171 }
3172 }
3173 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3174 wrb_mem_index = 0;
3175 offset = 0;
3176 size = 0;
3177
3178 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3179 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3180 &phwi_context->be_wrbq[i]);
3181 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303182 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3183 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003184 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303185 return status;
3186 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303187 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3188 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303189 }
3190 kfree(pwrb_arr);
3191 return 0;
3192}
3193
3194static void free_wrb_handles(struct beiscsi_hba *phba)
3195{
3196 unsigned int index;
3197 struct hwi_controller *phwi_ctrlr;
3198 struct hwi_wrb_context *pwrb_context;
3199
3200 phwi_ctrlr = phba->phwi_ctrlr;
3201 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3202 pwrb_context = &phwi_ctrlr->wrb_context[index];
3203 kfree(pwrb_context->pwrb_handle_base);
3204 kfree(pwrb_context->pwrb_handle_basestd);
3205 }
3206}
3207
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303208static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3209{
3210 struct be_queue_info *q;
3211 struct be_ctrl_info *ctrl = &phba->ctrl;
3212
3213 q = &phba->ctrl.mcc_obj.q;
3214 if (q->created)
3215 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3216 be_queue_free(phba, q);
3217
3218 q = &phba->ctrl.mcc_obj.cq;
3219 if (q->created)
3220 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3221 be_queue_free(phba, q);
3222}
3223
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303224static void hwi_cleanup(struct beiscsi_hba *phba)
3225{
3226 struct be_queue_info *q;
3227 struct be_ctrl_info *ctrl = &phba->ctrl;
3228 struct hwi_controller *phwi_ctrlr;
3229 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303230 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303231
3232 phwi_ctrlr = phba->phwi_ctrlr;
3233 phwi_context = phwi_ctrlr->phwi_ctxt;
3234 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3235 q = &phwi_context->be_wrbq[i];
3236 if (q->created)
3237 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3238 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303239 free_wrb_handles(phba);
3240
3241 q = &phwi_context->be_def_hdrq;
3242 if (q->created)
3243 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3244
3245 q = &phwi_context->be_def_dataq;
3246 if (q->created)
3247 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3248
3249 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3250
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303251 for (i = 0; i < (phba->num_cpus); i++) {
3252 q = &phwi_context->be_cq[i];
3253 if (q->created)
3254 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3255 }
3256 if (phba->msix_enabled)
3257 eq_num = 1;
3258 else
3259 eq_num = 0;
3260 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3261 q = &phwi_context->be_eq[i].q;
3262 if (q->created)
3263 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3264 }
3265 be_mcc_queues_destroy(phba);
3266}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303267
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303268static int be_mcc_queues_create(struct beiscsi_hba *phba,
3269 struct hwi_context_memory *phwi_context)
3270{
3271 struct be_queue_info *q, *cq;
3272 struct be_ctrl_info *ctrl = &phba->ctrl;
3273
3274 /* Alloc MCC compl queue */
3275 cq = &phba->ctrl.mcc_obj.cq;
3276 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3277 sizeof(struct be_mcc_compl)))
3278 goto err;
3279 /* Ask BE to create MCC compl queue; */
3280 if (phba->msix_enabled) {
3281 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3282 [phba->num_cpus].q, false, true, 0))
3283 goto mcc_cq_free;
3284 } else {
3285 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3286 false, true, 0))
3287 goto mcc_cq_free;
3288 }
3289
3290 /* Alloc MCC queue */
3291 q = &phba->ctrl.mcc_obj.q;
3292 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3293 goto mcc_cq_destroy;
3294
3295 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303296 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303297 goto mcc_q_free;
3298
3299 return 0;
3300
3301mcc_q_free:
3302 be_queue_free(phba, q);
3303mcc_cq_destroy:
3304 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3305mcc_cq_free:
3306 be_queue_free(phba, cq);
3307err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303308 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303309}
3310
3311static int find_num_cpus(void)
3312{
3313 int num_cpus = 0;
3314
3315 num_cpus = num_online_cpus();
3316 if (num_cpus >= MAX_CPUS)
3317 num_cpus = MAX_CPUS - 1;
3318
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303319 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303320}
3321
3322static int hwi_init_port(struct beiscsi_hba *phba)
3323{
3324 struct hwi_controller *phwi_ctrlr;
3325 struct hwi_context_memory *phwi_context;
3326 unsigned int def_pdu_ring_sz;
3327 struct be_ctrl_info *ctrl = &phba->ctrl;
3328 int status;
3329
3330 def_pdu_ring_sz =
3331 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3332 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303333 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303334 phwi_context->max_eqd = 0;
3335 phwi_context->min_eqd = 0;
3336 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303337 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303338
3339 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303340 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303341 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3342 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303343 goto error;
3344 }
3345
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303346 status = be_mcc_queues_create(phba, phwi_context);
3347 if (status != 0)
3348 goto error;
3349
3350 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303351 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303352 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3353 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303354 goto error;
3355 }
3356
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303357 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303358 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303359 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3360 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303361 goto error;
3362 }
3363
3364 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3365 def_pdu_ring_sz);
3366 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303367 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3368 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303369 goto error;
3370 }
3371
3372 status = beiscsi_create_def_data(phba, phwi_context,
3373 phwi_ctrlr, def_pdu_ring_sz);
3374 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303375 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3376 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303377 goto error;
3378 }
3379
3380 status = beiscsi_post_pages(phba);
3381 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303382 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3383 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303384 goto error;
3385 }
3386
3387 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3388 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303389 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3390 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303391 goto error;
3392 }
3393
John Soni Jose99bc5d52012-08-20 23:00:18 +05303394 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3395 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303396 return 0;
3397
3398error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303399 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3400 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303401 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003402 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303403}
3404
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303405static int hwi_init_controller(struct beiscsi_hba *phba)
3406{
3407 struct hwi_controller *phwi_ctrlr;
3408
3409 phwi_ctrlr = phba->phwi_ctrlr;
3410 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3411 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3412 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303413 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3414 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3415 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303416 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303417 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3418 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3419 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303420 return -ENOMEM;
3421 }
3422
3423 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003424 if (beiscsi_init_wrb_handle(phba))
3425 return -ENOMEM;
3426
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303427 hwi_init_async_pdu_ctx(phba);
3428 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303429 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3430 "BM_%d : hwi_init_controller failed\n");
3431
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303432 return -ENOMEM;
3433 }
3434 return 0;
3435}
3436
3437static void beiscsi_free_mem(struct beiscsi_hba *phba)
3438{
3439 struct be_mem_descriptor *mem_descr;
3440 int i, j;
3441
3442 mem_descr = phba->init_mem;
3443 i = 0;
3444 j = 0;
3445 for (i = 0; i < SE_MEM_MAX; i++) {
3446 for (j = mem_descr->num_elements; j > 0; j--) {
3447 pci_free_consistent(phba->pcidev,
3448 mem_descr->mem_array[j - 1].size,
3449 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303450 (unsigned long)mem_descr->mem_array[j - 1].
3451 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303452 }
3453 kfree(mem_descr->mem_array);
3454 mem_descr++;
3455 }
3456 kfree(phba->init_mem);
3457 kfree(phba->phwi_ctrlr);
3458}
3459
3460static int beiscsi_init_controller(struct beiscsi_hba *phba)
3461{
3462 int ret = -ENOMEM;
3463
3464 ret = beiscsi_get_memory(phba);
3465 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303466 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3467 "BM_%d : beiscsi_dev_probe -"
3468 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303469 return ret;
3470 }
3471
3472 ret = hwi_init_controller(phba);
3473 if (ret)
3474 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303475 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3476 "BM_%d : Return success from beiscsi_init_controller");
3477
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303478 return 0;
3479
3480free_init:
3481 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003482 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303483}
3484
3485static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3486{
3487 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3488 struct sgl_handle *psgl_handle;
3489 struct iscsi_sge *pfrag;
3490 unsigned int arr_index, i, idx;
3491
3492 phba->io_sgl_hndl_avbl = 0;
3493 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303494
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303495 mem_descr_sglh = phba->init_mem;
3496 mem_descr_sglh += HWI_MEM_SGLH;
3497 if (1 == mem_descr_sglh->num_elements) {
3498 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3499 phba->params.ios_per_ctrl,
3500 GFP_KERNEL);
3501 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303502 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3503 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303504 return -ENOMEM;
3505 }
3506 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3507 (phba->params.icds_per_ctrl -
3508 phba->params.ios_per_ctrl),
3509 GFP_KERNEL);
3510 if (!phba->eh_sgl_hndl_base) {
3511 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303512 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3513 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303514 return -ENOMEM;
3515 }
3516 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303517 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3518 "BM_%d : HWI_MEM_SGLH is more than one element."
3519 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303520 return -ENOMEM;
3521 }
3522
3523 arr_index = 0;
3524 idx = 0;
3525 while (idx < mem_descr_sglh->num_elements) {
3526 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3527
3528 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3529 sizeof(struct sgl_handle)); i++) {
3530 if (arr_index < phba->params.ios_per_ctrl) {
3531 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3532 phba->io_sgl_hndl_avbl++;
3533 arr_index++;
3534 } else {
3535 phba->eh_sgl_hndl_base[arr_index -
3536 phba->params.ios_per_ctrl] =
3537 psgl_handle;
3538 arr_index++;
3539 phba->eh_sgl_hndl_avbl++;
3540 }
3541 psgl_handle++;
3542 }
3543 idx++;
3544 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303545 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3546 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3547 "phba->eh_sgl_hndl_avbl=%d\n",
3548 phba->io_sgl_hndl_avbl,
3549 phba->eh_sgl_hndl_avbl);
3550
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303551 mem_descr_sg = phba->init_mem;
3552 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303553 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3554 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3555 mem_descr_sg->num_elements);
3556
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303557 arr_index = 0;
3558 idx = 0;
3559 while (idx < mem_descr_sg->num_elements) {
3560 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3561
3562 for (i = 0;
3563 i < (mem_descr_sg->mem_array[idx].size) /
3564 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3565 i++) {
3566 if (arr_index < phba->params.ios_per_ctrl)
3567 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3568 else
3569 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3570 phba->params.ios_per_ctrl];
3571 psgl_handle->pfrag = pfrag;
3572 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3573 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3574 pfrag += phba->params.num_sge_per_io;
3575 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303576 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303577 }
3578 idx++;
3579 }
3580 phba->io_sgl_free_index = 0;
3581 phba->io_sgl_alloc_index = 0;
3582 phba->eh_sgl_free_index = 0;
3583 phba->eh_sgl_alloc_index = 0;
3584 return 0;
3585}
3586
3587static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3588{
3589 int i, new_cid;
3590
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303591 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303592 GFP_KERNEL);
3593 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303594 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3595 "BM_%d : Failed to allocate memory in "
3596 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303597 return -ENOMEM;
3598 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303599 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303600 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3601 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303602 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3603 "BM_%d : Failed to allocate memory in "
3604 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303605 kfree(phba->cid_array);
3606 return -ENOMEM;
3607 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303608 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303609 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3610 phba->cid_array[i] = new_cid;
3611 new_cid += 2;
3612 }
3613 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3614 return 0;
3615}
3616
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303617static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303618{
3619 struct be_ctrl_info *ctrl = &phba->ctrl;
3620 struct hwi_controller *phwi_ctrlr;
3621 struct hwi_context_memory *phwi_context;
3622 struct be_queue_info *eq;
3623 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303624 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303625 u32 enabled;
3626
3627 phwi_ctrlr = phba->phwi_ctrlr;
3628 phwi_context = phwi_ctrlr->phwi_ctxt;
3629
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303630 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3631 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3632 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303633
3634 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3635 if (!enabled) {
3636 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303637 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3638 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303639 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003640 }
3641
3642 if (!phba->msix_enabled) {
3643 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303644 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3645 "BM_%d : eq->id=%d\n", eq->id);
3646
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003647 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3648 } else {
3649 for (i = 0; i <= phba->num_cpus; i++) {
3650 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303651 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3652 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303653 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3654 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303655 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303656}
3657
3658static void hwi_disable_intr(struct beiscsi_hba *phba)
3659{
3660 struct be_ctrl_info *ctrl = &phba->ctrl;
3661
3662 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3663 u32 reg = ioread32(addr);
3664
3665 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3666 if (enabled) {
3667 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3668 iowrite32(reg, addr);
3669 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303670 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3671 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303672}
3673
John Soni Jose9aef4202012-08-20 23:00:08 +05303674/**
3675 * beiscsi_get_boot_info()- Get the boot session info
3676 * @phba: The device priv structure instance
3677 *
3678 * Get the boot target info and store in driver priv structure
3679 *
3680 * return values
3681 * Success: 0
3682 * Failure: Non-Zero Value
3683 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303684static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3685{
Mike Christie0e438952012-04-03 23:41:51 -05003686 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303687 struct be_mcc_wrb *wrb;
3688 struct be_dma_mem nonemb_cmd;
3689 unsigned int tag, wrb_num;
3690 unsigned short status, extd_status;
John Soni Jose9aef4202012-08-20 23:00:08 +05303691 unsigned int s_handle;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303692 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003693 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303694
John Soni Jose9aef4202012-08-20 23:00:08 +05303695 /* Get the session handle of the boot target */
3696 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3697 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303698 beiscsi_log(phba, KERN_ERR,
3699 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3700 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303701 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303702 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303703 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3704 sizeof(*session_resp),
3705 &nonemb_cmd.dma);
3706 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303707 beiscsi_log(phba, KERN_ERR,
3708 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3709 "BM_%d : Failed to allocate memory for"
3710 "beiscsi_get_session_info\n");
3711
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303712 return -ENOMEM;
3713 }
3714
3715 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303716 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003717 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303718 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303719 beiscsi_log(phba, KERN_ERR,
3720 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3721 "BM_%d : beiscsi_get_session_info"
3722 " Failed\n");
3723
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303724 goto boot_freemem;
3725 } else
3726 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3727 phba->ctrl.mcc_numtag[tag]);
3728
3729 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3730 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3731 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3732 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303733 beiscsi_log(phba, KERN_ERR,
3734 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3735 "BM_%d : beiscsi_get_session_info Failed"
3736 " status = %d extd_status = %d\n",
3737 status, extd_status);
3738
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303739 free_mcc_tag(&phba->ctrl, tag);
3740 goto boot_freemem;
3741 }
3742 wrb = queue_get_wrb(mccq, wrb_num);
3743 free_mcc_tag(&phba->ctrl, tag);
3744 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003745
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303746 memcpy(&phba->boot_sess, &session_resp->session_info,
3747 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003748 ret = 0;
3749
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303750boot_freemem:
3751 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3752 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003753 return ret;
3754}
3755
3756static void beiscsi_boot_release(void *data)
3757{
3758 struct beiscsi_hba *phba = data;
3759
3760 scsi_host_put(phba->shost);
3761}
3762
3763static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3764{
3765 struct iscsi_boot_kobj *boot_kobj;
3766
3767 /* get boot info using mgmt cmd */
3768 if (beiscsi_get_boot_info(phba))
3769 /* Try to see if we can carry on without this */
3770 return 0;
3771
3772 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3773 if (!phba->boot_kset)
3774 return -ENOMEM;
3775
3776 /* get a ref because the show function will ref the phba */
3777 if (!scsi_host_get(phba->shost))
3778 goto free_kset;
3779 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3780 beiscsi_show_boot_tgt_info,
3781 beiscsi_tgt_get_attr_visibility,
3782 beiscsi_boot_release);
3783 if (!boot_kobj)
3784 goto put_shost;
3785
3786 if (!scsi_host_get(phba->shost))
3787 goto free_kset;
3788 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3789 beiscsi_show_boot_ini_info,
3790 beiscsi_ini_get_attr_visibility,
3791 beiscsi_boot_release);
3792 if (!boot_kobj)
3793 goto put_shost;
3794
3795 if (!scsi_host_get(phba->shost))
3796 goto free_kset;
3797 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3798 beiscsi_show_boot_eth_info,
3799 beiscsi_eth_get_attr_visibility,
3800 beiscsi_boot_release);
3801 if (!boot_kobj)
3802 goto put_shost;
3803 return 0;
3804
3805put_shost:
3806 scsi_host_put(phba->shost);
3807free_kset:
3808 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303809 return -ENOMEM;
3810}
3811
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303812static int beiscsi_init_port(struct beiscsi_hba *phba)
3813{
3814 int ret;
3815
3816 ret = beiscsi_init_controller(phba);
3817 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303818 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3819 "BM_%d : beiscsi_dev_probe - Failed in"
3820 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303821 return ret;
3822 }
3823 ret = beiscsi_init_sgl_handle(phba);
3824 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303825 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3826 "BM_%d : beiscsi_dev_probe - Failed in"
3827 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303828 goto do_cleanup_ctrlr;
3829 }
3830
3831 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303832 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3833 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303834 kfree(phba->io_sgl_hndl_base);
3835 kfree(phba->eh_sgl_hndl_base);
3836 goto do_cleanup_ctrlr;
3837 }
3838
3839 return ret;
3840
3841do_cleanup_ctrlr:
3842 hwi_cleanup(phba);
3843 return ret;
3844}
3845
3846static void hwi_purge_eq(struct beiscsi_hba *phba)
3847{
3848 struct hwi_controller *phwi_ctrlr;
3849 struct hwi_context_memory *phwi_context;
3850 struct be_queue_info *eq;
3851 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303852 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303853 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303854
3855 phwi_ctrlr = phba->phwi_ctrlr;
3856 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303857 if (phba->msix_enabled)
3858 eq_msix = 1;
3859 else
3860 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303861
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303862 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3863 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303864 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303865 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303866 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3867 & EQE_VALID_MASK) {
3868 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3869 queue_tail_inc(eq);
3870 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303871 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303872 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303873
3874 if (num_processed)
3875 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303876 }
3877}
3878
3879static void beiscsi_clean_port(struct beiscsi_hba *phba)
3880{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303881 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303882
3883 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3884 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05303885 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3886 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303887
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303888 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303889 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303890 kfree(phba->io_sgl_hndl_base);
3891 kfree(phba->eh_sgl_hndl_base);
3892 kfree(phba->cid_array);
3893 kfree(phba->ep_array);
3894}
3895
Mike Christie1282ab72012-04-18 03:06:00 -05003896static void beiscsi_cleanup_task(struct iscsi_task *task)
3897{
3898 struct beiscsi_io_task *io_task = task->dd_data;
3899 struct iscsi_conn *conn = task->conn;
3900 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3901 struct beiscsi_hba *phba = beiscsi_conn->phba;
3902 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3903 struct hwi_wrb_context *pwrb_context;
3904 struct hwi_controller *phwi_ctrlr;
3905
3906 phwi_ctrlr = phba->phwi_ctrlr;
3907 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3908 - phba->fw_config.iscsi_cid_start];
3909
3910 if (io_task->cmd_bhs) {
3911 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3912 io_task->bhs_pa.u.a64.address);
3913 io_task->cmd_bhs = NULL;
3914 }
3915
3916 if (task->sc) {
3917 if (io_task->pwrb_handle) {
3918 free_wrb_handle(phba, pwrb_context,
3919 io_task->pwrb_handle);
3920 io_task->pwrb_handle = NULL;
3921 }
3922
3923 if (io_task->psgl_handle) {
3924 spin_lock(&phba->io_sgl_lock);
3925 free_io_sgl_handle(phba, io_task->psgl_handle);
3926 spin_unlock(&phba->io_sgl_lock);
3927 io_task->psgl_handle = NULL;
3928 }
3929 } else {
3930 if (!beiscsi_conn->login_in_progress) {
3931 if (io_task->pwrb_handle) {
3932 free_wrb_handle(phba, pwrb_context,
3933 io_task->pwrb_handle);
3934 io_task->pwrb_handle = NULL;
3935 }
3936 if (io_task->psgl_handle) {
3937 spin_lock(&phba->mgmt_sgl_lock);
3938 free_mgmt_sgl_handle(phba,
3939 io_task->psgl_handle);
3940 spin_unlock(&phba->mgmt_sgl_lock);
3941 io_task->psgl_handle = NULL;
3942 }
3943 }
3944 }
3945}
3946
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303947void
3948beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3949 struct beiscsi_offload_params *params)
3950{
3951 struct wrb_handle *pwrb_handle;
3952 struct iscsi_target_context_update_wrb *pwrb = NULL;
3953 struct be_mem_descriptor *mem_descr;
3954 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003955 struct iscsi_task *task = beiscsi_conn->task;
3956 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303957 u32 doorbell = 0;
3958
3959 /*
3960 * We can always use 0 here because it is reserved by libiscsi for
3961 * login/startup related tasks.
3962 */
Mike Christie1282ab72012-04-18 03:06:00 -05003963 beiscsi_conn->login_in_progress = 0;
3964 spin_lock_bh(&session->lock);
3965 beiscsi_cleanup_task(task);
3966 spin_unlock_bh(&session->lock);
3967
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303968 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303969 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303970 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3971 memset(pwrb, 0, sizeof(*pwrb));
3972 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3973 max_burst_length, pwrb, params->dw[offsetof
3974 (struct amap_beiscsi_offload_params,
3975 max_burst_length) / 32]);
3976 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3977 max_send_data_segment_length, pwrb,
3978 params->dw[offsetof(struct amap_beiscsi_offload_params,
3979 max_send_data_segment_length) / 32]);
3980 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3981 first_burst_length,
3982 pwrb,
3983 params->dw[offsetof(struct amap_beiscsi_offload_params,
3984 first_burst_length) / 32]);
3985
3986 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3987 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3988 erl) / 32] & OFFLD_PARAMS_ERL));
3989 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3990 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3991 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3992 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3993 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3994 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3995 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3996 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3997 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3998 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3999 (params->dw[offsetof(struct amap_beiscsi_offload_params,
4000 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
4001 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
4002 pwrb,
4003 (params->dw[offsetof(struct amap_beiscsi_offload_params,
4004 exp_statsn) / 32] + 1));
4005 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
4006 0x7);
4007 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
4008 pwrb, pwrb_handle->wrb_index);
4009 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
4010 pwrb, pwrb_handle->nxt_wrb_index);
4011 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
4012 session_state, pwrb, 0);
4013 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
4014 pwrb, 1);
4015 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
4016 pwrb, 0);
4017 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
4018 0);
4019
4020 mem_descr = phba->init_mem;
4021 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
4022
4023 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
4024 pad_buffer_addr_hi, pwrb,
4025 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
4026 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
4027 pad_buffer_addr_lo, pwrb,
4028 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
4029
4030 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
4031
4032 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304033 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304034 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304035 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4036
4037 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4038}
4039
4040static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4041 int *index, int *age)
4042{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304043 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304044 if (age)
4045 *age = conn->session->age;
4046}
4047
4048/**
4049 * beiscsi_alloc_pdu - allocates pdu and related resources
4050 * @task: libiscsi task
4051 * @opcode: opcode of pdu for task
4052 *
4053 * This is called with the session lock held. It will allocate
4054 * the wrb and sgl if needed for the command. And it will prep
4055 * the pdu's itt. beiscsi_parse_pdu will later translate
4056 * the pdu itt to the libiscsi task itt.
4057 */
4058static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4059{
4060 struct beiscsi_io_task *io_task = task->dd_data;
4061 struct iscsi_conn *conn = task->conn;
4062 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4063 struct beiscsi_hba *phba = beiscsi_conn->phba;
4064 struct hwi_wrb_context *pwrb_context;
4065 struct hwi_controller *phwi_ctrlr;
4066 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304067 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4068 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304069
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304070 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004071 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304072 if (!io_task->cmd_bhs)
4073 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304074 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304075 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304076 io_task->conn = beiscsi_conn;
4077
4078 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4079 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304080 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004081 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304082
4083 if (task->sc) {
4084 spin_lock(&phba->io_sgl_lock);
4085 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4086 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304087 if (!io_task->psgl_handle)
4088 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304089 io_task->pwrb_handle = alloc_wrb_handle(phba,
4090 beiscsi_conn->beiscsi_conn_cid -
4091 phba->fw_config.iscsi_cid_start);
4092 if (!io_task->pwrb_handle)
4093 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304094 } else {
4095 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304096 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304097 if (!beiscsi_conn->login_in_progress) {
4098 spin_lock(&phba->mgmt_sgl_lock);
4099 io_task->psgl_handle = (struct sgl_handle *)
4100 alloc_mgmt_sgl_handle(phba);
4101 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304102 if (!io_task->psgl_handle)
4103 goto free_hndls;
4104
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304105 beiscsi_conn->login_in_progress = 1;
4106 beiscsi_conn->plogin_sgl_handle =
4107 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304108 io_task->pwrb_handle =
4109 alloc_wrb_handle(phba,
4110 beiscsi_conn->beiscsi_conn_cid -
4111 phba->fw_config.iscsi_cid_start);
4112 if (!io_task->pwrb_handle)
4113 goto free_io_hndls;
4114 beiscsi_conn->plogin_wrb_handle =
4115 io_task->pwrb_handle;
4116
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304117 } else {
4118 io_task->psgl_handle =
4119 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304120 io_task->pwrb_handle =
4121 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304122 }
Mike Christie1282ab72012-04-18 03:06:00 -05004123 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304124 } else {
4125 spin_lock(&phba->mgmt_sgl_lock);
4126 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4127 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304128 if (!io_task->psgl_handle)
4129 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304130 io_task->pwrb_handle =
4131 alloc_wrb_handle(phba,
4132 beiscsi_conn->beiscsi_conn_cid -
4133 phba->fw_config.iscsi_cid_start);
4134 if (!io_task->pwrb_handle)
4135 goto free_mgmt_hndls;
4136
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304137 }
4138 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304139 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4140 wrb_index << 16) | (unsigned int)
4141 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304142 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304143
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304144 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4145 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304146
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304147free_io_hndls:
4148 spin_lock(&phba->io_sgl_lock);
4149 free_io_sgl_handle(phba, io_task->psgl_handle);
4150 spin_unlock(&phba->io_sgl_lock);
4151 goto free_hndls;
4152free_mgmt_hndls:
4153 spin_lock(&phba->mgmt_sgl_lock);
4154 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4155 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304156free_hndls:
4157 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304158 pwrb_context = &phwi_ctrlr->wrb_context[
4159 beiscsi_conn->beiscsi_conn_cid -
4160 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304161 if (io_task->pwrb_handle)
4162 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304163 io_task->pwrb_handle = NULL;
4164 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4165 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004166 io_task->cmd_bhs = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304167 beiscsi_log(phba, KERN_ERR,
4168 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4169 "BM_%d : Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304170 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304171}
4172
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304173static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4174 unsigned int num_sg, unsigned int xferlen,
4175 unsigned int writedir)
4176{
4177
4178 struct beiscsi_io_task *io_task = task->dd_data;
4179 struct iscsi_conn *conn = task->conn;
4180 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4181 struct beiscsi_hba *phba = beiscsi_conn->phba;
4182 struct iscsi_wrb *pwrb = NULL;
4183 unsigned int doorbell = 0;
4184
4185 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304186 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4187 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4188
4189 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304190 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4191 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304192 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304193 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304194 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4195 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304196 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4197 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304198
4199 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004200 cpu_to_be16(*(unsigned short *)
4201 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304202 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4203 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4204 io_task->pwrb_handle->wrb_index);
4205 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4206 be32_to_cpu(task->cmdsn));
4207 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4208 io_task->psgl_handle->sgl_index);
4209
4210 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4211
4212 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4213 io_task->pwrb_handle->nxt_wrb_index);
4214 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4215
4216 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304217 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304218 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4219 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4220
4221 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4222 return 0;
4223}
4224
4225static int beiscsi_mtask(struct iscsi_task *task)
4226{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304227 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304228 struct iscsi_conn *conn = task->conn;
4229 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4230 struct beiscsi_hba *phba = beiscsi_conn->phba;
4231 struct iscsi_wrb *pwrb = NULL;
4232 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304233 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304234
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304235 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304236 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304237 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304238 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4239 be32_to_cpu(task->cmdsn));
4240 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4241 io_task->pwrb_handle->wrb_index);
4242 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4243 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304244
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304245 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4246 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304247 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4248 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304249 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4250 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4251 hwi_write_buffer(pwrb, task);
4252 break;
4253 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004254 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4255 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4256 TGT_DM_CMD);
4257 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4258 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004259 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004260 } else {
4261 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4262 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004263 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004264 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304265 hwi_write_buffer(pwrb, task);
4266 break;
4267 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304268 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304269 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304270 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304271 hwi_write_buffer(pwrb, task);
4272 break;
4273 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304274 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4275 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304276 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4277 hwi_write_buffer(pwrb, task);
4278 break;
4279 case ISCSI_OP_LOGOUT:
4280 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4281 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304282 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304283 hwi_write_buffer(pwrb, task);
4284 break;
4285
4286 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304287 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4288 "BM_%d : opcode =%d Not supported\n",
4289 task->hdr->opcode & ISCSI_OPCODE_MASK);
4290
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304291 return -EINVAL;
4292 }
4293
4294 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304295 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304296 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4297 io_task->pwrb_handle->nxt_wrb_index);
4298 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4299
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304300 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304301 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304302 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4303 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4304 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4305 return 0;
4306}
4307
4308static int beiscsi_task_xmit(struct iscsi_task *task)
4309{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304310 struct beiscsi_io_task *io_task = task->dd_data;
4311 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304312 struct scatterlist *sg;
4313 int num_sg;
4314 unsigned int writedir = 0, xferlen = 0;
4315
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304316 if (!sc)
4317 return beiscsi_mtask(task);
4318
4319 io_task->scsi_cmnd = sc;
4320 num_sg = scsi_dma_map(sc);
4321 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304322 struct iscsi_conn *conn = task->conn;
4323 struct beiscsi_hba *phba = NULL;
4324
4325 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4326 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4327 "BM_%d : scsi_dma_map Failed\n");
4328
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304329 return num_sg;
4330 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304331 xferlen = scsi_bufflen(sc);
4332 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304333 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304334 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304335 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304336 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304337
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304338 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4339}
4340
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004341/**
4342 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4343 * @job: job to handle
4344 */
4345static int beiscsi_bsg_request(struct bsg_job *job)
4346{
4347 struct Scsi_Host *shost;
4348 struct beiscsi_hba *phba;
4349 struct iscsi_bsg_request *bsg_req = job->request;
4350 int rc = -EINVAL;
4351 unsigned int tag;
4352 struct be_dma_mem nonemb_cmd;
4353 struct be_cmd_resp_hdr *resp;
4354 struct iscsi_bsg_reply *bsg_reply = job->reply;
4355 unsigned short status, extd_status;
4356
4357 shost = iscsi_job_to_shost(job);
4358 phba = iscsi_host_priv(shost);
4359
4360 switch (bsg_req->msgcode) {
4361 case ISCSI_BSG_HST_VENDOR:
4362 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4363 job->request_payload.payload_len,
4364 &nonemb_cmd.dma);
4365 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304366 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4367 "BM_%d : Failed to allocate memory for "
4368 "beiscsi_bsg_request\n");
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004369 return -EIO;
4370 }
4371 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4372 &nonemb_cmd);
4373 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304374 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4375 "BM_%d : be_cmd_get_mac_addr Failed\n");
4376
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004377 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4378 nonemb_cmd.va, nonemb_cmd.dma);
4379 return -EAGAIN;
4380 } else
4381 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
4382 phba->ctrl.mcc_numtag[tag]);
4383 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4384 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4385 free_mcc_tag(&phba->ctrl, tag);
4386 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4387 sg_copy_from_buffer(job->reply_payload.sg_list,
4388 job->reply_payload.sg_cnt,
4389 nonemb_cmd.va, (resp->response_length
4390 + sizeof(*resp)));
4391 bsg_reply->reply_payload_rcv_len = resp->response_length;
4392 bsg_reply->result = status;
4393 bsg_job_done(job, bsg_reply->result,
4394 bsg_reply->reply_payload_rcv_len);
4395 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4396 nonemb_cmd.va, nonemb_cmd.dma);
4397 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304398 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4399 "BM_%d : be_cmd_get_mac_addr Failed"
4400 " status = %d extd_status = %d\n",
4401 status, extd_status);
4402
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004403 return -EIO;
4404 }
4405 break;
4406
4407 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304408 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4409 "BM_%d : Unsupported bsg command: 0x%x\n",
4410 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004411 break;
4412 }
4413
4414 return rc;
4415}
4416
John Soni Jose99bc5d52012-08-20 23:00:18 +05304417void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4418{
4419 /* Set the logging parameter */
4420 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4421}
4422
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004423static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304424{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304425 struct hwi_controller *phwi_ctrlr;
4426 struct hwi_context_memory *phwi_context;
4427 struct be_eq_obj *pbe_eq;
4428 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304429 u8 *real_offset = 0;
4430 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304431
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304432 phwi_ctrlr = phba->phwi_ctrlr;
4433 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304434 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304435 if (phba->msix_enabled) {
4436 for (i = 0; i <= phba->num_cpus; i++) {
4437 msix_vec = phba->msix_entries[i].vector;
4438 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004439 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304440 }
4441 } else
4442 if (phba->pcidev->irq)
4443 free_irq(phba->pcidev->irq, phba);
4444 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304445 destroy_workqueue(phba->wq);
4446 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304447 for (i = 0; i < phba->num_cpus; i++) {
4448 pbe_eq = &phwi_context->be_eq[i];
4449 blk_iopoll_disable(&pbe_eq->iopoll);
4450 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304451
4452 beiscsi_clean_port(phba);
4453 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304454 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4455
4456 value = readl((void *)real_offset);
4457
4458 if (value & 0x00010000) {
4459 value &= 0xfffeffff;
4460 writel(value, (void *)real_offset);
4461 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304462 beiscsi_unmap_pci_function(phba);
4463 pci_free_consistent(phba->pcidev,
4464 phba->ctrl.mbox_mem_alloced.size,
4465 phba->ctrl.mbox_mem_alloced.va,
4466 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004467}
4468
4469static void beiscsi_remove(struct pci_dev *pcidev)
4470{
4471
4472 struct beiscsi_hba *phba = NULL;
4473
4474 phba = pci_get_drvdata(pcidev);
4475 if (!phba) {
4476 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4477 return;
4478 }
4479
Mike Christie0e438952012-04-03 23:41:51 -05004480 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004481 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004482 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304483 iscsi_host_remove(phba->shost);
4484 pci_dev_put(phba->pcidev);
4485 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004486 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304487}
4488
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004489static void beiscsi_shutdown(struct pci_dev *pcidev)
4490{
4491
4492 struct beiscsi_hba *phba = NULL;
4493
4494 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4495 if (!phba) {
4496 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4497 return;
4498 }
4499
4500 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004501 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004502}
4503
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304504static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4505{
4506 int i, status;
4507
4508 for (i = 0; i <= phba->num_cpus; i++)
4509 phba->msix_entries[i].entry = i;
4510
4511 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4512 (phba->num_cpus + 1));
4513 if (!status)
4514 phba->msix_enabled = true;
4515
4516 return;
4517}
4518
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304519static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4520 const struct pci_device_id *id)
4521{
4522 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304523 struct hwi_controller *phwi_ctrlr;
4524 struct hwi_context_memory *phwi_context;
4525 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304526 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304527 u8 *real_offset = 0;
4528 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304529
4530 ret = beiscsi_enable_pci(pcidev);
4531 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304532 dev_err(&pcidev->dev,
4533 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304534 return ret;
4535 }
4536
4537 phba = beiscsi_hba_alloc(pcidev);
4538 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304539 dev_err(&pcidev->dev,
4540 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304541 goto disable_pci;
4542 }
4543
John Soni Jose99bc5d52012-08-20 23:00:18 +05304544 /* Initialize Driver configuration Paramters */
4545 beiscsi_hba_attrs_init(phba);
4546
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304547 switch (pcidev->device) {
4548 case BE_DEVICE_ID1:
4549 case OC_DEVICE_ID1:
4550 case OC_DEVICE_ID2:
4551 phba->generation = BE_GEN2;
4552 break;
4553 case BE_DEVICE_ID2:
4554 case OC_DEVICE_ID3:
4555 phba->generation = BE_GEN3;
4556 break;
4557 default:
4558 phba->generation = 0;
4559 }
4560
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304561 if (enable_msix)
4562 num_cpus = find_num_cpus();
4563 else
4564 num_cpus = 1;
4565 phba->num_cpus = num_cpus;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304566 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4567 "BM_%d : num_cpus = %d\n",
4568 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304569
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004570 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304571 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004572 if (!phba->msix_enabled)
4573 phba->num_cpus = 1;
4574 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304575 ret = be_ctrl_init(phba, pcidev);
4576 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304577 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4578 "BM_%d : beiscsi_dev_probe-"
4579 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304580 goto hba_free;
4581 }
4582
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304583 if (!num_hba) {
4584 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4585 value = readl((void *)real_offset);
4586 if (value & 0x00010000) {
4587 gcrashmode++;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304588 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4589 "BM_%d : Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004590 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304591 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304592 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4593 "BM_%d : Reset Failed. Aborting Crashdump\n");
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304594 goto hba_free;
4595 }
4596 ret = be_chk_reset_complete(phba);
4597 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304598 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4599 "BM_%d : Failed to get out of reset."
4600 "Aborting Crashdump\n");
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304601 goto hba_free;
4602 }
4603 } else {
4604 value |= 0x00010000;
4605 writel(value, (void *)real_offset);
4606 num_hba++;
4607 }
4608 }
4609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304610 spin_lock_init(&phba->io_sgl_lock);
4611 spin_lock_init(&phba->mgmt_sgl_lock);
4612 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304613 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4614 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304615 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4616 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304617 goto free_port;
4618 }
4619 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304620 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304621 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304622 ret = beiscsi_init_port(phba);
4623 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304624 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4625 "BM_%d : beiscsi_dev_probe-"
4626 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304627 goto free_port;
4628 }
4629
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304630 for (i = 0; i < MAX_MCC_CMD ; i++) {
4631 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4632 phba->ctrl.mcc_tag[i] = i + 1;
4633 phba->ctrl.mcc_numtag[i + 1] = 0;
4634 phba->ctrl.mcc_tag_available++;
4635 }
4636
4637 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4638
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304639 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4640 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004641 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304642 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304643 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4644 "BM_%d : beiscsi_dev_probe-"
4645 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304646 goto free_twq;
4647 }
4648
4649 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4650
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304651 phwi_ctrlr = phba->phwi_ctrlr;
4652 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304653 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304654 for (i = 0; i < phba->num_cpus; i++) {
4655 pbe_eq = &phwi_context->be_eq[i];
4656 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4657 be_iopoll);
4658 blk_iopoll_enable(&pbe_eq->iopoll);
4659 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304660 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304661 ret = beiscsi_init_irqs(phba);
4662 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304663 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4664 "BM_%d : beiscsi_dev_probe-"
4665 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304666 goto free_blkenbld;
4667 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304668 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004669
4670 if (beiscsi_setup_boot_info(phba))
4671 /*
4672 * log error but continue, because we may not be using
4673 * iscsi boot.
4674 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304675 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4676 "BM_%d : Could not set up "
4677 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004678
Mike Christie0e438952012-04-03 23:41:51 -05004679 beiscsi_create_def_ifaces(phba);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304680 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4681 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304682 return 0;
4683
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304684free_blkenbld:
4685 destroy_workqueue(phba->wq);
4686 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304687 for (i = 0; i < phba->num_cpus; i++) {
4688 pbe_eq = &phwi_context->be_eq[i];
4689 blk_iopoll_disable(&pbe_eq->iopoll);
4690 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304691free_twq:
4692 beiscsi_clean_port(phba);
4693 beiscsi_free_mem(phba);
4694free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304695 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4696
4697 value = readl((void *)real_offset);
4698
4699 if (value & 0x00010000) {
4700 value &= 0xfffeffff;
4701 writel(value, (void *)real_offset);
4702 }
4703
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304704 pci_free_consistent(phba->pcidev,
4705 phba->ctrl.mbox_mem_alloced.size,
4706 phba->ctrl.mbox_mem_alloced.va,
4707 phba->ctrl.mbox_mem_alloced.dma);
4708 beiscsi_unmap_pci_function(phba);
4709hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304710 if (phba->msix_enabled)
4711 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304712 iscsi_host_remove(phba->shost);
4713 pci_dev_put(phba->pcidev);
4714 iscsi_host_free(phba->shost);
4715disable_pci:
4716 pci_disable_device(pcidev);
4717 return ret;
4718}
4719
4720struct iscsi_transport beiscsi_iscsi_transport = {
4721 .owner = THIS_MODULE,
4722 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304723 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304724 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304725 .create_session = beiscsi_session_create,
4726 .destroy_session = beiscsi_session_destroy,
4727 .create_conn = beiscsi_conn_create,
4728 .bind_conn = beiscsi_conn_bind,
4729 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004730 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05004731 .set_iface_param = be2iscsi_iface_set_param,
4732 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304733 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004734 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304735 .get_session_param = iscsi_session_get_param,
4736 .get_host_param = beiscsi_get_host_param,
4737 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004738 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304739 .send_pdu = iscsi_conn_send_pdu,
4740 .xmit_task = beiscsi_task_xmit,
4741 .cleanup_task = beiscsi_cleanup_task,
4742 .alloc_pdu = beiscsi_alloc_pdu,
4743 .parse_pdu_itt = beiscsi_parse_pdu,
4744 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004745 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304746 .ep_connect = beiscsi_ep_connect,
4747 .ep_poll = beiscsi_ep_poll,
4748 .ep_disconnect = beiscsi_ep_disconnect,
4749 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004750 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304751};
4752
4753static struct pci_driver beiscsi_pci_driver = {
4754 .name = DRV_NAME,
4755 .probe = beiscsi_dev_probe,
4756 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004757 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304758 .id_table = beiscsi_pci_id_table
4759};
4760
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304761
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304762static int __init beiscsi_module_init(void)
4763{
4764 int ret;
4765
4766 beiscsi_scsi_transport =
4767 iscsi_register_transport(&beiscsi_iscsi_transport);
4768 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304769 printk(KERN_ERR
4770 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304771 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304772 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304773 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
4774 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304775
4776 ret = pci_register_driver(&beiscsi_pci_driver);
4777 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304778 printk(KERN_ERR
4779 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304780 goto unregister_iscsi_transport;
4781 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304782 return 0;
4783
4784unregister_iscsi_transport:
4785 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4786 return ret;
4787}
4788
4789static void __exit beiscsi_module_exit(void)
4790{
4791 pci_unregister_driver(&beiscsi_pci_driver);
4792 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4793}
4794
4795module_init(beiscsi_module_init);
4796module_exit(beiscsi_module_exit);