blob: ff89b4cc363417923cb4f2ead9d9641edeea3ad1 [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
John Soni Jose5faf17b2012-10-20 04:45:27 +05302 * Copyright (C) 2005 - 2012 Emulex
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070013 * linux-drivers@emulex.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070015 * Emulex
16 * 3333 Susan Street
17 * Costa Mesa, CA 92626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018 */
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070019
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053020#include <linux/reboot.h>
21#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053023#include <linux/interrupt.h>
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/semaphore.h>
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +053029#include <linux/iscsi_boot_sysfs.h>
Paul Gortmakeracf33682011-05-27 09:47:43 -040030#include <linux/module.h>
Jayamohan 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 Kallickal6733b392009-09-05 07:36:35 +053050
51MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
52MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050053MODULE_VERSION(BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050054MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053055MODULE_LICENSE("GPL");
56module_param(be_iopoll_budget, int, 0);
57module_param(enable_msix, int, 0);
58module_param(be_max_phys_size, uint, S_IRUGO);
John Soni Jose99bc5d52012-08-20 23:00:18 +053059MODULE_PARM_DESC(be_max_phys_size,
60 "Maximum Size (In Kilobytes) of physically contiguous "
61 "memory that can be allocated. Range is 16 - 128");
62
63#define beiscsi_disp_param(_name)\
64ssize_t \
65beiscsi_##_name##_disp(struct device *dev,\
66 struct device_attribute *attrib, char *buf) \
67{ \
68 struct Scsi_Host *shost = class_to_shost(dev);\
69 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
70 uint32_t param_val = 0; \
71 param_val = phba->attr_##_name;\
72 return snprintf(buf, PAGE_SIZE, "%d\n",\
73 phba->attr_##_name);\
74}
75
76#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
77int \
78beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
79{\
80 if (val >= _minval && val <= _maxval) {\
81 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
82 "BA_%d : beiscsi_"#_name" updated "\
83 "from 0x%x ==> 0x%x\n",\
84 phba->attr_##_name, val); \
85 phba->attr_##_name = val;\
86 return 0;\
87 } \
88 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
89 "BA_%d beiscsi_"#_name" attribute "\
90 "cannot be updated to 0x%x, "\
91 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
92 return -EINVAL;\
93}
94
95#define beiscsi_store_param(_name) \
96ssize_t \
97beiscsi_##_name##_store(struct device *dev,\
98 struct device_attribute *attr, const char *buf,\
99 size_t count) \
100{ \
101 struct Scsi_Host *shost = class_to_shost(dev);\
102 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
103 uint32_t param_val = 0;\
104 if (!isdigit(buf[0]))\
105 return -EINVAL;\
106 if (sscanf(buf, "%i", &param_val) != 1)\
107 return -EINVAL;\
108 if (beiscsi_##_name##_change(phba, param_val) == 0) \
109 return strlen(buf);\
110 else \
111 return -EINVAL;\
112}
113
114#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
115int \
116beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
117{ \
118 if (val >= _minval && val <= _maxval) {\
119 phba->attr_##_name = val;\
120 return 0;\
121 } \
122 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
123 "BA_%d beiscsi_"#_name" attribute " \
124 "cannot be updated to 0x%x, "\
125 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
126 phba->attr_##_name = _defval;\
127 return -EINVAL;\
128}
129
130#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
131static uint beiscsi_##_name = _defval;\
132module_param(beiscsi_##_name, uint, S_IRUGO);\
133MODULE_PARM_DESC(beiscsi_##_name, _descp);\
134beiscsi_disp_param(_name)\
135beiscsi_change_param(_name, _minval, _maxval, _defval)\
136beiscsi_store_param(_name)\
137beiscsi_init_param(_name, _minval, _maxval, _defval)\
138DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
139 beiscsi_##_name##_disp, beiscsi_##_name##_store)
140
141/*
142 * When new log level added update the
143 * the MAX allowed value for log_enable
144 */
145BEISCSI_RW_ATTR(log_enable, 0x00,
146 0xFF, 0x00, "Enable logging Bit Mask\n"
147 "\t\t\t\tInitialization Events : 0x01\n"
148 "\t\t\t\tMailbox Events : 0x02\n"
149 "\t\t\t\tMiscellaneous Events : 0x04\n"
150 "\t\t\t\tError Handling : 0x08\n"
151 "\t\t\t\tIO Path Events : 0x10\n"
152 "\t\t\t\tConfiguration Path : 0x20\n");
153
John Soni Jose5cac7592012-10-20 04:42:25 +0530154DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
John Soni Jose26000db2012-10-20 04:45:06 +0530155DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700156DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
Jayamohan Kallickal7ad4dfe2013-04-05 20:38:29 -0700157DEVICE_ATTR(beiscsi_active_cid_count, S_IRUGO, beiscsi_active_cid_disp, NULL);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530158struct device_attribute *beiscsi_attrs[] = {
159 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530160 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530161 &dev_attr_beiscsi_adapter_family,
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700162 &dev_attr_beiscsi_fw_ver,
Jayamohan Kallickal7ad4dfe2013-04-05 20:38:29 -0700163 &dev_attr_beiscsi_active_cid_count,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530164 NULL,
165};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530166
John Soni Jose6763daa2012-10-20 04:41:45 +0530167static char const *cqe_desc[] = {
168 "RESERVED_DESC",
169 "SOL_CMD_COMPLETE",
170 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
171 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
172 "CXN_KILLED_BURST_LEN_MISMATCH",
173 "CXN_KILLED_AHS_RCVD",
174 "CXN_KILLED_HDR_DIGEST_ERR",
175 "CXN_KILLED_UNKNOWN_HDR",
176 "CXN_KILLED_STALE_ITT_TTT_RCVD",
177 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
178 "CXN_KILLED_RST_RCVD",
179 "CXN_KILLED_TIMED_OUT",
180 "CXN_KILLED_RST_SENT",
181 "CXN_KILLED_FIN_RCVD",
182 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
183 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
184 "CXN_KILLED_OVER_RUN_RESIDUAL",
185 "CXN_KILLED_UNDER_RUN_RESIDUAL",
186 "CMD_KILLED_INVALID_STATSN_RCVD",
187 "CMD_KILLED_INVALID_R2T_RCVD",
188 "CMD_CXN_KILLED_LUN_INVALID",
189 "CMD_CXN_KILLED_ICD_INVALID",
190 "CMD_CXN_KILLED_ITT_INVALID",
191 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
192 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
193 "CXN_INVALIDATE_NOTIFY",
194 "CXN_INVALIDATE_INDEX_NOTIFY",
195 "CMD_INVALIDATED_NOTIFY",
196 "UNSOL_HDR_NOTIFY",
197 "UNSOL_DATA_NOTIFY",
198 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
199 "DRIVERMSG_NOTIFY",
200 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
201 "SOL_CMD_KILLED_DIF_ERR",
202 "CXN_KILLED_SYN_RCVD",
203 "CXN_KILLED_IMM_DATA_RCVD"
204};
205
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530206static int beiscsi_slave_configure(struct scsi_device *sdev)
207{
208 blk_queue_max_segment_size(sdev->request_queue, 65536);
209 return 0;
210}
211
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530212static int beiscsi_eh_abort(struct scsi_cmnd *sc)
213{
214 struct iscsi_cls_session *cls_session;
215 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
216 struct beiscsi_io_task *aborted_io_task;
217 struct iscsi_conn *conn;
218 struct beiscsi_conn *beiscsi_conn;
219 struct beiscsi_hba *phba;
220 struct iscsi_session *session;
221 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530222 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530223 unsigned int cid, tag, num_invalidate;
224
225 cls_session = starget_to_session(scsi_target(sc->device));
226 session = cls_session->dd_data;
227
228 spin_lock_bh(&session->lock);
229 if (!aborted_task || !aborted_task->sc) {
230 /* we raced */
231 spin_unlock_bh(&session->lock);
232 return SUCCESS;
233 }
234
235 aborted_io_task = aborted_task->dd_data;
236 if (!aborted_io_task->scsi_cmnd) {
237 /* raced or invalid command */
238 spin_unlock_bh(&session->lock);
239 return SUCCESS;
240 }
241 spin_unlock_bh(&session->lock);
242 conn = aborted_task->conn;
243 beiscsi_conn = conn->dd_data;
244 phba = beiscsi_conn->phba;
245
246 /* invalidate iocb */
247 cid = beiscsi_conn->beiscsi_conn_cid;
248 inv_tbl = phba->inv_tbl;
249 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
250 inv_tbl->cid = cid;
251 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
252 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530253 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
254 sizeof(struct invalidate_commands_params_in),
255 &nonemb_cmd.dma);
256 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530257 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
258 "BM_%d : Failed to allocate memory for"
259 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530260 return FAILED;
261 }
262 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
263
264 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
265 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530266 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530267 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
268 "BM_%d : mgmt_invalidate_icds could not be"
269 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530270 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
271 nonemb_cmd.va, nonemb_cmd.dma);
272
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530273 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530274 }
John Soni Josee175def2012-10-20 04:45:40 +0530275
276 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530277 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
278 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530279 return iscsi_eh_abort(sc);
280}
281
282static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
283{
284 struct iscsi_task *abrt_task;
285 struct beiscsi_io_task *abrt_io_task;
286 struct iscsi_conn *conn;
287 struct beiscsi_conn *beiscsi_conn;
288 struct beiscsi_hba *phba;
289 struct iscsi_session *session;
290 struct iscsi_cls_session *cls_session;
291 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530292 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530293 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530294
295 /* invalidate iocbs */
296 cls_session = starget_to_session(scsi_target(sc->device));
297 session = cls_session->dd_data;
298 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500299 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
300 spin_unlock_bh(&session->lock);
301 return FAILED;
302 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530303 conn = session->leadconn;
304 beiscsi_conn = conn->dd_data;
305 phba = beiscsi_conn->phba;
306 cid = beiscsi_conn->beiscsi_conn_cid;
307 inv_tbl = phba->inv_tbl;
308 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
309 num_invalidate = 0;
310 for (i = 0; i < conn->session->cmds_max; i++) {
311 abrt_task = conn->session->cmds[i];
312 abrt_io_task = abrt_task->dd_data;
313 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
314 continue;
315
316 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
317 continue;
318
319 inv_tbl->cid = cid;
320 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
321 num_invalidate++;
322 inv_tbl++;
323 }
324 spin_unlock_bh(&session->lock);
325 inv_tbl = phba->inv_tbl;
326
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530327 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
328 sizeof(struct invalidate_commands_params_in),
329 &nonemb_cmd.dma);
330 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530331 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
332 "BM_%d : Failed to allocate memory for"
333 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530334 return FAILED;
335 }
336 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
337 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
338 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
339 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530340 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530341 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
342 "BM_%d : mgmt_invalidate_icds could not be"
343 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530344 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
345 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530346 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530347 }
John Soni Josee175def2012-10-20 04:45:40 +0530348
349 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530350 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
351 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530352 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530353}
354
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530355static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
356{
357 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500358 struct mgmt_session_info *boot_sess = &phba->boot_sess;
359 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530360 char *str = buf;
361 int rc;
362
363 switch (type) {
364 case ISCSI_BOOT_TGT_NAME:
365 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500366 (int)strlen(boot_sess->target_name),
367 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530368 break;
369 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500370 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530371 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500372 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530373 else
374 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500375 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530376 break;
377 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500378 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530379 break;
380
381 case ISCSI_BOOT_TGT_CHAP_NAME:
382 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500383 boot_conn->negotiated_login_options.auth_data.chap.
384 target_chap_name_length,
385 (char *)&boot_conn->negotiated_login_options.
386 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530387 break;
388 case ISCSI_BOOT_TGT_CHAP_SECRET:
389 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500390 boot_conn->negotiated_login_options.auth_data.chap.
391 target_secret_length,
392 (char *)&boot_conn->negotiated_login_options.
393 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530394 break;
395 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
396 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500397 boot_conn->negotiated_login_options.auth_data.chap.
398 intr_chap_name_length,
399 (char *)&boot_conn->negotiated_login_options.
400 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530401 break;
402 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500403 rc = sprintf(str, "%.*s\n",
404 boot_conn->negotiated_login_options.auth_data.chap.
405 intr_secret_length,
406 (char *)&boot_conn->negotiated_login_options.
407 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530408 break;
409 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500410 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530411 break;
412 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500413 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530414 break;
415 default:
416 rc = -ENOSYS;
417 break;
418 }
419 return rc;
420}
421
422static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
423{
424 struct beiscsi_hba *phba = data;
425 char *str = buf;
426 int rc;
427
428 switch (type) {
429 case ISCSI_BOOT_INI_INITIATOR_NAME:
430 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
431 break;
432 default:
433 rc = -ENOSYS;
434 break;
435 }
436 return rc;
437}
438
439static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
440{
441 struct beiscsi_hba *phba = data;
442 char *str = buf;
443 int rc;
444
445 switch (type) {
446 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500447 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530448 break;
449 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500450 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530451 break;
452 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500453 rc = beiscsi_get_macaddr(str, phba);
454 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530455 default:
456 rc = -ENOSYS;
457 break;
458 }
459 return rc;
460}
461
462
Al Viro587a1f12011-07-23 23:11:19 -0400463static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464{
Al Viro587a1f12011-07-23 23:11:19 -0400465 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530466
467 switch (type) {
468 case ISCSI_BOOT_TGT_NAME:
469 case ISCSI_BOOT_TGT_IP_ADDR:
470 case ISCSI_BOOT_TGT_PORT:
471 case ISCSI_BOOT_TGT_CHAP_NAME:
472 case ISCSI_BOOT_TGT_CHAP_SECRET:
473 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
474 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
475 case ISCSI_BOOT_TGT_NIC_ASSOC:
476 case ISCSI_BOOT_TGT_FLAGS:
477 rc = S_IRUGO;
478 break;
479 default:
480 rc = 0;
481 break;
482 }
483 return rc;
484}
485
Al Viro587a1f12011-07-23 23:11:19 -0400486static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530487{
Al Viro587a1f12011-07-23 23:11:19 -0400488 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530489
490 switch (type) {
491 case ISCSI_BOOT_INI_INITIATOR_NAME:
492 rc = S_IRUGO;
493 break;
494 default:
495 rc = 0;
496 break;
497 }
498 return rc;
499}
500
501
Al Viro587a1f12011-07-23 23:11:19 -0400502static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530503{
Al Viro587a1f12011-07-23 23:11:19 -0400504 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530505
506 switch (type) {
507 case ISCSI_BOOT_ETH_FLAGS:
508 case ISCSI_BOOT_ETH_MAC:
509 case ISCSI_BOOT_ETH_INDEX:
510 rc = S_IRUGO;
511 break;
512 default:
513 rc = 0;
514 break;
515 }
516 return rc;
517}
518
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530519/*------------------- PCI Driver operations and data ----------------- */
520static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
521 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530522 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530523 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
524 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
525 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530526 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530527 { 0 }
528};
529MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
530
John Soni Jose99bc5d52012-08-20 23:00:18 +0530531
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530532static struct scsi_host_template beiscsi_sht = {
533 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500534 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530535 .proc_name = DRV_NAME,
536 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530537 .change_queue_depth = iscsi_change_queue_depth,
538 .slave_configure = beiscsi_slave_configure,
539 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530540 .eh_abort_handler = beiscsi_eh_abort,
541 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530542 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530543 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530544 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
545 .can_queue = BE2_IO_DEPTH,
546 .this_id = -1,
547 .max_sectors = BEISCSI_MAX_SECTORS,
548 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
549 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500550 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
551
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530552};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530553
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530554static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530555
556static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
557{
558 struct beiscsi_hba *phba;
559 struct Scsi_Host *shost;
560
561 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
562 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530563 dev_err(&pcidev->dev,
564 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530565 return NULL;
566 }
567 shost->dma_boundary = pcidev->dma_mask;
568 shost->max_id = BE2_MAX_SESSIONS;
569 shost->max_channel = 0;
570 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
571 shost->max_lun = BEISCSI_NUM_MAX_LUN;
572 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530573 phba = iscsi_host_priv(shost);
574 memset(phba, 0, sizeof(*phba));
575 phba->shost = shost;
576 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530577 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500578 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530579
580 if (iscsi_host_add(shost, &phba->pcidev->dev))
581 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530582
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530583 return phba;
584
585free_devices:
586 pci_dev_put(phba->pcidev);
587 iscsi_host_free(phba->shost);
588 return NULL;
589}
590
591static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
592{
593 if (phba->csr_va) {
594 iounmap(phba->csr_va);
595 phba->csr_va = NULL;
596 }
597 if (phba->db_va) {
598 iounmap(phba->db_va);
599 phba->db_va = NULL;
600 }
601 if (phba->pci_va) {
602 iounmap(phba->pci_va);
603 phba->pci_va = NULL;
604 }
605}
606
607static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
608 struct pci_dev *pcidev)
609{
610 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530611 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530612
613 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
614 pci_resource_len(pcidev, 2));
615 if (addr == NULL)
616 return -ENOMEM;
617 phba->ctrl.csr = addr;
618 phba->csr_va = addr;
619 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
620
621 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
622 if (addr == NULL)
623 goto pci_map_err;
624 phba->ctrl.db = addr;
625 phba->db_va = addr;
626 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
627
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530628 if (phba->generation == BE_GEN2)
629 pcicfg_reg = 1;
630 else
631 pcicfg_reg = 0;
632
633 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
634 pci_resource_len(pcidev, pcicfg_reg));
635
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530636 if (addr == NULL)
637 goto pci_map_err;
638 phba->ctrl.pcicfg = addr;
639 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530640 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530641 return 0;
642
643pci_map_err:
644 beiscsi_unmap_pci_function(phba);
645 return -ENOMEM;
646}
647
648static int beiscsi_enable_pci(struct pci_dev *pcidev)
649{
650 int ret;
651
652 ret = pci_enable_device(pcidev);
653 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530654 dev_err(&pcidev->dev,
655 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530656 return ret;
657 }
658
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530659 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530660 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
661 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
662 if (ret) {
663 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
664 pci_disable_device(pcidev);
665 return ret;
666 }
667 }
668 return 0;
669}
670
671static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
672{
673 struct be_ctrl_info *ctrl = &phba->ctrl;
674 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
675 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
676 int status = 0;
677
678 ctrl->pdev = pdev;
679 status = beiscsi_map_pci_bars(phba, pdev);
680 if (status)
681 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530682 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
683 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
684 mbox_mem_alloc->size,
685 &mbox_mem_alloc->dma);
686 if (!mbox_mem_alloc->va) {
687 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500688 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530689 }
690
691 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
692 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
693 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
694 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
695 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530696 spin_lock_init(&phba->ctrl.mcc_lock);
697 spin_lock_init(&phba->ctrl.mcc_cq_lock);
698
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530699 return status;
700}
701
702static void beiscsi_get_params(struct beiscsi_hba *phba)
703{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530704 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
705 - (phba->fw_config.iscsi_cid_count
706 + BE2_TMFS
707 + BE2_NOPOUT_REQ));
708 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530709 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700710 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530711 phba->params.num_sge_per_io = BE2_SGE;
712 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
713 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
714 phba->params.eq_timer = 64;
715 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530716 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
717 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530718 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
719 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530720 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
721 "BM_%d : phba->params.num_eq_entries=%d\n",
722 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530723 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530724 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
725 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530726 phba->params.wrbs_per_cxn = 256;
727}
728
729static void hwi_ring_eq_db(struct beiscsi_hba *phba,
730 unsigned int id, unsigned int clr_interrupt,
731 unsigned int num_processed,
732 unsigned char rearm, unsigned char event)
733{
734 u32 val = 0;
735 val |= id & DB_EQ_RING_ID_MASK;
736 if (rearm)
737 val |= 1 << DB_EQ_REARM_SHIFT;
738 if (clr_interrupt)
739 val |= 1 << DB_EQ_CLR_SHIFT;
740 if (event)
741 val |= 1 << DB_EQ_EVNT_SHIFT;
742 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
743 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
744}
745
746/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530747 * be_isr_mcc - The isr routine of the driver.
748 * @irq: Not used
749 * @dev_id: Pointer to host adapter structure
750 */
751static irqreturn_t be_isr_mcc(int irq, void *dev_id)
752{
753 struct beiscsi_hba *phba;
754 struct be_eq_entry *eqe = NULL;
755 struct be_queue_info *eq;
756 struct be_queue_info *mcc;
757 unsigned int num_eq_processed;
758 struct be_eq_obj *pbe_eq;
759 unsigned long flags;
760
761 pbe_eq = dev_id;
762 eq = &pbe_eq->q;
763 phba = pbe_eq->phba;
764 mcc = &phba->ctrl.mcc_obj.cq;
765 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530766
767 num_eq_processed = 0;
768
769 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
770 & EQE_VALID_MASK) {
771 if (((eqe->dw[offsetof(struct amap_eq_entry,
772 resource_id) / 32] &
773 EQE_RESID_MASK) >> 16) == mcc->id) {
774 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530775 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530776 spin_unlock_irqrestore(&phba->isr_lock, flags);
777 }
778 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
779 queue_tail_inc(eq);
780 eqe = queue_tail_node(eq);
781 num_eq_processed++;
782 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530783 if (pbe_eq->todo_mcc_cq)
784 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530785 if (num_eq_processed)
786 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
787
788 return IRQ_HANDLED;
789}
790
791/**
792 * be_isr_msix - The isr routine of the driver.
793 * @irq: Not used
794 * @dev_id: Pointer to host adapter structure
795 */
796static irqreturn_t be_isr_msix(int irq, void *dev_id)
797{
798 struct beiscsi_hba *phba;
799 struct be_eq_entry *eqe = NULL;
800 struct be_queue_info *eq;
801 struct be_queue_info *cq;
802 unsigned int num_eq_processed;
803 struct be_eq_obj *pbe_eq;
804 unsigned long flags;
805
806 pbe_eq = dev_id;
807 eq = &pbe_eq->q;
808 cq = pbe_eq->cq;
809 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530810
811 phba = pbe_eq->phba;
812 num_eq_processed = 0;
813 if (blk_iopoll_enabled) {
814 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
815 & EQE_VALID_MASK) {
816 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
817 blk_iopoll_sched(&pbe_eq->iopoll);
818
819 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
820 queue_tail_inc(eq);
821 eqe = queue_tail_node(eq);
822 num_eq_processed++;
823 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530824 } else {
825 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
826 & EQE_VALID_MASK) {
827 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530828 pbe_eq->todo_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530829 spin_unlock_irqrestore(&phba->isr_lock, flags);
830 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
831 queue_tail_inc(eq);
832 eqe = queue_tail_node(eq);
833 num_eq_processed++;
834 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530835
John Soni Jose72fb46a2012-10-20 04:42:49 +0530836 if (pbe_eq->todo_cq)
837 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530838 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530839
840 if (num_eq_processed)
841 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
842
843 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530844}
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);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530891 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530892 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) {
John Soni Jose72fb46a2012-10-20 04:42:49 +0530904 if (pbe_eq->todo_mcc_cq)
905 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530906
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);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530928 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530929 spin_unlock_irqrestore(&phba->isr_lock, flags);
930 } else {
931 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530932 pbe_eq->todo_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530933 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 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530940 if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
941 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530942
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,
John Soni Jose73133262012-10-20 04:44:49 +05301253 struct iscsi_task *task,
1254 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301255{
1256 struct beiscsi_io_task *io_task = task->dd_data;
1257 struct be_status_bhs *sts_bhs =
1258 (struct be_status_bhs *)io_task->cmd_bhs;
1259 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301260 unsigned char *sense;
1261 u32 resid = 0, exp_cmdsn, max_cmdsn;
1262 u8 rsp, status, flags;
1263
John Soni Jose73133262012-10-20 04:44:49 +05301264 exp_cmdsn = csol_cqe->exp_cmdsn;
1265 max_cmdsn = (csol_cqe->exp_cmdsn +
1266 csol_cqe->cmd_wnd - 1);
1267 rsp = csol_cqe->i_resp;
1268 status = csol_cqe->i_sts;
1269 flags = csol_cqe->i_flags;
1270 resid = csol_cqe->res_cnt;
1271
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001272 if (!task->sc) {
1273 if (io_task->scsi_cmnd)
1274 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301275
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001276 return;
1277 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301278 task->sc->result = (DID_OK << 16) | status;
1279 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1280 task->sc->result = DID_ERROR << 16;
1281 goto unmap;
1282 }
1283
1284 /* bidi not initially supported */
1285 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301286 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1287 task->sc->result = DID_ERROR << 16;
1288
1289 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1290 scsi_set_resid(task->sc, resid);
1291 if (!status && (scsi_bufflen(task->sc) - resid <
1292 task->sc->underflow))
1293 task->sc->result = DID_ERROR << 16;
1294 }
1295 }
1296
1297 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001298 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301299 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001300
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301301 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001302 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301303 memcpy(task->sc->sense_buffer, sense,
1304 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1305 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301306
John Soni Jose73133262012-10-20 04:44:49 +05301307 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1308 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301309unmap:
1310 scsi_dma_unmap(io_task->scsi_cmnd);
1311 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1312}
1313
1314static void
1315be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301316 struct iscsi_task *task,
1317 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301318{
1319 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301320 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301321 struct iscsi_conn *conn = beiscsi_conn->conn;
1322
1323 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301324 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301325 hdr->t2wait = 5;
1326 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301327 hdr->flags = csol_cqe->i_flags;
1328 hdr->response = csol_cqe->i_resp;
1329 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1330 hdr->max_cmdsn = (csol_cqe->exp_cmdsn + csol_cqe->cmd_wnd - 1);
1331
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301332 hdr->dlength[0] = 0;
1333 hdr->dlength[1] = 0;
1334 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301336 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301337 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1338}
1339
1340static void
1341be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301342 struct iscsi_task *task,
1343 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301344{
1345 struct iscsi_tm_rsp *hdr;
1346 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301347 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301348
1349 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301350 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301351 hdr->flags = csol_cqe->i_flags;
1352 hdr->response = csol_cqe->i_resp;
1353 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1354 hdr->max_cmdsn = (csol_cqe->exp_cmdsn +
1355 csol_cqe->cmd_wnd - 1);
1356
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301357 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301358 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1359}
1360
1361static void
1362hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1363 struct beiscsi_hba *phba, struct sol_cqe *psol)
1364{
1365 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301366 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301367 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301368 struct iscsi_task *task;
1369 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301370 struct iscsi_conn *conn = beiscsi_conn->conn;
1371 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301372 uint16_t wrb_index, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301373
1374 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001375 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301376 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1377 wrb_idx, psol);
1378 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1379 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001380 } else {
1381 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1382 wrb_idx, psol);
1383 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1384 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301385 }
1386
1387 pwrb_context = &phwi_ctrlr->wrb_context[
1388 cid - phba->fw_config.iscsi_cid_start];
1389 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301390 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301391
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301392 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001393 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301394 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001395 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301396 spin_lock_bh(&session->lock);
1397 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1398 spin_unlock_bh(&session->lock);
1399}
1400
1401static void
1402be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301403 struct iscsi_task *task,
1404 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301405{
1406 struct iscsi_nopin *hdr;
1407 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301408 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301409
1410 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301411 hdr->flags = csol_cqe->i_flags;
1412 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1413 hdr->max_cmdsn = be32_to_cpu(hdr->exp_cmdsn +
1414 csol_cqe->cmd_wnd - 1);
1415
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301416 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301417 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301418 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1419}
1420
John Soni Jose73133262012-10-20 04:44:49 +05301421static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1422 struct sol_cqe *psol,
1423 struct common_sol_cqe *csol_cqe)
1424{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001425 if (is_chip_be2_be3r(phba)) {
1426 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1427 i_exp_cmd_sn, psol);
1428 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1429 i_res_cnt, psol);
1430 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1431 i_cmd_wnd, psol);
1432 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1433 wrb_index, psol);
1434 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1435 cid, psol);
1436 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1437 hw_sts, psol);
1438 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1439 i_resp, psol);
1440 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1441 i_sts, psol);
1442 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1443 i_flags, psol);
1444 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301445 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1446 i_exp_cmd_sn, psol);
1447 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1448 i_res_cnt, psol);
1449 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1450 wrb_index, psol);
1451 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1452 cid, psol);
1453 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1454 hw_sts, psol);
1455 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1456 i_cmd_wnd, psol);
1457 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1458 cmd_cmpl, psol))
1459 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1460 i_sts, psol);
1461 else
1462 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1463 i_sts, psol);
1464 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1465 u, psol))
1466 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1467
1468 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1469 o, psol))
1470 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301471 }
1472}
1473
1474
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301475static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1476 struct beiscsi_hba *phba, struct sol_cqe *psol)
1477{
1478 struct hwi_wrb_context *pwrb_context;
1479 struct wrb_handle *pwrb_handle;
1480 struct iscsi_wrb *pwrb = NULL;
1481 struct hwi_controller *phwi_ctrlr;
1482 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301483 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301484 struct iscsi_conn *conn = beiscsi_conn->conn;
1485 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301486 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301487
1488 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301489
1490 /* Copy the elements to a common structure */
1491 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1492
1493 pwrb_context = &phwi_ctrlr->wrb_context[
1494 csol_cqe.cid - phba->fw_config.iscsi_cid_start];
1495
1496 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1497 csol_cqe.wrb_index];
1498
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301499 task = pwrb_handle->pio_handle;
1500 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301501 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301502
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301503 spin_lock_bh(&session->lock);
1504 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301505 case HWH_TYPE_IO:
1506 case HWH_TYPE_IO_RD:
1507 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301508 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301509 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301510 else
John Soni Jose73133262012-10-20 04:44:49 +05301511 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301512 break;
1513
1514 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301515 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301516 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301517 else
John Soni Jose73133262012-10-20 04:44:49 +05301518 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301519 break;
1520
1521 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301522 beiscsi_log(phba, KERN_ERR,
1523 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1524 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1525 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 break;
1527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301528 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301529 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301530 break;
1531
1532 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301533 beiscsi_log(phba, KERN_WARNING,
1534 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1535 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1536 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301537 csol_cqe.wrb_index,
1538 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301539 break;
1540 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301541
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301542 spin_unlock_bh(&session->lock);
1543}
1544
1545static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1546 *pasync_ctx, unsigned int is_header,
1547 unsigned int host_write_ptr)
1548{
1549 if (is_header)
1550 return &pasync_ctx->async_entry[host_write_ptr].
1551 header_busy_list;
1552 else
1553 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1554}
1555
1556static struct async_pdu_handle *
1557hwi_get_async_handle(struct beiscsi_hba *phba,
1558 struct beiscsi_conn *beiscsi_conn,
1559 struct hwi_async_pdu_context *pasync_ctx,
1560 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1561{
1562 struct be_bus_address phys_addr;
1563 struct list_head *pbusy_list;
1564 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301565 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301566 unsigned int index, dpl;
1567
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001568 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301569 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1570 dpl, pdpdu_cqe);
1571 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1572 index, pdpdu_cqe);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001573 } else {
1574 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1575 dpl, pdpdu_cqe);
1576 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1577 index, pdpdu_cqe);
John Soni Jose73133262012-10-20 04:44:49 +05301578 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301579
1580 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301581 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1582 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301583 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301584 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1585 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301586
1587 phys_addr.u.a64.address =
1588 *((unsigned long long *)(&phys_addr.u.a64.address));
1589
1590 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1591 & PDUCQE_CODE_MASK) {
1592 case UNSOL_HDR_NOTIFY:
1593 is_header = 1;
1594
John Soni Jose73133262012-10-20 04:44:49 +05301595 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1596 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301597 break;
1598 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301599 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1600 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301601 break;
1602 default:
1603 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301604 beiscsi_log(phba, KERN_WARNING,
1605 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1606 "BM_%d : Unexpected code=%d\n",
1607 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1608 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301609 return NULL;
1610 }
1611
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301612 WARN_ON(list_empty(pbusy_list));
1613 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001614 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301615 break;
1616 }
1617
1618 WARN_ON(!pasync_handle);
1619
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301620 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1621 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301622 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301623 pasync_handle->buffer_len = dpl;
1624 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301625
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301626 return pasync_handle;
1627}
1628
1629static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301630hwi_update_async_writables(struct beiscsi_hba *phba,
1631 struct hwi_async_pdu_context *pasync_ctx,
1632 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301633{
1634 struct list_head *pbusy_list;
1635 struct async_pdu_handle *pasync_handle;
1636 unsigned int num_entries, writables = 0;
1637 unsigned int *pep_read_ptr, *pwritables;
1638
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001639 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301640 if (is_header) {
1641 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1642 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301643 } else {
1644 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1645 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301646 }
1647
1648 while ((*pep_read_ptr) != cq_index) {
1649 (*pep_read_ptr)++;
1650 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1651
1652 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1653 *pep_read_ptr);
1654 if (writables == 0)
1655 WARN_ON(list_empty(pbusy_list));
1656
1657 if (!list_empty(pbusy_list)) {
1658 pasync_handle = list_entry(pbusy_list->next,
1659 struct async_pdu_handle,
1660 link);
1661 WARN_ON(!pasync_handle);
1662 pasync_handle->consumed = 1;
1663 }
1664
1665 writables++;
1666 }
1667
1668 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301669 beiscsi_log(phba, KERN_ERR,
1670 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1671 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1672 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301673 WARN_ON(1);
1674 }
1675
1676 *pwritables = *pwritables + writables;
1677 return 0;
1678}
1679
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001680static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301681 unsigned int cri)
1682{
1683 struct hwi_controller *phwi_ctrlr;
1684 struct hwi_async_pdu_context *pasync_ctx;
1685 struct async_pdu_handle *pasync_handle, *tmp_handle;
1686 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301687
1688 phwi_ctrlr = phba->phwi_ctrlr;
1689 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1690
1691 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1692
1693 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1694 list_del(&pasync_handle->link);
1695
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001696 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301697 list_add_tail(&pasync_handle->link,
1698 &pasync_ctx->async_header.free_list);
1699 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301700 } else {
1701 list_add_tail(&pasync_handle->link,
1702 &pasync_ctx->async_data.free_list);
1703 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301704 }
1705 }
1706
1707 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1708 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1709 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301710}
1711
1712static struct phys_addr *
1713hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1714 unsigned int is_header, unsigned int host_write_ptr)
1715{
1716 struct phys_addr *pasync_sge = NULL;
1717
1718 if (is_header)
1719 pasync_sge = pasync_ctx->async_header.ring_base;
1720 else
1721 pasync_sge = pasync_ctx->async_data.ring_base;
1722
1723 return pasync_sge + host_write_ptr;
1724}
1725
1726static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1727 unsigned int is_header)
1728{
1729 struct hwi_controller *phwi_ctrlr;
1730 struct hwi_async_pdu_context *pasync_ctx;
1731 struct async_pdu_handle *pasync_handle;
1732 struct list_head *pfree_link, *pbusy_list;
1733 struct phys_addr *pasync_sge;
1734 unsigned int ring_id, num_entries;
1735 unsigned int host_write_num;
1736 unsigned int writables;
1737 unsigned int i = 0;
1738 u32 doorbell = 0;
1739
1740 phwi_ctrlr = phba->phwi_ctrlr;
1741 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001742 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301743
1744 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301745 writables = min(pasync_ctx->async_header.writables,
1746 pasync_ctx->async_header.free_entries);
1747 pfree_link = pasync_ctx->async_header.free_list.next;
1748 host_write_num = pasync_ctx->async_header.host_write_ptr;
1749 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1750 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301751 writables = min(pasync_ctx->async_data.writables,
1752 pasync_ctx->async_data.free_entries);
1753 pfree_link = pasync_ctx->async_data.free_list.next;
1754 host_write_num = pasync_ctx->async_data.host_write_ptr;
1755 ring_id = phwi_ctrlr->default_pdu_data.id;
1756 }
1757
1758 writables = (writables / 8) * 8;
1759 if (writables) {
1760 for (i = 0; i < writables; i++) {
1761 pbusy_list =
1762 hwi_get_async_busy_list(pasync_ctx, is_header,
1763 host_write_num);
1764 pasync_handle =
1765 list_entry(pfree_link, struct async_pdu_handle,
1766 link);
1767 WARN_ON(!pasync_handle);
1768 pasync_handle->consumed = 0;
1769
1770 pfree_link = pfree_link->next;
1771
1772 pasync_sge = hwi_get_ring_address(pasync_ctx,
1773 is_header, host_write_num);
1774
1775 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1776 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1777
1778 list_move(&pasync_handle->link, pbusy_list);
1779
1780 host_write_num++;
1781 host_write_num = host_write_num % num_entries;
1782 }
1783
1784 if (is_header) {
1785 pasync_ctx->async_header.host_write_ptr =
1786 host_write_num;
1787 pasync_ctx->async_header.free_entries -= writables;
1788 pasync_ctx->async_header.writables -= writables;
1789 pasync_ctx->async_header.busy_entries += writables;
1790 } else {
1791 pasync_ctx->async_data.host_write_ptr = host_write_num;
1792 pasync_ctx->async_data.free_entries -= writables;
1793 pasync_ctx->async_data.writables -= writables;
1794 pasync_ctx->async_data.busy_entries += writables;
1795 }
1796
1797 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1798 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1799 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1800 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1801 << DB_DEF_PDU_CQPROC_SHIFT;
1802
1803 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1804 }
1805}
1806
1807static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1808 struct beiscsi_conn *beiscsi_conn,
1809 struct i_t_dpdu_cqe *pdpdu_cqe)
1810{
1811 struct hwi_controller *phwi_ctrlr;
1812 struct hwi_async_pdu_context *pasync_ctx;
1813 struct async_pdu_handle *pasync_handle = NULL;
1814 unsigned int cq_index = -1;
1815
1816 phwi_ctrlr = phba->phwi_ctrlr;
1817 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1818
1819 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1820 pdpdu_cqe, &cq_index);
1821 BUG_ON(pasync_handle->is_header != 0);
1822 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301823 hwi_update_async_writables(phba, pasync_ctx,
1824 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301825
1826 hwi_free_async_msg(phba, pasync_handle->cri);
1827 hwi_post_async_buffers(phba, pasync_handle->is_header);
1828}
1829
1830static unsigned int
1831hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1832 struct beiscsi_hba *phba,
1833 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1834{
1835 struct list_head *plist;
1836 struct async_pdu_handle *pasync_handle;
1837 void *phdr = NULL;
1838 unsigned int hdr_len = 0, buf_len = 0;
1839 unsigned int status, index = 0, offset = 0;
1840 void *pfirst_buffer = NULL;
1841 unsigned int num_buf = 0;
1842
1843 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1844
1845 list_for_each_entry(pasync_handle, plist, link) {
1846 if (index == 0) {
1847 phdr = pasync_handle->pbuffer;
1848 hdr_len = pasync_handle->buffer_len;
1849 } else {
1850 buf_len = pasync_handle->buffer_len;
1851 if (!num_buf) {
1852 pfirst_buffer = pasync_handle->pbuffer;
1853 num_buf++;
1854 }
1855 memcpy(pfirst_buffer + offset,
1856 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001857 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301858 }
1859 index++;
1860 }
1861
1862 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301863 (beiscsi_conn->beiscsi_conn_cid -
1864 phba->fw_config.iscsi_cid_start),
1865 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001866 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001868 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301869 return 0;
1870}
1871
1872static unsigned int
1873hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1874 struct beiscsi_hba *phba,
1875 struct async_pdu_handle *pasync_handle)
1876{
1877 struct hwi_async_pdu_context *pasync_ctx;
1878 struct hwi_controller *phwi_ctrlr;
1879 unsigned int bytes_needed = 0, status = 0;
1880 unsigned short cri = pasync_handle->cri;
1881 struct pdu_base *ppdu;
1882
1883 phwi_ctrlr = phba->phwi_ctrlr;
1884 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1885
1886 list_del(&pasync_handle->link);
1887 if (pasync_handle->is_header) {
1888 pasync_ctx->async_header.busy_entries--;
1889 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1890 hwi_free_async_msg(phba, cri);
1891 BUG();
1892 }
1893
1894 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1895 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1896 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1897 (unsigned short)pasync_handle->buffer_len;
1898 list_add_tail(&pasync_handle->link,
1899 &pasync_ctx->async_entry[cri].wait_queue.list);
1900
1901 ppdu = pasync_handle->pbuffer;
1902 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1903 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1904 0xFFFF0000) | ((be16_to_cpu((ppdu->
1905 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1906 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1907
1908 if (status == 0) {
1909 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1910 bytes_needed;
1911
1912 if (bytes_needed == 0)
1913 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1914 pasync_ctx, cri);
1915 }
1916 } else {
1917 pasync_ctx->async_data.busy_entries--;
1918 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1919 list_add_tail(&pasync_handle->link,
1920 &pasync_ctx->async_entry[cri].wait_queue.
1921 list);
1922 pasync_ctx->async_entry[cri].wait_queue.
1923 bytes_received +=
1924 (unsigned short)pasync_handle->buffer_len;
1925
1926 if (pasync_ctx->async_entry[cri].wait_queue.
1927 bytes_received >=
1928 pasync_ctx->async_entry[cri].wait_queue.
1929 bytes_needed)
1930 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1931 pasync_ctx, cri);
1932 }
1933 }
1934 return status;
1935}
1936
1937static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1938 struct beiscsi_hba *phba,
1939 struct i_t_dpdu_cqe *pdpdu_cqe)
1940{
1941 struct hwi_controller *phwi_ctrlr;
1942 struct hwi_async_pdu_context *pasync_ctx;
1943 struct async_pdu_handle *pasync_handle = NULL;
1944 unsigned int cq_index = -1;
1945
1946 phwi_ctrlr = phba->phwi_ctrlr;
1947 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1948 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1949 pdpdu_cqe, &cq_index);
1950
1951 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301952 hwi_update_async_writables(phba, pasync_ctx,
1953 pasync_handle->is_header, cq_index);
1954
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301955 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1956 hwi_post_async_buffers(phba, pasync_handle->is_header);
1957}
1958
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301959static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1960{
1961 struct be_queue_info *mcc_cq;
1962 struct be_mcc_compl *mcc_compl;
1963 unsigned int num_processed = 0;
1964
1965 mcc_cq = &phba->ctrl.mcc_obj.cq;
1966 mcc_compl = queue_tail_node(mcc_cq);
1967 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1968 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1969
1970 if (num_processed >= 32) {
1971 hwi_ring_cq_db(phba, mcc_cq->id,
1972 num_processed, 0, 0);
1973 num_processed = 0;
1974 }
1975 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1976 /* Interpret flags as an async trailer */
1977 if (is_link_state_evt(mcc_compl->flags))
1978 /* Interpret compl as a async link evt */
1979 beiscsi_async_link_state_process(phba,
1980 (struct be_async_event_link_state *) mcc_compl);
1981 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301982 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1983 "BM_%d : Unsupported Async Event, flags"
1984 " = 0x%08x\n",
1985 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301986 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1987 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1988 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1989 }
1990
1991 mcc_compl->flags = 0;
1992 queue_tail_inc(mcc_cq);
1993 mcc_compl = queue_tail_node(mcc_cq);
1994 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1995 num_processed++;
1996 }
1997
1998 if (num_processed > 0)
1999 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
2000
2001}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302002
John Soni Jose6763daa2012-10-20 04:41:45 +05302003/**
2004 * beiscsi_process_cq()- Process the Completion Queue
2005 * @pbe_eq: Event Q on which the Completion has come
2006 *
2007 * return
2008 * Number of Completion Entries processed.
2009 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302010static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302011{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302012 struct be_queue_info *cq;
2013 struct sol_cqe *sol;
2014 struct dmsg_cqe *dmsg;
2015 unsigned int num_processed = 0;
2016 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302017 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302019 struct beiscsi_endpoint *beiscsi_ep;
2020 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302021 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302023 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302025 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302026
2027 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2028 CQE_VALID_MASK) {
2029 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2030
John Soni Jose73133262012-10-20 04:44:49 +05302031 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2032 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302033
John Soni Jose73133262012-10-20 04:44:49 +05302034 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002035 if (is_chip_be2_be3r(phba)) {
2036 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2037 } else {
John Soni Jose73133262012-10-20 04:44:49 +05302038 if ((code == DRIVERMSG_NOTIFY) ||
2039 (code == UNSOL_HDR_NOTIFY) ||
2040 (code == UNSOL_DATA_NOTIFY))
2041 cid = AMAP_GET_BITS(
2042 struct amap_i_t_dpdu_cqe_v2,
2043 cid, sol);
2044 else
2045 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2046 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002047 }
John Soni Jose73133262012-10-20 04:44:49 +05302048
2049 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302050 beiscsi_ep = ep->dd_data;
2051 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302052
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302053 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302054 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302055 num_processed, 0, 0);
2056 tot_nump += num_processed;
2057 num_processed = 0;
2058 }
2059
John Soni Jose0a513dd2012-08-20 23:00:55 +05302060 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302061 case SOL_CMD_COMPLETE:
2062 hwi_complete_cmd(beiscsi_conn, phba, sol);
2063 break;
2064 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302065 beiscsi_log(phba, KERN_INFO,
2066 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302067 "BM_%d : Received %s[%d] on CID : %d\n",
2068 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302069
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302070 dmsg = (struct dmsg_cqe *)sol;
2071 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2072 break;
2073 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302074 beiscsi_log(phba, KERN_INFO,
2075 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302076 "BM_%d : Received %s[%d] on CID : %d\n",
2077 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302078
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302079 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2080 (struct i_t_dpdu_cqe *)sol);
2081 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302082 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302083 beiscsi_log(phba, KERN_INFO,
2084 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302085 "BM_%d : Received %s[%d] on CID : %d\n",
2086 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302087
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302088 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2089 (struct i_t_dpdu_cqe *)sol);
2090 break;
2091 case CXN_INVALIDATE_INDEX_NOTIFY:
2092 case CMD_INVALIDATED_NOTIFY:
2093 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302094 beiscsi_log(phba, KERN_ERR,
2095 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302096 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2097 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302098 break;
2099 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2100 case CMD_KILLED_INVALID_STATSN_RCVD:
2101 case CMD_KILLED_INVALID_R2T_RCVD:
2102 case CMD_CXN_KILLED_LUN_INVALID:
2103 case CMD_CXN_KILLED_ICD_INVALID:
2104 case CMD_CXN_KILLED_ITT_INVALID:
2105 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2106 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302107 beiscsi_log(phba, KERN_ERR,
2108 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302109 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2110 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302111 break;
2112 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302113 beiscsi_log(phba, KERN_ERR,
2114 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302115 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2116 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302117 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2118 (struct i_t_dpdu_cqe *) sol);
2119 break;
2120 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2121 case CXN_KILLED_BURST_LEN_MISMATCH:
2122 case CXN_KILLED_AHS_RCVD:
2123 case CXN_KILLED_HDR_DIGEST_ERR:
2124 case CXN_KILLED_UNKNOWN_HDR:
2125 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2126 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2127 case CXN_KILLED_TIMED_OUT:
2128 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302129 case CXN_KILLED_RST_SENT:
2130 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302131 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2132 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2133 case CXN_KILLED_OVER_RUN_RESIDUAL:
2134 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2135 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302136 beiscsi_log(phba, KERN_ERR,
2137 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302138 "BM_%d : Event %s[%d] received on CID : %d\n",
2139 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302140 if (beiscsi_conn)
2141 iscsi_conn_failure(beiscsi_conn->conn,
2142 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302143 break;
2144 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302145 beiscsi_log(phba, KERN_ERR,
2146 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302147 "BM_%d : Invalid CQE Event Received Code : %d"
2148 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302149 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302150 break;
2151 }
2152
2153 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2154 queue_tail_inc(cq);
2155 sol = queue_tail_node(cq);
2156 num_processed++;
2157 }
2158
2159 if (num_processed > 0) {
2160 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302161 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302162 }
2163 return tot_nump;
2164}
2165
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302166void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302167{
2168 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302169 struct hwi_controller *phwi_ctrlr;
2170 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302171 struct beiscsi_hba *phba;
2172 struct be_eq_obj *pbe_eq =
2173 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302174
John Soni Jose72fb46a2012-10-20 04:42:49 +05302175 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302176 phwi_ctrlr = phba->phwi_ctrlr;
2177 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302178
John Soni Jose72fb46a2012-10-20 04:42:49 +05302179 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302180 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302181 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302182 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302183 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302184 }
2185
John Soni Jose72fb46a2012-10-20 04:42:49 +05302186 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302187 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302188 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302189 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302190 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302191 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302192
2193 /* rearm EQ for further interrupts */
2194 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302195}
2196
2197static int be_iopoll(struct blk_iopoll *iop, int budget)
2198{
2199 static unsigned int ret;
2200 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302201 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302202
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302203 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2204 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302205 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302206 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302207 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302208 beiscsi_log(phba, KERN_INFO,
2209 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2210 "BM_%d : rearm pbe_eq->q.id =%d\n",
2211 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302212 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302213 }
2214 return ret;
2215}
2216
2217static void
John Soni Jose09a10932012-10-20 04:44:23 +05302218hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2219 unsigned int num_sg, struct beiscsi_io_task *io_task)
2220{
2221 struct iscsi_sge *psgl;
2222 unsigned int sg_len, index;
2223 unsigned int sge_len = 0;
2224 unsigned long long addr;
2225 struct scatterlist *l_sg;
2226 unsigned int offset;
2227
2228 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2229 io_task->bhs_pa.u.a32.address_lo);
2230 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2231 io_task->bhs_pa.u.a32.address_hi);
2232
2233 l_sg = sg;
2234 for (index = 0; (index < num_sg) && (index < 2); index++,
2235 sg = sg_next(sg)) {
2236 if (index == 0) {
2237 sg_len = sg_dma_len(sg);
2238 addr = (u64) sg_dma_address(sg);
2239 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2240 sge0_addr_lo, pwrb,
2241 lower_32_bits(addr));
2242 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2243 sge0_addr_hi, pwrb,
2244 upper_32_bits(addr));
2245 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2246 sge0_len, pwrb,
2247 sg_len);
2248 sge_len = sg_len;
2249 } else {
2250 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2251 pwrb, sge_len);
2252 sg_len = sg_dma_len(sg);
2253 addr = (u64) sg_dma_address(sg);
2254 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2255 sge1_addr_lo, pwrb,
2256 lower_32_bits(addr));
2257 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2258 sge1_addr_hi, pwrb,
2259 upper_32_bits(addr));
2260 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2261 sge1_len, pwrb,
2262 sg_len);
2263 }
2264 }
2265 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2266 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2267
2268 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2269
2270 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2271 io_task->bhs_pa.u.a32.address_hi);
2272 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2273 io_task->bhs_pa.u.a32.address_lo);
2274
2275 if (num_sg == 1) {
2276 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2277 1);
2278 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2279 0);
2280 } else if (num_sg == 2) {
2281 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2282 0);
2283 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2284 1);
2285 } else {
2286 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2287 0);
2288 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2289 0);
2290 }
2291
2292 sg = l_sg;
2293 psgl++;
2294 psgl++;
2295 offset = 0;
2296 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2297 sg_len = sg_dma_len(sg);
2298 addr = (u64) sg_dma_address(sg);
2299 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2300 lower_32_bits(addr));
2301 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2302 upper_32_bits(addr));
2303 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2304 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2305 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2306 offset += sg_len;
2307 }
2308 psgl--;
2309 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2310}
2311
2312static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302313hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2314 unsigned int num_sg, struct beiscsi_io_task *io_task)
2315{
2316 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302317 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302318 unsigned int sge_len = 0;
2319 unsigned long long addr;
2320 struct scatterlist *l_sg;
2321 unsigned int offset;
2322
2323 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2324 io_task->bhs_pa.u.a32.address_lo);
2325 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2326 io_task->bhs_pa.u.a32.address_hi);
2327
2328 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302329 for (index = 0; (index < num_sg) && (index < 2); index++,
2330 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302331 if (index == 0) {
2332 sg_len = sg_dma_len(sg);
2333 addr = (u64) sg_dma_address(sg);
2334 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302335 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302336 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302337 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302338 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2339 sg_len);
2340 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302341 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302342 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2343 pwrb, sge_len);
2344 sg_len = sg_dma_len(sg);
2345 addr = (u64) sg_dma_address(sg);
2346 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302347 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302348 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302349 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302350 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2351 sg_len);
2352 }
2353 }
2354 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2355 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2356
2357 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2358
2359 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2360 io_task->bhs_pa.u.a32.address_hi);
2361 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2362 io_task->bhs_pa.u.a32.address_lo);
2363
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302364 if (num_sg == 1) {
2365 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2366 1);
2367 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2368 0);
2369 } else if (num_sg == 2) {
2370 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2371 0);
2372 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2373 1);
2374 } else {
2375 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2376 0);
2377 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2378 0);
2379 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302380 sg = l_sg;
2381 psgl++;
2382 psgl++;
2383 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302384 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302385 sg_len = sg_dma_len(sg);
2386 addr = (u64) sg_dma_address(sg);
2387 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2388 (addr & 0xFFFFFFFF));
2389 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2390 (addr >> 32));
2391 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2392 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2393 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2394 offset += sg_len;
2395 }
2396 psgl--;
2397 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2398}
2399
John Soni Josed629c472012-10-20 04:42:00 +05302400/**
2401 * hwi_write_buffer()- Populate the WRB with task info
2402 * @pwrb: ptr to the WRB entry
2403 * @task: iscsi task which is to be executed
2404 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302405static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2406{
2407 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302408 struct beiscsi_io_task *io_task = task->dd_data;
2409 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2410 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302411 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302412
2413 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2414 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2415 io_task->bhs_pa.u.a32.address_lo);
2416 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2417 io_task->bhs_pa.u.a32.address_hi);
2418
2419 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302420
2421 /* Check for the data_count */
2422 dsp_value = (task->data_count) ? 1 : 0;
2423
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002424 if (is_chip_be2_be3r(phba))
2425 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302426 pwrb, dsp_value);
2427 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002428 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302429 pwrb, dsp_value);
2430
2431 /* Map addr only if there is data_count */
2432 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302433 io_task->mtask_addr = pci_map_single(phba->pcidev,
2434 task->data,
2435 task->data_count,
2436 PCI_DMA_TODEVICE);
John Soni Josed629c472012-10-20 04:42:00 +05302437 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302438 } else
John Soni Josed629c472012-10-20 04:42:00 +05302439 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302440
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302441 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302442 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302443 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302444 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302445 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2446 task->data_count);
2447
2448 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2449 } else {
2450 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302451 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302452 }
2453
2454 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2455
2456 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2457
2458 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2459 io_task->bhs_pa.u.a32.address_hi);
2460 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2461 io_task->bhs_pa.u.a32.address_lo);
2462 if (task->data) {
2463 psgl++;
2464 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2466 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2467 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2468 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2469 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2470
2471 psgl++;
2472 if (task->data) {
2473 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302474 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302475 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302476 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302477 }
2478 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2479 }
2480 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2481}
2482
2483static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2484{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302485 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302486 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2487 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2488
2489 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2490 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302491 num_async_pdu_buf_pages =
2492 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2493 phba->params.defpdu_hdr_sz);
2494 num_async_pdu_buf_sgl_pages =
2495 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2496 sizeof(struct phys_addr));
2497 num_async_pdu_data_pages =
2498 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2499 phba->params.defpdu_data_sz);
2500 num_async_pdu_data_sgl_pages =
2501 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2502 sizeof(struct phys_addr));
2503
2504 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2505
2506 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2507 BE_ISCSI_PDU_HEADER_SIZE;
2508 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2509 sizeof(struct hwi_context_memory);
2510
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302511
2512 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2513 * (phba->params.wrbs_per_cxn)
2514 * phba->params.cxns_per_ctrl;
2515 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2516 (phba->params.wrbs_per_cxn);
2517 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2518 phba->params.cxns_per_ctrl);
2519
2520 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2521 phba->params.icds_per_ctrl;
2522 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2523 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2524
2525 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2526 num_async_pdu_buf_pages * PAGE_SIZE;
2527 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2528 num_async_pdu_data_pages * PAGE_SIZE;
2529 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2530 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2531 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2532 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2533 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2534 phba->params.asyncpdus_per_ctrl *
2535 sizeof(struct async_pdu_handle);
2536 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2537 phba->params.asyncpdus_per_ctrl *
2538 sizeof(struct async_pdu_handle);
2539 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2540 sizeof(struct hwi_async_pdu_context) +
2541 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2542}
2543
2544static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2545{
2546 struct be_mem_descriptor *mem_descr;
2547 dma_addr_t bus_add;
2548 struct mem_array *mem_arr, *mem_arr_orig;
2549 unsigned int i, j, alloc_size, curr_alloc_size;
2550
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002551 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302552 if (!phba->phwi_ctrlr)
2553 return -ENOMEM;
2554
2555 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2556 GFP_KERNEL);
2557 if (!phba->init_mem) {
2558 kfree(phba->phwi_ctrlr);
2559 return -ENOMEM;
2560 }
2561
2562 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2563 GFP_KERNEL);
2564 if (!mem_arr_orig) {
2565 kfree(phba->init_mem);
2566 kfree(phba->phwi_ctrlr);
2567 return -ENOMEM;
2568 }
2569
2570 mem_descr = phba->init_mem;
2571 for (i = 0; i < SE_MEM_MAX; i++) {
2572 j = 0;
2573 mem_arr = mem_arr_orig;
2574 alloc_size = phba->mem_req[i];
2575 memset(mem_arr, 0, sizeof(struct mem_array) *
2576 BEISCSI_MAX_FRAGS_INIT);
2577 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2578 do {
2579 mem_arr->virtual_address = pci_alloc_consistent(
2580 phba->pcidev,
2581 curr_alloc_size,
2582 &bus_add);
2583 if (!mem_arr->virtual_address) {
2584 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2585 goto free_mem;
2586 if (curr_alloc_size -
2587 rounddown_pow_of_two(curr_alloc_size))
2588 curr_alloc_size = rounddown_pow_of_two
2589 (curr_alloc_size);
2590 else
2591 curr_alloc_size = curr_alloc_size / 2;
2592 } else {
2593 mem_arr->bus_address.u.
2594 a64.address = (__u64) bus_add;
2595 mem_arr->size = curr_alloc_size;
2596 alloc_size -= curr_alloc_size;
2597 curr_alloc_size = min(be_max_phys_size *
2598 1024, alloc_size);
2599 j++;
2600 mem_arr++;
2601 }
2602 } while (alloc_size);
2603 mem_descr->num_elements = j;
2604 mem_descr->size_in_bytes = phba->mem_req[i];
2605 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2606 GFP_KERNEL);
2607 if (!mem_descr->mem_array)
2608 goto free_mem;
2609
2610 memcpy(mem_descr->mem_array, mem_arr_orig,
2611 sizeof(struct mem_array) * j);
2612 mem_descr++;
2613 }
2614 kfree(mem_arr_orig);
2615 return 0;
2616free_mem:
2617 mem_descr->num_elements = j;
2618 while ((i) || (j)) {
2619 for (j = mem_descr->num_elements; j > 0; j--) {
2620 pci_free_consistent(phba->pcidev,
2621 mem_descr->mem_array[j - 1].size,
2622 mem_descr->mem_array[j - 1].
2623 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302624 (unsigned long)mem_descr->
2625 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302626 bus_address.u.a64.address);
2627 }
2628 if (i) {
2629 i--;
2630 kfree(mem_descr->mem_array);
2631 mem_descr--;
2632 }
2633 }
2634 kfree(mem_arr_orig);
2635 kfree(phba->init_mem);
2636 kfree(phba->phwi_ctrlr);
2637 return -ENOMEM;
2638}
2639
2640static int beiscsi_get_memory(struct beiscsi_hba *phba)
2641{
2642 beiscsi_find_mem_req(phba);
2643 return beiscsi_alloc_mem(phba);
2644}
2645
2646static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2647{
2648 struct pdu_data_out *pdata_out;
2649 struct pdu_nop_out *pnop_out;
2650 struct be_mem_descriptor *mem_descr;
2651
2652 mem_descr = phba->init_mem;
2653 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2654 pdata_out =
2655 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2656 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2657
2658 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2659 IIOC_SCSI_DATA);
2660
2661 pnop_out =
2662 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2663 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2664
2665 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2666 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2667 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2668 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2669}
2670
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002671static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302672{
2673 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002674 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302675 struct hwi_controller *phwi_ctrlr;
2676 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002677 struct iscsi_wrb *pwrb = NULL;
2678 unsigned int num_cxn_wrbh = 0;
2679 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302680
2681 mem_descr_wrbh = phba->init_mem;
2682 mem_descr_wrbh += HWI_MEM_WRBH;
2683
2684 mem_descr_wrb = phba->init_mem;
2685 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302686 phwi_ctrlr = phba->phwi_ctrlr;
2687
2688 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2689 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302690 pwrb_context->pwrb_handle_base =
2691 kzalloc(sizeof(struct wrb_handle *) *
2692 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002693 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302694 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2695 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002696 goto init_wrb_hndl_failed;
2697 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302698 pwrb_context->pwrb_handle_basestd =
2699 kzalloc(sizeof(struct wrb_handle *) *
2700 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002701 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302702 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2703 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002704 goto init_wrb_hndl_failed;
2705 }
2706 if (!num_cxn_wrbh) {
2707 pwrb_handle =
2708 mem_descr_wrbh->mem_array[idx].virtual_address;
2709 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2710 ((sizeof(struct wrb_handle)) *
2711 phba->params.wrbs_per_cxn));
2712 idx++;
2713 }
2714 pwrb_context->alloc_index = 0;
2715 pwrb_context->wrb_handles_available = 0;
2716 pwrb_context->free_index = 0;
2717
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302718 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302719 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2720 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2721 pwrb_context->pwrb_handle_basestd[j] =
2722 pwrb_handle;
2723 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302724 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302725 pwrb_handle++;
2726 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302727 num_cxn_wrbh--;
2728 }
2729 }
2730 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302731 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302732 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002733 if (!num_cxn_wrb) {
2734 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2735 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2736 ((sizeof(struct iscsi_wrb) *
2737 phba->params.wrbs_per_cxn));
2738 idx++;
2739 }
2740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302741 if (num_cxn_wrb) {
2742 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2743 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2744 pwrb_handle->pwrb = pwrb;
2745 pwrb++;
2746 }
2747 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302748 }
2749 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002750 return 0;
2751init_wrb_hndl_failed:
2752 for (j = index; j > 0; j--) {
2753 pwrb_context = &phwi_ctrlr->wrb_context[j];
2754 kfree(pwrb_context->pwrb_handle_base);
2755 kfree(pwrb_context->pwrb_handle_basestd);
2756 }
2757 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302758}
2759
2760static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2761{
2762 struct hwi_controller *phwi_ctrlr;
2763 struct hba_parameters *p = &phba->params;
2764 struct hwi_async_pdu_context *pasync_ctx;
2765 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002766 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302767 struct be_mem_descriptor *mem_descr;
2768
2769 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2770 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2771
2772 phwi_ctrlr = phba->phwi_ctrlr;
2773 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2774 mem_descr->mem_array[0].virtual_address;
2775 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2776 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2777
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002778 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2779 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302780
2781 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2782 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2783 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302784 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2785 "BM_%d : hwi_init_async_pdu_ctx"
2786 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2787 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302788 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302789 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2790 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302791
2792 pasync_ctx->async_header.va_base =
2793 mem_descr->mem_array[0].virtual_address;
2794
2795 pasync_ctx->async_header.pa_base.u.a64.address =
2796 mem_descr->mem_array[0].bus_address.u.a64.address;
2797
2798 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2799 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2800 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302801 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2802 "BM_%d : hwi_init_async_pdu_ctx"
2803 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2804 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302805 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302806 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2807 "BM_%d : No Virtual address\n");
2808
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302809 pasync_ctx->async_header.ring_base =
2810 mem_descr->mem_array[0].virtual_address;
2811
2812 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2813 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2814 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302815 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2816 "BM_%d : hwi_init_async_pdu_ctx"
2817 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2818 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302819 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302820 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2821 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302822
2823 pasync_ctx->async_header.handle_base =
2824 mem_descr->mem_array[0].virtual_address;
2825 pasync_ctx->async_header.writables = 0;
2826 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2827
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302828
2829 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2830 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2831 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302832 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2833 "BM_%d : hwi_init_async_pdu_ctx"
2834 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2835 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302836 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302837 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2838 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302839
2840 pasync_ctx->async_data.ring_base =
2841 mem_descr->mem_array[0].virtual_address;
2842
2843 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2844 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2845 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302846 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2847 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302848
2849 pasync_ctx->async_data.handle_base =
2850 mem_descr->mem_array[0].virtual_address;
2851 pasync_ctx->async_data.writables = 0;
2852 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2853
2854 pasync_header_h =
2855 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2856 pasync_data_h =
2857 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2858
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002859 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2860 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2861 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302862 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2863 "BM_%d : hwi_init_async_pdu_ctx"
2864 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2865 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002866 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302867 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2868 "BM_%d : No Virtual address\n");
2869
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002870 idx = 0;
2871 pasync_ctx->async_data.va_base =
2872 mem_descr->mem_array[idx].virtual_address;
2873 pasync_ctx->async_data.pa_base.u.a64.address =
2874 mem_descr->mem_array[idx].bus_address.u.a64.address;
2875
2876 num_async_data = ((mem_descr->mem_array[idx].size) /
2877 phba->params.defpdu_data_sz);
2878 num_per_mem = 0;
2879
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302880 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2881 pasync_header_h->cri = -1;
2882 pasync_header_h->index = (char)index;
2883 INIT_LIST_HEAD(&pasync_header_h->link);
2884 pasync_header_h->pbuffer =
2885 (void *)((unsigned long)
2886 (pasync_ctx->async_header.va_base) +
2887 (p->defpdu_hdr_sz * index));
2888
2889 pasync_header_h->pa.u.a64.address =
2890 pasync_ctx->async_header.pa_base.u.a64.address +
2891 (p->defpdu_hdr_sz * index);
2892
2893 list_add_tail(&pasync_header_h->link,
2894 &pasync_ctx->async_header.free_list);
2895 pasync_header_h++;
2896 pasync_ctx->async_header.free_entries++;
2897 pasync_ctx->async_header.writables++;
2898
2899 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2900 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2901 header_busy_list);
2902 pasync_data_h->cri = -1;
2903 pasync_data_h->index = (char)index;
2904 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002905
2906 if (!num_async_data) {
2907 num_per_mem = 0;
2908 idx++;
2909 pasync_ctx->async_data.va_base =
2910 mem_descr->mem_array[idx].virtual_address;
2911 pasync_ctx->async_data.pa_base.u.a64.address =
2912 mem_descr->mem_array[idx].
2913 bus_address.u.a64.address;
2914
2915 num_async_data = ((mem_descr->mem_array[idx].size) /
2916 phba->params.defpdu_data_sz);
2917 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302918 pasync_data_h->pbuffer =
2919 (void *)((unsigned long)
2920 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002921 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302922
2923 pasync_data_h->pa.u.a64.address =
2924 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002925 (p->defpdu_data_sz * num_per_mem);
2926 num_per_mem++;
2927 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302928
2929 list_add_tail(&pasync_data_h->link,
2930 &pasync_ctx->async_data.free_list);
2931 pasync_data_h++;
2932 pasync_ctx->async_data.free_entries++;
2933 pasync_ctx->async_data.writables++;
2934
2935 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2936 }
2937
2938 pasync_ctx->async_header.host_write_ptr = 0;
2939 pasync_ctx->async_header.ep_read_ptr = -1;
2940 pasync_ctx->async_data.host_write_ptr = 0;
2941 pasync_ctx->async_data.ep_read_ptr = -1;
2942}
2943
2944static int
2945be_sgl_create_contiguous(void *virtual_address,
2946 u64 physical_address, u32 length,
2947 struct be_dma_mem *sgl)
2948{
2949 WARN_ON(!virtual_address);
2950 WARN_ON(!physical_address);
2951 WARN_ON(!length > 0);
2952 WARN_ON(!sgl);
2953
2954 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302955 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302956 sgl->size = length;
2957
2958 return 0;
2959}
2960
2961static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2962{
2963 memset(sgl, 0, sizeof(*sgl));
2964}
2965
2966static void
2967hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2968 struct mem_array *pmem, struct be_dma_mem *sgl)
2969{
2970 if (sgl->va)
2971 be_sgl_destroy_contiguous(sgl);
2972
2973 be_sgl_create_contiguous(pmem->virtual_address,
2974 pmem->bus_address.u.a64.address,
2975 pmem->size, sgl);
2976}
2977
2978static void
2979hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2980 struct mem_array *pmem, struct be_dma_mem *sgl)
2981{
2982 if (sgl->va)
2983 be_sgl_destroy_contiguous(sgl);
2984
2985 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2986 pmem->bus_address.u.a64.address,
2987 pmem->size, sgl);
2988}
2989
2990static int be_fill_queue(struct be_queue_info *q,
2991 u16 len, u16 entry_size, void *vaddress)
2992{
2993 struct be_dma_mem *mem = &q->dma_mem;
2994
2995 memset(q, 0, sizeof(*q));
2996 q->len = len;
2997 q->entry_size = entry_size;
2998 mem->size = len * entry_size;
2999 mem->va = vaddress;
3000 if (!mem->va)
3001 return -ENOMEM;
3002 memset(mem->va, 0, mem->size);
3003 return 0;
3004}
3005
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303006static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303007 struct hwi_context_memory *phwi_context)
3008{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303009 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303010 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303011 struct be_queue_info *eq;
3012 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303013 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303014 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303015
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303016 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3017 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303018
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303019 if (phba->msix_enabled)
3020 eq_for_mcc = 1;
3021 else
3022 eq_for_mcc = 0;
3023 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3024 eq = &phwi_context->be_eq[i].q;
3025 mem = &eq->dma_mem;
3026 phwi_context->be_eq[i].phba = phba;
3027 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3028 num_eq_pages * PAGE_SIZE,
3029 &paddr);
3030 if (!eq_vaddress)
3031 goto create_eq_error;
3032
3033 mem->va = eq_vaddress;
3034 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3035 sizeof(struct be_eq_entry), eq_vaddress);
3036 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303037 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3038 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303039 goto create_eq_error;
3040 }
3041
3042 mem->dma = paddr;
3043 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3044 phwi_context->cur_eqd);
3045 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303046 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3047 "BM_%d : beiscsi_cmd_eq_create"
3048 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303049 goto create_eq_error;
3050 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303051
3052 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3053 "BM_%d : eqid = %d\n",
3054 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303055 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303056 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303057create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303058 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303059 eq = &phwi_context->be_eq[i].q;
3060 mem = &eq->dma_mem;
3061 if (mem->va)
3062 pci_free_consistent(phba->pcidev, num_eq_pages
3063 * PAGE_SIZE,
3064 mem->va, mem->dma);
3065 }
3066 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303067}
3068
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303069static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303070 struct hwi_context_memory *phwi_context)
3071{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303072 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303073 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303074 struct be_queue_info *cq, *eq;
3075 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303076 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303078 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303079
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303080 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3081 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303082
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303083 for (i = 0; i < phba->num_cpus; i++) {
3084 cq = &phwi_context->be_cq[i];
3085 eq = &phwi_context->be_eq[i].q;
3086 pbe_eq = &phwi_context->be_eq[i];
3087 pbe_eq->cq = cq;
3088 pbe_eq->phba = phba;
3089 mem = &cq->dma_mem;
3090 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3091 num_cq_pages * PAGE_SIZE,
3092 &paddr);
3093 if (!cq_vaddress)
3094 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303095 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303096 sizeof(struct sol_cqe), cq_vaddress);
3097 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303098 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3099 "BM_%d : be_fill_queue Failed "
3100 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303101 goto create_cq_error;
3102 }
3103
3104 mem->dma = paddr;
3105 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3106 false, 0);
3107 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303108 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3109 "BM_%d : beiscsi_cmd_eq_create"
3110 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303111 goto create_cq_error;
3112 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303113 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3114 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3115 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303116 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303117 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303118
3119create_cq_error:
3120 for (i = 0; i < phba->num_cpus; i++) {
3121 cq = &phwi_context->be_cq[i];
3122 mem = &cq->dma_mem;
3123 if (mem->va)
3124 pci_free_consistent(phba->pcidev, num_cq_pages
3125 * PAGE_SIZE,
3126 mem->va, mem->dma);
3127 }
3128 return ret;
3129
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303130}
3131
3132static int
3133beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3134 struct hwi_context_memory *phwi_context,
3135 struct hwi_controller *phwi_ctrlr,
3136 unsigned int def_pdu_ring_sz)
3137{
3138 unsigned int idx;
3139 int ret;
3140 struct be_queue_info *dq, *cq;
3141 struct be_dma_mem *mem;
3142 struct be_mem_descriptor *mem_descr;
3143 void *dq_vaddress;
3144
3145 idx = 0;
3146 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303147 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303148 mem = &dq->dma_mem;
3149 mem_descr = phba->init_mem;
3150 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
3151 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3152 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3153 sizeof(struct phys_addr),
3154 sizeof(struct phys_addr), dq_vaddress);
3155 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303156 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3157 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303158 return ret;
3159 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303160 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3161 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303162 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3163 def_pdu_ring_sz,
3164 phba->params.defpdu_hdr_sz);
3165 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303166 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3167 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303168 return ret;
3169 }
3170 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303171 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3172 "BM_%d : iscsi def pdu id is %d\n",
3173 phwi_context->be_def_hdrq.id);
3174
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303175 hwi_post_async_buffers(phba, 1);
3176 return 0;
3177}
3178
3179static int
3180beiscsi_create_def_data(struct beiscsi_hba *phba,
3181 struct hwi_context_memory *phwi_context,
3182 struct hwi_controller *phwi_ctrlr,
3183 unsigned int def_pdu_ring_sz)
3184{
3185 unsigned int idx;
3186 int ret;
3187 struct be_queue_info *dataq, *cq;
3188 struct be_dma_mem *mem;
3189 struct be_mem_descriptor *mem_descr;
3190 void *dq_vaddress;
3191
3192 idx = 0;
3193 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303194 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303195 mem = &dataq->dma_mem;
3196 mem_descr = phba->init_mem;
3197 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3198 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3199 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3200 sizeof(struct phys_addr),
3201 sizeof(struct phys_addr), dq_vaddress);
3202 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303203 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3204 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303205 return ret;
3206 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303207 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3208 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303209 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3210 def_pdu_ring_sz,
3211 phba->params.defpdu_data_sz);
3212 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303213 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3214 "BM_%d be_cmd_create_default_pdu_queue"
3215 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303216 return ret;
3217 }
3218 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303219 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3220 "BM_%d : iscsi def data id is %d\n",
3221 phwi_context->be_def_dataq.id);
3222
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303223 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303224 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3225 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3226
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303227 return 0;
3228}
3229
3230static int
3231beiscsi_post_pages(struct beiscsi_hba *phba)
3232{
3233 struct be_mem_descriptor *mem_descr;
3234 struct mem_array *pm_arr;
3235 unsigned int page_offset, i;
3236 struct be_dma_mem sgl;
3237 int status;
3238
3239 mem_descr = phba->init_mem;
3240 mem_descr += HWI_MEM_SGE;
3241 pm_arr = mem_descr->mem_array;
3242
3243 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3244 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3245 for (i = 0; i < mem_descr->num_elements; i++) {
3246 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3247 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3248 page_offset,
3249 (pm_arr->size / PAGE_SIZE));
3250 page_offset += pm_arr->size / PAGE_SIZE;
3251 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303252 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3253 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303254 return status;
3255 }
3256 pm_arr++;
3257 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303258 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3259 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303260 return 0;
3261}
3262
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303263static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3264{
3265 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003266 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303267 pci_free_consistent(phba->pcidev, mem->size,
3268 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003269 mem->va = NULL;
3270 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303271}
3272
3273static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3274 u16 len, u16 entry_size)
3275{
3276 struct be_dma_mem *mem = &q->dma_mem;
3277
3278 memset(q, 0, sizeof(*q));
3279 q->len = len;
3280 q->entry_size = entry_size;
3281 mem->size = len * entry_size;
3282 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3283 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303284 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303285 memset(mem->va, 0, mem->size);
3286 return 0;
3287}
3288
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303289static int
3290beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3291 struct hwi_context_memory *phwi_context,
3292 struct hwi_controller *phwi_ctrlr)
3293{
3294 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3295 u64 pa_addr_lo;
3296 unsigned int idx, num, i;
3297 struct mem_array *pwrb_arr;
3298 void *wrb_vaddr;
3299 struct be_dma_mem sgl;
3300 struct be_mem_descriptor *mem_descr;
3301 int status;
3302
3303 idx = 0;
3304 mem_descr = phba->init_mem;
3305 mem_descr += HWI_MEM_WRB;
3306 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3307 GFP_KERNEL);
3308 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303309 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3310 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303311 return -ENOMEM;
3312 }
3313 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3314 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3315 num_wrb_rings = mem_descr->mem_array[idx].size /
3316 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3317
3318 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3319 if (num_wrb_rings) {
3320 pwrb_arr[num].virtual_address = wrb_vaddr;
3321 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3322 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3323 sizeof(struct iscsi_wrb);
3324 wrb_vaddr += pwrb_arr[num].size;
3325 pa_addr_lo += pwrb_arr[num].size;
3326 num_wrb_rings--;
3327 } else {
3328 idx++;
3329 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3330 pa_addr_lo = mem_descr->mem_array[idx].\
3331 bus_address.u.a64.address;
3332 num_wrb_rings = mem_descr->mem_array[idx].size /
3333 (phba->params.wrbs_per_cxn *
3334 sizeof(struct iscsi_wrb));
3335 pwrb_arr[num].virtual_address = wrb_vaddr;
3336 pwrb_arr[num].bus_address.u.a64.address\
3337 = pa_addr_lo;
3338 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3339 sizeof(struct iscsi_wrb);
3340 wrb_vaddr += pwrb_arr[num].size;
3341 pa_addr_lo += pwrb_arr[num].size;
3342 num_wrb_rings--;
3343 }
3344 }
3345 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3346 wrb_mem_index = 0;
3347 offset = 0;
3348 size = 0;
3349
3350 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3351 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3352 &phwi_context->be_wrbq[i]);
3353 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303354 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3355 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003356 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303357 return status;
3358 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303359 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3360 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303361 }
3362 kfree(pwrb_arr);
3363 return 0;
3364}
3365
3366static void free_wrb_handles(struct beiscsi_hba *phba)
3367{
3368 unsigned int index;
3369 struct hwi_controller *phwi_ctrlr;
3370 struct hwi_wrb_context *pwrb_context;
3371
3372 phwi_ctrlr = phba->phwi_ctrlr;
3373 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3374 pwrb_context = &phwi_ctrlr->wrb_context[index];
3375 kfree(pwrb_context->pwrb_handle_base);
3376 kfree(pwrb_context->pwrb_handle_basestd);
3377 }
3378}
3379
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303380static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3381{
3382 struct be_queue_info *q;
3383 struct be_ctrl_info *ctrl = &phba->ctrl;
3384
3385 q = &phba->ctrl.mcc_obj.q;
3386 if (q->created)
3387 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3388 be_queue_free(phba, q);
3389
3390 q = &phba->ctrl.mcc_obj.cq;
3391 if (q->created)
3392 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3393 be_queue_free(phba, q);
3394}
3395
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303396static void hwi_cleanup(struct beiscsi_hba *phba)
3397{
3398 struct be_queue_info *q;
3399 struct be_ctrl_info *ctrl = &phba->ctrl;
3400 struct hwi_controller *phwi_ctrlr;
3401 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303402 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303403
3404 phwi_ctrlr = phba->phwi_ctrlr;
3405 phwi_context = phwi_ctrlr->phwi_ctxt;
3406 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3407 q = &phwi_context->be_wrbq[i];
3408 if (q->created)
3409 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3410 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303411 free_wrb_handles(phba);
3412
3413 q = &phwi_context->be_def_hdrq;
3414 if (q->created)
3415 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3416
3417 q = &phwi_context->be_def_dataq;
3418 if (q->created)
3419 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3420
3421 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3422
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303423 for (i = 0; i < (phba->num_cpus); i++) {
3424 q = &phwi_context->be_cq[i];
3425 if (q->created)
3426 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3427 }
3428 if (phba->msix_enabled)
3429 eq_num = 1;
3430 else
3431 eq_num = 0;
3432 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3433 q = &phwi_context->be_eq[i].q;
3434 if (q->created)
3435 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3436 }
3437 be_mcc_queues_destroy(phba);
Jayamohan Kallickal0283fbb2013-04-05 20:38:21 -07003438 be_cmd_fw_uninit(ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303439}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303440
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303441static int be_mcc_queues_create(struct beiscsi_hba *phba,
3442 struct hwi_context_memory *phwi_context)
3443{
3444 struct be_queue_info *q, *cq;
3445 struct be_ctrl_info *ctrl = &phba->ctrl;
3446
3447 /* Alloc MCC compl queue */
3448 cq = &phba->ctrl.mcc_obj.cq;
3449 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3450 sizeof(struct be_mcc_compl)))
3451 goto err;
3452 /* Ask BE to create MCC compl queue; */
3453 if (phba->msix_enabled) {
3454 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3455 [phba->num_cpus].q, false, true, 0))
3456 goto mcc_cq_free;
3457 } else {
3458 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3459 false, true, 0))
3460 goto mcc_cq_free;
3461 }
3462
3463 /* Alloc MCC queue */
3464 q = &phba->ctrl.mcc_obj.q;
3465 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3466 goto mcc_cq_destroy;
3467
3468 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303469 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303470 goto mcc_q_free;
3471
3472 return 0;
3473
3474mcc_q_free:
3475 be_queue_free(phba, q);
3476mcc_cq_destroy:
3477 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3478mcc_cq_free:
3479 be_queue_free(phba, cq);
3480err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303481 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303482}
3483
John Soni Jose107dfcb2012-10-20 04:42:13 +05303484/**
3485 * find_num_cpus()- Get the CPU online count
3486 * @phba: ptr to priv structure
3487 *
3488 * CPU count is used for creating EQ.
3489 **/
3490static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303491{
3492 int num_cpus = 0;
3493
3494 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303495
John Soni Jose22abeef2012-10-20 04:43:32 +05303496 switch (phba->generation) {
3497 case BE_GEN2:
3498 case BE_GEN3:
3499 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3500 BEISCSI_MAX_NUM_CPUS : num_cpus;
3501 break;
3502 case BE_GEN4:
3503 phba->num_cpus = (num_cpus > OC_SKH_MAX_NUM_CPUS) ?
3504 OC_SKH_MAX_NUM_CPUS : num_cpus;
3505 break;
3506 default:
3507 phba->num_cpus = 1;
3508 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303509}
3510
3511static int hwi_init_port(struct beiscsi_hba *phba)
3512{
3513 struct hwi_controller *phwi_ctrlr;
3514 struct hwi_context_memory *phwi_context;
3515 unsigned int def_pdu_ring_sz;
3516 struct be_ctrl_info *ctrl = &phba->ctrl;
3517 int status;
3518
3519 def_pdu_ring_sz =
3520 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3521 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303522 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303523 phwi_context->max_eqd = 0;
3524 phwi_context->min_eqd = 0;
3525 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303526 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303527
3528 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303529 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303530 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3531 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303532 goto error;
3533 }
3534
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303535 status = be_mcc_queues_create(phba, phwi_context);
3536 if (status != 0)
3537 goto error;
3538
3539 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303540 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303541 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3542 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303543 goto error;
3544 }
3545
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303546 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303547 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303548 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3549 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303550 goto error;
3551 }
3552
3553 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3554 def_pdu_ring_sz);
3555 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303556 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3557 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303558 goto error;
3559 }
3560
3561 status = beiscsi_create_def_data(phba, phwi_context,
3562 phwi_ctrlr, def_pdu_ring_sz);
3563 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303564 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3565 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303566 goto error;
3567 }
3568
3569 status = beiscsi_post_pages(phba);
3570 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303571 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3572 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303573 goto error;
3574 }
3575
3576 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3577 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303578 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3579 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303580 goto error;
3581 }
3582
John Soni Jose99bc5d52012-08-20 23:00:18 +05303583 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3584 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303585 return 0;
3586
3587error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303588 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3589 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003591 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303592}
3593
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303594static int hwi_init_controller(struct beiscsi_hba *phba)
3595{
3596 struct hwi_controller *phwi_ctrlr;
3597
3598 phwi_ctrlr = phba->phwi_ctrlr;
3599 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3600 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3601 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303602 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3603 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3604 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303605 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303606 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3607 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3608 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303609 return -ENOMEM;
3610 }
3611
3612 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003613 if (beiscsi_init_wrb_handle(phba))
3614 return -ENOMEM;
3615
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303616 hwi_init_async_pdu_ctx(phba);
3617 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303618 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3619 "BM_%d : hwi_init_controller failed\n");
3620
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303621 return -ENOMEM;
3622 }
3623 return 0;
3624}
3625
3626static void beiscsi_free_mem(struct beiscsi_hba *phba)
3627{
3628 struct be_mem_descriptor *mem_descr;
3629 int i, j;
3630
3631 mem_descr = phba->init_mem;
3632 i = 0;
3633 j = 0;
3634 for (i = 0; i < SE_MEM_MAX; i++) {
3635 for (j = mem_descr->num_elements; j > 0; j--) {
3636 pci_free_consistent(phba->pcidev,
3637 mem_descr->mem_array[j - 1].size,
3638 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303639 (unsigned long)mem_descr->mem_array[j - 1].
3640 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303641 }
3642 kfree(mem_descr->mem_array);
3643 mem_descr++;
3644 }
3645 kfree(phba->init_mem);
3646 kfree(phba->phwi_ctrlr);
3647}
3648
3649static int beiscsi_init_controller(struct beiscsi_hba *phba)
3650{
3651 int ret = -ENOMEM;
3652
3653 ret = beiscsi_get_memory(phba);
3654 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303655 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3656 "BM_%d : beiscsi_dev_probe -"
3657 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303658 return ret;
3659 }
3660
3661 ret = hwi_init_controller(phba);
3662 if (ret)
3663 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303664 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3665 "BM_%d : Return success from beiscsi_init_controller");
3666
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303667 return 0;
3668
3669free_init:
3670 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003671 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303672}
3673
3674static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3675{
3676 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3677 struct sgl_handle *psgl_handle;
3678 struct iscsi_sge *pfrag;
3679 unsigned int arr_index, i, idx;
3680
3681 phba->io_sgl_hndl_avbl = 0;
3682 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303683
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303684 mem_descr_sglh = phba->init_mem;
3685 mem_descr_sglh += HWI_MEM_SGLH;
3686 if (1 == mem_descr_sglh->num_elements) {
3687 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3688 phba->params.ios_per_ctrl,
3689 GFP_KERNEL);
3690 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303691 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3692 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303693 return -ENOMEM;
3694 }
3695 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3696 (phba->params.icds_per_ctrl -
3697 phba->params.ios_per_ctrl),
3698 GFP_KERNEL);
3699 if (!phba->eh_sgl_hndl_base) {
3700 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303701 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3702 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303703 return -ENOMEM;
3704 }
3705 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303706 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3707 "BM_%d : HWI_MEM_SGLH is more than one element."
3708 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303709 return -ENOMEM;
3710 }
3711
3712 arr_index = 0;
3713 idx = 0;
3714 while (idx < mem_descr_sglh->num_elements) {
3715 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3716
3717 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3718 sizeof(struct sgl_handle)); i++) {
3719 if (arr_index < phba->params.ios_per_ctrl) {
3720 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3721 phba->io_sgl_hndl_avbl++;
3722 arr_index++;
3723 } else {
3724 phba->eh_sgl_hndl_base[arr_index -
3725 phba->params.ios_per_ctrl] =
3726 psgl_handle;
3727 arr_index++;
3728 phba->eh_sgl_hndl_avbl++;
3729 }
3730 psgl_handle++;
3731 }
3732 idx++;
3733 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303734 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3735 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3736 "phba->eh_sgl_hndl_avbl=%d\n",
3737 phba->io_sgl_hndl_avbl,
3738 phba->eh_sgl_hndl_avbl);
3739
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303740 mem_descr_sg = phba->init_mem;
3741 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303742 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3743 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3744 mem_descr_sg->num_elements);
3745
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303746 arr_index = 0;
3747 idx = 0;
3748 while (idx < mem_descr_sg->num_elements) {
3749 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3750
3751 for (i = 0;
3752 i < (mem_descr_sg->mem_array[idx].size) /
3753 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3754 i++) {
3755 if (arr_index < phba->params.ios_per_ctrl)
3756 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3757 else
3758 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3759 phba->params.ios_per_ctrl];
3760 psgl_handle->pfrag = pfrag;
3761 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3762 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3763 pfrag += phba->params.num_sge_per_io;
3764 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303765 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303766 }
3767 idx++;
3768 }
3769 phba->io_sgl_free_index = 0;
3770 phba->io_sgl_alloc_index = 0;
3771 phba->eh_sgl_free_index = 0;
3772 phba->eh_sgl_alloc_index = 0;
3773 return 0;
3774}
3775
3776static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3777{
3778 int i, new_cid;
3779
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303780 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303781 GFP_KERNEL);
3782 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303783 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3784 "BM_%d : Failed to allocate memory in "
3785 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303786 return -ENOMEM;
3787 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303788 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303789 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3790 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303791 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3792 "BM_%d : Failed to allocate memory in "
3793 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303794 kfree(phba->cid_array);
3795 return -ENOMEM;
3796 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303797 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303798 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3799 phba->cid_array[i] = new_cid;
3800 new_cid += 2;
3801 }
3802 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3803 return 0;
3804}
3805
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303806static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303807{
3808 struct be_ctrl_info *ctrl = &phba->ctrl;
3809 struct hwi_controller *phwi_ctrlr;
3810 struct hwi_context_memory *phwi_context;
3811 struct be_queue_info *eq;
3812 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303813 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303814 u32 enabled;
3815
3816 phwi_ctrlr = phba->phwi_ctrlr;
3817 phwi_context = phwi_ctrlr->phwi_ctxt;
3818
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303819 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3820 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3821 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303822
3823 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3824 if (!enabled) {
3825 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303826 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3827 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303828 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003829 }
3830
3831 if (!phba->msix_enabled) {
3832 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303833 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3834 "BM_%d : eq->id=%d\n", eq->id);
3835
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003836 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3837 } else {
3838 for (i = 0; i <= phba->num_cpus; i++) {
3839 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303840 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3841 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303842 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3843 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303844 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303845}
3846
3847static void hwi_disable_intr(struct beiscsi_hba *phba)
3848{
3849 struct be_ctrl_info *ctrl = &phba->ctrl;
3850
3851 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3852 u32 reg = ioread32(addr);
3853
3854 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3855 if (enabled) {
3856 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3857 iowrite32(reg, addr);
3858 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303859 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3860 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303861}
3862
John Soni Jose9aef4202012-08-20 23:00:08 +05303863/**
3864 * beiscsi_get_boot_info()- Get the boot session info
3865 * @phba: The device priv structure instance
3866 *
3867 * Get the boot target info and store in driver priv structure
3868 *
3869 * return values
3870 * Success: 0
3871 * Failure: Non-Zero Value
3872 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303873static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3874{
Mike Christie0e438952012-04-03 23:41:51 -05003875 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303876 struct be_dma_mem nonemb_cmd;
John Soni Josee175def2012-10-20 04:45:40 +05303877 unsigned int tag;
John Soni Jose9aef4202012-08-20 23:00:08 +05303878 unsigned int s_handle;
Mike Christief457a462011-06-24 15:11:53 -05003879 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303880
John Soni Jose9aef4202012-08-20 23:00:08 +05303881 /* Get the session handle of the boot target */
3882 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3883 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303884 beiscsi_log(phba, KERN_ERR,
3885 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3886 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303887 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303888 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303889 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3890 sizeof(*session_resp),
3891 &nonemb_cmd.dma);
3892 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303893 beiscsi_log(phba, KERN_ERR,
3894 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3895 "BM_%d : Failed to allocate memory for"
3896 "beiscsi_get_session_info\n");
3897
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303898 return -ENOMEM;
3899 }
3900
3901 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303902 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003903 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303904 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303905 beiscsi_log(phba, KERN_ERR,
3906 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3907 "BM_%d : beiscsi_get_session_info"
3908 " Failed\n");
3909
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303910 goto boot_freemem;
John Soni Josee175def2012-10-20 04:45:40 +05303911 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303912
John Soni Josee175def2012-10-20 04:45:40 +05303913 ret = beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
3914 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303915 beiscsi_log(phba, KERN_ERR,
3916 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
John Soni Josee175def2012-10-20 04:45:40 +05303917 "BM_%d : beiscsi_get_session_info Failed");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303918 goto boot_freemem;
3919 }
John Soni Josee175def2012-10-20 04:45:40 +05303920
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303921 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003922
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303923 memcpy(&phba->boot_sess, &session_resp->session_info,
3924 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003925 ret = 0;
3926
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303927boot_freemem:
3928 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3929 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003930 return ret;
3931}
3932
3933static void beiscsi_boot_release(void *data)
3934{
3935 struct beiscsi_hba *phba = data;
3936
3937 scsi_host_put(phba->shost);
3938}
3939
3940static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3941{
3942 struct iscsi_boot_kobj *boot_kobj;
3943
3944 /* get boot info using mgmt cmd */
3945 if (beiscsi_get_boot_info(phba))
3946 /* Try to see if we can carry on without this */
3947 return 0;
3948
3949 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3950 if (!phba->boot_kset)
3951 return -ENOMEM;
3952
3953 /* get a ref because the show function will ref the phba */
3954 if (!scsi_host_get(phba->shost))
3955 goto free_kset;
3956 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3957 beiscsi_show_boot_tgt_info,
3958 beiscsi_tgt_get_attr_visibility,
3959 beiscsi_boot_release);
3960 if (!boot_kobj)
3961 goto put_shost;
3962
3963 if (!scsi_host_get(phba->shost))
3964 goto free_kset;
3965 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3966 beiscsi_show_boot_ini_info,
3967 beiscsi_ini_get_attr_visibility,
3968 beiscsi_boot_release);
3969 if (!boot_kobj)
3970 goto put_shost;
3971
3972 if (!scsi_host_get(phba->shost))
3973 goto free_kset;
3974 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3975 beiscsi_show_boot_eth_info,
3976 beiscsi_eth_get_attr_visibility,
3977 beiscsi_boot_release);
3978 if (!boot_kobj)
3979 goto put_shost;
3980 return 0;
3981
3982put_shost:
3983 scsi_host_put(phba->shost);
3984free_kset:
3985 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303986 return -ENOMEM;
3987}
3988
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303989static int beiscsi_init_port(struct beiscsi_hba *phba)
3990{
3991 int ret;
3992
3993 ret = beiscsi_init_controller(phba);
3994 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303995 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3996 "BM_%d : beiscsi_dev_probe - Failed in"
3997 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303998 return ret;
3999 }
4000 ret = beiscsi_init_sgl_handle(phba);
4001 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304002 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4003 "BM_%d : beiscsi_dev_probe - Failed in"
4004 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304005 goto do_cleanup_ctrlr;
4006 }
4007
4008 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304009 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4010 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304011 kfree(phba->io_sgl_hndl_base);
4012 kfree(phba->eh_sgl_hndl_base);
4013 goto do_cleanup_ctrlr;
4014 }
4015
4016 return ret;
4017
4018do_cleanup_ctrlr:
4019 hwi_cleanup(phba);
4020 return ret;
4021}
4022
4023static void hwi_purge_eq(struct beiscsi_hba *phba)
4024{
4025 struct hwi_controller *phwi_ctrlr;
4026 struct hwi_context_memory *phwi_context;
4027 struct be_queue_info *eq;
4028 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304029 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304030 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304031
4032 phwi_ctrlr = phba->phwi_ctrlr;
4033 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304034 if (phba->msix_enabled)
4035 eq_msix = 1;
4036 else
4037 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304038
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304039 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4040 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304041 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304042 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304043 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4044 & EQE_VALID_MASK) {
4045 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4046 queue_tail_inc(eq);
4047 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304048 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304049 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304050
4051 if (num_processed)
4052 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304053 }
4054}
4055
4056static void beiscsi_clean_port(struct beiscsi_hba *phba)
4057{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05304058 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304059
4060 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
4061 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304062 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4063 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304064
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304065 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304066 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304067 kfree(phba->io_sgl_hndl_base);
4068 kfree(phba->eh_sgl_hndl_base);
4069 kfree(phba->cid_array);
4070 kfree(phba->ep_array);
4071}
4072
John Soni Josed629c472012-10-20 04:42:00 +05304073/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004074 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4075 * @beiscsi_conn: ptr to the conn to be cleaned up
4076 *
4077 * Free driver mgmt resources binded to CXN.
4078 **/
4079void
4080beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn)
4081{
4082 struct beiscsi_io_task *io_task;
4083 struct beiscsi_hba *phba = beiscsi_conn->phba;
4084 struct hwi_wrb_context *pwrb_context;
4085 struct hwi_controller *phwi_ctrlr;
4086
4087 phwi_ctrlr = phba->phwi_ctrlr;
4088 pwrb_context = &phwi_ctrlr->wrb_context
4089 [beiscsi_conn->beiscsi_conn_cid
4090 - phba->fw_config.iscsi_cid_start];
4091 io_task = beiscsi_conn->task->dd_data;
4092
4093 if (io_task->pwrb_handle) {
4094 memset(io_task->pwrb_handle->pwrb, 0,
4095 sizeof(struct iscsi_wrb));
4096 free_wrb_handle(phba, pwrb_context,
4097 io_task->pwrb_handle);
4098 io_task->pwrb_handle = NULL;
4099 }
4100
4101 if (io_task->psgl_handle) {
4102 spin_lock_bh(&phba->mgmt_sgl_lock);
4103 free_mgmt_sgl_handle(phba,
4104 io_task->psgl_handle);
4105 spin_unlock_bh(&phba->mgmt_sgl_lock);
4106 io_task->psgl_handle = NULL;
4107 }
4108
4109 if (io_task->mtask_addr)
4110 pci_unmap_single(phba->pcidev,
4111 io_task->mtask_addr,
4112 io_task->mtask_data_count,
4113 PCI_DMA_TODEVICE);
4114}
4115
4116/**
John Soni Josed629c472012-10-20 04:42:00 +05304117 * beiscsi_cleanup_task()- Free driver resources of the task
4118 * @task: ptr to the iscsi task
4119 *
4120 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004121static void beiscsi_cleanup_task(struct iscsi_task *task)
4122{
4123 struct beiscsi_io_task *io_task = task->dd_data;
4124 struct iscsi_conn *conn = task->conn;
4125 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4126 struct beiscsi_hba *phba = beiscsi_conn->phba;
4127 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4128 struct hwi_wrb_context *pwrb_context;
4129 struct hwi_controller *phwi_ctrlr;
4130
4131 phwi_ctrlr = phba->phwi_ctrlr;
4132 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
4133 - phba->fw_config.iscsi_cid_start];
4134
4135 if (io_task->cmd_bhs) {
4136 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4137 io_task->bhs_pa.u.a64.address);
4138 io_task->cmd_bhs = NULL;
4139 }
4140
4141 if (task->sc) {
4142 if (io_task->pwrb_handle) {
4143 free_wrb_handle(phba, pwrb_context,
4144 io_task->pwrb_handle);
4145 io_task->pwrb_handle = NULL;
4146 }
4147
4148 if (io_task->psgl_handle) {
4149 spin_lock(&phba->io_sgl_lock);
4150 free_io_sgl_handle(phba, io_task->psgl_handle);
4151 spin_unlock(&phba->io_sgl_lock);
4152 io_task->psgl_handle = NULL;
4153 }
4154 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004155 if (!beiscsi_conn->login_in_progress)
4156 beiscsi_free_mgmt_task_handles(beiscsi_conn);
Mike Christie1282ab72012-04-18 03:06:00 -05004157 }
4158}
4159
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304160void
4161beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4162 struct beiscsi_offload_params *params)
4163{
4164 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304165 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004166 struct iscsi_task *task = beiscsi_conn->task;
4167 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304168 u32 doorbell = 0;
4169
4170 /*
4171 * We can always use 0 here because it is reserved by libiscsi for
4172 * login/startup related tasks.
4173 */
Mike Christie1282ab72012-04-18 03:06:00 -05004174 beiscsi_conn->login_in_progress = 0;
4175 spin_lock_bh(&session->lock);
4176 beiscsi_cleanup_task(task);
4177 spin_unlock_bh(&session->lock);
4178
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304179 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05304180 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304181
John Soni Joseacb96932012-10-20 04:44:35 +05304182 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004183 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304184 beiscsi_offload_cxn_v0(params, pwrb_handle,
4185 phba->init_mem);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004186 else
4187 beiscsi_offload_cxn_v2(params, pwrb_handle);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304188
John Soni Joseacb96932012-10-20 04:44:35 +05304189 be_dws_le_to_cpu(pwrb_handle->pwrb,
4190 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304191
4192 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304193 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304194 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304195 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4196
4197 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4198}
4199
4200static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4201 int *index, int *age)
4202{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304203 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304204 if (age)
4205 *age = conn->session->age;
4206}
4207
4208/**
4209 * beiscsi_alloc_pdu - allocates pdu and related resources
4210 * @task: libiscsi task
4211 * @opcode: opcode of pdu for task
4212 *
4213 * This is called with the session lock held. It will allocate
4214 * the wrb and sgl if needed for the command. And it will prep
4215 * the pdu's itt. beiscsi_parse_pdu will later translate
4216 * the pdu itt to the libiscsi task itt.
4217 */
4218static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4219{
4220 struct beiscsi_io_task *io_task = task->dd_data;
4221 struct iscsi_conn *conn = task->conn;
4222 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4223 struct beiscsi_hba *phba = beiscsi_conn->phba;
4224 struct hwi_wrb_context *pwrb_context;
4225 struct hwi_controller *phwi_ctrlr;
4226 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304227 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4228 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304229
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304230 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004231 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304232 if (!io_task->cmd_bhs)
4233 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304234 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304235 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304236 io_task->conn = beiscsi_conn;
4237
4238 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4239 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304240 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004241 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304242
4243 if (task->sc) {
4244 spin_lock(&phba->io_sgl_lock);
4245 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4246 spin_unlock(&phba->io_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304247 if (!io_task->psgl_handle) {
4248 beiscsi_log(phba, KERN_ERR,
4249 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4250 "BM_%d : Alloc of IO_SGL_ICD Failed"
4251 "for the CID : %d\n",
4252 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304253 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304254 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304255 io_task->pwrb_handle = alloc_wrb_handle(phba,
4256 beiscsi_conn->beiscsi_conn_cid -
4257 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304258 if (!io_task->pwrb_handle) {
4259 beiscsi_log(phba, KERN_ERR,
4260 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4261 "BM_%d : Alloc of WRB_HANDLE Failed"
4262 "for the CID : %d\n",
4263 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304264 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304265 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304266 } else {
4267 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304268 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004269 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304270 if (!beiscsi_conn->login_in_progress) {
4271 spin_lock(&phba->mgmt_sgl_lock);
4272 io_task->psgl_handle = (struct sgl_handle *)
4273 alloc_mgmt_sgl_handle(phba);
4274 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304275 if (!io_task->psgl_handle) {
4276 beiscsi_log(phba, KERN_ERR,
4277 BEISCSI_LOG_IO |
4278 BEISCSI_LOG_CONFIG,
4279 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4280 "for the CID : %d\n",
4281 beiscsi_conn->
4282 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304283 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304284 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304285
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304286 beiscsi_conn->login_in_progress = 1;
4287 beiscsi_conn->plogin_sgl_handle =
4288 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304289 io_task->pwrb_handle =
4290 alloc_wrb_handle(phba,
4291 beiscsi_conn->beiscsi_conn_cid -
4292 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304293 if (!io_task->pwrb_handle) {
4294 beiscsi_log(phba, KERN_ERR,
4295 BEISCSI_LOG_IO |
4296 BEISCSI_LOG_CONFIG,
4297 "BM_%d : Alloc of WRB_HANDLE Failed"
4298 "for the CID : %d\n",
4299 beiscsi_conn->
4300 beiscsi_conn_cid);
4301 goto free_mgmt_hndls;
4302 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304303 beiscsi_conn->plogin_wrb_handle =
4304 io_task->pwrb_handle;
4305
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304306 } else {
4307 io_task->psgl_handle =
4308 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304309 io_task->pwrb_handle =
4310 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304311 }
4312 } else {
4313 spin_lock(&phba->mgmt_sgl_lock);
4314 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4315 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304316 if (!io_task->psgl_handle) {
4317 beiscsi_log(phba, KERN_ERR,
4318 BEISCSI_LOG_IO |
4319 BEISCSI_LOG_CONFIG,
4320 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4321 "for the CID : %d\n",
4322 beiscsi_conn->
4323 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304324 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304325 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304326 io_task->pwrb_handle =
4327 alloc_wrb_handle(phba,
4328 beiscsi_conn->beiscsi_conn_cid -
4329 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304330 if (!io_task->pwrb_handle) {
4331 beiscsi_log(phba, KERN_ERR,
4332 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4333 "BM_%d : Alloc of WRB_HANDLE Failed"
4334 "for the CID : %d\n",
4335 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304336 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304337 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304338
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304339 }
4340 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304341 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4342 wrb_index << 16) | (unsigned int)
4343 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304344 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304345
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304346 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4347 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304348
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304349free_io_hndls:
4350 spin_lock(&phba->io_sgl_lock);
4351 free_io_sgl_handle(phba, io_task->psgl_handle);
4352 spin_unlock(&phba->io_sgl_lock);
4353 goto free_hndls;
4354free_mgmt_hndls:
4355 spin_lock(&phba->mgmt_sgl_lock);
4356 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4357 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304358free_hndls:
4359 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304360 pwrb_context = &phwi_ctrlr->wrb_context[
4361 beiscsi_conn->beiscsi_conn_cid -
4362 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304363 if (io_task->pwrb_handle)
4364 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304365 io_task->pwrb_handle = NULL;
4366 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4367 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004368 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304369 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304370}
John Soni Jose09a10932012-10-20 04:44:23 +05304371int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4372 unsigned int num_sg, unsigned int xferlen,
4373 unsigned int writedir)
4374{
4375
4376 struct beiscsi_io_task *io_task = task->dd_data;
4377 struct iscsi_conn *conn = task->conn;
4378 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4379 struct beiscsi_hba *phba = beiscsi_conn->phba;
4380 struct iscsi_wrb *pwrb = NULL;
4381 unsigned int doorbell = 0;
4382
4383 pwrb = io_task->pwrb_handle->pwrb;
4384 memset(pwrb, 0, sizeof(*pwrb));
4385
4386 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4387 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4388
4389 if (writedir) {
4390 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4391 INI_WR_CMD);
4392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4393 } else {
4394 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4395 INI_RD_CMD);
4396 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4397 }
4398
4399 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4400 type, pwrb);
4401
4402 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4403 cpu_to_be16(*(unsigned short *)
4404 &io_task->cmd_bhs->iscsi_hdr.lun));
4405 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4406 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4407 io_task->pwrb_handle->wrb_index);
4408 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4409 be32_to_cpu(task->cmdsn));
4410 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4411 io_task->psgl_handle->sgl_index);
4412
4413 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4414 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4415 io_task->pwrb_handle->nxt_wrb_index);
4416
4417 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4418
4419 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4420 doorbell |= (io_task->pwrb_handle->wrb_index &
4421 DB_DEF_PDU_WRB_INDEX_MASK) <<
4422 DB_DEF_PDU_WRB_INDEX_SHIFT;
4423 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4424 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4425 return 0;
4426}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304427
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304428static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4429 unsigned int num_sg, unsigned int xferlen,
4430 unsigned int writedir)
4431{
4432
4433 struct beiscsi_io_task *io_task = task->dd_data;
4434 struct iscsi_conn *conn = task->conn;
4435 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4436 struct beiscsi_hba *phba = beiscsi_conn->phba;
4437 struct iscsi_wrb *pwrb = NULL;
4438 unsigned int doorbell = 0;
4439
4440 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304441 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4442 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4443
4444 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304445 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4446 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304447 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304448 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304449 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4450 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304451 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4452 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304453
John Soni Jose09a10932012-10-20 04:44:23 +05304454 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4455 type, pwrb);
4456
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304457 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004458 cpu_to_be16(*(unsigned short *)
4459 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304460 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4461 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4462 io_task->pwrb_handle->wrb_index);
4463 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4464 be32_to_cpu(task->cmdsn));
4465 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4466 io_task->psgl_handle->sgl_index);
4467
4468 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4469
4470 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4471 io_task->pwrb_handle->nxt_wrb_index);
4472 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4473
4474 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304475 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304476 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4477 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4478
4479 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4480 return 0;
4481}
4482
4483static int beiscsi_mtask(struct iscsi_task *task)
4484{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304485 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304486 struct iscsi_conn *conn = task->conn;
4487 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4488 struct beiscsi_hba *phba = beiscsi_conn->phba;
4489 struct iscsi_wrb *pwrb = NULL;
4490 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304491 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304492 unsigned int pwrb_typeoffset = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304493
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304494 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304495 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304496 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304497
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004498 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304499 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4500 be32_to_cpu(task->cmdsn));
4501 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4502 io_task->pwrb_handle->wrb_index);
4503 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4504 io_task->psgl_handle->sgl_index);
4505 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4506 task->data_count);
4507 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4508 io_task->pwrb_handle->nxt_wrb_index);
4509 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004510 } else {
4511 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4512 be32_to_cpu(task->cmdsn));
4513 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4514 io_task->pwrb_handle->wrb_index);
4515 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4516 io_task->psgl_handle->sgl_index);
4517 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4518 task->data_count);
4519 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4520 io_task->pwrb_handle->nxt_wrb_index);
4521 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304522 }
4523
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304524
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304525 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4526 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304527 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304528 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304529 hwi_write_buffer(pwrb, task);
4530 break;
4531 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004532 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304533 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004534 if (is_chip_be2_be3r(phba))
4535 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304536 dmsg, pwrb, 1);
4537 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004538 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304539 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004540 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304541 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004542 if (is_chip_be2_be3r(phba))
4543 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304544 dmsg, pwrb, 0);
4545 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004546 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304547 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004548 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304549 hwi_write_buffer(pwrb, task);
4550 break;
4551 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304552 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304553 hwi_write_buffer(pwrb, task);
4554 break;
4555 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304556 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304557 hwi_write_buffer(pwrb, task);
4558 break;
4559 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304560 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304561 hwi_write_buffer(pwrb, task);
4562 break;
4563
4564 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304565 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4566 "BM_%d : opcode =%d Not supported\n",
4567 task->hdr->opcode & ISCSI_OPCODE_MASK);
4568
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304569 return -EINVAL;
4570 }
4571
John Soni Jose09a10932012-10-20 04:44:23 +05304572 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004573 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4574 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4575 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304576
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304577 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304578 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304579 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4580 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4581 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4582 return 0;
4583}
4584
4585static int beiscsi_task_xmit(struct iscsi_task *task)
4586{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304587 struct beiscsi_io_task *io_task = task->dd_data;
4588 struct scsi_cmnd *sc = task->sc;
John Soni Jose09a10932012-10-20 04:44:23 +05304589 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304590 struct scatterlist *sg;
4591 int num_sg;
4592 unsigned int writedir = 0, xferlen = 0;
4593
John Soni Jose09a10932012-10-20 04:44:23 +05304594 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
4595
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304596 if (!sc)
4597 return beiscsi_mtask(task);
4598
4599 io_task->scsi_cmnd = sc;
4600 num_sg = scsi_dma_map(sc);
4601 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304602 struct iscsi_conn *conn = task->conn;
4603 struct beiscsi_hba *phba = NULL;
4604
4605 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4606 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4607 "BM_%d : scsi_dma_map Failed\n");
4608
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304609 return num_sg;
4610 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304611 xferlen = scsi_bufflen(sc);
4612 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304613 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304614 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304615 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304616 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304617
John Soni Jose09a10932012-10-20 04:44:23 +05304618 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304619}
4620
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004621/**
4622 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4623 * @job: job to handle
4624 */
4625static int beiscsi_bsg_request(struct bsg_job *job)
4626{
4627 struct Scsi_Host *shost;
4628 struct beiscsi_hba *phba;
4629 struct iscsi_bsg_request *bsg_req = job->request;
4630 int rc = -EINVAL;
4631 unsigned int tag;
4632 struct be_dma_mem nonemb_cmd;
4633 struct be_cmd_resp_hdr *resp;
4634 struct iscsi_bsg_reply *bsg_reply = job->reply;
4635 unsigned short status, extd_status;
4636
4637 shost = iscsi_job_to_shost(job);
4638 phba = iscsi_host_priv(shost);
4639
4640 switch (bsg_req->msgcode) {
4641 case ISCSI_BSG_HST_VENDOR:
4642 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4643 job->request_payload.payload_len,
4644 &nonemb_cmd.dma);
4645 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304646 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4647 "BM_%d : Failed to allocate memory for "
4648 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304649 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004650 }
4651 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4652 &nonemb_cmd);
4653 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304654 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304655 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304656
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004657 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4658 nonemb_cmd.va, nonemb_cmd.dma);
4659 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304660 }
4661
4662 rc = wait_event_interruptible_timeout(
4663 phba->ctrl.mcc_wait[tag],
4664 phba->ctrl.mcc_numtag[tag],
4665 msecs_to_jiffies(
4666 BEISCSI_HOST_MBX_TIMEOUT));
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004667 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4668 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4669 free_mcc_tag(&phba->ctrl, tag);
4670 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4671 sg_copy_from_buffer(job->reply_payload.sg_list,
4672 job->reply_payload.sg_cnt,
4673 nonemb_cmd.va, (resp->response_length
4674 + sizeof(*resp)));
4675 bsg_reply->reply_payload_rcv_len = resp->response_length;
4676 bsg_reply->result = status;
4677 bsg_job_done(job, bsg_reply->result,
4678 bsg_reply->reply_payload_rcv_len);
4679 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4680 nonemb_cmd.va, nonemb_cmd.dma);
4681 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304682 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304683 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304684 " status = %d extd_status = %d\n",
4685 status, extd_status);
4686
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004687 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304688 } else {
4689 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004690 }
4691 break;
4692
4693 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304694 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4695 "BM_%d : Unsupported bsg command: 0x%x\n",
4696 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004697 break;
4698 }
4699
4700 return rc;
4701}
4702
John Soni Jose99bc5d52012-08-20 23:00:18 +05304703void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4704{
4705 /* Set the logging parameter */
4706 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4707}
4708
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304709/*
4710 * beiscsi_quiesce()- Cleanup Driver resources
4711 * @phba: Instance Priv structure
4712 *
4713 * Free the OS and HW resources held by the driver
4714 **/
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004715static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304716{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304717 struct hwi_controller *phwi_ctrlr;
4718 struct hwi_context_memory *phwi_context;
4719 struct be_eq_obj *pbe_eq;
4720 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304721
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304722 phwi_ctrlr = phba->phwi_ctrlr;
4723 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304724 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304725 if (phba->msix_enabled) {
4726 for (i = 0; i <= phba->num_cpus; i++) {
4727 msix_vec = phba->msix_entries[i].vector;
4728 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004729 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304730 }
4731 } else
4732 if (phba->pcidev->irq)
4733 free_irq(phba->pcidev->irq, phba);
4734 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304735 destroy_workqueue(phba->wq);
4736 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304737 for (i = 0; i < phba->num_cpus; i++) {
4738 pbe_eq = &phwi_context->be_eq[i];
4739 blk_iopoll_disable(&pbe_eq->iopoll);
4740 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304741
4742 beiscsi_clean_port(phba);
4743 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304744
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304745 beiscsi_unmap_pci_function(phba);
4746 pci_free_consistent(phba->pcidev,
4747 phba->ctrl.mbox_mem_alloced.size,
4748 phba->ctrl.mbox_mem_alloced.va,
4749 phba->ctrl.mbox_mem_alloced.dma);
John Soni Jose7a158002012-10-20 04:45:51 +05304750
4751 cancel_delayed_work_sync(&phba->beiscsi_hw_check_task);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004752}
4753
4754static void beiscsi_remove(struct pci_dev *pcidev)
4755{
4756
4757 struct beiscsi_hba *phba = NULL;
4758
4759 phba = pci_get_drvdata(pcidev);
4760 if (!phba) {
4761 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4762 return;
4763 }
4764
Mike Christie0e438952012-04-03 23:41:51 -05004765 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004766 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004767 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304768 iscsi_host_remove(phba->shost);
4769 pci_dev_put(phba->pcidev);
4770 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004771 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304772}
4773
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004774static void beiscsi_shutdown(struct pci_dev *pcidev)
4775{
4776
4777 struct beiscsi_hba *phba = NULL;
4778
4779 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4780 if (!phba) {
4781 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4782 return;
4783 }
4784
4785 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004786 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004787}
4788
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304789static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4790{
4791 int i, status;
4792
4793 for (i = 0; i <= phba->num_cpus; i++)
4794 phba->msix_entries[i].entry = i;
4795
4796 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4797 (phba->num_cpus + 1));
4798 if (!status)
4799 phba->msix_enabled = true;
4800
4801 return;
4802}
4803
John Soni Jose7a158002012-10-20 04:45:51 +05304804/*
4805 * beiscsi_hw_health_check()- Check adapter health
4806 * @work: work item to check HW health
4807 *
4808 * Check if adapter in an unrecoverable state or not.
4809 **/
4810static void
4811beiscsi_hw_health_check(struct work_struct *work)
4812{
4813 struct beiscsi_hba *phba =
4814 container_of(work, struct beiscsi_hba,
4815 beiscsi_hw_check_task.work);
4816
4817 beiscsi_ue_detect(phba);
4818
4819 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4820 msecs_to_jiffies(1000));
4821}
4822
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004823static int beiscsi_dev_probe(struct pci_dev *pcidev,
4824 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304825{
4826 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304827 struct hwi_controller *phwi_ctrlr;
4828 struct hwi_context_memory *phwi_context;
4829 struct be_eq_obj *pbe_eq;
John Soni Jose107dfcb2012-10-20 04:42:13 +05304830 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304831
4832 ret = beiscsi_enable_pci(pcidev);
4833 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304834 dev_err(&pcidev->dev,
4835 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304836 return ret;
4837 }
4838
4839 phba = beiscsi_hba_alloc(pcidev);
4840 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304841 dev_err(&pcidev->dev,
4842 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304843 goto disable_pci;
4844 }
4845
John Soni Jose99bc5d52012-08-20 23:00:18 +05304846 /* Initialize Driver configuration Paramters */
4847 beiscsi_hba_attrs_init(phba);
4848
John Soni Josee175def2012-10-20 04:45:40 +05304849 phba->fw_timeout = false;
4850
4851
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304852 switch (pcidev->device) {
4853 case BE_DEVICE_ID1:
4854 case OC_DEVICE_ID1:
4855 case OC_DEVICE_ID2:
4856 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05304857 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304858 break;
4859 case BE_DEVICE_ID2:
4860 case OC_DEVICE_ID3:
4861 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05304862 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304863 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05304864 case OC_SKH_ID1:
4865 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05304866 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07004867 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304868 default:
4869 phba->generation = 0;
4870 }
4871
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304872 if (enable_msix)
John Soni Jose107dfcb2012-10-20 04:42:13 +05304873 find_num_cpus(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304874 else
John Soni Jose107dfcb2012-10-20 04:42:13 +05304875 phba->num_cpus = 1;
4876
John Soni Jose99bc5d52012-08-20 23:00:18 +05304877 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4878 "BM_%d : num_cpus = %d\n",
4879 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304880
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004881 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304882 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004883 if (!phba->msix_enabled)
4884 phba->num_cpus = 1;
4885 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304886 ret = be_ctrl_init(phba, pcidev);
4887 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304888 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4889 "BM_%d : beiscsi_dev_probe-"
4890 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304891 goto hba_free;
4892 }
4893
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304894 ret = beiscsi_cmd_reset_function(phba);
4895 if (ret) {
4896 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4897 "BM_%d : Reset Failed. Aborting Crashdump\n");
4898 goto hba_free;
4899 }
4900 ret = be_chk_reset_complete(phba);
4901 if (ret) {
4902 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4903 "BM_%d : Failed to get out of reset."
4904 "Aborting Crashdump\n");
4905 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304906 }
4907
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304908 spin_lock_init(&phba->io_sgl_lock);
4909 spin_lock_init(&phba->mgmt_sgl_lock);
4910 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304911 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4912 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304913 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4914 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304915 goto free_port;
4916 }
4917 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304918 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304919 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304920 ret = beiscsi_init_port(phba);
4921 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304922 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4923 "BM_%d : beiscsi_dev_probe-"
4924 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304925 goto free_port;
4926 }
4927
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304928 for (i = 0; i < MAX_MCC_CMD ; i++) {
4929 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4930 phba->ctrl.mcc_tag[i] = i + 1;
4931 phba->ctrl.mcc_numtag[i + 1] = 0;
4932 phba->ctrl.mcc_tag_available++;
4933 }
4934
4935 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4936
John Soni Jose72fb46a2012-10-20 04:42:49 +05304937 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304938 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004939 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304940 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304941 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4942 "BM_%d : beiscsi_dev_probe-"
4943 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304944 goto free_twq;
4945 }
4946
John Soni Jose7a158002012-10-20 04:45:51 +05304947 INIT_DELAYED_WORK(&phba->beiscsi_hw_check_task,
4948 beiscsi_hw_health_check);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304949
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304950 phwi_ctrlr = phba->phwi_ctrlr;
4951 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05304952
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304953 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304954 for (i = 0; i < phba->num_cpus; i++) {
4955 pbe_eq = &phwi_context->be_eq[i];
4956 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4957 be_iopoll);
4958 blk_iopoll_enable(&pbe_eq->iopoll);
4959 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304960
4961 i = (phba->msix_enabled) ? i : 0;
4962 /* Work item for MCC handling */
4963 pbe_eq = &phwi_context->be_eq[i];
4964 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
4965 } else {
4966 if (phba->msix_enabled) {
4967 for (i = 0; i <= phba->num_cpus; i++) {
4968 pbe_eq = &phwi_context->be_eq[i];
4969 INIT_WORK(&pbe_eq->work_cqs,
4970 beiscsi_process_all_cqs);
4971 }
4972 } else {
4973 pbe_eq = &phwi_context->be_eq[0];
4974 INIT_WORK(&pbe_eq->work_cqs,
4975 beiscsi_process_all_cqs);
4976 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304977 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304978
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304979 ret = beiscsi_init_irqs(phba);
4980 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304981 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4982 "BM_%d : beiscsi_dev_probe-"
4983 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304984 goto free_blkenbld;
4985 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304986 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004987
4988 if (beiscsi_setup_boot_info(phba))
4989 /*
4990 * log error but continue, because we may not be using
4991 * iscsi boot.
4992 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304993 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4994 "BM_%d : Could not set up "
4995 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004996
Mike Christie0e438952012-04-03 23:41:51 -05004997 beiscsi_create_def_ifaces(phba);
John Soni Jose7a158002012-10-20 04:45:51 +05304998 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4999 msecs_to_jiffies(1000));
5000
John Soni Jose99bc5d52012-08-20 23:00:18 +05305001 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5002 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305003 return 0;
5004
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305005free_blkenbld:
5006 destroy_workqueue(phba->wq);
5007 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305008 for (i = 0; i < phba->num_cpus; i++) {
5009 pbe_eq = &phwi_context->be_eq[i];
5010 blk_iopoll_disable(&pbe_eq->iopoll);
5011 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305012free_twq:
5013 beiscsi_clean_port(phba);
5014 beiscsi_free_mem(phba);
5015free_port:
5016 pci_free_consistent(phba->pcidev,
5017 phba->ctrl.mbox_mem_alloced.size,
5018 phba->ctrl.mbox_mem_alloced.va,
5019 phba->ctrl.mbox_mem_alloced.dma);
5020 beiscsi_unmap_pci_function(phba);
5021hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305022 if (phba->msix_enabled)
5023 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305024 iscsi_host_remove(phba->shost);
5025 pci_dev_put(phba->pcidev);
5026 iscsi_host_free(phba->shost);
5027disable_pci:
5028 pci_disable_device(pcidev);
5029 return ret;
5030}
5031
5032struct iscsi_transport beiscsi_iscsi_transport = {
5033 .owner = THIS_MODULE,
5034 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305035 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305036 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305037 .create_session = beiscsi_session_create,
5038 .destroy_session = beiscsi_session_destroy,
5039 .create_conn = beiscsi_conn_create,
5040 .bind_conn = beiscsi_conn_bind,
5041 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05005042 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05005043 .set_iface_param = be2iscsi_iface_set_param,
5044 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305045 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005046 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305047 .get_session_param = iscsi_session_get_param,
5048 .get_host_param = beiscsi_get_host_param,
5049 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005050 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305051 .send_pdu = iscsi_conn_send_pdu,
5052 .xmit_task = beiscsi_task_xmit,
5053 .cleanup_task = beiscsi_cleanup_task,
5054 .alloc_pdu = beiscsi_alloc_pdu,
5055 .parse_pdu_itt = beiscsi_parse_pdu,
5056 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005057 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305058 .ep_connect = beiscsi_ep_connect,
5059 .ep_poll = beiscsi_ep_poll,
5060 .ep_disconnect = beiscsi_ep_disconnect,
5061 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005062 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305063};
5064
5065static struct pci_driver beiscsi_pci_driver = {
5066 .name = DRV_NAME,
5067 .probe = beiscsi_dev_probe,
5068 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005069 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305070 .id_table = beiscsi_pci_id_table
5071};
5072
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305073
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305074static int __init beiscsi_module_init(void)
5075{
5076 int ret;
5077
5078 beiscsi_scsi_transport =
5079 iscsi_register_transport(&beiscsi_iscsi_transport);
5080 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305081 printk(KERN_ERR
5082 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305083 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305084 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305085 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5086 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305087
5088 ret = pci_register_driver(&beiscsi_pci_driver);
5089 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305090 printk(KERN_ERR
5091 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305092 goto unregister_iscsi_transport;
5093 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305094 return 0;
5095
5096unregister_iscsi_transport:
5097 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5098 return ret;
5099}
5100
5101static void __exit beiscsi_module_exit(void)
5102{
5103 pci_unregister_driver(&beiscsi_pci_driver);
5104 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5105}
5106
5107module_init(beiscsi_module_init);
5108module_exit(beiscsi_module_exit);