blob: 7561e13322577ab35c2103ece461ec7a4d06e78f [file] [log] [blame]
Jitendra Bhivare942b7652017-03-24 14:11:48 +05301/*
Jitendra Bhivare0172dc62017-10-10 16:18:19 +05302 * Copyright 2017 Broadcom. All Rights Reserved.
Jitendra Bhivare942b7652017-03-24 14:11:48 +05303 * The term "Broadcom" refers to Broadcom Limited and/or its subsidiaries.
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304 *
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
Jitendra Bhivare942b7652017-03-24 14:11:48 +05307 * as published by the Free Software Foundation. The full GNU General
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05308 * Public License is included in this distribution in the file called COPYING.
9 *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053010 * Contact Information:
Jitendra Bhivare60f36e02016-08-19 15:20:24 +053011 * linux-drivers@broadcom.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053012 *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053013 */
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070014
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053015#include <linux/reboot.h>
16#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018#include <linux/interrupt.h>
19#include <linux/blkdev.h>
20#include <linux/pci.h>
21#include <linux/string.h>
22#include <linux/kernel.h>
23#include <linux/semaphore.h>
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +053024#include <linux/iscsi_boot_sysfs.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040025#include <linux/module.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050026#include <linux/bsg-lib.h>
Jitendra Bhivare1094cf62016-01-20 14:10:56 +053027#include <linux/irq_poll.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053028
29#include <scsi/libiscsi.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050030#include <scsi/scsi_bsg_iscsi.h>
31#include <scsi/scsi_netlink.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053032#include <scsi/scsi_transport_iscsi.h>
33#include <scsi/scsi_transport.h>
34#include <scsi/scsi_cmnd.h>
35#include <scsi/scsi_device.h>
36#include <scsi/scsi_host.h>
37#include <scsi/scsi.h>
38#include "be_main.h"
39#include "be_iscsi.h"
40#include "be_mgmt.h"
John Soni Jose0a513dd2012-08-20 23:00:55 +053041#include "be_cmds.h"
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053042
43static unsigned int be_iopoll_budget = 10;
44static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053045static unsigned int enable_msix = 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053046
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053047MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050048MODULE_VERSION(BUILD_STR);
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +053049MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053050MODULE_LICENSE("GPL");
51module_param(be_iopoll_budget, int, 0);
52module_param(enable_msix, int, 0);
53module_param(be_max_phys_size, uint, S_IRUGO);
John Soni Jose99bc5d52012-08-20 23:00:18 +053054MODULE_PARM_DESC(be_max_phys_size,
55 "Maximum Size (In Kilobytes) of physically contiguous "
56 "memory that can be allocated. Range is 16 - 128");
57
58#define beiscsi_disp_param(_name)\
Baoyou Xie0825b8e2016-09-26 20:00:59 +080059static ssize_t \
John Soni Jose99bc5d52012-08-20 23:00:18 +053060beiscsi_##_name##_disp(struct device *dev,\
61 struct device_attribute *attrib, char *buf) \
62{ \
63 struct Scsi_Host *shost = class_to_shost(dev);\
64 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
John Soni Jose99bc5d52012-08-20 23:00:18 +053065 return snprintf(buf, PAGE_SIZE, "%d\n",\
66 phba->attr_##_name);\
67}
68
69#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
Baoyou Xie0825b8e2016-09-26 20:00:59 +080070static int \
John Soni Jose99bc5d52012-08-20 23:00:18 +053071beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
72{\
73 if (val >= _minval && val <= _maxval) {\
74 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
75 "BA_%d : beiscsi_"#_name" updated "\
76 "from 0x%x ==> 0x%x\n",\
77 phba->attr_##_name, val); \
78 phba->attr_##_name = val;\
79 return 0;\
80 } \
81 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
82 "BA_%d beiscsi_"#_name" attribute "\
83 "cannot be updated to 0x%x, "\
84 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
85 return -EINVAL;\
86}
87
88#define beiscsi_store_param(_name) \
Baoyou Xie0825b8e2016-09-26 20:00:59 +080089static ssize_t \
John Soni Jose99bc5d52012-08-20 23:00:18 +053090beiscsi_##_name##_store(struct device *dev,\
91 struct device_attribute *attr, const char *buf,\
92 size_t count) \
93{ \
94 struct Scsi_Host *shost = class_to_shost(dev);\
95 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
96 uint32_t param_val = 0;\
97 if (!isdigit(buf[0]))\
98 return -EINVAL;\
99 if (sscanf(buf, "%i", &param_val) != 1)\
100 return -EINVAL;\
101 if (beiscsi_##_name##_change(phba, param_val) == 0) \
102 return strlen(buf);\
103 else \
104 return -EINVAL;\
105}
106
107#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
Baoyou Xie0825b8e2016-09-26 20:00:59 +0800108static int \
John Soni Jose99bc5d52012-08-20 23:00:18 +0530109beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
110{ \
111 if (val >= _minval && val <= _maxval) {\
112 phba->attr_##_name = val;\
113 return 0;\
114 } \
115 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
116 "BA_%d beiscsi_"#_name" attribute " \
117 "cannot be updated to 0x%x, "\
118 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
119 phba->attr_##_name = _defval;\
120 return -EINVAL;\
121}
122
123#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
124static uint beiscsi_##_name = _defval;\
125module_param(beiscsi_##_name, uint, S_IRUGO);\
126MODULE_PARM_DESC(beiscsi_##_name, _descp);\
127beiscsi_disp_param(_name)\
128beiscsi_change_param(_name, _minval, _maxval, _defval)\
129beiscsi_store_param(_name)\
130beiscsi_init_param(_name, _minval, _maxval, _defval)\
131DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
132 beiscsi_##_name##_disp, beiscsi_##_name##_store)
133
134/*
135 * When new log level added update the
136 * the MAX allowed value for log_enable
137 */
138BEISCSI_RW_ATTR(log_enable, 0x00,
139 0xFF, 0x00, "Enable logging Bit Mask\n"
140 "\t\t\t\tInitialization Events : 0x01\n"
141 "\t\t\t\tMailbox Events : 0x02\n"
142 "\t\t\t\tMiscellaneous Events : 0x04\n"
143 "\t\t\t\tError Handling : 0x08\n"
144 "\t\t\t\tIO Path Events : 0x10\n"
Jayamohan Kallickalafb96052013-09-28 15:35:55 -0700145 "\t\t\t\tConfiguration Path : 0x20\n"
146 "\t\t\t\tiSCSI Protocol : 0x40\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +0530147
John Soni Jose5cac7592012-10-20 04:42:25 +0530148DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
John Soni Jose26000db2012-10-20 04:45:06 +0530149DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700150DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
Jayamohan Kallickald3fea9a2013-09-28 15:35:53 -0700151DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
Jayamohan Kallickal6103c1f2013-09-28 15:35:52 -0700152DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
153 beiscsi_active_session_disp, NULL);
154DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
155 beiscsi_free_session_disp, NULL);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530156struct device_attribute *beiscsi_attrs[] = {
157 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530158 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530159 &dev_attr_beiscsi_adapter_family,
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700160 &dev_attr_beiscsi_fw_ver,
Jayamohan Kallickal6103c1f2013-09-28 15:35:52 -0700161 &dev_attr_beiscsi_active_session_count,
162 &dev_attr_beiscsi_free_session_count,
Jayamohan Kallickald3fea9a2013-09-28 15:35:53 -0700163 &dev_attr_beiscsi_phys_port,
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{
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530214 struct iscsi_task *abrt_task = (struct iscsi_task *)sc->SCp.ptr;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530215 struct iscsi_cls_session *cls_session;
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530216 struct beiscsi_io_task *abrt_io_task;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530217 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530218 struct iscsi_session *session;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530219 struct invldt_cmd_tbl inv_tbl;
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530220 struct beiscsi_hba *phba;
221 struct iscsi_conn *conn;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -0500222 int rc;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530223
224 cls_session = starget_to_session(scsi_target(sc->device));
225 session = cls_session->dd_data;
226
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530227 /* check if we raced, task just got cleaned up under us */
228 spin_lock_bh(&session->back_lock);
229 if (!abrt_task || !abrt_task->sc) {
230 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530231 return SUCCESS;
232 }
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530233 /* get a task ref till FW processes the req for the ICD used */
234 __iscsi_get_task(abrt_task);
235 abrt_io_task = abrt_task->dd_data;
236 conn = abrt_task->conn;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530237 beiscsi_conn = conn->dd_data;
238 phba = beiscsi_conn->phba;
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530239 /* mark WRB invalid which have been not processed by FW yet */
Jitendra Bhivare392b7d22016-12-13 15:55:57 +0530240 if (is_chip_be2_be3r(phba)) {
241 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
242 abrt_io_task->pwrb_handle->pwrb, 1);
243 } else {
244 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
245 abrt_io_task->pwrb_handle->pwrb, 1);
246 }
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530247 inv_tbl.cid = beiscsi_conn->beiscsi_conn_cid;
248 inv_tbl.icd = abrt_io_task->psgl_handle->sgl_index;
249 spin_unlock_bh(&session->back_lock);
250
Jitendra Bhivare98713212016-12-13 15:55:55 +0530251 rc = beiscsi_mgmt_invalidate_icds(phba, &inv_tbl, 1);
Jitendra Bhivarefaa0a222016-12-13 15:55:56 +0530252 iscsi_put_task(abrt_task);
Jitendra Bhivare98713212016-12-13 15:55:55 +0530253 if (rc) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530254 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
Jitendra Bhivare98713212016-12-13 15:55:55 +0530255 "BM_%d : sc %p invalidation failed %d\n",
256 sc, rc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530257 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530258 }
John Soni Josee175def2012-10-20 04:45:40 +0530259
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530260 return iscsi_eh_abort(sc);
261}
262
263static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
264{
Jitendra Bhivare98713212016-12-13 15:55:55 +0530265 struct beiscsi_invldt_cmd_tbl {
266 struct invldt_cmd_tbl tbl[BE_INVLDT_CMD_TBL_SZ];
267 struct iscsi_task *task[BE_INVLDT_CMD_TBL_SZ];
268 } *inv_tbl;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530269 struct iscsi_cls_session *cls_session;
Jitendra Bhivare98713212016-12-13 15:55:55 +0530270 struct beiscsi_conn *beiscsi_conn;
271 struct beiscsi_io_task *io_task;
272 struct iscsi_session *session;
273 struct beiscsi_hba *phba;
274 struct iscsi_conn *conn;
275 struct iscsi_task *task;
276 unsigned int i, nents;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530277 int rc, more = 0;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530278
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530279 cls_session = starget_to_session(scsi_target(sc->device));
280 session = cls_session->dd_data;
Jitendra Bhivare98713212016-12-13 15:55:55 +0530281
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600282 spin_lock_bh(&session->frwd_lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500283 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600284 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500285 return FAILED;
286 }
Jitendra Bhivare98713212016-12-13 15:55:55 +0530287
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530288 conn = session->leadconn;
289 beiscsi_conn = conn->dd_data;
290 phba = beiscsi_conn->phba;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530291
Wei Yongjunf2534732017-01-12 14:21:32 +0000292 inv_tbl = kzalloc(sizeof(*inv_tbl), GFP_ATOMIC);
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530293 if (!inv_tbl) {
294 spin_unlock_bh(&session->frwd_lock);
295 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
296 "BM_%d : invldt_cmd_tbl alloc failed\n");
297 return FAILED;
298 }
299 nents = 0;
Jitendra Bhivare98713212016-12-13 15:55:55 +0530300 /* take back_lock to prevent task from getting cleaned up under us */
301 spin_lock(&session->back_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530302 for (i = 0; i < conn->session->cmds_max; i++) {
Jitendra Bhivare98713212016-12-13 15:55:55 +0530303 task = conn->session->cmds[i];
304 if (!task->sc)
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530305 continue;
306
Jitendra Bhivare98713212016-12-13 15:55:55 +0530307 if (sc->device->lun != task->sc->device->lun)
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530308 continue;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530309 /**
310 * Can't fit in more cmds? Normally this won't happen b'coz
311 * BEISCSI_CMD_PER_LUN is same as BE_INVLDT_CMD_TBL_SZ.
312 */
313 if (nents == BE_INVLDT_CMD_TBL_SZ) {
314 more = 1;
315 break;
316 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530317
Jitendra Bhivare98713212016-12-13 15:55:55 +0530318 /* get a task ref till FW processes the req for the ICD used */
319 __iscsi_get_task(task);
320 io_task = task->dd_data;
321 /* mark WRB invalid which have been not processed by FW yet */
Jitendra Bhivare392b7d22016-12-13 15:55:57 +0530322 if (is_chip_be2_be3r(phba)) {
323 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
324 io_task->pwrb_handle->pwrb, 1);
325 } else {
326 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, invld,
327 io_task->pwrb_handle->pwrb, 1);
328 }
Jayamohan Kallickal7626c062013-09-28 15:35:57 -0700329
Jitendra Bhivare98713212016-12-13 15:55:55 +0530330 inv_tbl->tbl[nents].cid = beiscsi_conn->beiscsi_conn_cid;
331 inv_tbl->tbl[nents].icd = io_task->psgl_handle->sgl_index;
332 inv_tbl->task[nents] = task;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530333 nents++;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530334 }
Jitendra Bhivared1e1d632017-03-24 14:11:42 +0530335 spin_unlock(&session->back_lock);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600336 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530337
Jitendra Bhivare98713212016-12-13 15:55:55 +0530338 rc = SUCCESS;
339 if (!nents)
340 goto end_reset;
341
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530342 if (more) {
343 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
344 "BM_%d : number of cmds exceeds size of invalidation table\n");
Jitendra Bhivare98713212016-12-13 15:55:55 +0530345 rc = FAILED;
346 goto end_reset;
Jitendra Bhivaref3505012016-12-13 15:55:54 +0530347 }
348
Jitendra Bhivare98713212016-12-13 15:55:55 +0530349 if (beiscsi_mgmt_invalidate_icds(phba, &inv_tbl->tbl[0], nents)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530350 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
Jitendra Bhivare98713212016-12-13 15:55:55 +0530351 "BM_%d : cid %u scmds invalidation failed\n",
352 beiscsi_conn->beiscsi_conn_cid);
353 rc = FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530354 }
John Soni Josee175def2012-10-20 04:45:40 +0530355
Jitendra Bhivare98713212016-12-13 15:55:55 +0530356end_reset:
357 for (i = 0; i < nents; i++)
358 iscsi_put_task(inv_tbl->task[i]);
359 kfree(inv_tbl);
360
361 if (rc == SUCCESS)
362 rc = iscsi_eh_device_reset(sc);
363 return rc;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530364}
365
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530366/*------------------- PCI Driver operations and data ----------------- */
Benoit Taine9baa3c32014-08-08 15:56:03 +0200367static const struct pci_device_id beiscsi_pci_id_table[] = {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530368 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530369 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530370 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
371 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
372 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530373 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530374 { 0 }
375};
376MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
377
John Soni Jose99bc5d52012-08-20 23:00:18 +0530378
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530379static struct scsi_host_template beiscsi_sht = {
380 .module = THIS_MODULE,
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +0530381 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530382 .proc_name = DRV_NAME,
383 .queuecommand = iscsi_queuecommand,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100384 .change_queue_depth = scsi_change_queue_depth,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530385 .slave_configure = beiscsi_slave_configure,
386 .target_alloc = iscsi_target_alloc,
Christoph Hellwigb6a05c82017-01-30 13:18:58 +0100387 .eh_timed_out = iscsi_eh_cmd_timed_out,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530388 .eh_abort_handler = beiscsi_eh_abort,
389 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530390 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530391 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530392 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
393 .can_queue = BE2_IO_DEPTH,
394 .this_id = -1,
395 .max_sectors = BEISCSI_MAX_SECTORS,
396 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
397 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500398 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100399 .track_queue_depth = 1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530400};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530401
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530402static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530403
404static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
405{
406 struct beiscsi_hba *phba;
407 struct Scsi_Host *shost;
408
409 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
410 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530411 dev_err(&pcidev->dev,
412 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530413 return NULL;
414 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530415 shost->max_id = BE2_MAX_SESSIONS;
416 shost->max_channel = 0;
417 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
418 shost->max_lun = BEISCSI_NUM_MAX_LUN;
419 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530420 phba = iscsi_host_priv(shost);
421 memset(phba, 0, sizeof(*phba));
422 phba->shost = shost;
423 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530424 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500425 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530426
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530427 return phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530428}
429
430static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
431{
432 if (phba->csr_va) {
433 iounmap(phba->csr_va);
434 phba->csr_va = NULL;
435 }
436 if (phba->db_va) {
437 iounmap(phba->db_va);
438 phba->db_va = NULL;
439 }
440 if (phba->pci_va) {
441 iounmap(phba->pci_va);
442 phba->pci_va = NULL;
443 }
444}
445
446static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
447 struct pci_dev *pcidev)
448{
449 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530450 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530451
452 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
453 pci_resource_len(pcidev, 2));
454 if (addr == NULL)
455 return -ENOMEM;
456 phba->ctrl.csr = addr;
457 phba->csr_va = addr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530458
459 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
460 if (addr == NULL)
461 goto pci_map_err;
462 phba->ctrl.db = addr;
463 phba->db_va = addr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530464
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530465 if (phba->generation == BE_GEN2)
466 pcicfg_reg = 1;
467 else
468 pcicfg_reg = 0;
469
470 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
471 pci_resource_len(pcidev, pcicfg_reg));
472
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530473 if (addr == NULL)
474 goto pci_map_err;
475 phba->ctrl.pcicfg = addr;
476 phba->pci_va = addr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530477 return 0;
478
479pci_map_err:
480 beiscsi_unmap_pci_function(phba);
481 return -ENOMEM;
482}
483
484static int beiscsi_enable_pci(struct pci_dev *pcidev)
485{
486 int ret;
487
488 ret = pci_enable_device(pcidev);
489 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530490 dev_err(&pcidev->dev,
491 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530492 return ret;
493 }
494
John Soni Josee307f3a2015-04-25 08:17:19 +0530495 ret = pci_request_regions(pcidev, DRV_NAME);
496 if (ret) {
497 dev_err(&pcidev->dev,
498 "beiscsi_enable_pci - request region failed\n");
499 goto pci_dev_disable;
500 }
501
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530502 pci_set_master(pcidev);
Jayamohan Kallickal6c57625b2014-01-29 02:16:45 -0500503 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
504 if (ret) {
505 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
506 if (ret) {
507 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530508 goto pci_region_release;
Jayamohan Kallickal6c57625b2014-01-29 02:16:45 -0500509 } else {
510 ret = pci_set_consistent_dma_mask(pcidev,
511 DMA_BIT_MASK(32));
512 }
513 } else {
514 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530515 if (ret) {
516 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530517 goto pci_region_release;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530518 }
519 }
520 return 0;
John Soni Josee307f3a2015-04-25 08:17:19 +0530521
522pci_region_release:
523 pci_release_regions(pcidev);
524pci_dev_disable:
525 pci_disable_device(pcidev);
526
527 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530528}
529
530static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
531{
532 struct be_ctrl_info *ctrl = &phba->ctrl;
533 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
534 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
535 int status = 0;
536
537 ctrl->pdev = pdev;
538 status = beiscsi_map_pci_bars(phba, pdev);
539 if (status)
540 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530541 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
542 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
543 mbox_mem_alloc->size,
544 &mbox_mem_alloc->dma);
545 if (!mbox_mem_alloc->va) {
546 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500547 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530548 }
549
550 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
551 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
552 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
553 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Jitendra Bhivarec03a50f2016-01-20 14:10:46 +0530554 mutex_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530555 spin_lock_init(&phba->ctrl.mcc_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530556
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530557 return status;
558}
559
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700560/**
561 * beiscsi_get_params()- Set the config paramters
562 * @phba: ptr device priv structure
563 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530564static void beiscsi_get_params(struct beiscsi_hba *phba)
565{
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700566 uint32_t total_cid_count = 0;
567 uint32_t total_icd_count = 0;
568 uint8_t ulp_num = 0;
569
570 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
571 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
572
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700573 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
574 uint32_t align_mask = 0;
575 uint32_t icd_post_per_page = 0;
576 uint32_t icd_count_unavailable = 0;
577 uint32_t icd_start = 0, icd_count = 0;
578 uint32_t icd_start_align = 0, icd_count_align = 0;
579
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700580 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700581 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
582 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
583
584 /* Get ICD count that can be posted on each page */
585 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
586 sizeof(struct iscsi_sge)));
587 align_mask = (icd_post_per_page - 1);
588
589 /* Check if icd_start is aligned ICD per page posting */
590 if (icd_start % icd_post_per_page) {
591 icd_start_align = ((icd_start +
592 icd_post_per_page) &
593 ~(align_mask));
594 phba->fw_config.
595 iscsi_icd_start[ulp_num] =
596 icd_start_align;
597 }
598
599 icd_count_align = (icd_count & ~align_mask);
600
601 /* ICD discarded in the process of alignment */
602 if (icd_start_align)
603 icd_count_unavailable = ((icd_start_align -
604 icd_start) +
605 (icd_count -
606 icd_count_align));
607
608 /* Updated ICD count available */
609 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
610 icd_count_unavailable);
611
612 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
613 "BM_%d : Aligned ICD values\n"
614 "\t ICD Start : %d\n"
615 "\t ICD Count : %d\n"
616 "\t ICD Discarded : %d\n",
617 phba->fw_config.
618 iscsi_icd_start[ulp_num],
619 phba->fw_config.
620 iscsi_icd_count[ulp_num],
621 icd_count_unavailable);
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700622 break;
623 }
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700624 }
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700625
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700626 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700627 phba->params.ios_per_ctrl = (total_icd_count -
628 (total_cid_count +
629 BE2_TMFS + BE2_NOPOUT_REQ));
630 phba->params.cxns_per_ctrl = total_cid_count;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700631 phba->params.icds_per_ctrl = total_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530632 phba->params.num_sge_per_io = BE2_SGE;
633 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
634 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700635 phba->params.num_eq_entries = 1024;
636 phba->params.num_cq_entries = 1024;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530637 phba->params.wrbs_per_cxn = 256;
638}
639
640static void hwi_ring_eq_db(struct beiscsi_hba *phba,
641 unsigned int id, unsigned int clr_interrupt,
642 unsigned int num_processed,
643 unsigned char rearm, unsigned char event)
644{
645 u32 val = 0;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500646
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530647 if (rearm)
648 val |= 1 << DB_EQ_REARM_SHIFT;
649 if (clr_interrupt)
650 val |= 1 << DB_EQ_CLR_SHIFT;
651 if (event)
652 val |= 1 << DB_EQ_EVNT_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500653
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530654 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500655 /* Setting lower order EQ_ID Bits */
656 val |= (id & DB_EQ_RING_ID_LOW_MASK);
657
658 /* Setting Higher order EQ_ID Bits */
659 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
660 DB_EQ_RING_ID_HIGH_MASK)
661 << DB_EQ_HIGH_SET_SHIFT);
662
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530663 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
664}
665
666/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530667 * be_isr_mcc - The isr routine of the driver.
668 * @irq: Not used
669 * @dev_id: Pointer to host adapter structure
670 */
671static irqreturn_t be_isr_mcc(int irq, void *dev_id)
672{
673 struct beiscsi_hba *phba;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530674 struct be_eq_entry *eqe;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530675 struct be_queue_info *eq;
676 struct be_queue_info *mcc;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530677 unsigned int mcc_events;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530678 struct be_eq_obj *pbe_eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530679
680 pbe_eq = dev_id;
681 eq = &pbe_eq->q;
682 phba = pbe_eq->phba;
683 mcc = &phba->ctrl.mcc_obj.cq;
684 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530685
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530686 mcc_events = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530687 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
688 & EQE_VALID_MASK) {
689 if (((eqe->dw[offsetof(struct amap_eq_entry,
690 resource_id) / 32] &
691 EQE_RESID_MASK) >> 16) == mcc->id) {
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530692 mcc_events++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530693 }
694 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
695 queue_tail_inc(eq);
696 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530697 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530698
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530699 if (mcc_events) {
700 queue_work(phba->wq, &pbe_eq->mcc_work);
701 hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
702 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530703 return IRQ_HANDLED;
704}
705
706/**
707 * be_isr_msix - The isr routine of the driver.
708 * @irq: Not used
709 * @dev_id: Pointer to host adapter structure
710 */
711static irqreturn_t be_isr_msix(int irq, void *dev_id)
712{
713 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530714 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530715 struct be_eq_obj *pbe_eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530716
717 pbe_eq = dev_id;
718 eq = &pbe_eq->q;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530719
720 phba = pbe_eq->phba;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530721 /* disable interrupt till iopoll completes */
722 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
723 irq_poll_sched(&pbe_eq->iopoll);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530724
725 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530726}
727
728/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530729 * be_isr - The isr routine of the driver.
730 * @irq: Not used
731 * @dev_id: Pointer to host adapter structure
732 */
733static irqreturn_t be_isr(int irq, void *dev_id)
734{
735 struct beiscsi_hba *phba;
736 struct hwi_controller *phwi_ctrlr;
737 struct hwi_context_memory *phwi_context;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530738 struct be_eq_entry *eqe;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530739 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530740 struct be_queue_info *mcc;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530741 unsigned int mcc_events, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530742 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530743 struct be_eq_obj *pbe_eq;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530744 int isr, rearm;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530745
746 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700747 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530748 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
749 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
750 if (!isr)
751 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530752
753 phwi_ctrlr = phba->phwi_ctrlr;
754 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530755 pbe_eq = &phwi_context->be_eq[0];
756
757 eq = &phwi_context->be_eq[0].q;
758 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530759 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530760
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530761 io_events = 0;
762 mcc_events = 0;
Jens Axboe89f8b332014-03-13 09:38:42 -0600763 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
764 & EQE_VALID_MASK) {
765 if (((eqe->dw[offsetof(struct amap_eq_entry,
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530766 resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
767 mcc_events++;
768 else
769 io_events++;
Jens Axboe89f8b332014-03-13 09:38:42 -0600770 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
771 queue_tail_inc(eq);
772 eqe = queue_tail_node(eq);
773 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530774 if (!io_events && !mcc_events)
Jens Axboe89f8b332014-03-13 09:38:42 -0600775 return IRQ_NONE;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530776
777 /* no need to rearm if interrupt is only for IOs */
778 rearm = 0;
779 if (mcc_events) {
780 queue_work(phba->wq, &pbe_eq->mcc_work);
781 /* rearm for MCCQ */
782 rearm = 1;
783 }
784 if (io_events)
785 irq_poll_sched(&pbe_eq->iopoll);
786 hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
787 return IRQ_HANDLED;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530788}
789
Jitendra Bhivare45371aa2017-10-10 16:18:13 +0530790static void beiscsi_free_irqs(struct beiscsi_hba *phba)
791{
792 struct hwi_context_memory *phwi_context;
793 int i;
794
795 if (!phba->pcidev->msix_enabled) {
796 if (phba->pcidev->irq)
797 free_irq(phba->pcidev->irq, phba);
798 return;
799 }
800
801 phwi_context = phba->phwi_ctrlr->phwi_ctxt;
802 for (i = 0; i <= phba->num_cpus; i++) {
803 free_irq(pci_irq_vector(phba->pcidev, i),
804 &phwi_context->be_eq[i]);
805 kfree(phba->msi_name[i]);
806 }
807}
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530808
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530809static int beiscsi_init_irqs(struct beiscsi_hba *phba)
810{
811 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530812 struct hwi_controller *phwi_ctrlr;
813 struct hwi_context_memory *phwi_context;
Christoph Hellwig83148862017-01-13 17:29:48 +0100814 int ret, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530815
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530816 phwi_ctrlr = phba->phwi_ctrlr;
817 phwi_context = phwi_ctrlr->phwi_ctxt;
818
Christoph Hellwig83148862017-01-13 17:29:48 +0100819 if (pcidev->msix_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530820 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700821 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
822 GFP_KERNEL);
823 if (!phba->msi_name[i]) {
824 ret = -ENOMEM;
825 goto free_msix_irqs;
826 }
827
828 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
829 phba->shost->host_no, i);
Christoph Hellwig83148862017-01-13 17:29:48 +0100830 ret = request_irq(pci_irq_vector(pcidev, i),
831 be_isr_msix, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530832 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530833 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530834 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
835 "BM_%d : beiscsi_init_irqs-Failed to"
836 "register msix for i = %d\n",
837 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700838 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530839 goto free_msix_irqs;
840 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530841 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700842 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
843 if (!phba->msi_name[i]) {
844 ret = -ENOMEM;
845 goto free_msix_irqs;
846 }
847 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
848 phba->shost->host_no);
Christoph Hellwig83148862017-01-13 17:29:48 +0100849 ret = request_irq(pci_irq_vector(pcidev, i), be_isr_mcc, 0,
850 phba->msi_name[i], &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530851 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530852 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
853 "BM_%d : beiscsi_init_irqs-"
854 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700855 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530856 goto free_msix_irqs;
857 }
858
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 } else {
860 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
861 "beiscsi", phba);
862 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530863 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
864 "BM_%d : beiscsi_init_irqs-"
865 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 return ret;
867 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530868 }
869 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530870free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700871 for (j = i - 1; j >= 0; j--) {
Christoph Hellwig83148862017-01-13 17:29:48 +0100872 free_irq(pci_irq_vector(pcidev, i), &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700873 kfree(phba->msi_name[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700874 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530875 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530876}
877
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500878void hwi_ring_cq_db(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530879 unsigned int id, unsigned int num_processed,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530880 unsigned char rearm)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530881{
882 u32 val = 0;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500883
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530884 if (rearm)
885 val |= 1 << DB_CQ_REARM_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500886
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530887 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500888
889 /* Setting lower order CQ_ID Bits */
890 val |= (id & DB_CQ_RING_ID_LOW_MASK);
891
892 /* Setting Higher order CQ_ID Bits */
893 val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
894 DB_CQ_RING_ID_HIGH_MASK)
895 << DB_CQ_HIGH_SET_SHIFT);
896
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530897 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
898}
899
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530900static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
901{
902 struct sgl_handle *psgl_handle;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530903 unsigned long flags;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530904
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530905 spin_lock_irqsave(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530906 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530907 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
908 "BM_%d : In alloc_io_sgl_handle,"
909 " io_sgl_alloc_index=%d\n",
910 phba->io_sgl_alloc_index);
911
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530912 psgl_handle = phba->io_sgl_hndl_base[phba->
913 io_sgl_alloc_index];
914 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
915 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530916 if (phba->io_sgl_alloc_index == (phba->params.
917 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530918 phba->io_sgl_alloc_index = 0;
919 else
920 phba->io_sgl_alloc_index++;
921 } else
922 psgl_handle = NULL;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530923 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530924 return psgl_handle;
925}
926
927static void
928free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
929{
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530930 unsigned long flags;
931
932 spin_lock_irqsave(&phba->io_sgl_lock, flags);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530933 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
934 "BM_%d : In free_,io_sgl_free_index=%d\n",
935 phba->io_sgl_free_index);
936
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530937 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
938 /*
939 * this can happen if clean_task is called on a task that
940 * failed in xmit_task or alloc_pdu.
941 */
Jitendra Bhivare45efc9402017-10-10 16:18:18 +0530942 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
943 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d, value there=%p\n",
944 phba->io_sgl_free_index,
945 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
946 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530947 return;
948 }
949 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
950 phba->io_sgl_hndl_avbl++;
951 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
952 phba->io_sgl_free_index = 0;
953 else
954 phba->io_sgl_free_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530955 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530956}
957
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530958static inline struct wrb_handle *
959beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
960 unsigned int wrbs_per_cxn)
961{
962 struct wrb_handle *pwrb_handle;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530963 unsigned long flags;
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530964
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530965 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +0530966 if (!pwrb_context->wrb_handles_available) {
967 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
968 return NULL;
969 }
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530970 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
971 pwrb_context->wrb_handles_available--;
972 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
973 pwrb_context->alloc_index = 0;
974 else
975 pwrb_context->alloc_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530976 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
Jitendra Bhivarebf9b7552016-08-26 15:09:07 +0530977
978 if (pwrb_handle)
979 memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530980
981 return pwrb_handle;
982}
983
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530984/**
985 * alloc_wrb_handle - To allocate a wrb handle
986 * @phba: The hba pointer
987 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +0530988 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530989 *
990 * This happens under session_lock until submission to chip
991 */
John Soni Jose340c99e2015-08-20 04:44:30 +0530992struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530993 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530994{
995 struct hwi_wrb_context *pwrb_context;
996 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700997 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530998
999 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001000 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301001 /* return the context address */
1002 *pcontext = pwrb_context;
1003 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1004}
John Soni Jose340c99e2015-08-20 04:44:30 +05301005
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301006static inline void
1007beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1008 struct wrb_handle *pwrb_handle,
1009 unsigned int wrbs_per_cxn)
1010{
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301011 unsigned long flags;
1012
1013 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301014 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1015 pwrb_context->wrb_handles_available++;
1016 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1017 pwrb_context->free_index = 0;
1018 else
1019 pwrb_context->free_index++;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301020 pwrb_handle->pio_handle = NULL;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301021 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301022}
1023
1024/**
1025 * free_wrb_handle - To free the wrb handle back to pool
1026 * @phba: The hba pointer
1027 * @pwrb_context: The context to free from
1028 * @pwrb_handle: The wrb_handle to free
1029 *
1030 * This happens under session_lock until submission to chip
1031 */
1032static void
1033free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1034 struct wrb_handle *pwrb_handle)
1035{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301036 beiscsi_put_wrb_handle(pwrb_context,
1037 pwrb_handle,
1038 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301039 beiscsi_log(phba, KERN_INFO,
1040 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1041 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1042 "wrb_handles_available=%d\n",
1043 pwrb_handle, pwrb_context->free_index,
1044 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301045}
1046
1047static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1048{
1049 struct sgl_handle *psgl_handle;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301050 unsigned long flags;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301051
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301052 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301053 if (phba->eh_sgl_hndl_avbl) {
1054 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1055 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301056 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1057 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1058 phba->eh_sgl_alloc_index,
1059 phba->eh_sgl_alloc_index);
1060
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301061 phba->eh_sgl_hndl_avbl--;
1062 if (phba->eh_sgl_alloc_index ==
1063 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1064 1))
1065 phba->eh_sgl_alloc_index = 0;
1066 else
1067 phba->eh_sgl_alloc_index++;
1068 } else
1069 psgl_handle = NULL;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301070 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301071 return psgl_handle;
1072}
1073
1074void
1075free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1076{
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301077 unsigned long flags;
1078
1079 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301080 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1081 "BM_%d : In free_mgmt_sgl_handle,"
1082 "eh_sgl_free_index=%d\n",
1083 phba->eh_sgl_free_index);
1084
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301085 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1086 /*
1087 * this can happen if clean_task is called on a task that
1088 * failed in xmit_task or alloc_pdu.
1089 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301090 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1091 "BM_%d : Double Free in eh SGL ,"
1092 "eh_sgl_free_index=%d\n",
1093 phba->eh_sgl_free_index);
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301094 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301095 return;
1096 }
1097 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1098 phba->eh_sgl_hndl_avbl++;
1099 if (phba->eh_sgl_free_index ==
1100 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1101 phba->eh_sgl_free_index = 0;
1102 else
1103 phba->eh_sgl_free_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301104 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301105}
1106
1107static void
1108be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301109 struct iscsi_task *task,
1110 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301111{
1112 struct beiscsi_io_task *io_task = task->dd_data;
1113 struct be_status_bhs *sts_bhs =
1114 (struct be_status_bhs *)io_task->cmd_bhs;
1115 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301116 unsigned char *sense;
1117 u32 resid = 0, exp_cmdsn, max_cmdsn;
1118 u8 rsp, status, flags;
1119
John Soni Jose73133262012-10-20 04:44:49 +05301120 exp_cmdsn = csol_cqe->exp_cmdsn;
1121 max_cmdsn = (csol_cqe->exp_cmdsn +
1122 csol_cqe->cmd_wnd - 1);
1123 rsp = csol_cqe->i_resp;
1124 status = csol_cqe->i_sts;
1125 flags = csol_cqe->i_flags;
1126 resid = csol_cqe->res_cnt;
1127
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001128 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001129 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001130 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001131 io_task->scsi_cmnd = NULL;
1132 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301133
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001134 return;
1135 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301136 task->sc->result = (DID_OK << 16) | status;
1137 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1138 task->sc->result = DID_ERROR << 16;
1139 goto unmap;
1140 }
1141
1142 /* bidi not initially supported */
1143 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301144 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1145 task->sc->result = DID_ERROR << 16;
1146
1147 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1148 scsi_set_resid(task->sc, resid);
1149 if (!status && (scsi_bufflen(task->sc) - resid <
1150 task->sc->underflow))
1151 task->sc->result = DID_ERROR << 16;
1152 }
1153 }
1154
1155 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001156 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301157 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001158
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301159 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001160 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301161 memcpy(task->sc->sense_buffer, sense,
1162 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1163 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301164
John Soni Jose73133262012-10-20 04:44:49 +05301165 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1166 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301167unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301168 if (io_task->scsi_cmnd) {
1169 scsi_dma_unmap(io_task->scsi_cmnd);
1170 io_task->scsi_cmnd = NULL;
1171 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301172 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1173}
1174
1175static void
1176be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301177 struct iscsi_task *task,
1178 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301179{
1180 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301181 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301182 struct iscsi_conn *conn = beiscsi_conn->conn;
1183
1184 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301185 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301186 hdr->t2wait = 5;
1187 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301188 hdr->flags = csol_cqe->i_flags;
1189 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001190 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1191 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1192 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301193
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301194 hdr->dlength[0] = 0;
1195 hdr->dlength[1] = 0;
1196 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301197 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301198 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301199 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1200}
1201
1202static void
1203be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301204 struct iscsi_task *task,
1205 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301206{
1207 struct iscsi_tm_rsp *hdr;
1208 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301209 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301210
1211 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301212 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301213 hdr->flags = csol_cqe->i_flags;
1214 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001215 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1216 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1217 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301218
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301219 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301220 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1221}
1222
1223static void
1224hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1225 struct beiscsi_hba *phba, struct sol_cqe *psol)
1226{
1227 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001228 uint16_t wrb_index, cid, cri_index;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05301229 struct hwi_controller *phwi_ctrlr;
1230 struct wrb_handle *pwrb_handle;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301231 struct iscsi_session *session;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05301232 struct iscsi_task *task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301233
1234 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001235 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301236 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1237 wrb_idx, psol);
1238 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1239 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001240 } else {
1241 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1242 wrb_idx, psol);
1243 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1244 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301245 }
1246
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001247 cri_index = BE_GET_CRI_FROM_CID(cid);
1248 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301249 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301250 session = beiscsi_conn->conn->session;
1251 spin_lock_bh(&session->back_lock);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301252 task = pwrb_handle->pio_handle;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301253 if (task)
1254 __iscsi_put_task(task);
1255 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301256}
1257
1258static void
1259be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301260 struct iscsi_task *task,
1261 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301262{
1263 struct iscsi_nopin *hdr;
1264 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301265 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301266
1267 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301268 hdr->flags = csol_cqe->i_flags;
1269 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001270 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1271 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301272
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301273 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301274 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301275 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1276}
1277
John Soni Jose73133262012-10-20 04:44:49 +05301278static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1279 struct sol_cqe *psol,
1280 struct common_sol_cqe *csol_cqe)
1281{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001282 if (is_chip_be2_be3r(phba)) {
1283 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1284 i_exp_cmd_sn, psol);
1285 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1286 i_res_cnt, psol);
1287 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1288 i_cmd_wnd, psol);
1289 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1290 wrb_index, psol);
1291 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1292 cid, psol);
1293 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1294 hw_sts, psol);
1295 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1296 i_resp, psol);
1297 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1298 i_sts, psol);
1299 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1300 i_flags, psol);
1301 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301302 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1303 i_exp_cmd_sn, psol);
1304 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1305 i_res_cnt, psol);
1306 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1307 wrb_index, psol);
1308 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1309 cid, psol);
1310 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1311 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001312 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301313 i_cmd_wnd, psol);
1314 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1315 cmd_cmpl, psol))
1316 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1317 i_sts, psol);
1318 else
1319 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1320 i_sts, psol);
1321 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1322 u, psol))
1323 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1324
1325 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1326 o, psol))
1327 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301328 }
1329}
1330
1331
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301332static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1333 struct beiscsi_hba *phba, struct sol_cqe *psol)
1334{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 struct iscsi_conn *conn = beiscsi_conn->conn;
1336 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301337 struct common_sol_cqe csol_cqe = {0};
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301338 struct hwi_wrb_context *pwrb_context;
1339 struct hwi_controller *phwi_ctrlr;
1340 struct wrb_handle *pwrb_handle;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301341 struct iscsi_task *task;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001342 uint16_t cri_index = 0;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301343 uint8_t type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301344
1345 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301346
1347 /* Copy the elements to a common structure */
1348 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1349
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001350 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1351 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301352
1353 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1354 csol_cqe.wrb_index];
1355
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301356 spin_lock_bh(&session->back_lock);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301357 task = pwrb_handle->pio_handle;
Jitendra Bhivare3f7f62e2016-12-13 15:55:58 +05301358 if (!task) {
1359 spin_unlock_bh(&session->back_lock);
1360 return;
1361 }
John Soni Jose73133262012-10-20 04:44:49 +05301362 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301363
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301364 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301365 case HWH_TYPE_IO:
1366 case HWH_TYPE_IO_RD:
1367 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301368 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301369 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301370 else
John Soni Jose73133262012-10-20 04:44:49 +05301371 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301372 break;
1373
1374 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301375 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301376 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301377 else
John Soni Jose73133262012-10-20 04:44:49 +05301378 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301379 break;
1380
1381 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301382 beiscsi_log(phba, KERN_ERR,
1383 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1384 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1385 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301386 break;
1387
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301388 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301389 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301390 break;
1391
1392 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301393 beiscsi_log(phba, KERN_WARNING,
1394 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1395 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1396 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301397 csol_cqe.wrb_index,
1398 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301399 break;
1400 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301401
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001402 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301403}
1404
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301405/**
1406 * ASYNC PDUs include
1407 * a. Unsolicited NOP-In (target initiated NOP-In)
1408 * b. ASYNC Messages
1409 * c. Reject PDU
1410 * d. Login response
1411 * These headers arrive unprocessed by the EP firmware.
1412 * iSCSI layer processes them.
1413 */
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301414static unsigned int
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301415beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
1416 struct pdu_base *phdr, void *pdata, unsigned int dlen)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301417{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301418 struct beiscsi_hba *phba = beiscsi_conn->phba;
1419 struct iscsi_conn *conn = beiscsi_conn->conn;
1420 struct beiscsi_io_task *io_task;
1421 struct iscsi_hdr *login_hdr;
1422 struct iscsi_task *task;
1423 u8 code;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301424
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301425 code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
1426 switch (code) {
1427 case ISCSI_OP_NOOP_IN:
1428 pdata = NULL;
1429 dlen = 0;
1430 break;
1431 case ISCSI_OP_ASYNC_EVENT:
1432 break;
1433 case ISCSI_OP_REJECT:
1434 WARN_ON(!pdata);
1435 WARN_ON(!(dlen == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301436 beiscsi_log(phba, KERN_ERR,
1437 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301438 "BM_%d : In ISCSI_OP_REJECT\n");
1439 break;
1440 case ISCSI_OP_LOGIN_RSP:
1441 case ISCSI_OP_TEXT_RSP:
1442 task = conn->login_task;
1443 io_task = task->dd_data;
1444 login_hdr = (struct iscsi_hdr *)phdr;
1445 login_hdr->itt = io_task->libiscsi_itt;
1446 break;
1447 default:
1448 beiscsi_log(phba, KERN_WARNING,
1449 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1450 "BM_%d : unrecognized async PDU opcode 0x%x\n",
1451 code);
1452 return 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301453 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301454 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301455 return 0;
1456}
1457
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301458static inline void
1459beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
1460 struct hd_async_handle *pasync_handle)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301461{
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301462 pasync_handle->is_final = 0;
1463 pasync_handle->buffer_len = 0;
1464 pasync_handle->in_use = 0;
1465 list_del_init(&pasync_handle->link);
1466}
1467
1468static void
1469beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
1470 struct hd_async_context *pasync_ctx,
1471 u16 cri)
1472{
1473 struct hd_async_handle *pasync_handle, *tmp_handle;
1474 struct list_head *plist;
1475
1476 plist = &pasync_ctx->async_entry[cri].wq.list;
1477 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link)
1478 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1479
1480 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
1481 pasync_ctx->async_entry[cri].wq.hdr_len = 0;
1482 pasync_ctx->async_entry[cri].wq.bytes_received = 0;
1483 pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301484}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301485
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301486static struct hd_async_handle *
1487beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
1488 struct hd_async_context *pasync_ctx,
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301489 struct i_t_dpdu_cqe *pdpdu_cqe,
1490 u8 *header)
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301491{
1492 struct beiscsi_hba *phba = beiscsi_conn->phba;
1493 struct hd_async_handle *pasync_handle;
1494 struct be_bus_address phys_addr;
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301495 u16 cid, code, ci, cri;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301496 u8 final, error = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301497 u32 dpl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301498
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301499 cid = beiscsi_conn->beiscsi_conn_cid;
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301500 cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301501 /**
1502 * This function is invoked to get the right async_handle structure
1503 * from a given DEF PDU CQ entry.
1504 *
1505 * - index in CQ entry gives the vertical index
1506 * - address in CQ entry is the offset where the DMA last ended
1507 * - final - no more notifications for this PDU
1508 */
1509 if (is_chip_be2_be3r(phba)) {
1510 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1511 dpl, pdpdu_cqe);
1512 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1513 index, pdpdu_cqe);
1514 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1515 final, pdpdu_cqe);
1516 } else {
1517 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1518 dpl, pdpdu_cqe);
1519 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1520 index, pdpdu_cqe);
1521 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1522 final, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301523 }
1524
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301525 /**
1526 * DB addr Hi/Lo is same for BE and SKH.
1527 * Subtract the dataplacementlength to get to the base.
1528 */
1529 phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1530 db_addr_lo, pdpdu_cqe);
1531 phys_addr.u.a32.address_lo -= dpl;
1532 phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1533 db_addr_hi, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301534
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301535 code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
1536 switch (code) {
1537 case UNSOL_HDR_NOTIFY:
1538 pasync_handle = pasync_ctx->async_entry[ci].header;
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301539 *header = 1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301540 break;
1541 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1542 error = 1;
1543 case UNSOL_DATA_NOTIFY:
1544 pasync_handle = pasync_ctx->async_entry[ci].data;
1545 break;
1546 /* called only for above codes */
1547 default:
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301548 return NULL;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301549 }
1550
1551 if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
1552 pasync_handle->index != ci) {
1553 /* driver bug - if ci does not match async handle index */
1554 error = 1;
1555 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1556 "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
1557 cid, pasync_handle->is_header ? 'H' : 'D',
1558 pasync_handle->pa.u.a64.address,
1559 pasync_handle->index,
1560 phys_addr.u.a64.address, ci);
1561 /* FW has stale address - attempt continuing by dropping */
1562 }
1563
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301564 /**
1565 * DEF PDU header and data buffers with errors should be simply
1566 * dropped as there are no consumers for it.
1567 */
1568 if (error) {
1569 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301570 return NULL;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301571 }
Jitendra Bhivareba6983a2017-03-24 14:11:46 +05301572
1573 if (pasync_handle->in_use || !list_empty(&pasync_handle->link)) {
1574 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1575 "BM_%d : cid %d async PDU handle in use - code %d ci %d addr %llx\n",
1576 cid, code, ci, phys_addr.u.a64.address);
1577 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1578 }
1579
1580 list_del_init(&pasync_handle->link);
1581 /**
1582 * Each CID is associated with unique CRI.
1583 * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
1584 **/
1585 pasync_handle->cri = cri;
1586 pasync_handle->is_final = final;
1587 pasync_handle->buffer_len = dpl;
1588 pasync_handle->in_use = 1;
1589
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301590 return pasync_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301591}
1592
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301593static unsigned int
1594beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
1595 struct hd_async_context *pasync_ctx,
1596 u16 cri)
1597{
1598 struct iscsi_session *session = beiscsi_conn->conn->session;
1599 struct hd_async_handle *pasync_handle, *plast_handle;
1600 struct beiscsi_hba *phba = beiscsi_conn->phba;
1601 void *phdr = NULL, *pdata = NULL;
1602 u32 dlen = 0, status = 0;
1603 struct list_head *plist;
1604
1605 plist = &pasync_ctx->async_entry[cri].wq.list;
1606 plast_handle = NULL;
1607 list_for_each_entry(pasync_handle, plist, link) {
1608 plast_handle = pasync_handle;
1609 /* get the header, the first entry */
1610 if (!phdr) {
1611 phdr = pasync_handle->pbuffer;
1612 continue;
1613 }
1614 /* use first buffer to collect all the data */
1615 if (!pdata) {
1616 pdata = pasync_handle->pbuffer;
1617 dlen = pasync_handle->buffer_len;
1618 continue;
1619 }
Jitendra Bhivare0ddee502017-03-24 14:11:47 +05301620 if (!pasync_handle->buffer_len ||
1621 (dlen + pasync_handle->buffer_len) >
1622 pasync_ctx->async_data.buffer_size)
1623 break;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301624 memcpy(pdata + dlen, pasync_handle->pbuffer,
1625 pasync_handle->buffer_len);
1626 dlen += pasync_handle->buffer_len;
1627 }
1628
1629 if (!plast_handle->is_final) {
1630 /* last handle should have final PDU notification from FW */
1631 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
Jitendra Bhivare0ddee502017-03-24 14:11:47 +05301632 "BM_%d : cid %u %p fwd async PDU opcode %x with last handle missing - HL%u:DN%u:DR%u\n",
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301633 beiscsi_conn->beiscsi_conn_cid, plast_handle,
Jitendra Bhivare0ddee502017-03-24 14:11:47 +05301634 AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr),
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301635 pasync_ctx->async_entry[cri].wq.hdr_len,
1636 pasync_ctx->async_entry[cri].wq.bytes_needed,
1637 pasync_ctx->async_entry[cri].wq.bytes_received);
1638 }
1639 spin_lock_bh(&session->back_lock);
1640 status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
1641 spin_unlock_bh(&session->back_lock);
1642 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1643 return status;
1644}
1645
1646static unsigned int
1647beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
1648 struct hd_async_context *pasync_ctx,
1649 struct hd_async_handle *pasync_handle)
1650{
1651 unsigned int bytes_needed = 0, status = 0;
1652 u16 cri = pasync_handle->cri;
1653 struct cri_wait_queue *wq;
1654 struct beiscsi_hba *phba;
1655 struct pdu_base *ppdu;
1656 char *err = "";
1657
1658 phba = beiscsi_conn->phba;
1659 wq = &pasync_ctx->async_entry[cri].wq;
1660 if (pasync_handle->is_header) {
1661 /* check if PDU hdr is rcv'd when old hdr not completed */
1662 if (wq->hdr_len) {
1663 err = "incomplete";
1664 goto drop_pdu;
1665 }
1666 ppdu = pasync_handle->pbuffer;
1667 bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
1668 data_len_hi, ppdu);
1669 bytes_needed <<= 16;
1670 bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
1671 data_len_lo, ppdu));
1672 wq->hdr_len = pasync_handle->buffer_len;
1673 wq->bytes_received = 0;
1674 wq->bytes_needed = bytes_needed;
1675 list_add_tail(&pasync_handle->link, &wq->list);
1676 if (!bytes_needed)
1677 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1678 pasync_ctx, cri);
1679 } else {
1680 /* check if data received has header and is needed */
1681 if (!wq->hdr_len || !wq->bytes_needed) {
1682 err = "header less";
1683 goto drop_pdu;
1684 }
1685 wq->bytes_received += pasync_handle->buffer_len;
1686 /* Something got overwritten? Better catch it here. */
1687 if (wq->bytes_received > wq->bytes_needed) {
1688 err = "overflow";
1689 goto drop_pdu;
1690 }
1691 list_add_tail(&pasync_handle->link, &wq->list);
1692 if (wq->bytes_received == wq->bytes_needed)
1693 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1694 pasync_ctx, cri);
1695 }
1696 return status;
1697
1698drop_pdu:
1699 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1700 "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
1701 beiscsi_conn->beiscsi_conn_cid, err,
1702 pasync_handle->is_header ? 'H' : 'D',
1703 wq->hdr_len, wq->bytes_needed,
1704 pasync_handle->buffer_len);
1705 /* discard this handle */
1706 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1707 /* free all the other handles in cri_wait_queue */
1708 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1709 /* try continuing */
1710 return status;
1711}
1712
1713static void
1714beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301715 u8 header, u8 ulp_num, u16 nbuf)
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301716{
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301717 struct hd_async_handle *pasync_handle;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301718 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301719 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301720 struct phys_addr *pasync_sge;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301721 u32 ring_id, doorbell = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301722 u32 doorbell_offset;
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301723 u16 prod, pi;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301724
1725 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001726 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301727 if (header) {
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301728 pasync_sge = pasync_ctx->async_header.ring_base;
1729 pi = pasync_ctx->async_header.pi;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001730 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1731 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301732 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301733 } else {
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301734 pasync_sge = pasync_ctx->async_data.ring_base;
1735 pi = pasync_ctx->async_data.pi;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001736 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1737 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301738 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301739 }
1740
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301741 for (prod = 0; prod < nbuf; prod++) {
1742 if (header)
1743 pasync_handle = pasync_ctx->async_entry[pi].header;
1744 else
1745 pasync_handle = pasync_ctx->async_entry[pi].data;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301746 WARN_ON(pasync_handle->is_header != header);
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301747 WARN_ON(pasync_handle->index != pi);
1748 /* setup the ring only once */
1749 if (nbuf == pasync_ctx->num_entries) {
1750 /* note hi is lo */
1751 pasync_sge[pi].hi = pasync_handle->pa.u.a32.address_lo;
1752 pasync_sge[pi].lo = pasync_handle->pa.u.a32.address_hi;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301753 }
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301754 if (++pi == pasync_ctx->num_entries)
1755 pi = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301756 }
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301757
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301758 if (header)
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301759 pasync_ctx->async_header.pi = pi;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301760 else
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301761 pasync_ctx->async_data.pi = pi;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301762
1763 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1764 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1765 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1766 doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
1767 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301768}
1769
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301770static void
1771beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
1772 struct i_t_dpdu_cqe *pdpdu_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301773{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301774 struct beiscsi_hba *phba = beiscsi_conn->phba;
1775 struct hd_async_handle *pasync_handle = NULL;
1776 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301777 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301778 u8 ulp_num, consumed, header = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301779 u16 cid_cri;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301780
1781 phwi_ctrlr = phba->phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301782 cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
1783 ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
1784 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1785 pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05301786 pdpdu_cqe, &header);
1787 if (is_chip_be2_be3r(phba))
1788 consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1789 num_cons, pdpdu_cqe);
1790 else
1791 consumed = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1792 num_cons, pdpdu_cqe);
1793 if (pasync_handle)
1794 beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
1795 /* num_cons indicates number of 8 RQEs consumed */
1796 if (consumed)
1797 beiscsi_hdq_post_handles(phba, header, ulp_num, 8 * consumed);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301798}
1799
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301800void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301801{
1802 struct be_queue_info *mcc_cq;
1803 struct be_mcc_compl *mcc_compl;
1804 unsigned int num_processed = 0;
1805
1806 mcc_cq = &phba->ctrl.mcc_obj.cq;
1807 mcc_compl = queue_tail_node(mcc_cq);
1808 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1809 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301810 if (beiscsi_hba_in_error(phba))
1811 return;
1812
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301813 if (num_processed >= 32) {
1814 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301815 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301816 num_processed = 0;
1817 }
1818 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05301819 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301820 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301821 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301822 }
1823
1824 mcc_compl->flags = 0;
1825 queue_tail_inc(mcc_cq);
1826 mcc_compl = queue_tail_node(mcc_cq);
1827 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1828 num_processed++;
1829 }
1830
1831 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301832 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301833}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301834
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301835static void beiscsi_mcc_work(struct work_struct *work)
1836{
1837 struct be_eq_obj *pbe_eq;
1838 struct beiscsi_hba *phba;
1839
1840 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1841 phba = pbe_eq->phba;
1842 beiscsi_process_mcc_cq(phba);
1843 /* rearm EQ for further interrupts */
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301844 if (!beiscsi_hba_in_error(phba))
1845 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301846}
1847
John Soni Jose6763daa2012-10-20 04:41:45 +05301848/**
1849 * beiscsi_process_cq()- Process the Completion Queue
1850 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301851 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05301852 *
1853 * return
1854 * Number of Completion Entries processed.
1855 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301856unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301857{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301858 struct be_queue_info *cq;
1859 struct sol_cqe *sol;
1860 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301861 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301862 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301863 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001864 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301865 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301866 struct beiscsi_endpoint *beiscsi_ep;
1867 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301868 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301869
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301870 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301871 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301872 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301873
1874 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1875 CQE_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301876 if (beiscsi_hba_in_error(phba))
1877 return 0;
1878
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301879 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1880
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05301881 code = (sol->dw[offsetof(struct amap_sol_cqe, code) / 32] &
1882 CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301883
John Soni Jose73133262012-10-20 04:44:49 +05301884 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001885 if (is_chip_be2_be3r(phba)) {
1886 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1887 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301888 if ((code == DRIVERMSG_NOTIFY) ||
1889 (code == UNSOL_HDR_NOTIFY) ||
1890 (code == UNSOL_DATA_NOTIFY))
1891 cid = AMAP_GET_BITS(
1892 struct amap_i_t_dpdu_cqe_v2,
1893 cid, sol);
1894 else
1895 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1896 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001897 }
John Soni Jose73133262012-10-20 04:44:49 +05301898
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001899 cri_index = BE_GET_CRI_FROM_CID(cid);
1900 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04001901
1902 if (ep == NULL) {
1903 /* connection has already been freed
1904 * just move on to next one
1905 */
1906 beiscsi_log(phba, KERN_WARNING,
1907 BEISCSI_LOG_INIT,
1908 "BM_%d : proc cqe of disconn ep: cid %d\n",
1909 cid);
1910 goto proc_next_cqe;
1911 }
1912
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301913 beiscsi_ep = ep->dd_data;
1914 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301915
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301916 /* replenish cq */
1917 if (num_processed == 32) {
1918 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301919 num_processed = 0;
1920 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301921 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301922
John Soni Jose0a513dd2012-08-20 23:00:55 +05301923 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301924 case SOL_CMD_COMPLETE:
1925 hwi_complete_cmd(beiscsi_conn, phba, sol);
1926 break;
1927 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301928 beiscsi_log(phba, KERN_INFO,
1929 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301930 "BM_%d : Received %s[%d] on CID : %d\n",
1931 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301932
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301933 dmsg = (struct dmsg_cqe *)sol;
1934 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1935 break;
1936 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301937 beiscsi_log(phba, KERN_INFO,
1938 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301939 "BM_%d : Received %s[%d] on CID : %d\n",
1940 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301941
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001942 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301943 beiscsi_hdq_process_compl(beiscsi_conn,
1944 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001945 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301946 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301947 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301948 beiscsi_log(phba, KERN_INFO,
1949 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301950 "BM_%d : Received %s[%d] on CID : %d\n",
1951 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301952
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001953 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301954 beiscsi_hdq_process_compl(beiscsi_conn,
1955 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001956 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301957 break;
1958 case CXN_INVALIDATE_INDEX_NOTIFY:
1959 case CMD_INVALIDATED_NOTIFY:
1960 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301961 beiscsi_log(phba, KERN_ERR,
1962 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301963 "BM_%d : Ignoring %s[%d] on CID : %d\n",
1964 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301965 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301966 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301967 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301968 beiscsi_log(phba, KERN_ERR,
1969 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1970 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1971 cqe_desc[code], code, cid);
1972 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301973 case CMD_KILLED_INVALID_STATSN_RCVD:
1974 case CMD_KILLED_INVALID_R2T_RCVD:
1975 case CMD_CXN_KILLED_LUN_INVALID:
1976 case CMD_CXN_KILLED_ICD_INVALID:
1977 case CMD_CXN_KILLED_ITT_INVALID:
1978 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1979 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301980 beiscsi_log(phba, KERN_ERR,
1981 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301982 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1983 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301984 break;
1985 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301986 beiscsi_log(phba, KERN_ERR,
1987 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301988 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
1989 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001990 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301991 /* driver consumes the entry and drops the contents */
1992 beiscsi_hdq_process_compl(beiscsi_conn,
1993 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001994 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301995 break;
1996 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1997 case CXN_KILLED_BURST_LEN_MISMATCH:
1998 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301999 case CXN_KILLED_UNKNOWN_HDR:
2000 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2001 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2002 case CXN_KILLED_TIMED_OUT:
2003 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302004 case CXN_KILLED_RST_SENT:
2005 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302006 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2007 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2008 case CXN_KILLED_OVER_RUN_RESIDUAL:
2009 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2010 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302011 beiscsi_log(phba, KERN_ERR,
2012 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302013 "BM_%d : Event %s[%d] received on CID : %d\n",
2014 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302015 if (beiscsi_conn)
2016 iscsi_conn_failure(beiscsi_conn->conn,
2017 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 break;
2019 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302020 beiscsi_log(phba, KERN_ERR,
2021 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302022 "BM_%d : Invalid CQE Event Received Code : %d"
2023 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302024 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302025 break;
2026 }
2027
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002028proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302029 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2030 queue_tail_inc(cq);
2031 sol = queue_tail_node(cq);
2032 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302033 if (total == budget)
2034 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302035 }
2036
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302037 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2038 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302039}
2040
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002041static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302042{
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302043 unsigned int ret, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302044 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302045 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302046 struct be_eq_entry *eqe = NULL;
2047 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302048
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302049 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302050 phba = pbe_eq->phba;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302051 if (beiscsi_hba_in_error(phba)) {
2052 irq_poll_complete(iop);
2053 return 0;
2054 }
2055
2056 io_events = 0;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302057 eq = &pbe_eq->q;
2058 eqe = queue_tail_node(eq);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302059 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2060 EQE_VALID_MASK) {
2061 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2062 queue_tail_inc(eq);
2063 eqe = queue_tail_node(eq);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302064 io_events++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302065 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302066 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302067
2068 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002069 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302070 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002071 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302072 beiscsi_log(phba, KERN_INFO,
2073 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302074 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2075 pbe_eq->q.id, ret);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302076 if (!beiscsi_hba_in_error(phba))
2077 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302078 }
2079 return ret;
2080}
2081
2082static void
John Soni Jose09a10932012-10-20 04:44:23 +05302083hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2084 unsigned int num_sg, struct beiscsi_io_task *io_task)
2085{
2086 struct iscsi_sge *psgl;
2087 unsigned int sg_len, index;
2088 unsigned int sge_len = 0;
2089 unsigned long long addr;
2090 struct scatterlist *l_sg;
2091 unsigned int offset;
2092
2093 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2094 io_task->bhs_pa.u.a32.address_lo);
2095 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2096 io_task->bhs_pa.u.a32.address_hi);
2097
2098 l_sg = sg;
2099 for (index = 0; (index < num_sg) && (index < 2); index++,
2100 sg = sg_next(sg)) {
2101 if (index == 0) {
2102 sg_len = sg_dma_len(sg);
2103 addr = (u64) sg_dma_address(sg);
2104 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2105 sge0_addr_lo, pwrb,
2106 lower_32_bits(addr));
2107 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2108 sge0_addr_hi, pwrb,
2109 upper_32_bits(addr));
2110 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2111 sge0_len, pwrb,
2112 sg_len);
2113 sge_len = sg_len;
2114 } else {
2115 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2116 pwrb, sge_len);
2117 sg_len = sg_dma_len(sg);
2118 addr = (u64) sg_dma_address(sg);
2119 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2120 sge1_addr_lo, pwrb,
2121 lower_32_bits(addr));
2122 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2123 sge1_addr_hi, pwrb,
2124 upper_32_bits(addr));
2125 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2126 sge1_len, pwrb,
2127 sg_len);
2128 }
2129 }
2130 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2131 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2132
2133 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2134
2135 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2136 io_task->bhs_pa.u.a32.address_hi);
2137 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2138 io_task->bhs_pa.u.a32.address_lo);
2139
2140 if (num_sg == 1) {
2141 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2142 1);
2143 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2144 0);
2145 } else if (num_sg == 2) {
2146 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2147 0);
2148 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2149 1);
2150 } else {
2151 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2152 0);
2153 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2154 0);
2155 }
2156
2157 sg = l_sg;
2158 psgl++;
2159 psgl++;
2160 offset = 0;
2161 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2162 sg_len = sg_dma_len(sg);
2163 addr = (u64) sg_dma_address(sg);
2164 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2165 lower_32_bits(addr));
2166 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2167 upper_32_bits(addr));
2168 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2169 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2170 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2171 offset += sg_len;
2172 }
2173 psgl--;
2174 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2175}
2176
2177static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302178hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2179 unsigned int num_sg, struct beiscsi_io_task *io_task)
2180{
2181 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302182 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302183 unsigned int sge_len = 0;
2184 unsigned long long addr;
2185 struct scatterlist *l_sg;
2186 unsigned int offset;
2187
2188 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2189 io_task->bhs_pa.u.a32.address_lo);
2190 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2191 io_task->bhs_pa.u.a32.address_hi);
2192
2193 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302194 for (index = 0; (index < num_sg) && (index < 2); index++,
2195 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302196 if (index == 0) {
2197 sg_len = sg_dma_len(sg);
2198 addr = (u64) sg_dma_address(sg);
2199 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302200 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302201 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302202 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302203 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2204 sg_len);
2205 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302206 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302207 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2208 pwrb, sge_len);
2209 sg_len = sg_dma_len(sg);
2210 addr = (u64) sg_dma_address(sg);
2211 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302212 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302213 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302214 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302215 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2216 sg_len);
2217 }
2218 }
2219 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2220 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2221
2222 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2223
2224 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2225 io_task->bhs_pa.u.a32.address_hi);
2226 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2227 io_task->bhs_pa.u.a32.address_lo);
2228
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302229 if (num_sg == 1) {
2230 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2231 1);
2232 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2233 0);
2234 } else if (num_sg == 2) {
2235 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2236 0);
2237 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2238 1);
2239 } else {
2240 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2241 0);
2242 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2243 0);
2244 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302245 sg = l_sg;
2246 psgl++;
2247 psgl++;
2248 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302249 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302250 sg_len = sg_dma_len(sg);
2251 addr = (u64) sg_dma_address(sg);
2252 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2253 (addr & 0xFFFFFFFF));
2254 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2255 (addr >> 32));
2256 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2257 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2258 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2259 offset += sg_len;
2260 }
2261 psgl--;
2262 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2263}
2264
John Soni Josed629c472012-10-20 04:42:00 +05302265/**
2266 * hwi_write_buffer()- Populate the WRB with task info
2267 * @pwrb: ptr to the WRB entry
2268 * @task: iscsi task which is to be executed
2269 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002270static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302271{
2272 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302273 struct beiscsi_io_task *io_task = task->dd_data;
2274 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2275 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302276 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302277
2278 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2279 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2280 io_task->bhs_pa.u.a32.address_lo);
2281 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2282 io_task->bhs_pa.u.a32.address_hi);
2283
2284 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302285
2286 /* Check for the data_count */
2287 dsp_value = (task->data_count) ? 1 : 0;
2288
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002289 if (is_chip_be2_be3r(phba))
2290 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302291 pwrb, dsp_value);
2292 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002293 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302294 pwrb, dsp_value);
2295
2296 /* Map addr only if there is data_count */
2297 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302298 io_task->mtask_addr = pci_map_single(phba->pcidev,
2299 task->data,
2300 task->data_count,
2301 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002302 if (pci_dma_mapping_error(phba->pcidev,
2303 io_task->mtask_addr))
2304 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302305 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302306 } else
John Soni Josed629c472012-10-20 04:42:00 +05302307 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302308
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302309 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302310 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302311 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302312 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302313 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2314 task->data_count);
2315
2316 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2317 } else {
2318 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302319 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302320 }
2321
2322 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2323
2324 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2325
2326 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2327 io_task->bhs_pa.u.a32.address_hi);
2328 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2329 io_task->bhs_pa.u.a32.address_lo);
2330 if (task->data) {
2331 psgl++;
2332 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2333 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2334 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2335 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2336 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2337 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2338
2339 psgl++;
2340 if (task->data) {
2341 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302342 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302343 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302344 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302345 }
2346 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2347 }
2348 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002349 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302350}
2351
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002352/**
2353 * beiscsi_find_mem_req()- Find mem needed
2354 * @phba: ptr to HBA struct
2355 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302356static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2357{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002358 uint8_t mem_descr_index, ulp_num;
Jitendra Bhivarefa1261c2016-12-13 15:56:01 +05302359 unsigned int num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302360 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2361 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2362
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302363 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2364
2365 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2366 BE_ISCSI_PDU_HEADER_SIZE;
2367 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2368 sizeof(struct hwi_context_memory);
2369
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302370
2371 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2372 * (phba->params.wrbs_per_cxn)
2373 * phba->params.cxns_per_ctrl;
2374 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2375 (phba->params.wrbs_per_cxn);
2376 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2377 phba->params.cxns_per_ctrl);
2378
2379 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2380 phba->params.icds_per_ctrl;
2381 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2382 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002383 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2384 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302385
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002386 num_async_pdu_buf_sgl_pages =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302387 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002388 phba, ulp_num) *
2389 sizeof(struct phys_addr));
2390
2391 num_async_pdu_buf_pages =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302392 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002393 phba, ulp_num) *
2394 phba->params.defpdu_hdr_sz);
2395
2396 num_async_pdu_data_pages =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302397 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002398 phba, ulp_num) *
2399 phba->params.defpdu_data_sz);
2400
2401 num_async_pdu_data_sgl_pages =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302402 PAGES_REQUIRED(BEISCSI_ASYNC_HDQ_SIZE(
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002403 phba, ulp_num) *
2404 sizeof(struct phys_addr));
2405
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002406 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2407 (ulp_num * MEM_DESCR_OFFSET));
2408 phba->mem_req[mem_descr_index] =
2409 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2410 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2411
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002412 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2413 (ulp_num * MEM_DESCR_OFFSET));
2414 phba->mem_req[mem_descr_index] =
2415 num_async_pdu_buf_pages *
2416 PAGE_SIZE;
2417
2418 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2419 (ulp_num * MEM_DESCR_OFFSET));
2420 phba->mem_req[mem_descr_index] =
2421 num_async_pdu_data_pages *
2422 PAGE_SIZE;
2423
2424 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2425 (ulp_num * MEM_DESCR_OFFSET));
2426 phba->mem_req[mem_descr_index] =
2427 num_async_pdu_buf_sgl_pages *
2428 PAGE_SIZE;
2429
2430 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2431 (ulp_num * MEM_DESCR_OFFSET));
2432 phba->mem_req[mem_descr_index] =
2433 num_async_pdu_data_sgl_pages *
2434 PAGE_SIZE;
2435
2436 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2437 (ulp_num * MEM_DESCR_OFFSET));
2438 phba->mem_req[mem_descr_index] =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302439 BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2440 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002441
2442 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2443 (ulp_num * MEM_DESCR_OFFSET));
2444 phba->mem_req[mem_descr_index] =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302445 BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2446 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002447
2448 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2449 (ulp_num * MEM_DESCR_OFFSET));
2450 phba->mem_req[mem_descr_index] =
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302451 sizeof(struct hd_async_context) +
2452 (BEISCSI_ASYNC_HDQ_SIZE(phba, ulp_num) *
2453 sizeof(struct hd_async_entry));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002454 }
2455 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302456}
2457
2458static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2459{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302460 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002461 struct hwi_controller *phwi_ctrlr;
2462 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302463 struct mem_array *mem_arr, *mem_arr_orig;
2464 unsigned int i, j, alloc_size, curr_alloc_size;
2465
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002466 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302467 if (!phba->phwi_ctrlr)
2468 return -ENOMEM;
2469
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002470 /* Allocate memory for wrb_context */
2471 phwi_ctrlr = phba->phwi_ctrlr;
2472 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2473 phba->params.cxns_per_ctrl,
2474 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002475 if (!phwi_ctrlr->wrb_context) {
2476 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002477 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002478 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002479
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302480 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2481 GFP_KERNEL);
2482 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002483 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302484 kfree(phba->phwi_ctrlr);
2485 return -ENOMEM;
2486 }
2487
2488 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2489 GFP_KERNEL);
2490 if (!mem_arr_orig) {
2491 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002492 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302493 kfree(phba->phwi_ctrlr);
2494 return -ENOMEM;
2495 }
2496
2497 mem_descr = phba->init_mem;
2498 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002499 if (!phba->mem_req[i]) {
2500 mem_descr->mem_array = NULL;
2501 mem_descr++;
2502 continue;
2503 }
2504
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302505 j = 0;
2506 mem_arr = mem_arr_orig;
2507 alloc_size = phba->mem_req[i];
2508 memset(mem_arr, 0, sizeof(struct mem_array) *
2509 BEISCSI_MAX_FRAGS_INIT);
2510 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2511 do {
2512 mem_arr->virtual_address = pci_alloc_consistent(
2513 phba->pcidev,
2514 curr_alloc_size,
2515 &bus_add);
2516 if (!mem_arr->virtual_address) {
2517 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2518 goto free_mem;
2519 if (curr_alloc_size -
2520 rounddown_pow_of_two(curr_alloc_size))
2521 curr_alloc_size = rounddown_pow_of_two
2522 (curr_alloc_size);
2523 else
2524 curr_alloc_size = curr_alloc_size / 2;
2525 } else {
2526 mem_arr->bus_address.u.
2527 a64.address = (__u64) bus_add;
2528 mem_arr->size = curr_alloc_size;
2529 alloc_size -= curr_alloc_size;
2530 curr_alloc_size = min(be_max_phys_size *
2531 1024, alloc_size);
2532 j++;
2533 mem_arr++;
2534 }
2535 } while (alloc_size);
2536 mem_descr->num_elements = j;
2537 mem_descr->size_in_bytes = phba->mem_req[i];
2538 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2539 GFP_KERNEL);
2540 if (!mem_descr->mem_array)
2541 goto free_mem;
2542
2543 memcpy(mem_descr->mem_array, mem_arr_orig,
2544 sizeof(struct mem_array) * j);
2545 mem_descr++;
2546 }
2547 kfree(mem_arr_orig);
2548 return 0;
2549free_mem:
2550 mem_descr->num_elements = j;
2551 while ((i) || (j)) {
2552 for (j = mem_descr->num_elements; j > 0; j--) {
2553 pci_free_consistent(phba->pcidev,
2554 mem_descr->mem_array[j - 1].size,
2555 mem_descr->mem_array[j - 1].
2556 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302557 (unsigned long)mem_descr->
2558 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302559 bus_address.u.a64.address);
2560 }
2561 if (i) {
2562 i--;
2563 kfree(mem_descr->mem_array);
2564 mem_descr--;
2565 }
2566 }
2567 kfree(mem_arr_orig);
2568 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002569 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302570 kfree(phba->phwi_ctrlr);
2571 return -ENOMEM;
2572}
2573
2574static int beiscsi_get_memory(struct beiscsi_hba *phba)
2575{
2576 beiscsi_find_mem_req(phba);
2577 return beiscsi_alloc_mem(phba);
2578}
2579
2580static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2581{
2582 struct pdu_data_out *pdata_out;
2583 struct pdu_nop_out *pnop_out;
2584 struct be_mem_descriptor *mem_descr;
2585
2586 mem_descr = phba->init_mem;
2587 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2588 pdata_out =
2589 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2590 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2591
2592 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2593 IIOC_SCSI_DATA);
2594
2595 pnop_out =
2596 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2597 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2598
2599 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2600 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2601 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2602 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2603}
2604
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002605static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302606{
2607 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002608 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002609 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302610 struct hwi_controller *phwi_ctrlr;
2611 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002612 struct iscsi_wrb *pwrb = NULL;
2613 unsigned int num_cxn_wrbh = 0;
2614 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302615
2616 mem_descr_wrbh = phba->init_mem;
2617 mem_descr_wrbh += HWI_MEM_WRBH;
2618
2619 mem_descr_wrb = phba->init_mem;
2620 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302621 phwi_ctrlr = phba->phwi_ctrlr;
2622
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002623 /* Allocate memory for WRBQ */
2624 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2625 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002626 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002627 GFP_KERNEL);
2628 if (!phwi_ctxt->be_wrbq) {
2629 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2630 "BM_%d : WRBQ Mem Alloc Failed\n");
2631 return -ENOMEM;
2632 }
2633
2634 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302635 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302636 pwrb_context->pwrb_handle_base =
2637 kzalloc(sizeof(struct wrb_handle *) *
2638 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002639 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302640 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2641 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002642 goto init_wrb_hndl_failed;
2643 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302644 pwrb_context->pwrb_handle_basestd =
2645 kzalloc(sizeof(struct wrb_handle *) *
2646 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002647 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302648 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2649 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002650 goto init_wrb_hndl_failed;
2651 }
2652 if (!num_cxn_wrbh) {
2653 pwrb_handle =
2654 mem_descr_wrbh->mem_array[idx].virtual_address;
2655 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2656 ((sizeof(struct wrb_handle)) *
2657 phba->params.wrbs_per_cxn));
2658 idx++;
2659 }
2660 pwrb_context->alloc_index = 0;
2661 pwrb_context->wrb_handles_available = 0;
2662 pwrb_context->free_index = 0;
2663
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302664 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302665 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2666 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2667 pwrb_context->pwrb_handle_basestd[j] =
2668 pwrb_handle;
2669 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302670 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302671 pwrb_handle++;
2672 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302673 num_cxn_wrbh--;
2674 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302675 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302676 }
2677 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002678 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302679 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002680 if (!num_cxn_wrb) {
2681 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2682 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2683 ((sizeof(struct iscsi_wrb) *
2684 phba->params.wrbs_per_cxn));
2685 idx++;
2686 }
2687
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302688 if (num_cxn_wrb) {
2689 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2690 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2691 pwrb_handle->pwrb = pwrb;
2692 pwrb++;
2693 }
2694 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302695 }
2696 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002697 return 0;
2698init_wrb_hndl_failed:
2699 for (j = index; j > 0; j--) {
2700 pwrb_context = &phwi_ctrlr->wrb_context[j];
2701 kfree(pwrb_context->pwrb_handle_base);
2702 kfree(pwrb_context->pwrb_handle_basestd);
2703 }
2704 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302705}
2706
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002707static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302708{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002709 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302710 struct hwi_controller *phwi_ctrlr;
2711 struct hba_parameters *p = &phba->params;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302712 struct hd_async_context *pasync_ctx;
2713 struct hd_async_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002714 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302715 struct be_mem_descriptor *mem_descr;
2716
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002717 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2718 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jitendra Bhivared7401052016-12-13 15:55:59 +05302719 /* get async_ctx for each ULP */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002720 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2721 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2722 (ulp_num * MEM_DESCR_OFFSET));
2723
2724 phwi_ctrlr = phba->phwi_ctrlr;
2725 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302726 (struct hd_async_context *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002727 mem_descr->mem_array[0].virtual_address;
2728
2729 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2730 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2731
2732 pasync_ctx->async_entry =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302733 (struct hd_async_entry *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002734 ((long unsigned int)pasync_ctx +
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302735 sizeof(struct hd_async_context));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002736
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302737 pasync_ctx->num_entries = BEISCSI_ASYNC_HDQ_SIZE(phba,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002738 ulp_num);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302739 /* setup header buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002740 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2741 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2742 (ulp_num * MEM_DESCR_OFFSET);
2743 if (mem_descr->mem_array[0].virtual_address) {
2744 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2745 "BM_%d : hwi_init_async_pdu_ctx"
2746 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2747 ulp_num,
2748 mem_descr->mem_array[0].
2749 virtual_address);
2750 } else
2751 beiscsi_log(phba, KERN_WARNING,
2752 BEISCSI_LOG_INIT,
2753 "BM_%d : No Virtual address for ULP : %d\n",
2754 ulp_num);
2755
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05302756 pasync_ctx->async_header.pi = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302757 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002758 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302759 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302760
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002761 pasync_ctx->async_header.pa_base.u.a64.address =
2762 mem_descr->mem_array[0].
2763 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002764
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302765 /* setup header buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002766 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2767 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2768 (ulp_num * MEM_DESCR_OFFSET);
2769 if (mem_descr->mem_array[0].virtual_address) {
2770 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2771 "BM_%d : hwi_init_async_pdu_ctx"
2772 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2773 ulp_num,
2774 mem_descr->mem_array[0].
2775 virtual_address);
2776 } else
2777 beiscsi_log(phba, KERN_WARNING,
2778 BEISCSI_LOG_INIT,
2779 "BM_%d : No Virtual address for ULP : %d\n",
2780 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302781
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002782 pasync_ctx->async_header.ring_base =
2783 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302784
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302785 /* setup header buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002786 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2787 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2788 (ulp_num * MEM_DESCR_OFFSET);
2789 if (mem_descr->mem_array[0].virtual_address) {
2790 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2791 "BM_%d : hwi_init_async_pdu_ctx"
2792 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2793 ulp_num,
2794 mem_descr->mem_array[0].
2795 virtual_address);
2796 } else
2797 beiscsi_log(phba, KERN_WARNING,
2798 BEISCSI_LOG_INIT,
2799 "BM_%d : No Virtual address for ULP : %d\n",
2800 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302801
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002802 pasync_ctx->async_header.handle_base =
2803 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302804
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302805 /* setup data buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002806 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2807 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2808 (ulp_num * MEM_DESCR_OFFSET);
2809 if (mem_descr->mem_array[0].virtual_address) {
2810 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2811 "BM_%d : hwi_init_async_pdu_ctx"
2812 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2813 ulp_num,
2814 mem_descr->mem_array[0].
2815 virtual_address);
2816 } else
2817 beiscsi_log(phba, KERN_WARNING,
2818 BEISCSI_LOG_INIT,
2819 "BM_%d : No Virtual address for ULP : %d\n",
2820 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302821
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002822 pasync_ctx->async_data.ring_base =
2823 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302824
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302825 /* setup data buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002826 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2827 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2828 (ulp_num * MEM_DESCR_OFFSET);
2829 if (!mem_descr->mem_array[0].virtual_address)
2830 beiscsi_log(phba, KERN_WARNING,
2831 BEISCSI_LOG_INIT,
2832 "BM_%d : No Virtual address for ULP : %d\n",
2833 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302834
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002835 pasync_ctx->async_data.handle_base =
2836 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302837
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002838 pasync_header_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302839 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002840 pasync_ctx->async_header.handle_base;
2841 pasync_data_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302842 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002843 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302844
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302845 /* setup data buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002846 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2847 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2848 (ulp_num * MEM_DESCR_OFFSET);
2849 if (mem_descr->mem_array[0].virtual_address) {
2850 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2851 "BM_%d : hwi_init_async_pdu_ctx"
2852 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2853 ulp_num,
2854 mem_descr->mem_array[0].
2855 virtual_address);
2856 } else
2857 beiscsi_log(phba, KERN_WARNING,
2858 BEISCSI_LOG_INIT,
2859 "BM_%d : No Virtual address for ULP : %d\n",
2860 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302861
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002862 idx = 0;
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05302863 pasync_ctx->async_data.pi = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302864 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002865 pasync_ctx->async_data.va_base =
2866 mem_descr->mem_array[idx].virtual_address;
2867 pasync_ctx->async_data.pa_base.u.a64.address =
2868 mem_descr->mem_array[idx].
2869 bus_address.u.a64.address;
2870
2871 num_async_data = ((mem_descr->mem_array[idx].size) /
2872 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002873 num_per_mem = 0;
2874
Jitendra Bhivarefecc3822017-03-24 14:11:44 +05302875 for (index = 0; index < BEISCSI_ASYNC_HDQ_SIZE
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002876 (phba, ulp_num); index++) {
2877 pasync_header_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302878 pasync_header_h->is_header = 1;
2879 pasync_header_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002880 INIT_LIST_HEAD(&pasync_header_h->link);
2881 pasync_header_h->pbuffer =
2882 (void *)((unsigned long)
2883 (pasync_ctx->
2884 async_header.va_base) +
2885 (p->defpdu_hdr_sz * index));
2886
2887 pasync_header_h->pa.u.a64.address =
2888 pasync_ctx->async_header.pa_base.u.a64.
2889 address + (p->defpdu_hdr_sz * index);
2890
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05302891 pasync_ctx->async_entry[index].header =
2892 pasync_header_h;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002893 pasync_header_h++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302894 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2895 wq.list);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002896
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002897 pasync_data_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302898 pasync_data_h->is_header = 0;
2899 pasync_data_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002900 INIT_LIST_HEAD(&pasync_data_h->link);
2901
2902 if (!num_async_data) {
2903 num_per_mem = 0;
2904 idx++;
2905 pasync_ctx->async_data.va_base =
2906 mem_descr->mem_array[idx].
2907 virtual_address;
2908 pasync_ctx->async_data.pa_base.u.
2909 a64.address =
2910 mem_descr->mem_array[idx].
2911 bus_address.u.a64.address;
2912 num_async_data =
2913 ((mem_descr->mem_array[idx].
2914 size) /
2915 phba->params.defpdu_data_sz);
2916 }
2917 pasync_data_h->pbuffer =
2918 (void *)((unsigned long)
2919 (pasync_ctx->async_data.va_base) +
2920 (p->defpdu_data_sz * num_per_mem));
2921
2922 pasync_data_h->pa.u.a64.address =
2923 pasync_ctx->async_data.pa_base.u.a64.
2924 address + (p->defpdu_data_sz *
2925 num_per_mem);
2926 num_per_mem++;
2927 num_async_data--;
2928
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05302929 pasync_ctx->async_entry[index].data =
2930 pasync_data_h;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002931 pasync_data_h++;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002932 }
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002933 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302934 }
2935
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002936 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302937}
2938
2939static int
2940be_sgl_create_contiguous(void *virtual_address,
2941 u64 physical_address, u32 length,
2942 struct be_dma_mem *sgl)
2943{
2944 WARN_ON(!virtual_address);
2945 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06002946 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302947 WARN_ON(!sgl);
2948
2949 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302950 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302951 sgl->size = length;
2952
2953 return 0;
2954}
2955
2956static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2957{
2958 memset(sgl, 0, sizeof(*sgl));
2959}
2960
2961static void
2962hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2963 struct mem_array *pmem, struct be_dma_mem *sgl)
2964{
2965 if (sgl->va)
2966 be_sgl_destroy_contiguous(sgl);
2967
2968 be_sgl_create_contiguous(pmem->virtual_address,
2969 pmem->bus_address.u.a64.address,
2970 pmem->size, sgl);
2971}
2972
2973static void
2974hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2975 struct mem_array *pmem, struct be_dma_mem *sgl)
2976{
2977 if (sgl->va)
2978 be_sgl_destroy_contiguous(sgl);
2979
2980 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2981 pmem->bus_address.u.a64.address,
2982 pmem->size, sgl);
2983}
2984
2985static int be_fill_queue(struct be_queue_info *q,
2986 u16 len, u16 entry_size, void *vaddress)
2987{
2988 struct be_dma_mem *mem = &q->dma_mem;
2989
2990 memset(q, 0, sizeof(*q));
2991 q->len = len;
2992 q->entry_size = entry_size;
2993 mem->size = len * entry_size;
2994 mem->va = vaddress;
2995 if (!mem->va)
2996 return -ENOMEM;
2997 memset(mem->va, 0, mem->size);
2998 return 0;
2999}
3000
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303001static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303002 struct hwi_context_memory *phwi_context)
3003{
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303004 int ret = -ENOMEM, eq_for_mcc;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303005 unsigned int i, num_eq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303006 struct be_queue_info *eq;
3007 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303008 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303009 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303010
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303011 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3012 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303013
Christoph Hellwig83148862017-01-13 17:29:48 +01003014 if (phba->pcidev->msix_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303015 eq_for_mcc = 1;
3016 else
3017 eq_for_mcc = 0;
3018 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3019 eq = &phwi_context->be_eq[i].q;
3020 mem = &eq->dma_mem;
3021 phwi_context->be_eq[i].phba = phba;
3022 eq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303023 num_eq_pages * PAGE_SIZE,
3024 &paddr);
Pan Bian84a261f2016-12-04 13:23:04 +08003025 if (!eq_vaddress) {
3026 ret = -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303027 goto create_eq_error;
Pan Bian84a261f2016-12-04 13:23:04 +08003028 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303029
3030 mem->va = eq_vaddress;
3031 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3032 sizeof(struct be_eq_entry), eq_vaddress);
3033 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303034 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3035 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303036 goto create_eq_error;
3037 }
3038
3039 mem->dma = paddr;
3040 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05303041 BEISCSI_EQ_DELAY_DEF);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303042 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303043 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3044 "BM_%d : beiscsi_cmd_eq_create"
3045 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303046 goto create_eq_error;
3047 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303048
3049 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3050 "BM_%d : eqid = %d\n",
3051 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303052 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303053 return 0;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303054
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303055create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303056 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303057 eq = &phwi_context->be_eq[i].q;
3058 mem = &eq->dma_mem;
3059 if (mem->va)
3060 pci_free_consistent(phba->pcidev, num_eq_pages
3061 * PAGE_SIZE,
3062 mem->va, mem->dma);
3063 }
3064 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303065}
3066
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303067static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303068 struct hwi_context_memory *phwi_context)
3069{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303070 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303071 struct be_queue_info *cq, *eq;
3072 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303073 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303074 void *cq_vaddress;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303075 int ret = -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303076 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303078 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3079 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303080
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303081 for (i = 0; i < phba->num_cpus; i++) {
3082 cq = &phwi_context->be_cq[i];
3083 eq = &phwi_context->be_eq[i].q;
3084 pbe_eq = &phwi_context->be_eq[i];
3085 pbe_eq->cq = cq;
3086 pbe_eq->phba = phba;
3087 mem = &cq->dma_mem;
3088 cq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303089 num_cq_pages * PAGE_SIZE,
3090 &paddr);
Pan Bian29b33252016-12-04 13:21:42 +08003091 if (!cq_vaddress) {
3092 ret = -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303093 goto create_cq_error;
Pan Bian29b33252016-12-04 13:21:42 +08003094 }
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303095
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303096 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303097 sizeof(struct sol_cqe), cq_vaddress);
3098 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303099 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3100 "BM_%d : be_fill_queue Failed "
3101 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303102 goto create_cq_error;
3103 }
3104
3105 mem->dma = paddr;
3106 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3107 false, 0);
3108 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303109 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3110 "BM_%d : beiscsi_cmd_eq_create"
3111 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303112 goto create_cq_error;
3113 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303114 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3115 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3116 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303117 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303118 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303119
3120create_cq_error:
3121 for (i = 0; i < phba->num_cpus; i++) {
3122 cq = &phwi_context->be_cq[i];
3123 mem = &cq->dma_mem;
3124 if (mem->va)
3125 pci_free_consistent(phba->pcidev, num_cq_pages
3126 * PAGE_SIZE,
3127 mem->va, mem->dma);
3128 }
3129 return ret;
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,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003136 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303137{
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;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003146 dq = &phwi_context->be_def_hdrq[ulp_num];
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;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003150 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3151 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303152 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3153 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3154 sizeof(struct phys_addr),
3155 sizeof(struct phys_addr), dq_vaddress);
3156 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303157 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003158 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3159 ulp_num);
3160
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303161 return ret;
3162 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303163 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3164 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303165 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3166 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003167 phba->params.defpdu_hdr_sz,
3168 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303169 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303170 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003171 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3172 ulp_num);
3173
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303174 return ret;
3175 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303176
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003177 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3178 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3179 ulp_num,
3180 phwi_context->be_def_hdrq[ulp_num].id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303181 return 0;
3182}
3183
3184static int
3185beiscsi_create_def_data(struct beiscsi_hba *phba,
3186 struct hwi_context_memory *phwi_context,
3187 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003188 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303189{
3190 unsigned int idx;
3191 int ret;
3192 struct be_queue_info *dataq, *cq;
3193 struct be_dma_mem *mem;
3194 struct be_mem_descriptor *mem_descr;
3195 void *dq_vaddress;
3196
3197 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003198 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303199 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303200 mem = &dataq->dma_mem;
3201 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003202 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3203 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303204 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3205 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3206 sizeof(struct phys_addr),
3207 sizeof(struct phys_addr), dq_vaddress);
3208 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303209 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003210 "BM_%d : be_fill_queue Failed for DEF PDU "
3211 "DATA on ULP : %d\n",
3212 ulp_num);
3213
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303214 return ret;
3215 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303216 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3217 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303218 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3219 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003220 phba->params.defpdu_data_sz,
3221 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303222 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303223 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3224 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003225 " Failed for DEF PDU DATA on ULP : %d\n",
3226 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303227 return ret;
3228 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303229
John Soni Jose99bc5d52012-08-20 23:00:18 +05303230 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003231 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3232 ulp_num,
3233 phwi_context->be_def_dataq[ulp_num].id);
3234
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003235 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3236 "BM_%d : DEFAULT PDU DATA RING CREATED"
3237 "on ULP : %d\n", ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303238 return 0;
3239}
3240
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003241
3242static int
3243beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3244{
3245 struct be_mem_descriptor *mem_descr;
3246 struct mem_array *pm_arr;
3247 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003248 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003249
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003250 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3251 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3252 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3253 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3254 (ulp_num * MEM_DESCR_OFFSET);
3255 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003256
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003257 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3258 status = be_cmd_iscsi_post_template_hdr(
3259 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003260
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003261 if (status != 0) {
3262 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3263 "BM_%d : Post Template HDR Failed for"
3264 "ULP_%d\n", ulp_num);
3265 return status;
3266 }
3267
3268 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3269 "BM_%d : Template HDR Pages Posted for"
3270 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003271 }
3272 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003273 return 0;
3274}
3275
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303276static int
3277beiscsi_post_pages(struct beiscsi_hba *phba)
3278{
3279 struct be_mem_descriptor *mem_descr;
3280 struct mem_array *pm_arr;
3281 unsigned int page_offset, i;
3282 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003283 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303284
3285 mem_descr = phba->init_mem;
3286 mem_descr += HWI_MEM_SGE;
3287 pm_arr = mem_descr->mem_array;
3288
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003289 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3290 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3291 break;
3292
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303293 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003294 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303295 for (i = 0; i < mem_descr->num_elements; i++) {
3296 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3297 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3298 page_offset,
3299 (pm_arr->size / PAGE_SIZE));
3300 page_offset += pm_arr->size / PAGE_SIZE;
3301 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303302 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3303 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303304 return status;
3305 }
3306 pm_arr++;
3307 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303308 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3309 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303310 return 0;
3311}
3312
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303313static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3314{
3315 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003316 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303317 pci_free_consistent(phba->pcidev, mem->size,
3318 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003319 mem->va = NULL;
3320 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303321}
3322
3323static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3324 u16 len, u16 entry_size)
3325{
3326 struct be_dma_mem *mem = &q->dma_mem;
3327
3328 memset(q, 0, sizeof(*q));
3329 q->len = len;
3330 q->entry_size = entry_size;
3331 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003332 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303333 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303334 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303335 return 0;
3336}
3337
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303338static int
3339beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3340 struct hwi_context_memory *phwi_context,
3341 struct hwi_controller *phwi_ctrlr)
3342{
Jitendra Bhivarefa1261c2016-12-13 15:56:01 +05303343 unsigned int num_wrb_rings;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303344 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003345 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303346 struct mem_array *pwrb_arr;
3347 void *wrb_vaddr;
3348 struct be_dma_mem sgl;
3349 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003350 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303351 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003352 uint8_t ulp_count = 0, ulp_base_num = 0;
3353 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303354
3355 idx = 0;
3356 mem_descr = phba->init_mem;
3357 mem_descr += HWI_MEM_WRB;
3358 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3359 GFP_KERNEL);
3360 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303361 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3362 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303363 return -ENOMEM;
3364 }
3365 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3366 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3367 num_wrb_rings = mem_descr->mem_array[idx].size /
3368 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3369
3370 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3371 if (num_wrb_rings) {
3372 pwrb_arr[num].virtual_address = wrb_vaddr;
3373 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3374 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3375 sizeof(struct iscsi_wrb);
3376 wrb_vaddr += pwrb_arr[num].size;
3377 pa_addr_lo += pwrb_arr[num].size;
3378 num_wrb_rings--;
3379 } else {
3380 idx++;
3381 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3382 pa_addr_lo = mem_descr->mem_array[idx].\
3383 bus_address.u.a64.address;
3384 num_wrb_rings = mem_descr->mem_array[idx].size /
3385 (phba->params.wrbs_per_cxn *
3386 sizeof(struct iscsi_wrb));
3387 pwrb_arr[num].virtual_address = wrb_vaddr;
3388 pwrb_arr[num].bus_address.u.a64.address\
3389 = pa_addr_lo;
3390 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3391 sizeof(struct iscsi_wrb);
3392 wrb_vaddr += pwrb_arr[num].size;
3393 pa_addr_lo += pwrb_arr[num].size;
3394 num_wrb_rings--;
3395 }
3396 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003397
3398 /* Get the ULP Count */
3399 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3400 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3401 ulp_count++;
3402 ulp_base_num = ulp_num;
3403 cid_count_ulp[ulp_num] =
3404 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3405 }
3406
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303407 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003408 if (ulp_count > 1) {
3409 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3410
3411 if (!cid_count_ulp[ulp_base_num])
3412 ulp_base_num = (ulp_base_num + 1) %
3413 BEISCSI_ULP_COUNT;
3414
3415 cid_count_ulp[ulp_base_num]--;
3416 }
3417
3418
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303419 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3420 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003421 &phwi_context->be_wrbq[i],
3422 &phwi_ctrlr->wrb_context[i],
3423 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303424 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303425 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3426 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003427 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303428 return status;
3429 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003430 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003431 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303432 }
3433 kfree(pwrb_arr);
3434 return 0;
3435}
3436
3437static void free_wrb_handles(struct beiscsi_hba *phba)
3438{
3439 unsigned int index;
3440 struct hwi_controller *phwi_ctrlr;
3441 struct hwi_wrb_context *pwrb_context;
3442
3443 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003444 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303445 pwrb_context = &phwi_ctrlr->wrb_context[index];
3446 kfree(pwrb_context->pwrb_handle_base);
3447 kfree(pwrb_context->pwrb_handle_basestd);
3448 }
3449}
3450
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303451static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3452{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303453 struct be_ctrl_info *ctrl = &phba->ctrl;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303454 struct be_dma_mem *ptag_mem;
3455 struct be_queue_info *q;
3456 int i, tag;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303457
3458 q = &phba->ctrl.mcc_obj.q;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303459 for (i = 0; i < MAX_MCC_CMD; i++) {
3460 tag = i + 1;
3461 if (!test_bit(MCC_TAG_STATE_RUNNING,
3462 &ctrl->ptag_state[tag].tag_state))
3463 continue;
3464
3465 if (test_bit(MCC_TAG_STATE_TIMEOUT,
3466 &ctrl->ptag_state[tag].tag_state)) {
3467 ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
3468 if (ptag_mem->size) {
3469 pci_free_consistent(ctrl->pdev,
3470 ptag_mem->size,
3471 ptag_mem->va,
3472 ptag_mem->dma);
3473 ptag_mem->size = 0;
3474 }
3475 continue;
3476 }
3477 /**
3478 * If MCC is still active and waiting then wake up the process.
3479 * We are here only because port is going offline. The process
3480 * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
3481 * returned for the operation and allocated memory cleaned up.
3482 */
3483 if (waitqueue_active(&ctrl->mcc_wait[tag])) {
3484 ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
3485 ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
3486 wake_up_interruptible(&ctrl->mcc_wait[tag]);
3487 /*
3488 * Control tag info gets reinitialized in enable
3489 * so wait for the process to clear running state.
3490 */
3491 while (test_bit(MCC_TAG_STATE_RUNNING,
3492 &ctrl->ptag_state[tag].tag_state))
3493 schedule_timeout_uninterruptible(HZ);
3494 }
3495 /**
3496 * For MCC with tag_states MCC_TAG_STATE_ASYNC and
3497 * MCC_TAG_STATE_IGNORE nothing needs to done.
3498 */
3499 }
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303500 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303501 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303502 be_queue_free(phba, q);
3503 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303504
3505 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303506 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303507 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303508 be_queue_free(phba, q);
3509 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303510}
3511
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303512static int be_mcc_queues_create(struct beiscsi_hba *phba,
3513 struct hwi_context_memory *phwi_context)
3514{
3515 struct be_queue_info *q, *cq;
3516 struct be_ctrl_info *ctrl = &phba->ctrl;
3517
3518 /* Alloc MCC compl queue */
3519 cq = &phba->ctrl.mcc_obj.cq;
3520 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3521 sizeof(struct be_mcc_compl)))
3522 goto err;
3523 /* Ask BE to create MCC compl queue; */
Christoph Hellwig83148862017-01-13 17:29:48 +01003524 if (phba->pcidev->msix_enabled) {
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05303525 if (beiscsi_cmd_cq_create(ctrl, cq,
3526 &phwi_context->be_eq[phba->num_cpus].q,
3527 false, true, 0))
3528 goto mcc_cq_free;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303529 } else {
3530 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3531 false, true, 0))
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05303532 goto mcc_cq_free;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303533 }
3534
3535 /* Alloc MCC queue */
3536 q = &phba->ctrl.mcc_obj.q;
3537 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3538 goto mcc_cq_destroy;
3539
3540 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303541 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303542 goto mcc_q_free;
3543
3544 return 0;
3545
3546mcc_q_free:
3547 be_queue_free(phba, q);
3548mcc_cq_destroy:
3549 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3550mcc_cq_free:
3551 be_queue_free(phba, cq);
3552err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303553 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303554}
3555
Christoph Hellwig83148862017-01-13 17:29:48 +01003556static void be2iscsi_enable_msix(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303557{
Christoph Hellwig83148862017-01-13 17:29:48 +01003558 int nvec = 1;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303559
John Soni Jose22abeef2012-10-20 04:43:32 +05303560 switch (phba->generation) {
3561 case BE_GEN2:
3562 case BE_GEN3:
Christoph Hellwig83148862017-01-13 17:29:48 +01003563 nvec = BEISCSI_MAX_NUM_CPUS + 1;
John Soni Jose22abeef2012-10-20 04:43:32 +05303564 break;
3565 case BE_GEN4:
Christoph Hellwig83148862017-01-13 17:29:48 +01003566 nvec = phba->fw_config.eqid_count;
John Soni Jose22abeef2012-10-20 04:43:32 +05303567 break;
3568 default:
Christoph Hellwig83148862017-01-13 17:29:48 +01003569 nvec = 2;
3570 break;
John Soni Jose22abeef2012-10-20 04:43:32 +05303571 }
Christoph Hellwig83148862017-01-13 17:29:48 +01003572
3573 /* if eqid_count == 1 fall back to INTX */
3574 if (enable_msix && nvec > 1) {
3575 const struct irq_affinity desc = { .post_vectors = 1 };
3576
3577 if (pci_alloc_irq_vectors_affinity(phba->pcidev, 2, nvec,
3578 PCI_IRQ_MSIX | PCI_IRQ_AFFINITY, &desc) < 0) {
3579 phba->num_cpus = nvec - 1;
3580 return;
3581 }
3582 }
3583
3584 phba->num_cpus = 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303585}
3586
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303587static void hwi_purge_eq(struct beiscsi_hba *phba)
3588{
3589 struct hwi_controller *phwi_ctrlr;
3590 struct hwi_context_memory *phwi_context;
3591 struct be_queue_info *eq;
3592 struct be_eq_entry *eqe = NULL;
3593 int i, eq_msix;
3594 unsigned int num_processed;
3595
3596 if (beiscsi_hba_in_error(phba))
3597 return;
3598
3599 phwi_ctrlr = phba->phwi_ctrlr;
3600 phwi_context = phwi_ctrlr->phwi_ctxt;
Christoph Hellwig83148862017-01-13 17:29:48 +01003601 if (phba->pcidev->msix_enabled)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303602 eq_msix = 1;
3603 else
3604 eq_msix = 0;
3605
3606 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3607 eq = &phwi_context->be_eq[i].q;
3608 eqe = queue_tail_node(eq);
3609 num_processed = 0;
3610 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3611 & EQE_VALID_MASK) {
3612 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3613 queue_tail_inc(eq);
3614 eqe = queue_tail_node(eq);
3615 num_processed++;
3616 }
3617
3618 if (num_processed)
3619 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
3620 }
3621}
3622
3623static void hwi_cleanup_port(struct beiscsi_hba *phba)
3624{
3625 struct be_queue_info *q;
3626 struct be_ctrl_info *ctrl = &phba->ctrl;
3627 struct hwi_controller *phwi_ctrlr;
3628 struct hwi_context_memory *phwi_context;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303629 int i, eq_for_mcc, ulp_num;
3630
3631 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3632 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3633 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
3634
3635 /**
3636 * Purge all EQ entries that may have been left out. This is to
3637 * workaround a problem we've seen occasionally where driver gets an
3638 * interrupt with EQ entry bit set after stopping the controller.
3639 */
3640 hwi_purge_eq(phba);
3641
3642 phwi_ctrlr = phba->phwi_ctrlr;
3643 phwi_context = phwi_ctrlr->phwi_ctxt;
3644
3645 be_cmd_iscsi_remove_template_hdr(ctrl);
3646
3647 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3648 q = &phwi_context->be_wrbq[i];
3649 if (q->created)
3650 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3651 }
3652 kfree(phwi_context->be_wrbq);
3653 free_wrb_handles(phba);
3654
3655 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3656 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3657
3658 q = &phwi_context->be_def_hdrq[ulp_num];
3659 if (q->created)
3660 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3661
3662 q = &phwi_context->be_def_dataq[ulp_num];
3663 if (q->created)
3664 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303665 }
3666 }
3667
3668 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3669
3670 for (i = 0; i < (phba->num_cpus); i++) {
3671 q = &phwi_context->be_cq[i];
3672 if (q->created) {
3673 be_queue_free(phba, q);
3674 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3675 }
3676 }
3677
3678 be_mcc_queues_destroy(phba);
Christoph Hellwig83148862017-01-13 17:29:48 +01003679 if (phba->pcidev->msix_enabled)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303680 eq_for_mcc = 1;
3681 else
3682 eq_for_mcc = 0;
3683 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3684 q = &phwi_context->be_eq[i].q;
3685 if (q->created) {
3686 be_queue_free(phba, q);
3687 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3688 }
3689 }
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303690 /* this ensures complete FW cleanup */
3691 beiscsi_cmd_function_reset(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303692 /* last communication, indicate driver is unloading */
3693 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
3694}
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303695
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303696static int hwi_init_port(struct beiscsi_hba *phba)
3697{
3698 struct hwi_controller *phwi_ctrlr;
3699 struct hwi_context_memory *phwi_context;
3700 unsigned int def_pdu_ring_sz;
3701 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003702 int status, ulp_num;
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05303703 u16 nbufs;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303704
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303705 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303706 phwi_context = phwi_ctrlr->phwi_ctxt;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303707 /* set port optic state to unknown */
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303708 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303709
3710 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303711 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303712 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3713 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303714 goto error;
3715 }
3716
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303717 status = be_mcc_queues_create(phba, phwi_context);
3718 if (status != 0)
3719 goto error;
3720
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303721 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303722 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303723 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3724 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303725 goto error;
3726 }
3727
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303728 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303729 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303730 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3731 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303732 goto error;
3733 }
3734
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003735 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3736 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05303737 nbufs = phwi_context->pasync_ctx[ulp_num]->num_entries;
3738 def_pdu_ring_sz = nbufs * sizeof(struct phys_addr);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003739
3740 status = beiscsi_create_def_hdr(phba, phwi_context,
3741 phwi_ctrlr,
3742 def_pdu_ring_sz,
3743 ulp_num);
3744 if (status != 0) {
3745 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3746 "BM_%d : Default Header not created for ULP : %d\n",
3747 ulp_num);
3748 goto error;
3749 }
3750
3751 status = beiscsi_create_def_data(phba, phwi_context,
3752 phwi_ctrlr,
3753 def_pdu_ring_sz,
3754 ulp_num);
3755 if (status != 0) {
3756 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3757 "BM_%d : Default Data not created for ULP : %d\n",
3758 ulp_num);
3759 goto error;
3760 }
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303761 /**
3762 * Now that the default PDU rings have been created,
3763 * let EP know about it.
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303764 */
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303765 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05303766 ulp_num, nbufs);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303767 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
Jitendra Bhivare1e2931f2017-03-24 14:11:45 +05303768 ulp_num, nbufs);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003769 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303770 }
3771
3772 status = beiscsi_post_pages(phba);
3773 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303774 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3775 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303776 goto error;
3777 }
3778
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003779 status = beiscsi_post_template_hdr(phba);
3780 if (status != 0) {
3781 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3782 "BM_%d : Template HDR Posting for CXN Failed\n");
3783 }
3784
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303785 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3786 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303787 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3788 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303789 goto error;
3790 }
3791
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003792 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3793 uint16_t async_arr_idx = 0;
3794
3795 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3796 uint16_t cri = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303797 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003798
3799 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3800 phwi_ctrlr, ulp_num);
3801 for (cri = 0; cri <
3802 phba->params.cxns_per_ctrl; cri++) {
3803 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3804 (phwi_ctrlr, cri))
3805 pasync_ctx->cid_to_async_cri_map[
3806 phwi_ctrlr->wrb_context[cri].cid] =
3807 async_arr_idx++;
3808 }
3809 }
3810 }
3811
John Soni Jose99bc5d52012-08-20 23:00:18 +05303812 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3813 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303814 return 0;
3815
3816error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303817 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3818 "BM_%d : hwi_init_port failed");
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303819 hwi_cleanup_port(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003820 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303821}
3822
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303823static int hwi_init_controller(struct beiscsi_hba *phba)
3824{
3825 struct hwi_controller *phwi_ctrlr;
3826
3827 phwi_ctrlr = phba->phwi_ctrlr;
3828 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3829 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3830 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303831 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3832 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3833 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303834 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303835 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3836 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3837 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303838 return -ENOMEM;
3839 }
3840
3841 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003842 if (beiscsi_init_wrb_handle(phba))
3843 return -ENOMEM;
3844
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003845 if (hwi_init_async_pdu_ctx(phba)) {
3846 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3847 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3848 return -ENOMEM;
3849 }
3850
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303851 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303852 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3853 "BM_%d : hwi_init_controller failed\n");
3854
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303855 return -ENOMEM;
3856 }
3857 return 0;
3858}
3859
3860static void beiscsi_free_mem(struct beiscsi_hba *phba)
3861{
3862 struct be_mem_descriptor *mem_descr;
3863 int i, j;
3864
3865 mem_descr = phba->init_mem;
3866 i = 0;
3867 j = 0;
3868 for (i = 0; i < SE_MEM_MAX; i++) {
3869 for (j = mem_descr->num_elements; j > 0; j--) {
3870 pci_free_consistent(phba->pcidev,
3871 mem_descr->mem_array[j - 1].size,
3872 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303873 (unsigned long)mem_descr->mem_array[j - 1].
3874 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303875 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003876
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303877 kfree(mem_descr->mem_array);
3878 mem_descr++;
3879 }
3880 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003881 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303882 kfree(phba->phwi_ctrlr);
3883}
3884
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303885static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3886{
3887 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3888 struct sgl_handle *psgl_handle;
3889 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003890 unsigned int arr_index, i, idx;
3891 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303892
3893 phba->io_sgl_hndl_avbl = 0;
3894 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303895
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303896 mem_descr_sglh = phba->init_mem;
3897 mem_descr_sglh += HWI_MEM_SGLH;
3898 if (1 == mem_descr_sglh->num_elements) {
3899 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3900 phba->params.ios_per_ctrl,
3901 GFP_KERNEL);
3902 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303903 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3904 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303905 return -ENOMEM;
3906 }
3907 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3908 (phba->params.icds_per_ctrl -
3909 phba->params.ios_per_ctrl),
3910 GFP_KERNEL);
3911 if (!phba->eh_sgl_hndl_base) {
3912 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303913 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3914 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303915 return -ENOMEM;
3916 }
3917 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303918 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3919 "BM_%d : HWI_MEM_SGLH is more than one element."
3920 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303921 return -ENOMEM;
3922 }
3923
3924 arr_index = 0;
3925 idx = 0;
3926 while (idx < mem_descr_sglh->num_elements) {
3927 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3928
3929 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3930 sizeof(struct sgl_handle)); i++) {
3931 if (arr_index < phba->params.ios_per_ctrl) {
3932 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3933 phba->io_sgl_hndl_avbl++;
3934 arr_index++;
3935 } else {
3936 phba->eh_sgl_hndl_base[arr_index -
3937 phba->params.ios_per_ctrl] =
3938 psgl_handle;
3939 arr_index++;
3940 phba->eh_sgl_hndl_avbl++;
3941 }
3942 psgl_handle++;
3943 }
3944 idx++;
3945 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303946 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3947 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3948 "phba->eh_sgl_hndl_avbl=%d\n",
3949 phba->io_sgl_hndl_avbl,
3950 phba->eh_sgl_hndl_avbl);
3951
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303952 mem_descr_sg = phba->init_mem;
3953 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303954 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3955 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3956 mem_descr_sg->num_elements);
3957
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003958 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3959 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3960 break;
3961
3962 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
3963
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303964 arr_index = 0;
3965 idx = 0;
3966 while (idx < mem_descr_sg->num_elements) {
3967 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3968
3969 for (i = 0;
3970 i < (mem_descr_sg->mem_array[idx].size) /
3971 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3972 i++) {
3973 if (arr_index < phba->params.ios_per_ctrl)
3974 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3975 else
3976 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3977 phba->params.ios_per_ctrl];
3978 psgl_handle->pfrag = pfrag;
3979 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3980 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3981 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003982 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303983 }
3984 idx++;
3985 }
3986 phba->io_sgl_free_index = 0;
3987 phba->io_sgl_alloc_index = 0;
3988 phba->eh_sgl_free_index = 0;
3989 phba->eh_sgl_alloc_index = 0;
3990 return 0;
3991}
3992
3993static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3994{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07003995 int ret;
3996 uint16_t i, ulp_num;
3997 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303998
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07003999 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4000 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4001 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4002 GFP_KERNEL);
4003
4004 if (!ptr_cid_info) {
4005 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4006 "BM_%d : Failed to allocate memory"
4007 "for ULP_CID_INFO for ULP : %d\n",
4008 ulp_num);
4009 ret = -ENOMEM;
4010 goto free_memory;
4011
4012 }
4013
4014 /* Allocate memory for CID array */
Jitendra Bhivare413f3652016-12-13 15:56:03 +05304015 ptr_cid_info->cid_array =
4016 kcalloc(BEISCSI_GET_CID_COUNT(phba, ulp_num),
4017 sizeof(*ptr_cid_info->cid_array),
4018 GFP_KERNEL);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004019 if (!ptr_cid_info->cid_array) {
4020 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4021 "BM_%d : Failed to allocate memory"
4022 "for CID_ARRAY for ULP : %d\n",
4023 ulp_num);
4024 kfree(ptr_cid_info);
4025 ptr_cid_info = NULL;
4026 ret = -ENOMEM;
4027
4028 goto free_memory;
4029 }
4030 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4031 phba, ulp_num);
4032
4033 /* Save the cid_info_array ptr */
4034 phba->cid_array_info[ulp_num] = ptr_cid_info;
4035 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304036 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304037 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004038 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304039 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304040 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4041 "BM_%d : Failed to allocate memory in "
4042 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004043 ret = -ENOMEM;
4044
4045 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304046 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004047
4048 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4049 phba->params.cxns_per_ctrl, GFP_KERNEL);
4050 if (!phba->conn_table) {
4051 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4052 "BM_%d : Failed to allocate memory in"
4053 "hba_setup_cid_tbls\n");
4054
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004055 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004056 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004057 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004058
4059 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304060 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004061
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004062 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4063 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004064
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004065 ptr_cid_info = phba->cid_array_info[ulp_num];
4066 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4067 phba->phwi_ctrlr->wrb_context[i].cid;
4068
4069 }
4070
4071 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4072 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4073 ptr_cid_info = phba->cid_array_info[ulp_num];
4074
4075 ptr_cid_info->cid_alloc = 0;
4076 ptr_cid_info->cid_free = 0;
4077 }
4078 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304079 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004080
4081free_memory:
4082 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4083 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4084 ptr_cid_info = phba->cid_array_info[ulp_num];
4085
4086 if (ptr_cid_info) {
4087 kfree(ptr_cid_info->cid_array);
4088 kfree(ptr_cid_info);
4089 phba->cid_array_info[ulp_num] = NULL;
4090 }
4091 }
4092 }
4093
4094 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304095}
4096
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304097static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304098{
4099 struct be_ctrl_info *ctrl = &phba->ctrl;
4100 struct hwi_controller *phwi_ctrlr;
4101 struct hwi_context_memory *phwi_context;
4102 struct be_queue_info *eq;
4103 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304104 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304105 u32 enabled;
4106
4107 phwi_ctrlr = phba->phwi_ctrlr;
4108 phwi_context = phwi_ctrlr->phwi_ctxt;
4109
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304110 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4111 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4112 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304113
4114 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4115 if (!enabled) {
4116 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304117 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4118 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304119 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004120 }
4121
Christoph Hellwig83148862017-01-13 17:29:48 +01004122 if (!phba->pcidev->msix_enabled) {
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004123 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304124 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4125 "BM_%d : eq->id=%d\n", eq->id);
4126
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004127 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4128 } else {
4129 for (i = 0; i <= phba->num_cpus; i++) {
4130 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304131 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4132 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304133 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4134 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304135 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304136}
4137
4138static void hwi_disable_intr(struct beiscsi_hba *phba)
4139{
4140 struct be_ctrl_info *ctrl = &phba->ctrl;
4141
4142 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4143 u32 reg = ioread32(addr);
4144
4145 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4146 if (enabled) {
4147 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4148 iowrite32(reg, addr);
4149 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304150 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4151 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304152}
4153
4154static int beiscsi_init_port(struct beiscsi_hba *phba)
4155{
4156 int ret;
4157
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304158 ret = hwi_init_controller(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304159 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304160 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304161 "BM_%d : init controller failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304162 return ret;
4163 }
4164 ret = beiscsi_init_sgl_handle(phba);
4165 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304166 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304167 "BM_%d : init sgl handles failed\n");
4168 goto cleanup_port;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304169 }
4170
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05304171 ret = hba_setup_cid_tbls(phba);
4172 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304173 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304174 "BM_%d : setup CID table failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304175 kfree(phba->io_sgl_hndl_base);
4176 kfree(phba->eh_sgl_hndl_base);
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304177 goto cleanup_port;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304178 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304179 return ret;
4180
Jitendra Bhivaredd940972016-12-13 15:56:04 +05304181cleanup_port:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304182 hwi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304183 return ret;
4184}
4185
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304186static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304187{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004188 struct ulp_cid_info *ptr_cid_info = NULL;
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304189 int ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304190
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304191 kfree(phba->io_sgl_hndl_base);
4192 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304193 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004194 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004195
4196 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4197 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4198 ptr_cid_info = phba->cid_array_info[ulp_num];
4199
4200 if (ptr_cid_info) {
4201 kfree(ptr_cid_info->cid_array);
4202 kfree(ptr_cid_info);
4203 phba->cid_array_info[ulp_num] = NULL;
4204 }
4205 }
4206 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304207}
4208
John Soni Josed629c472012-10-20 04:42:00 +05304209/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004210 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4211 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004212 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004213 *
4214 * Free driver mgmt resources binded to CXN.
4215 **/
4216void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004217beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4218 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004219{
4220 struct beiscsi_io_task *io_task;
4221 struct beiscsi_hba *phba = beiscsi_conn->phba;
4222 struct hwi_wrb_context *pwrb_context;
4223 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004224 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4225 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004226
4227 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004228 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4229
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004230 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004231
4232 if (io_task->pwrb_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304233 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004234 io_task->pwrb_handle = NULL;
4235 }
4236
4237 if (io_task->psgl_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304238 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004239 io_task->psgl_handle = NULL;
4240 }
4241
John Soni Joseeb1c4692015-04-25 08:17:45 +05304242 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004243 pci_unmap_single(phba->pcidev,
4244 io_task->mtask_addr,
4245 io_task->mtask_data_count,
4246 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304247 io_task->mtask_addr = 0;
4248 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004249}
4250
4251/**
John Soni Josed629c472012-10-20 04:42:00 +05304252 * beiscsi_cleanup_task()- Free driver resources of the task
4253 * @task: ptr to the iscsi task
4254 *
4255 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004256static void beiscsi_cleanup_task(struct iscsi_task *task)
4257{
4258 struct beiscsi_io_task *io_task = task->dd_data;
4259 struct iscsi_conn *conn = task->conn;
4260 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4261 struct beiscsi_hba *phba = beiscsi_conn->phba;
4262 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4263 struct hwi_wrb_context *pwrb_context;
4264 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004265 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4266 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004267
4268 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004269 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004270
4271 if (io_task->cmd_bhs) {
Romain Perieraf007b02017-07-06 10:13:03 +02004272 dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
Mike Christie1282ab72012-04-18 03:06:00 -05004273 io_task->bhs_pa.u.a64.address);
4274 io_task->cmd_bhs = NULL;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304275 task->hdr = NULL;
Mike Christie1282ab72012-04-18 03:06:00 -05004276 }
4277
4278 if (task->sc) {
4279 if (io_task->pwrb_handle) {
4280 free_wrb_handle(phba, pwrb_context,
4281 io_task->pwrb_handle);
4282 io_task->pwrb_handle = NULL;
4283 }
4284
4285 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004286 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004287 io_task->psgl_handle = NULL;
4288 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004289
4290 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304291 if (io_task->num_sg)
4292 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004293 io_task->scsi_cmnd = NULL;
4294 }
Mike Christie1282ab72012-04-18 03:06:00 -05004295 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004296 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004297 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004298 }
4299}
4300
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304301void
4302beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4303 struct beiscsi_offload_params *params)
4304{
4305 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304306 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304307 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004308 struct iscsi_task *task = beiscsi_conn->task;
4309 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304310 u32 doorbell = 0;
4311
4312 /*
4313 * We can always use 0 here because it is reserved by libiscsi for
4314 * login/startup related tasks.
4315 */
Mike Christie1282ab72012-04-18 03:06:00 -05004316 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004317 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004318 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004319 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004320
John Soni Jose340c99e2015-08-20 04:44:30 +05304321 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4322 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304323
John Soni Joseacb96932012-10-20 04:44:35 +05304324 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004325 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304326 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304327 phba->init_mem,
4328 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004329 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304330 beiscsi_offload_cxn_v2(params, pwrb_handle,
4331 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304332
John Soni Joseacb96932012-10-20 04:44:35 +05304333 be_dws_le_to_cpu(pwrb_handle->pwrb,
4334 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304335
4336 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304337 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304338 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304339 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004340 iowrite32(doorbell, phba->db_va +
4341 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304342
4343 /*
4344 * There is no completion for CONTEXT_UPDATE. The completion of next
4345 * WRB posted guarantees FW's processing and DMA'ing of it.
4346 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4347 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4348 */
4349 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4350 phba->params.wrbs_per_cxn);
4351 beiscsi_log(phba, KERN_INFO,
4352 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4353 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4354 pwrb_handle, pwrb_context->free_index,
4355 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304356}
4357
4358static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4359 int *index, int *age)
4360{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304361 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304362 if (age)
4363 *age = conn->session->age;
4364}
4365
4366/**
4367 * beiscsi_alloc_pdu - allocates pdu and related resources
4368 * @task: libiscsi task
4369 * @opcode: opcode of pdu for task
4370 *
4371 * This is called with the session lock held. It will allocate
4372 * the wrb and sgl if needed for the command. And it will prep
4373 * the pdu's itt. beiscsi_parse_pdu will later translate
4374 * the pdu itt to the libiscsi task itt.
4375 */
4376static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4377{
4378 struct beiscsi_io_task *io_task = task->dd_data;
4379 struct iscsi_conn *conn = task->conn;
4380 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4381 struct beiscsi_hba *phba = beiscsi_conn->phba;
4382 struct hwi_wrb_context *pwrb_context;
4383 struct hwi_controller *phwi_ctrlr;
4384 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004385 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304386 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4387 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304388
Romain Perieraf007b02017-07-06 10:13:03 +02004389 io_task->cmd_bhs = dma_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004390 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304391 if (!io_task->cmd_bhs)
4392 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304393 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304394 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304395 io_task->conn = beiscsi_conn;
4396
4397 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4398 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304399 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004400 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304401
4402 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304403 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304404 if (!io_task->psgl_handle) {
4405 beiscsi_log(phba, KERN_ERR,
4406 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4407 "BM_%d : Alloc of IO_SGL_ICD Failed"
4408 "for the CID : %d\n",
4409 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304410 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304411 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304412 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304413 beiscsi_conn->beiscsi_conn_cid,
4414 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304415 if (!io_task->pwrb_handle) {
4416 beiscsi_log(phba, KERN_ERR,
4417 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4418 "BM_%d : Alloc of WRB_HANDLE Failed"
4419 "for the CID : %d\n",
4420 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304421 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304422 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304423 } else {
4424 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304425 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004426 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304427 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304428 io_task->psgl_handle = (struct sgl_handle *)
4429 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304430 if (!io_task->psgl_handle) {
4431 beiscsi_log(phba, KERN_ERR,
4432 BEISCSI_LOG_IO |
4433 BEISCSI_LOG_CONFIG,
4434 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4435 "for the CID : %d\n",
4436 beiscsi_conn->
4437 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304438 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304439 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304440
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304441 beiscsi_conn->login_in_progress = 1;
4442 beiscsi_conn->plogin_sgl_handle =
4443 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304444 io_task->pwrb_handle =
4445 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304446 beiscsi_conn->beiscsi_conn_cid,
4447 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304448 if (!io_task->pwrb_handle) {
4449 beiscsi_log(phba, KERN_ERR,
4450 BEISCSI_LOG_IO |
4451 BEISCSI_LOG_CONFIG,
4452 "BM_%d : Alloc of WRB_HANDLE Failed"
4453 "for the CID : %d\n",
4454 beiscsi_conn->
4455 beiscsi_conn_cid);
4456 goto free_mgmt_hndls;
4457 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304458 beiscsi_conn->plogin_wrb_handle =
4459 io_task->pwrb_handle;
4460
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304461 } else {
4462 io_task->psgl_handle =
4463 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304464 io_task->pwrb_handle =
4465 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304466 }
4467 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304468 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304469 if (!io_task->psgl_handle) {
4470 beiscsi_log(phba, KERN_ERR,
4471 BEISCSI_LOG_IO |
4472 BEISCSI_LOG_CONFIG,
4473 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4474 "for the CID : %d\n",
4475 beiscsi_conn->
4476 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304477 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304478 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304479 io_task->pwrb_handle =
4480 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304481 beiscsi_conn->beiscsi_conn_cid,
4482 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304483 if (!io_task->pwrb_handle) {
4484 beiscsi_log(phba, KERN_ERR,
4485 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4486 "BM_%d : Alloc of WRB_HANDLE Failed"
4487 "for the CID : %d\n",
4488 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304489 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304490 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304491
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304492 }
4493 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304494 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4495 wrb_index << 16) | (unsigned int)
4496 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304497 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304498
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304499 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4500 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304501
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304502free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304503 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304504 goto free_hndls;
4505free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304506 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004507 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304508free_hndls:
4509 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004510 cri_index = BE_GET_CRI_FROM_CID(
4511 beiscsi_conn->beiscsi_conn_cid);
4512 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304513 if (io_task->pwrb_handle)
4514 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304515 io_task->pwrb_handle = NULL;
Romain Perieraf007b02017-07-06 10:13:03 +02004516 dma_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304517 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004518 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304519 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304520}
Baoyou Xie0825b8e2016-09-26 20:00:59 +08004521static int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
John Soni Jose09a10932012-10-20 04:44:23 +05304522 unsigned int num_sg, unsigned int xferlen,
4523 unsigned int writedir)
4524{
4525
4526 struct beiscsi_io_task *io_task = task->dd_data;
4527 struct iscsi_conn *conn = task->conn;
4528 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4529 struct beiscsi_hba *phba = beiscsi_conn->phba;
4530 struct iscsi_wrb *pwrb = NULL;
4531 unsigned int doorbell = 0;
4532
4533 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304534
John Soni Jose09a10932012-10-20 04:44:23 +05304535 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4536
4537 if (writedir) {
4538 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4539 INI_WR_CMD);
4540 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4541 } else {
4542 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4543 INI_RD_CMD);
4544 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4545 }
4546
4547 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4548 type, pwrb);
4549
4550 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4551 cpu_to_be16(*(unsigned short *)
4552 &io_task->cmd_bhs->iscsi_hdr.lun));
4553 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4554 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4555 io_task->pwrb_handle->wrb_index);
4556 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4557 be32_to_cpu(task->cmdsn));
4558 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4559 io_task->psgl_handle->sgl_index);
4560
4561 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4562 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304563 io_task->pwrb_handle->wrb_index);
4564 if (io_task->pwrb_context->plast_wrb)
4565 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4566 io_task->pwrb_context->plast_wrb,
4567 io_task->pwrb_handle->wrb_index);
4568 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304569
4570 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4571
4572 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4573 doorbell |= (io_task->pwrb_handle->wrb_index &
4574 DB_DEF_PDU_WRB_INDEX_MASK) <<
4575 DB_DEF_PDU_WRB_INDEX_SHIFT;
4576 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004577 iowrite32(doorbell, phba->db_va +
4578 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304579 return 0;
4580}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304581
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304582static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4583 unsigned int num_sg, unsigned int xferlen,
4584 unsigned int writedir)
4585{
4586
4587 struct beiscsi_io_task *io_task = task->dd_data;
4588 struct iscsi_conn *conn = task->conn;
4589 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4590 struct beiscsi_hba *phba = beiscsi_conn->phba;
4591 struct iscsi_wrb *pwrb = NULL;
4592 unsigned int doorbell = 0;
4593
4594 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304595 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4596
4597 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304598 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4599 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304600 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304601 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304602 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4603 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304604 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4605 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304606
John Soni Jose09a10932012-10-20 04:44:23 +05304607 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4608 type, pwrb);
4609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304610 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004611 cpu_to_be16(*(unsigned short *)
4612 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304613 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4614 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4615 io_task->pwrb_handle->wrb_index);
4616 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4617 be32_to_cpu(task->cmdsn));
4618 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4619 io_task->psgl_handle->sgl_index);
4620
4621 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4622
4623 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304624 io_task->pwrb_handle->wrb_index);
4625 if (io_task->pwrb_context->plast_wrb)
4626 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4627 io_task->pwrb_context->plast_wrb,
4628 io_task->pwrb_handle->wrb_index);
4629 io_task->pwrb_context->plast_wrb = pwrb;
4630
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304631 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4632
4633 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304634 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304635 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4636 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4637
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004638 iowrite32(doorbell, phba->db_va +
4639 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304640 return 0;
4641}
4642
4643static int beiscsi_mtask(struct iscsi_task *task)
4644{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304645 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304646 struct iscsi_conn *conn = task->conn;
4647 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4648 struct beiscsi_hba *phba = beiscsi_conn->phba;
4649 struct iscsi_wrb *pwrb = NULL;
4650 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304651 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304652 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004653 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304654
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304655 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304656 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304657
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004658 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304659 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4660 be32_to_cpu(task->cmdsn));
4661 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4662 io_task->pwrb_handle->wrb_index);
4663 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4664 io_task->psgl_handle->sgl_index);
4665 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4666 task->data_count);
4667 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304668 io_task->pwrb_handle->wrb_index);
4669 if (io_task->pwrb_context->plast_wrb)
4670 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4671 io_task->pwrb_context->plast_wrb,
4672 io_task->pwrb_handle->wrb_index);
4673 io_task->pwrb_context->plast_wrb = pwrb;
4674
John Soni Jose09a10932012-10-20 04:44:23 +05304675 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004676 } else {
4677 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4678 be32_to_cpu(task->cmdsn));
4679 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4680 io_task->pwrb_handle->wrb_index);
4681 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4682 io_task->psgl_handle->sgl_index);
4683 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4684 task->data_count);
4685 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304686 io_task->pwrb_handle->wrb_index);
4687 if (io_task->pwrb_context->plast_wrb)
4688 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4689 io_task->pwrb_context->plast_wrb,
4690 io_task->pwrb_handle->wrb_index);
4691 io_task->pwrb_context->plast_wrb = pwrb;
4692
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004693 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304694 }
4695
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304696
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304697 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4698 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304699 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304700 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004701 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304702 break;
4703 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004704 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304705 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004706 if (is_chip_be2_be3r(phba))
4707 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304708 dmsg, pwrb, 1);
4709 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004710 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304711 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004712 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304713 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004714 if (is_chip_be2_be3r(phba))
4715 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304716 dmsg, pwrb, 0);
4717 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004718 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304719 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004720 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004721 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722 break;
4723 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304724 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004725 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304726 break;
4727 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304728 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004729 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304730 break;
4731 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304732 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004733 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304734 break;
4735
4736 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304737 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4738 "BM_%d : opcode =%d Not supported\n",
4739 task->hdr->opcode & ISCSI_OPCODE_MASK);
4740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304741 return -EINVAL;
4742 }
4743
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004744 if (ret)
4745 return ret;
4746
John Soni Jose09a10932012-10-20 04:44:23 +05304747 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004748 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4749 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4750 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304751
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304752 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304753 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304754 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4755 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004756 iowrite32(doorbell, phba->db_va +
4757 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304758 return 0;
4759}
4760
4761static int beiscsi_task_xmit(struct iscsi_task *task)
4762{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304763 struct beiscsi_io_task *io_task = task->dd_data;
4764 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304765 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304766 struct scatterlist *sg;
4767 int num_sg;
4768 unsigned int writedir = 0, xferlen = 0;
4769
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304770 phba = io_task->conn->phba;
4771 /**
4772 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4773 * operational if FW still gets heartbeat from EP FW. Is management
4774 * path really needed to continue further?
4775 */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304776 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304777 return -EIO;
4778
Jitendra Bhivare18683792016-02-04 15:49:18 +05304779 if (!io_task->conn->login_in_progress)
4780 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304781
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304782 if (!sc)
4783 return beiscsi_mtask(task);
4784
4785 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304786 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304787 num_sg = scsi_dma_map(sc);
4788 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004789 beiscsi_log(phba, KERN_ERR,
4790 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4791 "BM_%d : scsi_dma_map Failed "
4792 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4793 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4794 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304795
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304796 return num_sg;
4797 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304798 /**
4799 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4800 * For management task, cleanup_task checks mtask_addr before unmapping.
4801 */
4802 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304803 xferlen = scsi_bufflen(sc);
4804 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304805 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304806 writedir = 1;
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05304807 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304808 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304809
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05304810 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304811}
4812
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004813/**
4814 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4815 * @job: job to handle
4816 */
4817static int beiscsi_bsg_request(struct bsg_job *job)
4818{
4819 struct Scsi_Host *shost;
4820 struct beiscsi_hba *phba;
4821 struct iscsi_bsg_request *bsg_req = job->request;
4822 int rc = -EINVAL;
4823 unsigned int tag;
4824 struct be_dma_mem nonemb_cmd;
4825 struct be_cmd_resp_hdr *resp;
4826 struct iscsi_bsg_reply *bsg_reply = job->reply;
4827 unsigned short status, extd_status;
4828
4829 shost = iscsi_job_to_shost(job);
4830 phba = iscsi_host_priv(shost);
4831
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304832 if (!beiscsi_hba_is_online(phba)) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304833 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4834 "BM_%d : HBA in error 0x%lx\n", phba->state);
4835 return -ENXIO;
4836 }
4837
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004838 switch (bsg_req->msgcode) {
4839 case ISCSI_BSG_HST_VENDOR:
4840 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4841 job->request_payload.payload_len,
4842 &nonemb_cmd.dma);
4843 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304844 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4845 "BM_%d : Failed to allocate memory for "
4846 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304847 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004848 }
4849 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4850 &nonemb_cmd);
4851 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304852 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304853 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304854
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004855 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4856 nonemb_cmd.va, nonemb_cmd.dma);
4857 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304858 }
4859
4860 rc = wait_event_interruptible_timeout(
4861 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304862 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304863 msecs_to_jiffies(
4864 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304865
4866 if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
4867 clear_bit(MCC_TAG_STATE_RUNNING,
4868 &phba->ctrl.ptag_state[tag].tag_state);
4869 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4870 nonemb_cmd.va, nonemb_cmd.dma);
4871 return -EIO;
4872 }
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304873 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4874 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4875 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304876 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004877 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4878 sg_copy_from_buffer(job->reply_payload.sg_list,
4879 job->reply_payload.sg_cnt,
4880 nonemb_cmd.va, (resp->response_length
4881 + sizeof(*resp)));
4882 bsg_reply->reply_payload_rcv_len = resp->response_length;
4883 bsg_reply->result = status;
4884 bsg_job_done(job, bsg_reply->result,
4885 bsg_reply->reply_payload_rcv_len);
4886 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4887 nonemb_cmd.va, nonemb_cmd.dma);
4888 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304889 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304890 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304891 " status = %d extd_status = %d\n",
4892 status, extd_status);
4893
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004894 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304895 } else {
4896 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004897 }
4898 break;
4899
4900 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304901 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4902 "BM_%d : Unsupported bsg command: 0x%x\n",
4903 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004904 break;
4905 }
4906
4907 return rc;
4908}
4909
Baoyou Xie0825b8e2016-09-26 20:00:59 +08004910static void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304911{
4912 /* Set the logging parameter */
4913 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4914}
4915
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304916void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
4917{
4918 if (phba->boot_struct.boot_kset)
4919 return;
4920
4921 /* skip if boot work is already in progress */
4922 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
4923 return;
4924
4925 phba->boot_struct.retry = 3;
4926 phba->boot_struct.tag = 0;
4927 phba->boot_struct.s_handle = s_handle;
4928 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
4929 schedule_work(&phba->boot_work);
4930}
4931
Jitendra Bhivare8d9ecd42017-10-10 16:18:11 +05304932/**
4933 * Boot flag info for iscsi-utilities
4934 * Bit 0 Block valid flag
4935 * Bit 1 Firmware booting selected
4936 */
4937#define BEISCSI_SYSFS_ISCSI_BOOT_FLAGS 3
4938
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304939static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
4940{
4941 struct beiscsi_hba *phba = data;
4942 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
4943 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
4944 char *str = buf;
4945 int rc = -EPERM;
4946
4947 switch (type) {
4948 case ISCSI_BOOT_TGT_NAME:
4949 rc = sprintf(buf, "%.*s\n",
4950 (int)strlen(boot_sess->target_name),
4951 (char *)&boot_sess->target_name);
4952 break;
4953 case ISCSI_BOOT_TGT_IP_ADDR:
4954 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
4955 rc = sprintf(buf, "%pI4\n",
4956 (char *)&boot_conn->dest_ipaddr.addr);
4957 else
4958 rc = sprintf(str, "%pI6\n",
4959 (char *)&boot_conn->dest_ipaddr.addr);
4960 break;
4961 case ISCSI_BOOT_TGT_PORT:
4962 rc = sprintf(str, "%d\n", boot_conn->dest_port);
4963 break;
4964
4965 case ISCSI_BOOT_TGT_CHAP_NAME:
4966 rc = sprintf(str, "%.*s\n",
4967 boot_conn->negotiated_login_options.auth_data.chap.
4968 target_chap_name_length,
4969 (char *)&boot_conn->negotiated_login_options.
4970 auth_data.chap.target_chap_name);
4971 break;
4972 case ISCSI_BOOT_TGT_CHAP_SECRET:
4973 rc = sprintf(str, "%.*s\n",
4974 boot_conn->negotiated_login_options.auth_data.chap.
4975 target_secret_length,
4976 (char *)&boot_conn->negotiated_login_options.
4977 auth_data.chap.target_secret);
4978 break;
4979 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
4980 rc = sprintf(str, "%.*s\n",
4981 boot_conn->negotiated_login_options.auth_data.chap.
4982 intr_chap_name_length,
4983 (char *)&boot_conn->negotiated_login_options.
4984 auth_data.chap.intr_chap_name);
4985 break;
4986 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
4987 rc = sprintf(str, "%.*s\n",
4988 boot_conn->negotiated_login_options.auth_data.chap.
4989 intr_secret_length,
4990 (char *)&boot_conn->negotiated_login_options.
4991 auth_data.chap.intr_secret);
4992 break;
4993 case ISCSI_BOOT_TGT_FLAGS:
Jitendra Bhivare8d9ecd42017-10-10 16:18:11 +05304994 rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304995 break;
4996 case ISCSI_BOOT_TGT_NIC_ASSOC:
4997 rc = sprintf(str, "0\n");
4998 break;
4999 }
5000 return rc;
5001}
5002
5003static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5004{
5005 struct beiscsi_hba *phba = data;
5006 char *str = buf;
5007 int rc = -EPERM;
5008
5009 switch (type) {
5010 case ISCSI_BOOT_INI_INITIATOR_NAME:
5011 rc = sprintf(str, "%s\n",
5012 phba->boot_struct.boot_sess.initiator_iscsiname);
5013 break;
5014 }
5015 return rc;
5016}
5017
5018static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5019{
5020 struct beiscsi_hba *phba = data;
5021 char *str = buf;
5022 int rc = -EPERM;
5023
5024 switch (type) {
5025 case ISCSI_BOOT_ETH_FLAGS:
Jitendra Bhivare8d9ecd42017-10-10 16:18:11 +05305026 rc = sprintf(str, "%d\n", BEISCSI_SYSFS_ISCSI_BOOT_FLAGS);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305027 break;
5028 case ISCSI_BOOT_ETH_INDEX:
5029 rc = sprintf(str, "0\n");
5030 break;
5031 case ISCSI_BOOT_ETH_MAC:
5032 rc = beiscsi_get_macaddr(str, phba);
5033 break;
5034 }
5035 return rc;
5036}
5037
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305038static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5039{
5040 umode_t rc = 0;
5041
5042 switch (type) {
5043 case ISCSI_BOOT_TGT_NAME:
5044 case ISCSI_BOOT_TGT_IP_ADDR:
5045 case ISCSI_BOOT_TGT_PORT:
5046 case ISCSI_BOOT_TGT_CHAP_NAME:
5047 case ISCSI_BOOT_TGT_CHAP_SECRET:
5048 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5049 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5050 case ISCSI_BOOT_TGT_NIC_ASSOC:
5051 case ISCSI_BOOT_TGT_FLAGS:
5052 rc = S_IRUGO;
5053 break;
5054 }
5055 return rc;
5056}
5057
5058static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5059{
5060 umode_t rc = 0;
5061
5062 switch (type) {
5063 case ISCSI_BOOT_INI_INITIATOR_NAME:
5064 rc = S_IRUGO;
5065 break;
5066 }
5067 return rc;
5068}
5069
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305070static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5071{
5072 umode_t rc = 0;
5073
5074 switch (type) {
5075 case ISCSI_BOOT_ETH_FLAGS:
5076 case ISCSI_BOOT_ETH_MAC:
5077 case ISCSI_BOOT_ETH_INDEX:
5078 rc = S_IRUGO;
5079 break;
5080 }
5081 return rc;
5082}
5083
5084static void beiscsi_boot_kobj_release(void *data)
5085{
5086 struct beiscsi_hba *phba = data;
5087
5088 scsi_host_put(phba->shost);
5089}
5090
5091static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5092{
5093 struct boot_struct *bs = &phba->boot_struct;
5094 struct iscsi_boot_kobj *boot_kobj;
5095
5096 if (bs->boot_kset) {
5097 __beiscsi_log(phba, KERN_ERR,
5098 "BM_%d: boot_kset already created\n");
5099 return 0;
5100 }
5101
5102 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5103 if (!bs->boot_kset) {
5104 __beiscsi_log(phba, KERN_ERR,
5105 "BM_%d: boot_kset alloc failed\n");
5106 return -ENOMEM;
5107 }
5108
5109 /* get shost ref because the show function will refer phba */
5110 if (!scsi_host_get(phba->shost))
5111 goto free_kset;
5112
5113 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5114 beiscsi_show_boot_tgt_info,
5115 beiscsi_tgt_get_attr_visibility,
5116 beiscsi_boot_kobj_release);
5117 if (!boot_kobj)
5118 goto put_shost;
5119
5120 if (!scsi_host_get(phba->shost))
5121 goto free_kset;
5122
5123 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5124 beiscsi_show_boot_ini_info,
5125 beiscsi_ini_get_attr_visibility,
5126 beiscsi_boot_kobj_release);
5127 if (!boot_kobj)
5128 goto put_shost;
5129
5130 if (!scsi_host_get(phba->shost))
5131 goto free_kset;
5132
5133 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5134 beiscsi_show_boot_eth_info,
5135 beiscsi_eth_get_attr_visibility,
5136 beiscsi_boot_kobj_release);
5137 if (!boot_kobj)
5138 goto put_shost;
5139
5140 return 0;
5141
5142put_shost:
5143 scsi_host_put(phba->shost);
5144free_kset:
5145 iscsi_boot_destroy_kset(bs->boot_kset);
5146 bs->boot_kset = NULL;
5147 return -ENOMEM;
5148}
5149
5150static void beiscsi_boot_work(struct work_struct *work)
5151{
5152 struct beiscsi_hba *phba =
5153 container_of(work, struct beiscsi_hba, boot_work);
5154 struct boot_struct *bs = &phba->boot_struct;
5155 unsigned int tag = 0;
5156
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305157 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305158 return;
5159
5160 beiscsi_log(phba, KERN_INFO,
5161 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5162 "BM_%d : %s action %d\n",
5163 __func__, phba->boot_struct.action);
5164
5165 switch (phba->boot_struct.action) {
5166 case BEISCSI_BOOT_REOPEN_SESS:
5167 tag = beiscsi_boot_reopen_sess(phba);
5168 break;
5169 case BEISCSI_BOOT_GET_SHANDLE:
5170 tag = __beiscsi_boot_get_shandle(phba, 1);
5171 break;
5172 case BEISCSI_BOOT_GET_SINFO:
5173 tag = beiscsi_boot_get_sinfo(phba);
5174 break;
5175 case BEISCSI_BOOT_LOGOUT_SESS:
5176 tag = beiscsi_boot_logout_sess(phba);
5177 break;
5178 case BEISCSI_BOOT_CREATE_KSET:
5179 beiscsi_boot_create_kset(phba);
5180 /**
5181 * updated boot_kset is made visible to all before
5182 * ending the boot work.
5183 */
5184 mb();
5185 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5186 return;
5187 }
5188 if (!tag) {
5189 if (bs->retry--)
5190 schedule_work(&phba->boot_work);
5191 else
5192 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5193 }
5194}
5195
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305196static void beiscsi_eqd_update_work(struct work_struct *work)
5197{
5198 struct hwi_context_memory *phwi_context;
5199 struct be_set_eqd set_eqd[MAX_CPUS];
5200 struct hwi_controller *phwi_ctrlr;
5201 struct be_eq_obj *pbe_eq;
5202 struct beiscsi_hba *phba;
5203 unsigned int pps, delta;
5204 struct be_aic_obj *aic;
5205 int eqd, i, num = 0;
5206 unsigned long now;
5207
5208 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305209 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305210 return;
5211
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005212 phwi_ctrlr = phba->phwi_ctrlr;
5213 phwi_context = phwi_ctrlr->phwi_ctxt;
5214
5215 for (i = 0; i <= phba->num_cpus; i++) {
5216 aic = &phba->aic_obj[i];
5217 pbe_eq = &phwi_context->be_eq[i];
5218 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305219 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005220 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305221 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005222 aic->eq_prev = pbe_eq->cq_count;
5223 continue;
5224 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305225 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005226 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5227 eqd = (pps / 1500) << 2;
5228
5229 if (eqd < 8)
5230 eqd = 0;
Jitendra Bhivare45efc9402017-10-10 16:18:18 +05305231 eqd = min_t(u32, eqd, BEISCSI_EQ_DELAY_MAX);
5232 eqd = max_t(u32, eqd, BEISCSI_EQ_DELAY_MIN);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005233
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305234 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005235 aic->eq_prev = pbe_eq->cq_count;
5236
5237 if (eqd != aic->prev_eqd) {
5238 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5239 set_eqd[num].eq_id = pbe_eq->q.id;
5240 aic->prev_eqd = eqd;
5241 num++;
5242 }
5243 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305244 if (num)
5245 /* completion of this is ignored */
5246 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005247
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305248 schedule_delayed_work(&phba->eqd_update,
5249 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305250}
5251
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305252static void beiscsi_hw_tpe_check(unsigned long ptr)
5253{
5254 struct beiscsi_hba *phba;
5255 u32 wait;
5256
5257 phba = (struct beiscsi_hba *)ptr;
5258 /* if not TPE, do nothing */
5259 if (!beiscsi_detect_tpe(phba))
5260 return;
5261
5262 /* wait default 4000ms before recovering */
5263 wait = 4000;
5264 if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
5265 wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
5266 queue_delayed_work(phba->wq, &phba->recover_port,
5267 msecs_to_jiffies(wait));
5268}
5269
5270static void beiscsi_hw_health_check(unsigned long ptr)
5271{
5272 struct beiscsi_hba *phba;
5273
5274 phba = (struct beiscsi_hba *)ptr;
5275 beiscsi_detect_ue(phba);
5276 if (beiscsi_detect_ue(phba)) {
5277 __beiscsi_log(phba, KERN_ERR,
5278 "BM_%d : port in error: %lx\n", phba->state);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305279 /* sessions are no longer valid, so first fail the sessions */
5280 queue_work(phba->wq, &phba->sess_work);
5281
5282 /* detect UER supported */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305283 if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
5284 return;
5285 /* modify this timer to check TPE */
5286 phba->hw_check.function = beiscsi_hw_tpe_check;
5287 }
5288
5289 mod_timer(&phba->hw_check,
5290 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5291}
5292
5293/*
5294 * beiscsi_enable_port()- Enables the disabled port.
5295 * Only port resources freed in disable function are reallocated.
5296 * This is called in HBA error handling path.
5297 *
5298 * @phba: Instance of driver private structure
5299 *
5300 **/
5301static int beiscsi_enable_port(struct beiscsi_hba *phba)
5302{
5303 struct hwi_context_memory *phwi_context;
5304 struct hwi_controller *phwi_ctrlr;
5305 struct be_eq_obj *pbe_eq;
5306 int ret, i;
5307
5308 if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
5309 __beiscsi_log(phba, KERN_ERR,
5310 "BM_%d : %s : port is online %lx\n",
5311 __func__, phba->state);
5312 return 0;
5313 }
5314
5315 ret = beiscsi_init_sliport(phba);
5316 if (ret)
5317 return ret;
5318
Christoph Hellwig83148862017-01-13 17:29:48 +01005319 be2iscsi_enable_msix(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305320
5321 beiscsi_get_params(phba);
Jitendra Bhivare1cb3c3f2017-10-10 16:18:17 +05305322 beiscsi_set_host_data(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305323 /* Re-enable UER. If different TPE occurs then it is recoverable. */
5324 beiscsi_set_uer_feature(phba);
5325
5326 phba->shost->max_id = phba->params.cxns_per_ctrl;
5327 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jitendra Bhivaredd940972016-12-13 15:56:04 +05305328 ret = beiscsi_init_port(phba);
5329 if (ret < 0) {
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305330 __beiscsi_log(phba, KERN_ERR,
Jitendra Bhivaredd940972016-12-13 15:56:04 +05305331 "BM_%d : init port failed\n");
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305332 goto disable_msix;
5333 }
5334
5335 for (i = 0; i < MAX_MCC_CMD; i++) {
5336 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5337 phba->ctrl.mcc_tag[i] = i + 1;
5338 phba->ctrl.mcc_tag_status[i + 1] = 0;
5339 phba->ctrl.mcc_tag_available++;
5340 }
5341
5342 phwi_ctrlr = phba->phwi_ctrlr;
5343 phwi_context = phwi_ctrlr->phwi_ctxt;
5344 for (i = 0; i < phba->num_cpus; i++) {
5345 pbe_eq = &phwi_context->be_eq[i];
5346 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5347 }
5348
Christoph Hellwig83148862017-01-13 17:29:48 +01005349 i = (phba->pcidev->msix_enabled) ? i : 0;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305350 /* Work item for MCC handling */
5351 pbe_eq = &phwi_context->be_eq[i];
5352 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5353
5354 ret = beiscsi_init_irqs(phba);
5355 if (ret < 0) {
5356 __beiscsi_log(phba, KERN_ERR,
5357 "BM_%d : setup IRQs failed %d\n", ret);
5358 goto cleanup_port;
5359 }
5360 hwi_enable_intr(phba);
5361 /* port operational: clear all error bits */
5362 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5363 __beiscsi_log(phba, KERN_INFO,
5364 "BM_%d : port online: 0x%lx\n", phba->state);
5365
5366 /* start hw_check timer and eqd_update work */
5367 schedule_delayed_work(&phba->eqd_update,
5368 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5369
5370 /**
5371 * Timer function gets modified for TPE detection.
5372 * Always reinit to do health check first.
5373 */
5374 phba->hw_check.function = beiscsi_hw_health_check;
5375 mod_timer(&phba->hw_check,
5376 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5377 return 0;
5378
5379cleanup_port:
5380 for (i = 0; i < phba->num_cpus; i++) {
5381 pbe_eq = &phwi_context->be_eq[i];
5382 irq_poll_disable(&pbe_eq->iopoll);
5383 }
5384 hwi_cleanup_port(phba);
5385
5386disable_msix:
Christoph Hellwig83148862017-01-13 17:29:48 +01005387 pci_free_irq_vectors(phba->pcidev);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305388 return ret;
5389}
5390
5391/*
5392 * beiscsi_disable_port()- Disable port and cleanup driver resources.
5393 * This is called in HBA error handling and driver removal.
5394 * @phba: Instance Priv structure
5395 * @unload: indicate driver is unloading
5396 *
5397 * Free the OS and HW resources held by the driver
5398 **/
5399static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
5400{
5401 struct hwi_context_memory *phwi_context;
5402 struct hwi_controller *phwi_ctrlr;
5403 struct be_eq_obj *pbe_eq;
Christoph Hellwig83148862017-01-13 17:29:48 +01005404 unsigned int i;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305405
5406 if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
5407 return;
5408
5409 phwi_ctrlr = phba->phwi_ctrlr;
5410 phwi_context = phwi_ctrlr->phwi_ctxt;
5411 hwi_disable_intr(phba);
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305412 beiscsi_free_irqs(phba);
Christoph Hellwig83148862017-01-13 17:29:48 +01005413 pci_free_irq_vectors(phba->pcidev);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305414
5415 for (i = 0; i < phba->num_cpus; i++) {
5416 pbe_eq = &phwi_context->be_eq[i];
5417 irq_poll_disable(&pbe_eq->iopoll);
5418 }
5419 cancel_delayed_work_sync(&phba->eqd_update);
5420 cancel_work_sync(&phba->boot_work);
5421 /* WQ might be running cancel queued mcc_work if we are not exiting */
5422 if (!unload && beiscsi_hba_in_error(phba)) {
5423 pbe_eq = &phwi_context->be_eq[i];
5424 cancel_work_sync(&pbe_eq->mcc_work);
5425 }
5426 hwi_cleanup_port(phba);
Jitendra Bhivaredd940972016-12-13 15:56:04 +05305427 beiscsi_cleanup_port(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305428}
5429
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305430static void beiscsi_sess_work(struct work_struct *work)
5431{
5432 struct beiscsi_hba *phba;
5433
5434 phba = container_of(work, struct beiscsi_hba, sess_work);
5435 /*
5436 * This work gets scheduled only in case of HBA error.
5437 * Old sessions are gone so need to be re-established.
5438 * iscsi_session_failure needs process context hence this work.
5439 */
5440 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5441}
5442
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305443static void beiscsi_recover_port(struct work_struct *work)
5444{
5445 struct beiscsi_hba *phba;
5446
5447 phba = container_of(work, struct beiscsi_hba, recover_port.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305448 beiscsi_disable_port(phba, 0);
5449 beiscsi_enable_port(phba);
5450}
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005451
5452static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5453 pci_channel_state_t state)
5454{
5455 struct beiscsi_hba *phba = NULL;
5456
5457 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305458 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005459
5460 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5461 "BM_%d : EEH error detected\n");
5462
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305463 /* first stop UE detection when PCI error detected */
5464 del_timer_sync(&phba->hw_check);
5465 cancel_delayed_work_sync(&phba->recover_port);
5466
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305467 /* sessions are no longer valid, so first fail the sessions */
5468 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305469 beiscsi_disable_port(phba, 0);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005470
5471 if (state == pci_channel_io_perm_failure) {
5472 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5473 "BM_%d : EEH : State PERM Failure");
5474 return PCI_ERS_RESULT_DISCONNECT;
5475 }
5476
5477 pci_disable_device(pdev);
5478
5479 /* The error could cause the FW to trigger a flash debug dump.
5480 * Resetting the card while flash dump is in progress
5481 * can cause it not to recover; wait for it to finish.
5482 * Wait only for first function as it is needed only once per
5483 * adapter.
5484 **/
5485 if (pdev->devfn == 0)
5486 ssleep(30);
5487
5488 return PCI_ERS_RESULT_NEED_RESET;
5489}
5490
5491static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5492{
5493 struct beiscsi_hba *phba = NULL;
5494 int status = 0;
5495
5496 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5497
5498 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5499 "BM_%d : EEH Reset\n");
5500
5501 status = pci_enable_device(pdev);
5502 if (status)
5503 return PCI_ERS_RESULT_DISCONNECT;
5504
5505 pci_set_master(pdev);
5506 pci_set_power_state(pdev, PCI_D0);
5507 pci_restore_state(pdev);
5508
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305509 status = beiscsi_check_fw_rdy(phba);
5510 if (status) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005511 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5512 "BM_%d : EEH Reset Completed\n");
5513 } else {
5514 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5515 "BM_%d : EEH Reset Completion Failure\n");
5516 return PCI_ERS_RESULT_DISCONNECT;
5517 }
5518
5519 pci_cleanup_aer_uncorrect_error_status(pdev);
5520 return PCI_ERS_RESULT_RECOVERED;
5521}
5522
5523static void beiscsi_eeh_resume(struct pci_dev *pdev)
5524{
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305525 struct beiscsi_hba *phba;
5526 int ret;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005527
5528 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5529 pci_save_state(pdev);
5530
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305531 ret = beiscsi_enable_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305532 if (ret)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305533 __beiscsi_log(phba, KERN_ERR,
5534 "BM_%d : AER EEH resume failed\n");
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005535}
5536
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005537static int beiscsi_dev_probe(struct pci_dev *pcidev,
5538 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305539{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305540 struct hwi_context_memory *phwi_context;
Jitendra Bhivare29e80b72016-12-13 15:56:02 +05305541 struct hwi_controller *phwi_ctrlr;
5542 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305543 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305544 unsigned int s_handle;
Jitendra Bhivare29e80b72016-12-13 15:56:02 +05305545 char wq_name[20];
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305546 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305547
5548 ret = beiscsi_enable_pci(pcidev);
5549 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305550 dev_err(&pcidev->dev,
5551 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305552 return ret;
5553 }
5554
5555 phba = beiscsi_hba_alloc(pcidev);
5556 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305557 dev_err(&pcidev->dev,
5558 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305559 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305560 goto disable_pci;
5561 }
5562
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005563 /* Enable EEH reporting */
5564 ret = pci_enable_pcie_error_reporting(pcidev);
5565 if (ret)
5566 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5567 "BM_%d : PCIe Error Reporting "
5568 "Enabling Failed\n");
5569
5570 pci_save_state(pcidev);
5571
John Soni Jose99bc5d52012-08-20 23:00:18 +05305572 /* Initialize Driver configuration Paramters */
5573 beiscsi_hba_attrs_init(phba);
5574
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005575 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305576
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305577 switch (pcidev->device) {
5578 case BE_DEVICE_ID1:
5579 case OC_DEVICE_ID1:
5580 case OC_DEVICE_ID2:
5581 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305582 phba->iotask_fn = beiscsi_iotask;
Ketan Mukadam5fa7db22016-12-13 15:56:05 +05305583 dev_warn(&pcidev->dev,
5584 "Obsolete/Unsupported BE2 Adapter Family\n");
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305585 break;
5586 case BE_DEVICE_ID2:
5587 case OC_DEVICE_ID3:
5588 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305589 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305590 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305591 case OC_SKH_ID1:
5592 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305593 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005594 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305595 default:
5596 phba->generation = 0;
5597 }
5598
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305599 ret = be_ctrl_init(phba, pcidev);
5600 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305601 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305602 "BM_%d : be_ctrl_init failed\n");
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305603 goto free_hba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305604 }
5605
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305606 ret = beiscsi_init_sliport(phba);
5607 if (ret)
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305608 goto free_hba;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305610 spin_lock_init(&phba->io_sgl_lock);
5611 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005612 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305613 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305614 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305615 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5616 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305617 goto free_port;
5618 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305619 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305620 beiscsi_get_params(phba);
Jitendra Bhivare1cb3c3f2017-10-10 16:18:17 +05305621 beiscsi_set_host_data(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305622 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005623
Christoph Hellwig83148862017-01-13 17:29:48 +01005624 be2iscsi_enable_msix(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005625
5626 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5627 "BM_%d : num_cpus = %d\n",
5628 phba->num_cpus);
5629
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005630 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305631 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jitendra Bhivaredd940972016-12-13 15:56:04 +05305632 ret = beiscsi_get_memory(phba);
5633 if (ret < 0) {
5634 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5635 "BM_%d : alloc host mem failed\n");
5636 goto free_port;
5637 }
5638
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305639 ret = beiscsi_init_port(phba);
5640 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305641 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivaredd940972016-12-13 15:56:04 +05305642 "BM_%d : init port failed\n");
5643 beiscsi_free_mem(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305644 goto free_port;
5645 }
5646
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005647 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305648 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5649 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305650 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305651 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005652 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005653 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305654 }
5655
5656 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5657
Jitendra Bhivare29e80b72016-12-13 15:56:02 +05305658 snprintf(wq_name, sizeof(wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305659 phba->shost->host_no);
Jitendra Bhivare29e80b72016-12-13 15:56:02 +05305660 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305661 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305662 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5663 "BM_%d : beiscsi_dev_probe-"
5664 "Failed to allocate work queue\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305665 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305666 goto free_twq;
5667 }
5668
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305669 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305670
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305671 phwi_ctrlr = phba->phwi_ctrlr;
5672 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305673
Jens Axboe89f8b332014-03-13 09:38:42 -06005674 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305675 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305676 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305677 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305678
Christoph Hellwig83148862017-01-13 17:29:48 +01005679 i = (phba->pcidev->msix_enabled) ? i : 0;
Jens Axboe89f8b332014-03-13 09:38:42 -06005680 /* Work item for MCC handling */
5681 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305682 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005683
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305684 ret = beiscsi_init_irqs(phba);
5685 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305686 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5687 "BM_%d : beiscsi_dev_probe-"
5688 "Failed to beiscsi_init_irqs\n");
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305689 goto disable_iopoll;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305690 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305691 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005692
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305693 ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
5694 if (ret)
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305695 goto free_irqs;
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005696
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305697 /* set online bit after port is operational */
5698 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5699 __beiscsi_log(phba, KERN_INFO,
5700 "BM_%d : port online: 0x%lx\n", phba->state);
5701
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305702 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5703 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5704 if (ret > 0) {
5705 beiscsi_start_boot_work(phba, s_handle);
5706 /**
5707 * Set this bit after starting the work to let
5708 * probe handle it first.
5709 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005710 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305711 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5712 }
Mike Christief457a462011-06-24 15:11:53 -05005713
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305714 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305715 schedule_delayed_work(&phba->eqd_update,
5716 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305717
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305718 INIT_WORK(&phba->sess_work, beiscsi_sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305719 INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305720 /**
5721 * Start UE detection here. UE before this will cause stall in probe
5722 * and eventually fail the probe.
5723 */
5724 init_timer(&phba->hw_check);
5725 phba->hw_check.function = beiscsi_hw_health_check;
5726 phba->hw_check.data = (unsigned long)phba;
5727 mod_timer(&phba->hw_check,
5728 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose99bc5d52012-08-20 23:00:18 +05305729 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5730 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305731 return 0;
5732
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305733free_irqs:
5734 hwi_disable_intr(phba);
5735 beiscsi_free_irqs(phba);
5736disable_iopoll:
Jens Axboe89f8b332014-03-13 09:38:42 -06005737 for (i = 0; i < phba->num_cpus; i++) {
5738 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005739 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005740 }
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305741 destroy_workqueue(phba->wq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305742free_twq:
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305743 hwi_cleanup_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305744 beiscsi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305745 beiscsi_free_mem(phba);
5746free_port:
5747 pci_free_consistent(phba->pcidev,
5748 phba->ctrl.mbox_mem_alloced.size,
5749 phba->ctrl.mbox_mem_alloced.va,
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305750 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305751 beiscsi_unmap_pci_function(phba);
Jitendra Bhivare45371aa2017-10-10 16:18:13 +05305752free_hba:
Christoph Hellwig83148862017-01-13 17:29:48 +01005753 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305754 pci_dev_put(phba->pcidev);
5755 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305756 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305757disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305758 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305759 pci_disable_device(pcidev);
5760 return ret;
5761}
5762
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305763static void beiscsi_remove(struct pci_dev *pcidev)
5764{
5765 struct beiscsi_hba *phba = NULL;
5766
5767 phba = pci_get_drvdata(pcidev);
5768 if (!phba) {
5769 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5770 return;
5771 }
5772
5773 /* first stop UE detection before unloading */
5774 del_timer_sync(&phba->hw_check);
5775 cancel_delayed_work_sync(&phba->recover_port);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305776 cancel_work_sync(&phba->sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305777
5778 beiscsi_iface_destroy_default(phba);
5779 iscsi_host_remove(phba->shost);
5780 beiscsi_disable_port(phba, 1);
5781
5782 /* after cancelling boot_work */
5783 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5784
5785 /* free all resources */
5786 destroy_workqueue(phba->wq);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305787 beiscsi_free_mem(phba);
5788
5789 /* ctrl uninit */
5790 beiscsi_unmap_pci_function(phba);
5791 pci_free_consistent(phba->pcidev,
5792 phba->ctrl.mbox_mem_alloced.size,
5793 phba->ctrl.mbox_mem_alloced.va,
5794 phba->ctrl.mbox_mem_alloced.dma);
5795
5796 pci_dev_put(phba->pcidev);
5797 iscsi_host_free(phba->shost);
5798 pci_disable_pcie_error_reporting(pcidev);
5799 pci_set_drvdata(pcidev, NULL);
5800 pci_release_regions(pcidev);
5801 pci_disable_device(pcidev);
5802}
5803
5804
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005805static struct pci_error_handlers beiscsi_eeh_handlers = {
5806 .error_detected = beiscsi_eeh_err_detected,
5807 .slot_reset = beiscsi_eeh_reset,
5808 .resume = beiscsi_eeh_resume,
5809};
5810
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305811struct iscsi_transport beiscsi_iscsi_transport = {
5812 .owner = THIS_MODULE,
5813 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305814 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305815 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305816 .create_session = beiscsi_session_create,
5817 .destroy_session = beiscsi_session_destroy,
5818 .create_conn = beiscsi_conn_create,
5819 .bind_conn = beiscsi_conn_bind,
5820 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305821 .attr_is_visible = beiscsi_attr_is_visible,
5822 .set_iface_param = beiscsi_iface_set_param,
5823 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305824 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005825 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305826 .get_session_param = iscsi_session_get_param,
5827 .get_host_param = beiscsi_get_host_param,
5828 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005829 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305830 .send_pdu = iscsi_conn_send_pdu,
5831 .xmit_task = beiscsi_task_xmit,
5832 .cleanup_task = beiscsi_cleanup_task,
5833 .alloc_pdu = beiscsi_alloc_pdu,
5834 .parse_pdu_itt = beiscsi_parse_pdu,
5835 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005836 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305837 .ep_connect = beiscsi_ep_connect,
5838 .ep_poll = beiscsi_ep_poll,
5839 .ep_disconnect = beiscsi_ep_disconnect,
5840 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005841 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305842};
5843
5844static struct pci_driver beiscsi_pci_driver = {
5845 .name = DRV_NAME,
5846 .probe = beiscsi_dev_probe,
5847 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005848 .id_table = beiscsi_pci_id_table,
5849 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305850};
5851
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305852static int __init beiscsi_module_init(void)
5853{
5854 int ret;
5855
5856 beiscsi_scsi_transport =
5857 iscsi_register_transport(&beiscsi_iscsi_transport);
5858 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305859 printk(KERN_ERR
5860 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305861 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305862 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305863 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5864 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305865
5866 ret = pci_register_driver(&beiscsi_pci_driver);
5867 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305868 printk(KERN_ERR
5869 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305870 goto unregister_iscsi_transport;
5871 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305872 return 0;
5873
5874unregister_iscsi_transport:
5875 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5876 return ret;
5877}
5878
5879static void __exit beiscsi_module_exit(void)
5880{
5881 pci_unregister_driver(&beiscsi_pci_driver);
5882 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5883}
5884
5885module_init(beiscsi_module_init);
5886module_exit(beiscsi_module_exit);