blob: 741cc96379cb7b0839c2fe87a503b21a6060ce6c [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jitendra Bhivare60f36e02016-08-19 15:20:24 +05302 * Copyright (C) 2005 - 2016 Broadcom
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
Jitendra Bhivare60f36e02016-08-19 15:20:24 +053010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@broadcom.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jitendra Bhivare60f36e02016-08-19 15:20:24 +053013 * linux-drivers@broadcom.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +053015 * Emulex
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070016 * 3333 Susan Street
17 * Costa Mesa, CA 92626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053018 */
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070019
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053020#include <linux/reboot.h>
21#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053023#include <linux/interrupt.h>
24#include <linux/blkdev.h>
25#include <linux/pci.h>
26#include <linux/string.h>
27#include <linux/kernel.h>
28#include <linux/semaphore.h>
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +053029#include <linux/iscsi_boot_sysfs.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040030#include <linux/module.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050031#include <linux/bsg-lib.h>
Jitendra Bhivare1094cf62016-01-20 14:10:56 +053032#include <linux/irq_poll.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053033
34#include <scsi/libiscsi.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050035#include <scsi/scsi_bsg_iscsi.h>
36#include <scsi/scsi_netlink.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053037#include <scsi/scsi_transport_iscsi.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_cmnd.h>
40#include <scsi/scsi_device.h>
41#include <scsi/scsi_host.h>
42#include <scsi/scsi.h>
43#include "be_main.h"
44#include "be_iscsi.h"
45#include "be_mgmt.h"
John Soni Jose0a513dd2012-08-20 23:00:55 +053046#include "be_cmds.h"
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053047
48static unsigned int be_iopoll_budget = 10;
49static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053050static unsigned int enable_msix = 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053051
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053052MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050053MODULE_VERSION(BUILD_STR);
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +053054MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053055MODULE_LICENSE("GPL");
56module_param(be_iopoll_budget, int, 0);
57module_param(enable_msix, int, 0);
58module_param(be_max_phys_size, uint, S_IRUGO);
John Soni Jose99bc5d52012-08-20 23:00:18 +053059MODULE_PARM_DESC(be_max_phys_size,
60 "Maximum Size (In Kilobytes) of physically contiguous "
61 "memory that can be allocated. Range is 16 - 128");
62
63#define beiscsi_disp_param(_name)\
Baoyou Xie0825b8e2016-09-26 20:00:59 +080064static ssize_t \
John Soni Jose99bc5d52012-08-20 23:00:18 +053065beiscsi_##_name##_disp(struct device *dev,\
66 struct device_attribute *attrib, char *buf) \
67{ \
68 struct Scsi_Host *shost = class_to_shost(dev);\
69 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
70 uint32_t param_val = 0; \
71 param_val = phba->attr_##_name;\
72 return snprintf(buf, PAGE_SIZE, "%d\n",\
73 phba->attr_##_name);\
74}
75
76#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
Baoyou Xie0825b8e2016-09-26 20:00:59 +080077static int \
John Soni Jose99bc5d52012-08-20 23:00:18 +053078beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
79{\
80 if (val >= _minval && val <= _maxval) {\
81 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
82 "BA_%d : beiscsi_"#_name" updated "\
83 "from 0x%x ==> 0x%x\n",\
84 phba->attr_##_name, val); \
85 phba->attr_##_name = val;\
86 return 0;\
87 } \
88 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
89 "BA_%d beiscsi_"#_name" attribute "\
90 "cannot be updated to 0x%x, "\
91 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
92 return -EINVAL;\
93}
94
95#define beiscsi_store_param(_name) \
Baoyou Xie0825b8e2016-09-26 20:00:59 +080096static ssize_t \
John Soni Jose99bc5d52012-08-20 23:00:18 +053097beiscsi_##_name##_store(struct device *dev,\
98 struct device_attribute *attr, const char *buf,\
99 size_t count) \
100{ \
101 struct Scsi_Host *shost = class_to_shost(dev);\
102 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
103 uint32_t param_val = 0;\
104 if (!isdigit(buf[0]))\
105 return -EINVAL;\
106 if (sscanf(buf, "%i", &param_val) != 1)\
107 return -EINVAL;\
108 if (beiscsi_##_name##_change(phba, param_val) == 0) \
109 return strlen(buf);\
110 else \
111 return -EINVAL;\
112}
113
114#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
Baoyou Xie0825b8e2016-09-26 20:00:59 +0800115static int \
John Soni Jose99bc5d52012-08-20 23:00:18 +0530116beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
117{ \
118 if (val >= _minval && val <= _maxval) {\
119 phba->attr_##_name = val;\
120 return 0;\
121 } \
122 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
123 "BA_%d beiscsi_"#_name" attribute " \
124 "cannot be updated to 0x%x, "\
125 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
126 phba->attr_##_name = _defval;\
127 return -EINVAL;\
128}
129
130#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
131static uint beiscsi_##_name = _defval;\
132module_param(beiscsi_##_name, uint, S_IRUGO);\
133MODULE_PARM_DESC(beiscsi_##_name, _descp);\
134beiscsi_disp_param(_name)\
135beiscsi_change_param(_name, _minval, _maxval, _defval)\
136beiscsi_store_param(_name)\
137beiscsi_init_param(_name, _minval, _maxval, _defval)\
138DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
139 beiscsi_##_name##_disp, beiscsi_##_name##_store)
140
141/*
142 * When new log level added update the
143 * the MAX allowed value for log_enable
144 */
145BEISCSI_RW_ATTR(log_enable, 0x00,
146 0xFF, 0x00, "Enable logging Bit Mask\n"
147 "\t\t\t\tInitialization Events : 0x01\n"
148 "\t\t\t\tMailbox Events : 0x02\n"
149 "\t\t\t\tMiscellaneous Events : 0x04\n"
150 "\t\t\t\tError Handling : 0x08\n"
151 "\t\t\t\tIO Path Events : 0x10\n"
Jayamohan Kallickalafb96052013-09-28 15:35:55 -0700152 "\t\t\t\tConfiguration Path : 0x20\n"
153 "\t\t\t\tiSCSI Protocol : 0x40\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +0530154
John Soni Jose5cac7592012-10-20 04:42:25 +0530155DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
John Soni Jose26000db2012-10-20 04:45:06 +0530156DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700157DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
Jayamohan Kallickald3fea9a2013-09-28 15:35:53 -0700158DEVICE_ATTR(beiscsi_phys_port, S_IRUGO, beiscsi_phys_port_disp, NULL);
Jayamohan Kallickal6103c1f2013-09-28 15:35:52 -0700159DEVICE_ATTR(beiscsi_active_session_count, S_IRUGO,
160 beiscsi_active_session_disp, NULL);
161DEVICE_ATTR(beiscsi_free_session_count, S_IRUGO,
162 beiscsi_free_session_disp, NULL);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530163struct device_attribute *beiscsi_attrs[] = {
164 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530165 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530166 &dev_attr_beiscsi_adapter_family,
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700167 &dev_attr_beiscsi_fw_ver,
Jayamohan Kallickal6103c1f2013-09-28 15:35:52 -0700168 &dev_attr_beiscsi_active_session_count,
169 &dev_attr_beiscsi_free_session_count,
Jayamohan Kallickald3fea9a2013-09-28 15:35:53 -0700170 &dev_attr_beiscsi_phys_port,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530171 NULL,
172};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530173
John Soni Jose6763daa2012-10-20 04:41:45 +0530174static char const *cqe_desc[] = {
175 "RESERVED_DESC",
176 "SOL_CMD_COMPLETE",
177 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
178 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
179 "CXN_KILLED_BURST_LEN_MISMATCH",
180 "CXN_KILLED_AHS_RCVD",
181 "CXN_KILLED_HDR_DIGEST_ERR",
182 "CXN_KILLED_UNKNOWN_HDR",
183 "CXN_KILLED_STALE_ITT_TTT_RCVD",
184 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
185 "CXN_KILLED_RST_RCVD",
186 "CXN_KILLED_TIMED_OUT",
187 "CXN_KILLED_RST_SENT",
188 "CXN_KILLED_FIN_RCVD",
189 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
190 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
191 "CXN_KILLED_OVER_RUN_RESIDUAL",
192 "CXN_KILLED_UNDER_RUN_RESIDUAL",
193 "CMD_KILLED_INVALID_STATSN_RCVD",
194 "CMD_KILLED_INVALID_R2T_RCVD",
195 "CMD_CXN_KILLED_LUN_INVALID",
196 "CMD_CXN_KILLED_ICD_INVALID",
197 "CMD_CXN_KILLED_ITT_INVALID",
198 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
199 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
200 "CXN_INVALIDATE_NOTIFY",
201 "CXN_INVALIDATE_INDEX_NOTIFY",
202 "CMD_INVALIDATED_NOTIFY",
203 "UNSOL_HDR_NOTIFY",
204 "UNSOL_DATA_NOTIFY",
205 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
206 "DRIVERMSG_NOTIFY",
207 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
208 "SOL_CMD_KILLED_DIF_ERR",
209 "CXN_KILLED_SYN_RCVD",
210 "CXN_KILLED_IMM_DATA_RCVD"
211};
212
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530213static int beiscsi_slave_configure(struct scsi_device *sdev)
214{
215 blk_queue_max_segment_size(sdev->request_queue, 65536);
216 return 0;
217}
218
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530219static int beiscsi_eh_abort(struct scsi_cmnd *sc)
220{
221 struct iscsi_cls_session *cls_session;
222 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
223 struct beiscsi_io_task *aborted_io_task;
224 struct iscsi_conn *conn;
225 struct beiscsi_conn *beiscsi_conn;
226 struct beiscsi_hba *phba;
227 struct iscsi_session *session;
228 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530229 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530230 unsigned int cid, tag, num_invalidate;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -0500231 int rc;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530232
233 cls_session = starget_to_session(scsi_target(sc->device));
234 session = cls_session->dd_data;
235
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600236 spin_lock_bh(&session->frwd_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530237 if (!aborted_task || !aborted_task->sc) {
238 /* we raced */
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600239 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530240 return SUCCESS;
241 }
242
243 aborted_io_task = aborted_task->dd_data;
244 if (!aborted_io_task->scsi_cmnd) {
245 /* raced or invalid command */
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600246 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530247 return SUCCESS;
248 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600249 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickal7626c062013-09-28 15:35:57 -0700250 /* Invalidate WRB Posted for this Task */
251 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
252 aborted_io_task->pwrb_handle->pwrb,
253 1);
254
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530255 conn = aborted_task->conn;
256 beiscsi_conn = conn->dd_data;
257 phba = beiscsi_conn->phba;
258
259 /* invalidate iocb */
260 cid = beiscsi_conn->beiscsi_conn_cid;
261 inv_tbl = phba->inv_tbl;
262 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
263 inv_tbl->cid = cid;
264 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
265 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530266 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
267 sizeof(struct invalidate_commands_params_in),
268 &nonemb_cmd.dma);
269 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530270 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
271 "BM_%d : Failed to allocate memory for"
272 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530273 return FAILED;
274 }
275 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
276
277 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
278 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530279 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530280 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
281 "BM_%d : mgmt_invalidate_icds could not be"
282 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530283 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
284 nonemb_cmd.va, nonemb_cmd.dma);
285
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530286 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530287 }
John Soni Josee175def2012-10-20 04:45:40 +0530288
Jitendra Bhivare88840332016-02-04 15:49:12 +0530289 rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -0500290 if (rc != -EBUSY)
291 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
292 nonemb_cmd.va, nonemb_cmd.dma);
293
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530294 return iscsi_eh_abort(sc);
295}
296
297static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
298{
299 struct iscsi_task *abrt_task;
300 struct beiscsi_io_task *abrt_io_task;
301 struct iscsi_conn *conn;
302 struct beiscsi_conn *beiscsi_conn;
303 struct beiscsi_hba *phba;
304 struct iscsi_session *session;
305 struct iscsi_cls_session *cls_session;
306 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530307 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530308 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -0500309 int rc;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530310
311 /* invalidate iocbs */
312 cls_session = starget_to_session(scsi_target(sc->device));
313 session = cls_session->dd_data;
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600314 spin_lock_bh(&session->frwd_lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500315 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600316 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500317 return FAILED;
318 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530319 conn = session->leadconn;
320 beiscsi_conn = conn->dd_data;
321 phba = beiscsi_conn->phba;
322 cid = beiscsi_conn->beiscsi_conn_cid;
323 inv_tbl = phba->inv_tbl;
324 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
325 num_invalidate = 0;
326 for (i = 0; i < conn->session->cmds_max; i++) {
327 abrt_task = conn->session->cmds[i];
328 abrt_io_task = abrt_task->dd_data;
329 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
330 continue;
331
Mike Christie126e9642013-12-19 01:16:21 -0600332 if (sc->device->lun != abrt_task->sc->device->lun)
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530333 continue;
334
Jayamohan Kallickal7626c062013-09-28 15:35:57 -0700335 /* Invalidate WRB Posted for this Task */
336 AMAP_SET_BITS(struct amap_iscsi_wrb, invld,
337 abrt_io_task->pwrb_handle->pwrb,
338 1);
339
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530340 inv_tbl->cid = cid;
341 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
342 num_invalidate++;
343 inv_tbl++;
344 }
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600345 spin_unlock_bh(&session->frwd_lock);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530346 inv_tbl = phba->inv_tbl;
347
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530348 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
349 sizeof(struct invalidate_commands_params_in),
350 &nonemb_cmd.dma);
351 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530352 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
353 "BM_%d : Failed to allocate memory for"
354 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530355 return FAILED;
356 }
357 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
358 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
359 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
360 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530361 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530362 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
363 "BM_%d : mgmt_invalidate_icds could not be"
364 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530365 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
366 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530367 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530368 }
John Soni Josee175def2012-10-20 04:45:40 +0530369
Jitendra Bhivare88840332016-02-04 15:49:12 +0530370 rc = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -0500371 if (rc != -EBUSY)
372 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
373 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530374 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530375}
376
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530377/*------------------- PCI Driver operations and data ----------------- */
Benoit Taine9baa3c32014-08-08 15:56:03 +0200378static const struct pci_device_id beiscsi_pci_id_table[] = {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530379 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530380 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530381 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
382 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
383 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530384 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530385 { 0 }
386};
387MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
388
John Soni Jose99bc5d52012-08-20 23:00:18 +0530389
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530390static struct scsi_host_template beiscsi_sht = {
391 .module = THIS_MODULE,
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +0530392 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530393 .proc_name = DRV_NAME,
394 .queuecommand = iscsi_queuecommand,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100395 .change_queue_depth = scsi_change_queue_depth,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530396 .slave_configure = beiscsi_slave_configure,
397 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530398 .eh_abort_handler = beiscsi_eh_abort,
399 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530400 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530401 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530402 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
403 .can_queue = BE2_IO_DEPTH,
404 .this_id = -1,
405 .max_sectors = BEISCSI_MAX_SECTORS,
406 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
407 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500408 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100409 .track_queue_depth = 1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530410};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530411
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530412static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530413
414static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
415{
416 struct beiscsi_hba *phba;
417 struct Scsi_Host *shost;
418
419 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
420 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530421 dev_err(&pcidev->dev,
422 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530423 return NULL;
424 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530425 shost->max_id = BE2_MAX_SESSIONS;
426 shost->max_channel = 0;
427 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
428 shost->max_lun = BEISCSI_NUM_MAX_LUN;
429 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530430 phba = iscsi_host_priv(shost);
431 memset(phba, 0, sizeof(*phba));
432 phba->shost = shost;
433 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530434 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500435 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530436
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530437 return phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530438}
439
440static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
441{
442 if (phba->csr_va) {
443 iounmap(phba->csr_va);
444 phba->csr_va = NULL;
445 }
446 if (phba->db_va) {
447 iounmap(phba->db_va);
448 phba->db_va = NULL;
449 }
450 if (phba->pci_va) {
451 iounmap(phba->pci_va);
452 phba->pci_va = NULL;
453 }
454}
455
456static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
457 struct pci_dev *pcidev)
458{
459 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530460 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530461
462 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
463 pci_resource_len(pcidev, 2));
464 if (addr == NULL)
465 return -ENOMEM;
466 phba->ctrl.csr = addr;
467 phba->csr_va = addr;
468 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
469
470 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
471 if (addr == NULL)
472 goto pci_map_err;
473 phba->ctrl.db = addr;
474 phba->db_va = addr;
475 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
476
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530477 if (phba->generation == BE_GEN2)
478 pcicfg_reg = 1;
479 else
480 pcicfg_reg = 0;
481
482 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
483 pci_resource_len(pcidev, pcicfg_reg));
484
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530485 if (addr == NULL)
486 goto pci_map_err;
487 phba->ctrl.pcicfg = addr;
488 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530489 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530490 return 0;
491
492pci_map_err:
493 beiscsi_unmap_pci_function(phba);
494 return -ENOMEM;
495}
496
497static int beiscsi_enable_pci(struct pci_dev *pcidev)
498{
499 int ret;
500
501 ret = pci_enable_device(pcidev);
502 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530503 dev_err(&pcidev->dev,
504 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530505 return ret;
506 }
507
John Soni Josee307f3a2015-04-25 08:17:19 +0530508 ret = pci_request_regions(pcidev, DRV_NAME);
509 if (ret) {
510 dev_err(&pcidev->dev,
511 "beiscsi_enable_pci - request region failed\n");
512 goto pci_dev_disable;
513 }
514
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530515 pci_set_master(pcidev);
Jayamohan Kallickal6c576252014-01-29 02:16:45 -0500516 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
517 if (ret) {
518 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
519 if (ret) {
520 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530521 goto pci_region_release;
Jayamohan Kallickal6c576252014-01-29 02:16:45 -0500522 } else {
523 ret = pci_set_consistent_dma_mask(pcidev,
524 DMA_BIT_MASK(32));
525 }
526 } else {
527 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530528 if (ret) {
529 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530530 goto pci_region_release;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530531 }
532 }
533 return 0;
John Soni Josee307f3a2015-04-25 08:17:19 +0530534
535pci_region_release:
536 pci_release_regions(pcidev);
537pci_dev_disable:
538 pci_disable_device(pcidev);
539
540 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530541}
542
543static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
544{
545 struct be_ctrl_info *ctrl = &phba->ctrl;
546 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
547 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
548 int status = 0;
549
550 ctrl->pdev = pdev;
551 status = beiscsi_map_pci_bars(phba, pdev);
552 if (status)
553 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530554 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
555 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
556 mbox_mem_alloc->size,
557 &mbox_mem_alloc->dma);
558 if (!mbox_mem_alloc->va) {
559 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500560 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530561 }
562
563 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
564 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
565 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
566 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Jitendra Bhivarec03a50f2016-01-20 14:10:46 +0530567 mutex_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530568 spin_lock_init(&phba->ctrl.mcc_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530569
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530570 return status;
571}
572
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700573/**
574 * beiscsi_get_params()- Set the config paramters
575 * @phba: ptr device priv structure
576 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530577static void beiscsi_get_params(struct beiscsi_hba *phba)
578{
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700579 uint32_t total_cid_count = 0;
580 uint32_t total_icd_count = 0;
581 uint8_t ulp_num = 0;
582
583 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
584 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
585
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700586 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
587 uint32_t align_mask = 0;
588 uint32_t icd_post_per_page = 0;
589 uint32_t icd_count_unavailable = 0;
590 uint32_t icd_start = 0, icd_count = 0;
591 uint32_t icd_start_align = 0, icd_count_align = 0;
592
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700593 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700594 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
595 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
596
597 /* Get ICD count that can be posted on each page */
598 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
599 sizeof(struct iscsi_sge)));
600 align_mask = (icd_post_per_page - 1);
601
602 /* Check if icd_start is aligned ICD per page posting */
603 if (icd_start % icd_post_per_page) {
604 icd_start_align = ((icd_start +
605 icd_post_per_page) &
606 ~(align_mask));
607 phba->fw_config.
608 iscsi_icd_start[ulp_num] =
609 icd_start_align;
610 }
611
612 icd_count_align = (icd_count & ~align_mask);
613
614 /* ICD discarded in the process of alignment */
615 if (icd_start_align)
616 icd_count_unavailable = ((icd_start_align -
617 icd_start) +
618 (icd_count -
619 icd_count_align));
620
621 /* Updated ICD count available */
622 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
623 icd_count_unavailable);
624
625 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
626 "BM_%d : Aligned ICD values\n"
627 "\t ICD Start : %d\n"
628 "\t ICD Count : %d\n"
629 "\t ICD Discarded : %d\n",
630 phba->fw_config.
631 iscsi_icd_start[ulp_num],
632 phba->fw_config.
633 iscsi_icd_count[ulp_num],
634 icd_count_unavailable);
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700635 break;
636 }
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700637 }
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700638
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700639 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700640 phba->params.ios_per_ctrl = (total_icd_count -
641 (total_cid_count +
642 BE2_TMFS + BE2_NOPOUT_REQ));
643 phba->params.cxns_per_ctrl = total_cid_count;
644 phba->params.asyncpdus_per_ctrl = total_cid_count;
645 phba->params.icds_per_ctrl = total_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530646 phba->params.num_sge_per_io = BE2_SGE;
647 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
648 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
649 phba->params.eq_timer = 64;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700650 phba->params.num_eq_entries = 1024;
651 phba->params.num_cq_entries = 1024;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530652 phba->params.wrbs_per_cxn = 256;
653}
654
655static void hwi_ring_eq_db(struct beiscsi_hba *phba,
656 unsigned int id, unsigned int clr_interrupt,
657 unsigned int num_processed,
658 unsigned char rearm, unsigned char event)
659{
660 u32 val = 0;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500661
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530662 if (rearm)
663 val |= 1 << DB_EQ_REARM_SHIFT;
664 if (clr_interrupt)
665 val |= 1 << DB_EQ_CLR_SHIFT;
666 if (event)
667 val |= 1 << DB_EQ_EVNT_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500668
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530669 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500670 /* Setting lower order EQ_ID Bits */
671 val |= (id & DB_EQ_RING_ID_LOW_MASK);
672
673 /* Setting Higher order EQ_ID Bits */
674 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
675 DB_EQ_RING_ID_HIGH_MASK)
676 << DB_EQ_HIGH_SET_SHIFT);
677
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530678 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
679}
680
681/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530682 * be_isr_mcc - The isr routine of the driver.
683 * @irq: Not used
684 * @dev_id: Pointer to host adapter structure
685 */
686static irqreturn_t be_isr_mcc(int irq, void *dev_id)
687{
688 struct beiscsi_hba *phba;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530689 struct be_eq_entry *eqe;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530690 struct be_queue_info *eq;
691 struct be_queue_info *mcc;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530692 unsigned int mcc_events;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530693 struct be_eq_obj *pbe_eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530694
695 pbe_eq = dev_id;
696 eq = &pbe_eq->q;
697 phba = pbe_eq->phba;
698 mcc = &phba->ctrl.mcc_obj.cq;
699 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530700
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530701 mcc_events = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530702 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
703 & EQE_VALID_MASK) {
704 if (((eqe->dw[offsetof(struct amap_eq_entry,
705 resource_id) / 32] &
706 EQE_RESID_MASK) >> 16) == mcc->id) {
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530707 mcc_events++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530708 }
709 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
710 queue_tail_inc(eq);
711 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530712 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530713
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530714 if (mcc_events) {
715 queue_work(phba->wq, &pbe_eq->mcc_work);
716 hwi_ring_eq_db(phba, eq->id, 1, mcc_events, 1, 1);
717 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530718 return IRQ_HANDLED;
719}
720
721/**
722 * be_isr_msix - The isr routine of the driver.
723 * @irq: Not used
724 * @dev_id: Pointer to host adapter structure
725 */
726static irqreturn_t be_isr_msix(int irq, void *dev_id)
727{
728 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530729 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530730 struct be_eq_obj *pbe_eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530731
732 pbe_eq = dev_id;
733 eq = &pbe_eq->q;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530734
735 phba = pbe_eq->phba;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530736 /* disable interrupt till iopoll completes */
737 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
738 irq_poll_sched(&pbe_eq->iopoll);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530739
740 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530741}
742
743/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530744 * be_isr - The isr routine of the driver.
745 * @irq: Not used
746 * @dev_id: Pointer to host adapter structure
747 */
748static irqreturn_t be_isr(int irq, void *dev_id)
749{
750 struct beiscsi_hba *phba;
751 struct hwi_controller *phwi_ctrlr;
752 struct hwi_context_memory *phwi_context;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530753 struct be_eq_entry *eqe;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530754 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530755 struct be_queue_info *mcc;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530756 unsigned int mcc_events, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530757 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530758 struct be_eq_obj *pbe_eq;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530759 int isr, rearm;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530760
761 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700762 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530763 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
764 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
765 if (!isr)
766 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530767
768 phwi_ctrlr = phba->phwi_ctrlr;
769 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530770 pbe_eq = &phwi_context->be_eq[0];
771
772 eq = &phwi_context->be_eq[0].q;
773 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530774 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530775
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530776 io_events = 0;
777 mcc_events = 0;
Jens Axboe89f8b332014-03-13 09:38:42 -0600778 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
779 & EQE_VALID_MASK) {
780 if (((eqe->dw[offsetof(struct amap_eq_entry,
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530781 resource_id) / 32] & EQE_RESID_MASK) >> 16) == mcc->id)
782 mcc_events++;
783 else
784 io_events++;
Jens Axboe89f8b332014-03-13 09:38:42 -0600785 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
786 queue_tail_inc(eq);
787 eqe = queue_tail_node(eq);
788 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530789 if (!io_events && !mcc_events)
Jens Axboe89f8b332014-03-13 09:38:42 -0600790 return IRQ_NONE;
Jitendra Bhivarea30950162016-08-19 15:20:10 +0530791
792 /* no need to rearm if interrupt is only for IOs */
793 rearm = 0;
794 if (mcc_events) {
795 queue_work(phba->wq, &pbe_eq->mcc_work);
796 /* rearm for MCCQ */
797 rearm = 1;
798 }
799 if (io_events)
800 irq_poll_sched(&pbe_eq->iopoll);
801 hwi_ring_eq_db(phba, eq->id, 0, (io_events + mcc_events), rearm, 1);
802 return IRQ_HANDLED;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530803}
804
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530805
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530806static int beiscsi_init_irqs(struct beiscsi_hba *phba)
807{
808 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530809 struct hwi_controller *phwi_ctrlr;
810 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530811 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530812
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530813 phwi_ctrlr = phba->phwi_ctrlr;
814 phwi_context = phwi_ctrlr->phwi_ctxt;
815
816 if (phba->msix_enabled) {
817 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700818 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
819 GFP_KERNEL);
820 if (!phba->msi_name[i]) {
821 ret = -ENOMEM;
822 goto free_msix_irqs;
823 }
824
825 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
826 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530827 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700828 ret = request_irq(msix_vec, be_isr_msix, 0,
829 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530830 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530831 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530832 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
833 "BM_%d : beiscsi_init_irqs-Failed to"
834 "register msix for i = %d\n",
835 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700836 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530837 goto free_msix_irqs;
838 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530839 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700840 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
841 if (!phba->msi_name[i]) {
842 ret = -ENOMEM;
843 goto free_msix_irqs;
844 }
845 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
846 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530847 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700848 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530849 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530850 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530851 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
852 "BM_%d : beiscsi_init_irqs-"
853 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700854 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530855 goto free_msix_irqs;
856 }
857
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530858 } else {
859 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
860 "beiscsi", phba);
861 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530862 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
863 "BM_%d : beiscsi_init_irqs-"
864 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530865 return ret;
866 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530867 }
868 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530869free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700870 for (j = i - 1; j >= 0; j--) {
871 kfree(phba->msi_name[j]);
872 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530873 free_irq(msix_vec, &phwi_context->be_eq[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 */
John Soni Jose99bc5d52012-08-20 23:00:18 +0530942 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
943 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
944 "value there=%p\n", phba->io_sgl_free_index,
945 phba->io_sgl_hndl_base
946 [phba->io_sgl_free_index]);
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530947 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530948 return;
949 }
950 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
951 phba->io_sgl_hndl_avbl++;
952 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
953 phba->io_sgl_free_index = 0;
954 else
955 phba->io_sgl_free_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530956 spin_unlock_irqrestore(&phba->io_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530957}
958
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530959static inline struct wrb_handle *
960beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
961 unsigned int wrbs_per_cxn)
962{
963 struct wrb_handle *pwrb_handle;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530964 unsigned long flags;
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530965
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530966 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530967 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
968 pwrb_context->wrb_handles_available--;
969 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
970 pwrb_context->alloc_index = 0;
971 else
972 pwrb_context->alloc_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +0530973 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
Jitendra Bhivarebf9b7552016-08-26 15:09:07 +0530974
975 if (pwrb_handle)
976 memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530977
978 return pwrb_handle;
979}
980
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530981/**
982 * alloc_wrb_handle - To allocate a wrb handle
983 * @phba: The hba pointer
984 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +0530985 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530986 *
987 * This happens under session_lock until submission to chip
988 */
John Soni Jose340c99e2015-08-20 04:44:30 +0530989struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530990 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530991{
992 struct hwi_wrb_context *pwrb_context;
993 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700994 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530995
996 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700997 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530998 /* return the context address */
999 *pcontext = pwrb_context;
1000 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1001}
John Soni Jose340c99e2015-08-20 04:44:30 +05301002
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301003static inline void
1004beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1005 struct wrb_handle *pwrb_handle,
1006 unsigned int wrbs_per_cxn)
1007{
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301008 unsigned long flags;
1009
1010 spin_lock_irqsave(&pwrb_context->wrb_lock, flags);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301011 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1012 pwrb_context->wrb_handles_available++;
1013 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1014 pwrb_context->free_index = 0;
1015 else
1016 pwrb_context->free_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301017 spin_unlock_irqrestore(&pwrb_context->wrb_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301018}
1019
1020/**
1021 * free_wrb_handle - To free the wrb handle back to pool
1022 * @phba: The hba pointer
1023 * @pwrb_context: The context to free from
1024 * @pwrb_handle: The wrb_handle to free
1025 *
1026 * This happens under session_lock until submission to chip
1027 */
1028static void
1029free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1030 struct wrb_handle *pwrb_handle)
1031{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301032 beiscsi_put_wrb_handle(pwrb_context,
1033 pwrb_handle,
1034 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301035 beiscsi_log(phba, KERN_INFO,
1036 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1037 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1038 "wrb_handles_available=%d\n",
1039 pwrb_handle, pwrb_context->free_index,
1040 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301041}
1042
1043static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1044{
1045 struct sgl_handle *psgl_handle;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301046 unsigned long flags;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301047
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301048 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301049 if (phba->eh_sgl_hndl_avbl) {
1050 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1051 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301052 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1053 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1054 phba->eh_sgl_alloc_index,
1055 phba->eh_sgl_alloc_index);
1056
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301057 phba->eh_sgl_hndl_avbl--;
1058 if (phba->eh_sgl_alloc_index ==
1059 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1060 1))
1061 phba->eh_sgl_alloc_index = 0;
1062 else
1063 phba->eh_sgl_alloc_index++;
1064 } else
1065 psgl_handle = NULL;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301066 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301067 return psgl_handle;
1068}
1069
1070void
1071free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1072{
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301073 unsigned long flags;
1074
1075 spin_lock_irqsave(&phba->mgmt_sgl_lock, flags);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301076 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1077 "BM_%d : In free_mgmt_sgl_handle,"
1078 "eh_sgl_free_index=%d\n",
1079 phba->eh_sgl_free_index);
1080
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301081 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1082 /*
1083 * this can happen if clean_task is called on a task that
1084 * failed in xmit_task or alloc_pdu.
1085 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301086 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1087 "BM_%d : Double Free in eh SGL ,"
1088 "eh_sgl_free_index=%d\n",
1089 phba->eh_sgl_free_index);
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301090 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301091 return;
1092 }
1093 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1094 phba->eh_sgl_hndl_avbl++;
1095 if (phba->eh_sgl_free_index ==
1096 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1097 phba->eh_sgl_free_index = 0;
1098 else
1099 phba->eh_sgl_free_index++;
Jitendra Bhivare7d2c0d62016-10-13 12:08:49 +05301100 spin_unlock_irqrestore(&phba->mgmt_sgl_lock, flags);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301101}
1102
1103static void
1104be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301105 struct iscsi_task *task,
1106 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301107{
1108 struct beiscsi_io_task *io_task = task->dd_data;
1109 struct be_status_bhs *sts_bhs =
1110 (struct be_status_bhs *)io_task->cmd_bhs;
1111 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301112 unsigned char *sense;
1113 u32 resid = 0, exp_cmdsn, max_cmdsn;
1114 u8 rsp, status, flags;
1115
John Soni Jose73133262012-10-20 04:44:49 +05301116 exp_cmdsn = csol_cqe->exp_cmdsn;
1117 max_cmdsn = (csol_cqe->exp_cmdsn +
1118 csol_cqe->cmd_wnd - 1);
1119 rsp = csol_cqe->i_resp;
1120 status = csol_cqe->i_sts;
1121 flags = csol_cqe->i_flags;
1122 resid = csol_cqe->res_cnt;
1123
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001124 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001125 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001126 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001127 io_task->scsi_cmnd = NULL;
1128 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001130 return;
1131 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301132 task->sc->result = (DID_OK << 16) | status;
1133 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1134 task->sc->result = DID_ERROR << 16;
1135 goto unmap;
1136 }
1137
1138 /* bidi not initially supported */
1139 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301140 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1141 task->sc->result = DID_ERROR << 16;
1142
1143 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1144 scsi_set_resid(task->sc, resid);
1145 if (!status && (scsi_bufflen(task->sc) - resid <
1146 task->sc->underflow))
1147 task->sc->result = DID_ERROR << 16;
1148 }
1149 }
1150
1151 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001152 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301153 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001154
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301155 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001156 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301157 memcpy(task->sc->sense_buffer, sense,
1158 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1159 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301160
John Soni Jose73133262012-10-20 04:44:49 +05301161 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1162 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301163unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301164 if (io_task->scsi_cmnd) {
1165 scsi_dma_unmap(io_task->scsi_cmnd);
1166 io_task->scsi_cmnd = NULL;
1167 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301168 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1169}
1170
1171static void
1172be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301173 struct iscsi_task *task,
1174 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301175{
1176 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301177 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301178 struct iscsi_conn *conn = beiscsi_conn->conn;
1179
1180 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301181 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301182 hdr->t2wait = 5;
1183 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301184 hdr->flags = csol_cqe->i_flags;
1185 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001186 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1187 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1188 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301189
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301190 hdr->dlength[0] = 0;
1191 hdr->dlength[1] = 0;
1192 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301193 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301194 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301195 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1196}
1197
1198static void
1199be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301200 struct iscsi_task *task,
1201 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301202{
1203 struct iscsi_tm_rsp *hdr;
1204 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301205 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301206
1207 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301208 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301209 hdr->flags = csol_cqe->i_flags;
1210 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001211 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1212 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1213 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301214
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301215 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301216 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1217}
1218
1219static void
1220hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1221 struct beiscsi_hba *phba, struct sol_cqe *psol)
1222{
1223 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001224 uint16_t wrb_index, cid, cri_index;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05301225 struct hwi_controller *phwi_ctrlr;
1226 struct wrb_handle *pwrb_handle;
1227 struct iscsi_task *task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301228
1229 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001230 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301231 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1232 wrb_idx, psol);
1233 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1234 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001235 } else {
1236 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1237 wrb_idx, psol);
1238 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1239 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301240 }
1241
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001242 cri_index = BE_GET_CRI_FROM_CID(cid);
1243 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301244 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301245 task = pwrb_handle->pio_handle;
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07001246 iscsi_put_task(task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301247}
1248
1249static void
1250be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301251 struct iscsi_task *task,
1252 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301253{
1254 struct iscsi_nopin *hdr;
1255 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301256 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301257
1258 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301259 hdr->flags = csol_cqe->i_flags;
1260 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001261 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1262 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301263
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301264 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301265 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301266 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1267}
1268
John Soni Jose73133262012-10-20 04:44:49 +05301269static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1270 struct sol_cqe *psol,
1271 struct common_sol_cqe *csol_cqe)
1272{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001273 if (is_chip_be2_be3r(phba)) {
1274 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1275 i_exp_cmd_sn, psol);
1276 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1277 i_res_cnt, psol);
1278 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1279 i_cmd_wnd, psol);
1280 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1281 wrb_index, psol);
1282 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1283 cid, psol);
1284 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1285 hw_sts, psol);
1286 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1287 i_resp, psol);
1288 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1289 i_sts, psol);
1290 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1291 i_flags, psol);
1292 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301293 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1294 i_exp_cmd_sn, psol);
1295 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1296 i_res_cnt, psol);
1297 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1298 wrb_index, psol);
1299 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1300 cid, psol);
1301 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1302 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001303 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301304 i_cmd_wnd, psol);
1305 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1306 cmd_cmpl, psol))
1307 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1308 i_sts, psol);
1309 else
1310 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1311 i_sts, psol);
1312 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1313 u, psol))
1314 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1315
1316 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1317 o, psol))
1318 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301319 }
1320}
1321
1322
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301323static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1324 struct beiscsi_hba *phba, struct sol_cqe *psol)
1325{
1326 struct hwi_wrb_context *pwrb_context;
1327 struct wrb_handle *pwrb_handle;
1328 struct iscsi_wrb *pwrb = NULL;
1329 struct hwi_controller *phwi_ctrlr;
1330 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301331 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301332 struct iscsi_conn *conn = beiscsi_conn->conn;
1333 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301334 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001335 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301336
1337 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301338
1339 /* Copy the elements to a common structure */
1340 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1341
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001342 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1343 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301344
1345 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1346 csol_cqe.wrb_index];
1347
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301348 task = pwrb_handle->pio_handle;
1349 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301350 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301351
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001352 spin_lock_bh(&session->back_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301353 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301354 case HWH_TYPE_IO:
1355 case HWH_TYPE_IO_RD:
1356 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301357 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301358 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301359 else
John Soni Jose73133262012-10-20 04:44:49 +05301360 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301361 break;
1362
1363 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301364 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301365 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301366 else
John Soni Jose73133262012-10-20 04:44:49 +05301367 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301368 break;
1369
1370 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301371 beiscsi_log(phba, KERN_ERR,
1372 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1373 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1374 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301375 break;
1376
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301377 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301378 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301379 break;
1380
1381 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301382 beiscsi_log(phba, KERN_WARNING,
1383 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1384 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1385 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301386 csol_cqe.wrb_index,
1387 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301388 break;
1389 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301390
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001391 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301392}
1393
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301394/**
1395 * ASYNC PDUs include
1396 * a. Unsolicited NOP-In (target initiated NOP-In)
1397 * b. ASYNC Messages
1398 * c. Reject PDU
1399 * d. Login response
1400 * These headers arrive unprocessed by the EP firmware.
1401 * iSCSI layer processes them.
1402 */
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301403static unsigned int
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301404beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
1405 struct pdu_base *phdr, void *pdata, unsigned int dlen)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301406{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301407 struct beiscsi_hba *phba = beiscsi_conn->phba;
1408 struct iscsi_conn *conn = beiscsi_conn->conn;
1409 struct beiscsi_io_task *io_task;
1410 struct iscsi_hdr *login_hdr;
1411 struct iscsi_task *task;
1412 u8 code;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301413
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301414 code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
1415 switch (code) {
1416 case ISCSI_OP_NOOP_IN:
1417 pdata = NULL;
1418 dlen = 0;
1419 break;
1420 case ISCSI_OP_ASYNC_EVENT:
1421 break;
1422 case ISCSI_OP_REJECT:
1423 WARN_ON(!pdata);
1424 WARN_ON(!(dlen == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301425 beiscsi_log(phba, KERN_ERR,
1426 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301427 "BM_%d : In ISCSI_OP_REJECT\n");
1428 break;
1429 case ISCSI_OP_LOGIN_RSP:
1430 case ISCSI_OP_TEXT_RSP:
1431 task = conn->login_task;
1432 io_task = task->dd_data;
1433 login_hdr = (struct iscsi_hdr *)phdr;
1434 login_hdr->itt = io_task->libiscsi_itt;
1435 break;
1436 default:
1437 beiscsi_log(phba, KERN_WARNING,
1438 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1439 "BM_%d : unrecognized async PDU opcode 0x%x\n",
1440 code);
1441 return 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301442 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301443 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301444 return 0;
1445}
1446
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301447static inline void
1448beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
1449 struct hd_async_handle *pasync_handle)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301450{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301451 if (pasync_handle->is_header) {
1452 list_add_tail(&pasync_handle->link,
1453 &pasync_ctx->async_header.free_list);
1454 pasync_ctx->async_header.free_entries++;
1455 } else {
1456 list_add_tail(&pasync_handle->link,
1457 &pasync_ctx->async_data.free_list);
1458 pasync_ctx->async_data.free_entries++;
1459 }
1460}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301461
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301462static struct hd_async_handle *
1463beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
1464 struct hd_async_context *pasync_ctx,
1465 struct i_t_dpdu_cqe *pdpdu_cqe)
1466{
1467 struct beiscsi_hba *phba = beiscsi_conn->phba;
1468 struct hd_async_handle *pasync_handle;
1469 struct be_bus_address phys_addr;
1470 u8 final, error = 0;
1471 u16 cid, code, ci;
1472 u32 dpl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301473
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301474 cid = beiscsi_conn->beiscsi_conn_cid;
1475 /**
1476 * This function is invoked to get the right async_handle structure
1477 * from a given DEF PDU CQ entry.
1478 *
1479 * - index in CQ entry gives the vertical index
1480 * - address in CQ entry is the offset where the DMA last ended
1481 * - final - no more notifications for this PDU
1482 */
1483 if (is_chip_be2_be3r(phba)) {
1484 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1485 dpl, pdpdu_cqe);
1486 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1487 index, pdpdu_cqe);
1488 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1489 final, pdpdu_cqe);
1490 } else {
1491 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1492 dpl, pdpdu_cqe);
1493 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1494 index, pdpdu_cqe);
1495 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1496 final, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301497 }
1498
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301499 /**
1500 * DB addr Hi/Lo is same for BE and SKH.
1501 * Subtract the dataplacementlength to get to the base.
1502 */
1503 phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1504 db_addr_lo, pdpdu_cqe);
1505 phys_addr.u.a32.address_lo -= dpl;
1506 phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1507 db_addr_hi, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301508
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301509 code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
1510 switch (code) {
1511 case UNSOL_HDR_NOTIFY:
1512 pasync_handle = pasync_ctx->async_entry[ci].header;
1513 break;
1514 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1515 error = 1;
1516 case UNSOL_DATA_NOTIFY:
1517 pasync_handle = pasync_ctx->async_entry[ci].data;
1518 break;
1519 /* called only for above codes */
1520 default:
1521 pasync_handle = NULL;
1522 break;
1523 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301524
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301525 if (!pasync_handle) {
1526 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1527 "BM_%d : cid %d async PDU handle not found - code %d ci %d addr %llx\n",
1528 cid, code, ci, phys_addr.u.a64.address);
1529 return pasync_handle;
1530 }
1531
1532 if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
1533 pasync_handle->index != ci) {
1534 /* driver bug - if ci does not match async handle index */
1535 error = 1;
1536 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1537 "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
1538 cid, pasync_handle->is_header ? 'H' : 'D',
1539 pasync_handle->pa.u.a64.address,
1540 pasync_handle->index,
1541 phys_addr.u.a64.address, ci);
1542 /* FW has stale address - attempt continuing by dropping */
1543 }
1544
1545 /**
1546 * Each CID is associated with unique CRI.
1547 * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
1548 **/
1549 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
1550 pasync_handle->is_final = final;
1551 pasync_handle->buffer_len = dpl;
1552 /* empty the slot */
1553 if (pasync_handle->is_header)
1554 pasync_ctx->async_entry[ci].header = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301555 else
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301556 pasync_ctx->async_entry[ci].data = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301557
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301558 /**
1559 * DEF PDU header and data buffers with errors should be simply
1560 * dropped as there are no consumers for it.
1561 */
1562 if (error) {
1563 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1564 pasync_handle = NULL;
1565 }
1566 return pasync_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301567}
1568
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301569static void
1570beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
1571 struct hd_async_context *pasync_ctx,
1572 u16 cri)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301573{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301574 struct hd_async_handle *pasync_handle, *tmp_handle;
1575 struct list_head *plist;
1576
1577 plist = &pasync_ctx->async_entry[cri].wq.list;
1578 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1579 list_del(&pasync_handle->link);
1580 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1581 }
1582
1583 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
1584 pasync_ctx->async_entry[cri].wq.hdr_len = 0;
1585 pasync_ctx->async_entry[cri].wq.bytes_received = 0;
1586 pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
1587}
1588
1589static unsigned int
1590beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
1591 struct hd_async_context *pasync_ctx,
1592 u16 cri)
1593{
1594 struct iscsi_session *session = beiscsi_conn->conn->session;
1595 struct hd_async_handle *pasync_handle, *plast_handle;
1596 struct beiscsi_hba *phba = beiscsi_conn->phba;
1597 void *phdr = NULL, *pdata = NULL;
1598 u32 dlen = 0, status = 0;
1599 struct list_head *plist;
1600
1601 plist = &pasync_ctx->async_entry[cri].wq.list;
1602 plast_handle = NULL;
1603 list_for_each_entry(pasync_handle, plist, link) {
1604 plast_handle = pasync_handle;
1605 /* get the header, the first entry */
1606 if (!phdr) {
1607 phdr = pasync_handle->pbuffer;
1608 continue;
1609 }
1610 /* use first buffer to collect all the data */
1611 if (!pdata) {
1612 pdata = pasync_handle->pbuffer;
1613 dlen = pasync_handle->buffer_len;
1614 continue;
1615 }
1616 memcpy(pdata + dlen, pasync_handle->pbuffer,
1617 pasync_handle->buffer_len);
1618 dlen += pasync_handle->buffer_len;
1619 }
1620
1621 if (!plast_handle->is_final) {
1622 /* last handle should have final PDU notification from FW */
1623 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1624 "BM_%d : cid %u %p fwd async PDU with last handle missing - HL%u:DN%u:DR%u\n",
1625 beiscsi_conn->beiscsi_conn_cid, plast_handle,
1626 pasync_ctx->async_entry[cri].wq.hdr_len,
1627 pasync_ctx->async_entry[cri].wq.bytes_needed,
1628 pasync_ctx->async_entry[cri].wq.bytes_received);
1629 }
1630 spin_lock_bh(&session->back_lock);
1631 status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
1632 spin_unlock_bh(&session->back_lock);
1633 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1634 return status;
1635}
1636
1637static unsigned int
1638beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
1639 struct hd_async_context *pasync_ctx,
1640 struct hd_async_handle *pasync_handle)
1641{
1642 unsigned int bytes_needed = 0, status = 0;
1643 u16 cri = pasync_handle->cri;
1644 struct cri_wait_queue *wq;
1645 struct beiscsi_hba *phba;
1646 struct pdu_base *ppdu;
1647 char *err = "";
1648
1649 phba = beiscsi_conn->phba;
1650 wq = &pasync_ctx->async_entry[cri].wq;
1651 if (pasync_handle->is_header) {
1652 /* check if PDU hdr is rcv'd when old hdr not completed */
1653 if (wq->hdr_len) {
1654 err = "incomplete";
1655 goto drop_pdu;
1656 }
1657 ppdu = pasync_handle->pbuffer;
1658 bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
1659 data_len_hi, ppdu);
1660 bytes_needed <<= 16;
1661 bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
1662 data_len_lo, ppdu));
1663 wq->hdr_len = pasync_handle->buffer_len;
1664 wq->bytes_received = 0;
1665 wq->bytes_needed = bytes_needed;
1666 list_add_tail(&pasync_handle->link, &wq->list);
1667 if (!bytes_needed)
1668 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1669 pasync_ctx, cri);
1670 } else {
1671 /* check if data received has header and is needed */
1672 if (!wq->hdr_len || !wq->bytes_needed) {
1673 err = "header less";
1674 goto drop_pdu;
1675 }
1676 wq->bytes_received += pasync_handle->buffer_len;
1677 /* Something got overwritten? Better catch it here. */
1678 if (wq->bytes_received > wq->bytes_needed) {
1679 err = "overflow";
1680 goto drop_pdu;
1681 }
1682 list_add_tail(&pasync_handle->link, &wq->list);
1683 if (wq->bytes_received == wq->bytes_needed)
1684 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1685 pasync_ctx, cri);
1686 }
1687 return status;
1688
1689drop_pdu:
1690 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1691 "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
1692 beiscsi_conn->beiscsi_conn_cid, err,
1693 pasync_handle->is_header ? 'H' : 'D',
1694 wq->hdr_len, wq->bytes_needed,
1695 pasync_handle->buffer_len);
1696 /* discard this handle */
1697 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1698 /* free all the other handles in cri_wait_queue */
1699 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1700 /* try continuing */
1701 return status;
1702}
1703
1704static void
1705beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
1706 u8 header, u8 ulp_num)
1707{
1708 struct hd_async_handle *pasync_handle, *tmp, **slot;
1709 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301710 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301711 struct list_head *hfree_list;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301712 struct phys_addr *pasync_sge;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301713 u32 ring_id, doorbell = 0;
1714 u16 index, num_entries;
1715 u32 doorbell_offset;
1716 u16 prod = 0, cons;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301717
1718 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001719 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001720 num_entries = pasync_ctx->num_entries;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301721 if (header) {
1722 cons = pasync_ctx->async_header.free_entries;
1723 hfree_list = &pasync_ctx->async_header.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001724 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1725 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301726 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301727 } else {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301728 cons = pasync_ctx->async_data.free_entries;
1729 hfree_list = &pasync_ctx->async_data.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001730 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1731 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301732 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301733 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301734 /* number of entries posted must be in multiples of 8 */
1735 if (cons % 8)
1736 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301737
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301738 list_for_each_entry_safe(pasync_handle, tmp, hfree_list, link) {
1739 list_del_init(&pasync_handle->link);
1740 pasync_handle->is_final = 0;
1741 pasync_handle->buffer_len = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301742
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301743 /* handles can be consumed out of order, use index in handle */
1744 index = pasync_handle->index;
1745 WARN_ON(pasync_handle->is_header != header);
1746 if (header)
1747 slot = &pasync_ctx->async_entry[index].header;
1748 else
1749 slot = &pasync_ctx->async_entry[index].data;
1750 /**
1751 * The slot just tracks handle's hold and release, so
1752 * overwriting at the same index won't do any harm but
1753 * needs to be caught.
1754 */
1755 if (*slot != NULL) {
1756 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1757 "BM_%d : async PDU %s slot at %u not empty\n",
1758 header ? "header" : "data", index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301759 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301760 /**
1761 * We use same freed index as in completion to post so this
1762 * operation is not required for refills. Its required only
1763 * for ring creation.
1764 */
1765 if (header)
1766 pasync_sge = pasync_ctx->async_header.ring_base;
1767 else
1768 pasync_sge = pasync_ctx->async_data.ring_base;
1769 pasync_sge += index;
1770 /* if its a refill then address is same; hi is lo */
1771 WARN_ON(pasync_sge->hi &&
1772 pasync_sge->hi != pasync_handle->pa.u.a32.address_lo);
1773 WARN_ON(pasync_sge->lo &&
1774 pasync_sge->lo != pasync_handle->pa.u.a32.address_hi);
1775 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1776 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301777
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301778 *slot = pasync_handle;
1779 if (++prod == cons)
1780 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301781 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301782 if (header)
1783 pasync_ctx->async_header.free_entries -= prod;
1784 else
1785 pasync_ctx->async_data.free_entries -= prod;
1786
1787 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1788 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1789 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1790 doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
1791 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301792}
1793
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301794static void
1795beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
1796 struct i_t_dpdu_cqe *pdpdu_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301797{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301798 struct beiscsi_hba *phba = beiscsi_conn->phba;
1799 struct hd_async_handle *pasync_handle = NULL;
1800 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301801 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301802 u16 cid_cri;
1803 u8 ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301804
1805 phwi_ctrlr = phba->phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301806 cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
1807 ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
1808 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1809 pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
1810 pdpdu_cqe);
1811 if (!pasync_handle)
1812 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301813
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301814 beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
1815 beiscsi_hdq_post_handles(phba, pasync_handle->is_header, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301816}
1817
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301818void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301819{
1820 struct be_queue_info *mcc_cq;
1821 struct be_mcc_compl *mcc_compl;
1822 unsigned int num_processed = 0;
1823
1824 mcc_cq = &phba->ctrl.mcc_obj.cq;
1825 mcc_compl = queue_tail_node(mcc_cq);
1826 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1827 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301828 if (beiscsi_hba_in_error(phba))
1829 return;
1830
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301831 if (num_processed >= 32) {
1832 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301833 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301834 num_processed = 0;
1835 }
1836 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05301837 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301838 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301839 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301840 }
1841
1842 mcc_compl->flags = 0;
1843 queue_tail_inc(mcc_cq);
1844 mcc_compl = queue_tail_node(mcc_cq);
1845 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1846 num_processed++;
1847 }
1848
1849 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301850 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301851}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301852
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301853static void beiscsi_mcc_work(struct work_struct *work)
1854{
1855 struct be_eq_obj *pbe_eq;
1856 struct beiscsi_hba *phba;
1857
1858 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1859 phba = pbe_eq->phba;
1860 beiscsi_process_mcc_cq(phba);
1861 /* rearm EQ for further interrupts */
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301862 if (!beiscsi_hba_in_error(phba))
1863 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301864}
1865
John Soni Jose6763daa2012-10-20 04:41:45 +05301866/**
1867 * beiscsi_process_cq()- Process the Completion Queue
1868 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301869 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05301870 *
1871 * return
1872 * Number of Completion Entries processed.
1873 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301874unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301875{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301876 struct be_queue_info *cq;
1877 struct sol_cqe *sol;
1878 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301879 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301880 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301881 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001882 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301883 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301884 struct beiscsi_endpoint *beiscsi_ep;
1885 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301886 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301887
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301888 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301889 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301890 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301891
1892 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1893 CQE_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301894 if (beiscsi_hba_in_error(phba))
1895 return 0;
1896
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301897 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1898
John Soni Jose73133262012-10-20 04:44:49 +05301899 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
1900 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301901
John Soni Jose73133262012-10-20 04:44:49 +05301902 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001903 if (is_chip_be2_be3r(phba)) {
1904 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1905 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301906 if ((code == DRIVERMSG_NOTIFY) ||
1907 (code == UNSOL_HDR_NOTIFY) ||
1908 (code == UNSOL_DATA_NOTIFY))
1909 cid = AMAP_GET_BITS(
1910 struct amap_i_t_dpdu_cqe_v2,
1911 cid, sol);
1912 else
1913 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1914 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001915 }
John Soni Jose73133262012-10-20 04:44:49 +05301916
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001917 cri_index = BE_GET_CRI_FROM_CID(cid);
1918 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04001919
1920 if (ep == NULL) {
1921 /* connection has already been freed
1922 * just move on to next one
1923 */
1924 beiscsi_log(phba, KERN_WARNING,
1925 BEISCSI_LOG_INIT,
1926 "BM_%d : proc cqe of disconn ep: cid %d\n",
1927 cid);
1928 goto proc_next_cqe;
1929 }
1930
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301931 beiscsi_ep = ep->dd_data;
1932 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301933
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301934 /* replenish cq */
1935 if (num_processed == 32) {
1936 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301937 num_processed = 0;
1938 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301939 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301940
John Soni Jose0a513dd2012-08-20 23:00:55 +05301941 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301942 case SOL_CMD_COMPLETE:
1943 hwi_complete_cmd(beiscsi_conn, phba, sol);
1944 break;
1945 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301946 beiscsi_log(phba, KERN_INFO,
1947 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301948 "BM_%d : Received %s[%d] on CID : %d\n",
1949 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301950
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301951 dmsg = (struct dmsg_cqe *)sol;
1952 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1953 break;
1954 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301955 beiscsi_log(phba, KERN_INFO,
1956 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301957 "BM_%d : Received %s[%d] on CID : %d\n",
1958 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301959
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001960 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301961 beiscsi_hdq_process_compl(beiscsi_conn,
1962 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001963 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301964 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301965 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301966 beiscsi_log(phba, KERN_INFO,
1967 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301968 "BM_%d : Received %s[%d] on CID : %d\n",
1969 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301970
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001971 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301972 beiscsi_hdq_process_compl(beiscsi_conn,
1973 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001974 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301975 break;
1976 case CXN_INVALIDATE_INDEX_NOTIFY:
1977 case CMD_INVALIDATED_NOTIFY:
1978 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301979 beiscsi_log(phba, KERN_ERR,
1980 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301981 "BM_%d : Ignoring %s[%d] on CID : %d\n",
1982 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301983 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301984 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301985 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301986 beiscsi_log(phba, KERN_ERR,
1987 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1988 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1989 cqe_desc[code], code, cid);
1990 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301991 case CMD_KILLED_INVALID_STATSN_RCVD:
1992 case CMD_KILLED_INVALID_R2T_RCVD:
1993 case CMD_CXN_KILLED_LUN_INVALID:
1994 case CMD_CXN_KILLED_ICD_INVALID:
1995 case CMD_CXN_KILLED_ITT_INVALID:
1996 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1997 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301998 beiscsi_log(phba, KERN_ERR,
1999 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302000 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2001 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302002 break;
2003 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302004 beiscsi_log(phba, KERN_ERR,
2005 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302006 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2007 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002008 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302009 /* driver consumes the entry and drops the contents */
2010 beiscsi_hdq_process_compl(beiscsi_conn,
2011 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002012 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302013 break;
2014 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2015 case CXN_KILLED_BURST_LEN_MISMATCH:
2016 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302017 case CXN_KILLED_UNKNOWN_HDR:
2018 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2019 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2020 case CXN_KILLED_TIMED_OUT:
2021 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302022 case CXN_KILLED_RST_SENT:
2023 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2025 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2026 case CXN_KILLED_OVER_RUN_RESIDUAL:
2027 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2028 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302029 beiscsi_log(phba, KERN_ERR,
2030 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302031 "BM_%d : Event %s[%d] received on CID : %d\n",
2032 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302033 if (beiscsi_conn)
2034 iscsi_conn_failure(beiscsi_conn->conn,
2035 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302036 break;
2037 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302038 beiscsi_log(phba, KERN_ERR,
2039 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302040 "BM_%d : Invalid CQE Event Received Code : %d"
2041 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302042 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302043 break;
2044 }
2045
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002046proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302047 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2048 queue_tail_inc(cq);
2049 sol = queue_tail_node(cq);
2050 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302051 if (total == budget)
2052 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302053 }
2054
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302055 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2056 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302057}
2058
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002059static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302060{
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302061 unsigned int ret, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302062 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302063 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302064 struct be_eq_entry *eqe = NULL;
2065 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302066
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302067 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302068 phba = pbe_eq->phba;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302069 if (beiscsi_hba_in_error(phba)) {
2070 irq_poll_complete(iop);
2071 return 0;
2072 }
2073
2074 io_events = 0;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302075 eq = &pbe_eq->q;
2076 eqe = queue_tail_node(eq);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302077 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2078 EQE_VALID_MASK) {
2079 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2080 queue_tail_inc(eq);
2081 eqe = queue_tail_node(eq);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302082 io_events++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302083 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302084 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302085
2086 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002087 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302088 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002089 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302090 beiscsi_log(phba, KERN_INFO,
2091 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302092 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2093 pbe_eq->q.id, ret);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302094 if (!beiscsi_hba_in_error(phba))
2095 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302096 }
2097 return ret;
2098}
2099
2100static void
John Soni Jose09a10932012-10-20 04:44:23 +05302101hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2102 unsigned int num_sg, struct beiscsi_io_task *io_task)
2103{
2104 struct iscsi_sge *psgl;
2105 unsigned int sg_len, index;
2106 unsigned int sge_len = 0;
2107 unsigned long long addr;
2108 struct scatterlist *l_sg;
2109 unsigned int offset;
2110
2111 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2112 io_task->bhs_pa.u.a32.address_lo);
2113 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2114 io_task->bhs_pa.u.a32.address_hi);
2115
2116 l_sg = sg;
2117 for (index = 0; (index < num_sg) && (index < 2); index++,
2118 sg = sg_next(sg)) {
2119 if (index == 0) {
2120 sg_len = sg_dma_len(sg);
2121 addr = (u64) sg_dma_address(sg);
2122 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2123 sge0_addr_lo, pwrb,
2124 lower_32_bits(addr));
2125 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2126 sge0_addr_hi, pwrb,
2127 upper_32_bits(addr));
2128 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2129 sge0_len, pwrb,
2130 sg_len);
2131 sge_len = sg_len;
2132 } else {
2133 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2134 pwrb, sge_len);
2135 sg_len = sg_dma_len(sg);
2136 addr = (u64) sg_dma_address(sg);
2137 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2138 sge1_addr_lo, pwrb,
2139 lower_32_bits(addr));
2140 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2141 sge1_addr_hi, pwrb,
2142 upper_32_bits(addr));
2143 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2144 sge1_len, pwrb,
2145 sg_len);
2146 }
2147 }
2148 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2149 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2150
2151 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2152
2153 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2154 io_task->bhs_pa.u.a32.address_hi);
2155 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2156 io_task->bhs_pa.u.a32.address_lo);
2157
2158 if (num_sg == 1) {
2159 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2160 1);
2161 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2162 0);
2163 } else if (num_sg == 2) {
2164 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2165 0);
2166 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2167 1);
2168 } else {
2169 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2170 0);
2171 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2172 0);
2173 }
2174
2175 sg = l_sg;
2176 psgl++;
2177 psgl++;
2178 offset = 0;
2179 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2180 sg_len = sg_dma_len(sg);
2181 addr = (u64) sg_dma_address(sg);
2182 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2183 lower_32_bits(addr));
2184 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2185 upper_32_bits(addr));
2186 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2187 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2188 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2189 offset += sg_len;
2190 }
2191 psgl--;
2192 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2193}
2194
2195static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302196hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2197 unsigned int num_sg, struct beiscsi_io_task *io_task)
2198{
2199 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302200 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302201 unsigned int sge_len = 0;
2202 unsigned long long addr;
2203 struct scatterlist *l_sg;
2204 unsigned int offset;
2205
2206 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2207 io_task->bhs_pa.u.a32.address_lo);
2208 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2209 io_task->bhs_pa.u.a32.address_hi);
2210
2211 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302212 for (index = 0; (index < num_sg) && (index < 2); index++,
2213 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302214 if (index == 0) {
2215 sg_len = sg_dma_len(sg);
2216 addr = (u64) sg_dma_address(sg);
2217 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302218 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302219 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302220 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302221 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2222 sg_len);
2223 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302224 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302225 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2226 pwrb, sge_len);
2227 sg_len = sg_dma_len(sg);
2228 addr = (u64) sg_dma_address(sg);
2229 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302230 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302231 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302232 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302233 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2234 sg_len);
2235 }
2236 }
2237 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2238 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2239
2240 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2241
2242 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2243 io_task->bhs_pa.u.a32.address_hi);
2244 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2245 io_task->bhs_pa.u.a32.address_lo);
2246
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302247 if (num_sg == 1) {
2248 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2249 1);
2250 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2251 0);
2252 } else if (num_sg == 2) {
2253 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2254 0);
2255 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2256 1);
2257 } else {
2258 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2259 0);
2260 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2261 0);
2262 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302263 sg = l_sg;
2264 psgl++;
2265 psgl++;
2266 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302267 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302268 sg_len = sg_dma_len(sg);
2269 addr = (u64) sg_dma_address(sg);
2270 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2271 (addr & 0xFFFFFFFF));
2272 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2273 (addr >> 32));
2274 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2275 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2276 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2277 offset += sg_len;
2278 }
2279 psgl--;
2280 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2281}
2282
John Soni Josed629c472012-10-20 04:42:00 +05302283/**
2284 * hwi_write_buffer()- Populate the WRB with task info
2285 * @pwrb: ptr to the WRB entry
2286 * @task: iscsi task which is to be executed
2287 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002288static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302289{
2290 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302291 struct beiscsi_io_task *io_task = task->dd_data;
2292 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2293 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302294 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302295
2296 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2297 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2298 io_task->bhs_pa.u.a32.address_lo);
2299 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2300 io_task->bhs_pa.u.a32.address_hi);
2301
2302 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302303
2304 /* Check for the data_count */
2305 dsp_value = (task->data_count) ? 1 : 0;
2306
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002307 if (is_chip_be2_be3r(phba))
2308 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302309 pwrb, dsp_value);
2310 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002311 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302312 pwrb, dsp_value);
2313
2314 /* Map addr only if there is data_count */
2315 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302316 io_task->mtask_addr = pci_map_single(phba->pcidev,
2317 task->data,
2318 task->data_count,
2319 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002320 if (pci_dma_mapping_error(phba->pcidev,
2321 io_task->mtask_addr))
2322 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302323 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302324 } else
John Soni Josed629c472012-10-20 04:42:00 +05302325 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302326
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302327 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302328 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302329 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302330 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302331 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2332 task->data_count);
2333
2334 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2335 } else {
2336 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302337 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302338 }
2339
2340 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2341
2342 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2343
2344 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2345 io_task->bhs_pa.u.a32.address_hi);
2346 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2347 io_task->bhs_pa.u.a32.address_lo);
2348 if (task->data) {
2349 psgl++;
2350 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2351 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2352 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2353 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2354 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2355 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2356
2357 psgl++;
2358 if (task->data) {
2359 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302360 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302361 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302362 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302363 }
2364 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2365 }
2366 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002367 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302368}
2369
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002370/**
2371 * beiscsi_find_mem_req()- Find mem needed
2372 * @phba: ptr to HBA struct
2373 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302374static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2375{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002376 uint8_t mem_descr_index, ulp_num;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302377 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302378 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2379 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2380
2381 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2382 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302383
2384 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2385
2386 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2387 BE_ISCSI_PDU_HEADER_SIZE;
2388 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2389 sizeof(struct hwi_context_memory);
2390
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302391
2392 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2393 * (phba->params.wrbs_per_cxn)
2394 * phba->params.cxns_per_ctrl;
2395 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2396 (phba->params.wrbs_per_cxn);
2397 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2398 phba->params.cxns_per_ctrl);
2399
2400 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2401 phba->params.icds_per_ctrl;
2402 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2403 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002404 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2405 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302406
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002407 num_async_pdu_buf_sgl_pages =
2408 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2409 phba, ulp_num) *
2410 sizeof(struct phys_addr));
2411
2412 num_async_pdu_buf_pages =
2413 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2414 phba, ulp_num) *
2415 phba->params.defpdu_hdr_sz);
2416
2417 num_async_pdu_data_pages =
2418 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2419 phba, ulp_num) *
2420 phba->params.defpdu_data_sz);
2421
2422 num_async_pdu_data_sgl_pages =
2423 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2424 phba, ulp_num) *
2425 sizeof(struct phys_addr));
2426
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002427 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2428 (ulp_num * MEM_DESCR_OFFSET));
2429 phba->mem_req[mem_descr_index] =
2430 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2431 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2432
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002433 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2434 (ulp_num * MEM_DESCR_OFFSET));
2435 phba->mem_req[mem_descr_index] =
2436 num_async_pdu_buf_pages *
2437 PAGE_SIZE;
2438
2439 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2440 (ulp_num * MEM_DESCR_OFFSET));
2441 phba->mem_req[mem_descr_index] =
2442 num_async_pdu_data_pages *
2443 PAGE_SIZE;
2444
2445 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2446 (ulp_num * MEM_DESCR_OFFSET));
2447 phba->mem_req[mem_descr_index] =
2448 num_async_pdu_buf_sgl_pages *
2449 PAGE_SIZE;
2450
2451 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2452 (ulp_num * MEM_DESCR_OFFSET));
2453 phba->mem_req[mem_descr_index] =
2454 num_async_pdu_data_sgl_pages *
2455 PAGE_SIZE;
2456
2457 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2458 (ulp_num * MEM_DESCR_OFFSET));
2459 phba->mem_req[mem_descr_index] =
2460 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302461 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002462
2463 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2464 (ulp_num * MEM_DESCR_OFFSET));
2465 phba->mem_req[mem_descr_index] =
2466 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302467 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002468
2469 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2470 (ulp_num * MEM_DESCR_OFFSET));
2471 phba->mem_req[mem_descr_index] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302472 sizeof(struct hd_async_context) +
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002473 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302474 sizeof(struct hd_async_entry));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002475 }
2476 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302477}
2478
2479static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2480{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302481 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002482 struct hwi_controller *phwi_ctrlr;
2483 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302484 struct mem_array *mem_arr, *mem_arr_orig;
2485 unsigned int i, j, alloc_size, curr_alloc_size;
2486
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002487 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302488 if (!phba->phwi_ctrlr)
2489 return -ENOMEM;
2490
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002491 /* Allocate memory for wrb_context */
2492 phwi_ctrlr = phba->phwi_ctrlr;
2493 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2494 phba->params.cxns_per_ctrl,
2495 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002496 if (!phwi_ctrlr->wrb_context) {
2497 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002498 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002499 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002500
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302501 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2502 GFP_KERNEL);
2503 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002504 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302505 kfree(phba->phwi_ctrlr);
2506 return -ENOMEM;
2507 }
2508
2509 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2510 GFP_KERNEL);
2511 if (!mem_arr_orig) {
2512 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002513 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302514 kfree(phba->phwi_ctrlr);
2515 return -ENOMEM;
2516 }
2517
2518 mem_descr = phba->init_mem;
2519 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002520 if (!phba->mem_req[i]) {
2521 mem_descr->mem_array = NULL;
2522 mem_descr++;
2523 continue;
2524 }
2525
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302526 j = 0;
2527 mem_arr = mem_arr_orig;
2528 alloc_size = phba->mem_req[i];
2529 memset(mem_arr, 0, sizeof(struct mem_array) *
2530 BEISCSI_MAX_FRAGS_INIT);
2531 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2532 do {
2533 mem_arr->virtual_address = pci_alloc_consistent(
2534 phba->pcidev,
2535 curr_alloc_size,
2536 &bus_add);
2537 if (!mem_arr->virtual_address) {
2538 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2539 goto free_mem;
2540 if (curr_alloc_size -
2541 rounddown_pow_of_two(curr_alloc_size))
2542 curr_alloc_size = rounddown_pow_of_two
2543 (curr_alloc_size);
2544 else
2545 curr_alloc_size = curr_alloc_size / 2;
2546 } else {
2547 mem_arr->bus_address.u.
2548 a64.address = (__u64) bus_add;
2549 mem_arr->size = curr_alloc_size;
2550 alloc_size -= curr_alloc_size;
2551 curr_alloc_size = min(be_max_phys_size *
2552 1024, alloc_size);
2553 j++;
2554 mem_arr++;
2555 }
2556 } while (alloc_size);
2557 mem_descr->num_elements = j;
2558 mem_descr->size_in_bytes = phba->mem_req[i];
2559 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2560 GFP_KERNEL);
2561 if (!mem_descr->mem_array)
2562 goto free_mem;
2563
2564 memcpy(mem_descr->mem_array, mem_arr_orig,
2565 sizeof(struct mem_array) * j);
2566 mem_descr++;
2567 }
2568 kfree(mem_arr_orig);
2569 return 0;
2570free_mem:
2571 mem_descr->num_elements = j;
2572 while ((i) || (j)) {
2573 for (j = mem_descr->num_elements; j > 0; j--) {
2574 pci_free_consistent(phba->pcidev,
2575 mem_descr->mem_array[j - 1].size,
2576 mem_descr->mem_array[j - 1].
2577 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302578 (unsigned long)mem_descr->
2579 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302580 bus_address.u.a64.address);
2581 }
2582 if (i) {
2583 i--;
2584 kfree(mem_descr->mem_array);
2585 mem_descr--;
2586 }
2587 }
2588 kfree(mem_arr_orig);
2589 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002590 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302591 kfree(phba->phwi_ctrlr);
2592 return -ENOMEM;
2593}
2594
2595static int beiscsi_get_memory(struct beiscsi_hba *phba)
2596{
2597 beiscsi_find_mem_req(phba);
2598 return beiscsi_alloc_mem(phba);
2599}
2600
2601static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2602{
2603 struct pdu_data_out *pdata_out;
2604 struct pdu_nop_out *pnop_out;
2605 struct be_mem_descriptor *mem_descr;
2606
2607 mem_descr = phba->init_mem;
2608 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2609 pdata_out =
2610 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2611 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2612
2613 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2614 IIOC_SCSI_DATA);
2615
2616 pnop_out =
2617 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2618 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2619
2620 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2621 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2622 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2623 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2624}
2625
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002626static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302627{
2628 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002629 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002630 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302631 struct hwi_controller *phwi_ctrlr;
2632 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002633 struct iscsi_wrb *pwrb = NULL;
2634 unsigned int num_cxn_wrbh = 0;
2635 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302636
2637 mem_descr_wrbh = phba->init_mem;
2638 mem_descr_wrbh += HWI_MEM_WRBH;
2639
2640 mem_descr_wrb = phba->init_mem;
2641 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302642 phwi_ctrlr = phba->phwi_ctrlr;
2643
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002644 /* Allocate memory for WRBQ */
2645 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2646 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002647 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002648 GFP_KERNEL);
2649 if (!phwi_ctxt->be_wrbq) {
2650 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2651 "BM_%d : WRBQ Mem Alloc Failed\n");
2652 return -ENOMEM;
2653 }
2654
2655 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302656 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302657 pwrb_context->pwrb_handle_base =
2658 kzalloc(sizeof(struct wrb_handle *) *
2659 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002660 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302661 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2662 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002663 goto init_wrb_hndl_failed;
2664 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302665 pwrb_context->pwrb_handle_basestd =
2666 kzalloc(sizeof(struct wrb_handle *) *
2667 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002668 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302669 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2670 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002671 goto init_wrb_hndl_failed;
2672 }
2673 if (!num_cxn_wrbh) {
2674 pwrb_handle =
2675 mem_descr_wrbh->mem_array[idx].virtual_address;
2676 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2677 ((sizeof(struct wrb_handle)) *
2678 phba->params.wrbs_per_cxn));
2679 idx++;
2680 }
2681 pwrb_context->alloc_index = 0;
2682 pwrb_context->wrb_handles_available = 0;
2683 pwrb_context->free_index = 0;
2684
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302685 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302686 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2687 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2688 pwrb_context->pwrb_handle_basestd[j] =
2689 pwrb_handle;
2690 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302691 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302692 pwrb_handle++;
2693 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302694 num_cxn_wrbh--;
2695 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302696 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302697 }
2698 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002699 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302700 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002701 if (!num_cxn_wrb) {
2702 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2703 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2704 ((sizeof(struct iscsi_wrb) *
2705 phba->params.wrbs_per_cxn));
2706 idx++;
2707 }
2708
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302709 if (num_cxn_wrb) {
2710 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2711 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2712 pwrb_handle->pwrb = pwrb;
2713 pwrb++;
2714 }
2715 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716 }
2717 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002718 return 0;
2719init_wrb_hndl_failed:
2720 for (j = index; j > 0; j--) {
2721 pwrb_context = &phwi_ctrlr->wrb_context[j];
2722 kfree(pwrb_context->pwrb_handle_base);
2723 kfree(pwrb_context->pwrb_handle_basestd);
2724 }
2725 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302726}
2727
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002728static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302729{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002730 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302731 struct hwi_controller *phwi_ctrlr;
2732 struct hba_parameters *p = &phba->params;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302733 struct hd_async_context *pasync_ctx;
2734 struct hd_async_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002735 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302736 struct be_mem_descriptor *mem_descr;
2737
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002738 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2739 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302740 /* get async_ctx for each ULP */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002741 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2742 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2743 (ulp_num * MEM_DESCR_OFFSET));
2744
2745 phwi_ctrlr = phba->phwi_ctrlr;
2746 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302747 (struct hd_async_context *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002748 mem_descr->mem_array[0].virtual_address;
2749
2750 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2751 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2752
2753 pasync_ctx->async_entry =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302754 (struct hd_async_entry *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002755 ((long unsigned int)pasync_ctx +
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302756 sizeof(struct hd_async_context));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002757
2758 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2759 ulp_num);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302760 /* setup header buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002761 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2762 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2763 (ulp_num * MEM_DESCR_OFFSET);
2764 if (mem_descr->mem_array[0].virtual_address) {
2765 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2766 "BM_%d : hwi_init_async_pdu_ctx"
2767 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2768 ulp_num,
2769 mem_descr->mem_array[0].
2770 virtual_address);
2771 } else
2772 beiscsi_log(phba, KERN_WARNING,
2773 BEISCSI_LOG_INIT,
2774 "BM_%d : No Virtual address for ULP : %d\n",
2775 ulp_num);
2776
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302777 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002778 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302779 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302780
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002781 pasync_ctx->async_header.pa_base.u.a64.address =
2782 mem_descr->mem_array[0].
2783 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002784
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302785 /* setup header buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002786 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2787 mem_descr += HWI_MEM_ASYNC_HEADER_RING_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_RING_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.ring_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 header buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002806 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2807 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_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_HEADER_HANDLE_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);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302821
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002822 pasync_ctx->async_header.handle_base =
2823 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002824 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302825
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302826 /* setup data buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002827 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2828 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2829 (ulp_num * MEM_DESCR_OFFSET);
2830 if (mem_descr->mem_array[0].virtual_address) {
2831 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2832 "BM_%d : hwi_init_async_pdu_ctx"
2833 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2834 ulp_num,
2835 mem_descr->mem_array[0].
2836 virtual_address);
2837 } else
2838 beiscsi_log(phba, KERN_WARNING,
2839 BEISCSI_LOG_INIT,
2840 "BM_%d : No Virtual address for ULP : %d\n",
2841 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302842
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002843 pasync_ctx->async_data.ring_base =
2844 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302845
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302846 /* setup data buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002847 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2848 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2849 (ulp_num * MEM_DESCR_OFFSET);
2850 if (!mem_descr->mem_array[0].virtual_address)
2851 beiscsi_log(phba, KERN_WARNING,
2852 BEISCSI_LOG_INIT,
2853 "BM_%d : No Virtual address for ULP : %d\n",
2854 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302855
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002856 pasync_ctx->async_data.handle_base =
2857 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002858 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302859
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002860 pasync_header_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302861 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002862 pasync_ctx->async_header.handle_base;
2863 pasync_data_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302864 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002865 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302866
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302867 /* setup data buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002868 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2869 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2870 (ulp_num * MEM_DESCR_OFFSET);
2871 if (mem_descr->mem_array[0].virtual_address) {
2872 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2873 "BM_%d : hwi_init_async_pdu_ctx"
2874 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2875 ulp_num,
2876 mem_descr->mem_array[0].
2877 virtual_address);
2878 } else
2879 beiscsi_log(phba, KERN_WARNING,
2880 BEISCSI_LOG_INIT,
2881 "BM_%d : No Virtual address for ULP : %d\n",
2882 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302883
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002884 idx = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302885 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002886 pasync_ctx->async_data.va_base =
2887 mem_descr->mem_array[idx].virtual_address;
2888 pasync_ctx->async_data.pa_base.u.a64.address =
2889 mem_descr->mem_array[idx].
2890 bus_address.u.a64.address;
2891
2892 num_async_data = ((mem_descr->mem_array[idx].size) /
2893 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002894 num_per_mem = 0;
2895
2896 for (index = 0; index < BEISCSI_GET_CID_COUNT
2897 (phba, ulp_num); index++) {
2898 pasync_header_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302899 pasync_header_h->is_header = 1;
2900 pasync_header_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002901 INIT_LIST_HEAD(&pasync_header_h->link);
2902 pasync_header_h->pbuffer =
2903 (void *)((unsigned long)
2904 (pasync_ctx->
2905 async_header.va_base) +
2906 (p->defpdu_hdr_sz * index));
2907
2908 pasync_header_h->pa.u.a64.address =
2909 pasync_ctx->async_header.pa_base.u.a64.
2910 address + (p->defpdu_hdr_sz * index);
2911
2912 list_add_tail(&pasync_header_h->link,
2913 &pasync_ctx->async_header.
2914 free_list);
2915 pasync_header_h++;
2916 pasync_ctx->async_header.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302917 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2918 wq.list);
2919 pasync_ctx->async_entry[index].header = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002920
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002921 pasync_data_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302922 pasync_data_h->is_header = 0;
2923 pasync_data_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002924 INIT_LIST_HEAD(&pasync_data_h->link);
2925
2926 if (!num_async_data) {
2927 num_per_mem = 0;
2928 idx++;
2929 pasync_ctx->async_data.va_base =
2930 mem_descr->mem_array[idx].
2931 virtual_address;
2932 pasync_ctx->async_data.pa_base.u.
2933 a64.address =
2934 mem_descr->mem_array[idx].
2935 bus_address.u.a64.address;
2936 num_async_data =
2937 ((mem_descr->mem_array[idx].
2938 size) /
2939 phba->params.defpdu_data_sz);
2940 }
2941 pasync_data_h->pbuffer =
2942 (void *)((unsigned long)
2943 (pasync_ctx->async_data.va_base) +
2944 (p->defpdu_data_sz * num_per_mem));
2945
2946 pasync_data_h->pa.u.a64.address =
2947 pasync_ctx->async_data.pa_base.u.a64.
2948 address + (p->defpdu_data_sz *
2949 num_per_mem);
2950 num_per_mem++;
2951 num_async_data--;
2952
2953 list_add_tail(&pasync_data_h->link,
2954 &pasync_ctx->async_data.
2955 free_list);
2956 pasync_data_h++;
2957 pasync_ctx->async_data.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302958 pasync_ctx->async_entry[index].data = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002959 }
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002960 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302961 }
2962
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002963 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302964}
2965
2966static int
2967be_sgl_create_contiguous(void *virtual_address,
2968 u64 physical_address, u32 length,
2969 struct be_dma_mem *sgl)
2970{
2971 WARN_ON(!virtual_address);
2972 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06002973 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302974 WARN_ON(!sgl);
2975
2976 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302977 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302978 sgl->size = length;
2979
2980 return 0;
2981}
2982
2983static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2984{
2985 memset(sgl, 0, sizeof(*sgl));
2986}
2987
2988static void
2989hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2990 struct mem_array *pmem, struct be_dma_mem *sgl)
2991{
2992 if (sgl->va)
2993 be_sgl_destroy_contiguous(sgl);
2994
2995 be_sgl_create_contiguous(pmem->virtual_address,
2996 pmem->bus_address.u.a64.address,
2997 pmem->size, sgl);
2998}
2999
3000static void
3001hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
3002 struct mem_array *pmem, struct be_dma_mem *sgl)
3003{
3004 if (sgl->va)
3005 be_sgl_destroy_contiguous(sgl);
3006
3007 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
3008 pmem->bus_address.u.a64.address,
3009 pmem->size, sgl);
3010}
3011
3012static int be_fill_queue(struct be_queue_info *q,
3013 u16 len, u16 entry_size, void *vaddress)
3014{
3015 struct be_dma_mem *mem = &q->dma_mem;
3016
3017 memset(q, 0, sizeof(*q));
3018 q->len = len;
3019 q->entry_size = entry_size;
3020 mem->size = len * entry_size;
3021 mem->va = vaddress;
3022 if (!mem->va)
3023 return -ENOMEM;
3024 memset(mem->va, 0, mem->size);
3025 return 0;
3026}
3027
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303028static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303029 struct hwi_context_memory *phwi_context)
3030{
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303031 int ret = -ENOMEM, eq_for_mcc;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303032 unsigned int i, num_eq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303033 struct be_queue_info *eq;
3034 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303035 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303036 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303037
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303038 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3039 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303040
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303041 if (phba->msix_enabled)
3042 eq_for_mcc = 1;
3043 else
3044 eq_for_mcc = 0;
3045 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3046 eq = &phwi_context->be_eq[i].q;
3047 mem = &eq->dma_mem;
3048 phwi_context->be_eq[i].phba = phba;
3049 eq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303050 num_eq_pages * PAGE_SIZE,
3051 &paddr);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303052 if (!eq_vaddress)
3053 goto create_eq_error;
3054
3055 mem->va = eq_vaddress;
3056 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3057 sizeof(struct be_eq_entry), eq_vaddress);
3058 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303059 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3060 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303061 goto create_eq_error;
3062 }
3063
3064 mem->dma = paddr;
3065 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3066 phwi_context->cur_eqd);
3067 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303068 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3069 "BM_%d : beiscsi_cmd_eq_create"
3070 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303071 goto create_eq_error;
3072 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303073
3074 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3075 "BM_%d : eqid = %d\n",
3076 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303078 return 0;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303079
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303080create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303081 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303082 eq = &phwi_context->be_eq[i].q;
3083 mem = &eq->dma_mem;
3084 if (mem->va)
3085 pci_free_consistent(phba->pcidev, num_eq_pages
3086 * PAGE_SIZE,
3087 mem->va, mem->dma);
3088 }
3089 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303090}
3091
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303092static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303093 struct hwi_context_memory *phwi_context)
3094{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303095 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303096 struct be_queue_info *cq, *eq;
3097 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303098 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303099 void *cq_vaddress;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303100 int ret = -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303101 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303102
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303103 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3104 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303105
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303106 for (i = 0; i < phba->num_cpus; i++) {
3107 cq = &phwi_context->be_cq[i];
3108 eq = &phwi_context->be_eq[i].q;
3109 pbe_eq = &phwi_context->be_eq[i];
3110 pbe_eq->cq = cq;
3111 pbe_eq->phba = phba;
3112 mem = &cq->dma_mem;
3113 cq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303114 num_cq_pages * PAGE_SIZE,
3115 &paddr);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303116 if (!cq_vaddress)
3117 goto create_cq_error;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303118
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303119 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303120 sizeof(struct sol_cqe), cq_vaddress);
3121 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303122 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3123 "BM_%d : be_fill_queue Failed "
3124 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303125 goto create_cq_error;
3126 }
3127
3128 mem->dma = paddr;
3129 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3130 false, 0);
3131 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303132 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3133 "BM_%d : beiscsi_cmd_eq_create"
3134 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303135 goto create_cq_error;
3136 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303137 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3138 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3139 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303140 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303141 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303142
3143create_cq_error:
3144 for (i = 0; i < phba->num_cpus; i++) {
3145 cq = &phwi_context->be_cq[i];
3146 mem = &cq->dma_mem;
3147 if (mem->va)
3148 pci_free_consistent(phba->pcidev, num_cq_pages
3149 * PAGE_SIZE,
3150 mem->va, mem->dma);
3151 }
3152 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303153}
3154
3155static int
3156beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3157 struct hwi_context_memory *phwi_context,
3158 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003159 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303160{
3161 unsigned int idx;
3162 int ret;
3163 struct be_queue_info *dq, *cq;
3164 struct be_dma_mem *mem;
3165 struct be_mem_descriptor *mem_descr;
3166 void *dq_vaddress;
3167
3168 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003169 dq = &phwi_context->be_def_hdrq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303170 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303171 mem = &dq->dma_mem;
3172 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003173 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3174 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303175 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3176 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3177 sizeof(struct phys_addr),
3178 sizeof(struct phys_addr), dq_vaddress);
3179 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303180 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003181 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3182 ulp_num);
3183
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303184 return ret;
3185 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303186 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3187 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303188 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3189 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003190 phba->params.defpdu_hdr_sz,
3191 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303192 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303193 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003194 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3195 ulp_num);
3196
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303197 return ret;
3198 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303199
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003200 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3201 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3202 ulp_num,
3203 phwi_context->be_def_hdrq[ulp_num].id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303204 return 0;
3205}
3206
3207static int
3208beiscsi_create_def_data(struct beiscsi_hba *phba,
3209 struct hwi_context_memory *phwi_context,
3210 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003211 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303212{
3213 unsigned int idx;
3214 int ret;
3215 struct be_queue_info *dataq, *cq;
3216 struct be_dma_mem *mem;
3217 struct be_mem_descriptor *mem_descr;
3218 void *dq_vaddress;
3219
3220 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003221 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303222 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303223 mem = &dataq->dma_mem;
3224 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003225 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3226 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303227 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3228 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3229 sizeof(struct phys_addr),
3230 sizeof(struct phys_addr), dq_vaddress);
3231 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303232 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003233 "BM_%d : be_fill_queue Failed for DEF PDU "
3234 "DATA on ULP : %d\n",
3235 ulp_num);
3236
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303237 return ret;
3238 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303239 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3240 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303241 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3242 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003243 phba->params.defpdu_data_sz,
3244 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303245 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303246 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3247 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003248 " Failed for DEF PDU DATA on ULP : %d\n",
3249 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303250 return ret;
3251 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303252
John Soni Jose99bc5d52012-08-20 23:00:18 +05303253 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003254 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3255 ulp_num,
3256 phwi_context->be_def_dataq[ulp_num].id);
3257
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003258 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3259 "BM_%d : DEFAULT PDU DATA RING CREATED"
3260 "on ULP : %d\n", ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303261 return 0;
3262}
3263
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003264
3265static int
3266beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3267{
3268 struct be_mem_descriptor *mem_descr;
3269 struct mem_array *pm_arr;
3270 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003271 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003272
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003273 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3274 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3275 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3276 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3277 (ulp_num * MEM_DESCR_OFFSET);
3278 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003279
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003280 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3281 status = be_cmd_iscsi_post_template_hdr(
3282 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003283
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003284 if (status != 0) {
3285 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3286 "BM_%d : Post Template HDR Failed for"
3287 "ULP_%d\n", ulp_num);
3288 return status;
3289 }
3290
3291 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3292 "BM_%d : Template HDR Pages Posted for"
3293 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003294 }
3295 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003296 return 0;
3297}
3298
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303299static int
3300beiscsi_post_pages(struct beiscsi_hba *phba)
3301{
3302 struct be_mem_descriptor *mem_descr;
3303 struct mem_array *pm_arr;
3304 unsigned int page_offset, i;
3305 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003306 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303307
3308 mem_descr = phba->init_mem;
3309 mem_descr += HWI_MEM_SGE;
3310 pm_arr = mem_descr->mem_array;
3311
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003312 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3313 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3314 break;
3315
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303316 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003317 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303318 for (i = 0; i < mem_descr->num_elements; i++) {
3319 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3320 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3321 page_offset,
3322 (pm_arr->size / PAGE_SIZE));
3323 page_offset += pm_arr->size / PAGE_SIZE;
3324 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303325 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3326 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303327 return status;
3328 }
3329 pm_arr++;
3330 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303331 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3332 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303333 return 0;
3334}
3335
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303336static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3337{
3338 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003339 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303340 pci_free_consistent(phba->pcidev, mem->size,
3341 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003342 mem->va = NULL;
3343 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303344}
3345
3346static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3347 u16 len, u16 entry_size)
3348{
3349 struct be_dma_mem *mem = &q->dma_mem;
3350
3351 memset(q, 0, sizeof(*q));
3352 q->len = len;
3353 q->entry_size = entry_size;
3354 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003355 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303356 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303357 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303358 return 0;
3359}
3360
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303361static int
3362beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3363 struct hwi_context_memory *phwi_context,
3364 struct hwi_controller *phwi_ctrlr)
3365{
3366 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3367 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003368 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303369 struct mem_array *pwrb_arr;
3370 void *wrb_vaddr;
3371 struct be_dma_mem sgl;
3372 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003373 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303374 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003375 uint8_t ulp_count = 0, ulp_base_num = 0;
3376 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303377
3378 idx = 0;
3379 mem_descr = phba->init_mem;
3380 mem_descr += HWI_MEM_WRB;
3381 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3382 GFP_KERNEL);
3383 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303384 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3385 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303386 return -ENOMEM;
3387 }
3388 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3389 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3390 num_wrb_rings = mem_descr->mem_array[idx].size /
3391 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3392
3393 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3394 if (num_wrb_rings) {
3395 pwrb_arr[num].virtual_address = wrb_vaddr;
3396 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3397 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3398 sizeof(struct iscsi_wrb);
3399 wrb_vaddr += pwrb_arr[num].size;
3400 pa_addr_lo += pwrb_arr[num].size;
3401 num_wrb_rings--;
3402 } else {
3403 idx++;
3404 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3405 pa_addr_lo = mem_descr->mem_array[idx].\
3406 bus_address.u.a64.address;
3407 num_wrb_rings = mem_descr->mem_array[idx].size /
3408 (phba->params.wrbs_per_cxn *
3409 sizeof(struct iscsi_wrb));
3410 pwrb_arr[num].virtual_address = wrb_vaddr;
3411 pwrb_arr[num].bus_address.u.a64.address\
3412 = pa_addr_lo;
3413 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3414 sizeof(struct iscsi_wrb);
3415 wrb_vaddr += pwrb_arr[num].size;
3416 pa_addr_lo += pwrb_arr[num].size;
3417 num_wrb_rings--;
3418 }
3419 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003420
3421 /* Get the ULP Count */
3422 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3423 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3424 ulp_count++;
3425 ulp_base_num = ulp_num;
3426 cid_count_ulp[ulp_num] =
3427 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3428 }
3429
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303430 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3431 wrb_mem_index = 0;
3432 offset = 0;
3433 size = 0;
3434
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003435 if (ulp_count > 1) {
3436 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3437
3438 if (!cid_count_ulp[ulp_base_num])
3439 ulp_base_num = (ulp_base_num + 1) %
3440 BEISCSI_ULP_COUNT;
3441
3442 cid_count_ulp[ulp_base_num]--;
3443 }
3444
3445
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303446 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3447 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003448 &phwi_context->be_wrbq[i],
3449 &phwi_ctrlr->wrb_context[i],
3450 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303451 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303452 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3453 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003454 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303455 return status;
3456 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003457 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003458 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303459 }
3460 kfree(pwrb_arr);
3461 return 0;
3462}
3463
3464static void free_wrb_handles(struct beiscsi_hba *phba)
3465{
3466 unsigned int index;
3467 struct hwi_controller *phwi_ctrlr;
3468 struct hwi_wrb_context *pwrb_context;
3469
3470 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003471 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303472 pwrb_context = &phwi_ctrlr->wrb_context[index];
3473 kfree(pwrb_context->pwrb_handle_base);
3474 kfree(pwrb_context->pwrb_handle_basestd);
3475 }
3476}
3477
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303478static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3479{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303480 struct be_ctrl_info *ctrl = &phba->ctrl;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303481 struct be_dma_mem *ptag_mem;
3482 struct be_queue_info *q;
3483 int i, tag;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303484
3485 q = &phba->ctrl.mcc_obj.q;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303486 for (i = 0; i < MAX_MCC_CMD; i++) {
3487 tag = i + 1;
3488 if (!test_bit(MCC_TAG_STATE_RUNNING,
3489 &ctrl->ptag_state[tag].tag_state))
3490 continue;
3491
3492 if (test_bit(MCC_TAG_STATE_TIMEOUT,
3493 &ctrl->ptag_state[tag].tag_state)) {
3494 ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
3495 if (ptag_mem->size) {
3496 pci_free_consistent(ctrl->pdev,
3497 ptag_mem->size,
3498 ptag_mem->va,
3499 ptag_mem->dma);
3500 ptag_mem->size = 0;
3501 }
3502 continue;
3503 }
3504 /**
3505 * If MCC is still active and waiting then wake up the process.
3506 * We are here only because port is going offline. The process
3507 * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
3508 * returned for the operation and allocated memory cleaned up.
3509 */
3510 if (waitqueue_active(&ctrl->mcc_wait[tag])) {
3511 ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
3512 ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
3513 wake_up_interruptible(&ctrl->mcc_wait[tag]);
3514 /*
3515 * Control tag info gets reinitialized in enable
3516 * so wait for the process to clear running state.
3517 */
3518 while (test_bit(MCC_TAG_STATE_RUNNING,
3519 &ctrl->ptag_state[tag].tag_state))
3520 schedule_timeout_uninterruptible(HZ);
3521 }
3522 /**
3523 * For MCC with tag_states MCC_TAG_STATE_ASYNC and
3524 * MCC_TAG_STATE_IGNORE nothing needs to done.
3525 */
3526 }
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303527 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303528 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303529 be_queue_free(phba, q);
3530 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303531
3532 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303533 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303534 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303535 be_queue_free(phba, q);
3536 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303537}
3538
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303539static int be_mcc_queues_create(struct beiscsi_hba *phba,
3540 struct hwi_context_memory *phwi_context)
3541{
3542 struct be_queue_info *q, *cq;
3543 struct be_ctrl_info *ctrl = &phba->ctrl;
3544
3545 /* Alloc MCC compl queue */
3546 cq = &phba->ctrl.mcc_obj.cq;
3547 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3548 sizeof(struct be_mcc_compl)))
3549 goto err;
3550 /* Ask BE to create MCC compl queue; */
3551 if (phba->msix_enabled) {
3552 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3553 [phba->num_cpus].q, false, true, 0))
3554 goto mcc_cq_free;
3555 } else {
3556 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3557 false, true, 0))
3558 goto mcc_cq_free;
3559 }
3560
3561 /* Alloc MCC queue */
3562 q = &phba->ctrl.mcc_obj.q;
3563 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3564 goto mcc_cq_destroy;
3565
3566 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303567 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303568 goto mcc_q_free;
3569
3570 return 0;
3571
3572mcc_q_free:
3573 be_queue_free(phba, q);
3574mcc_cq_destroy:
3575 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3576mcc_cq_free:
3577 be_queue_free(phba, cq);
3578err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303579 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303580}
3581
John Soni Jose107dfcb2012-10-20 04:42:13 +05303582/**
3583 * find_num_cpus()- Get the CPU online count
3584 * @phba: ptr to priv structure
3585 *
3586 * CPU count is used for creating EQ.
3587 **/
3588static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303589{
3590 int num_cpus = 0;
3591
3592 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303593
John Soni Jose22abeef2012-10-20 04:43:32 +05303594 switch (phba->generation) {
3595 case BE_GEN2:
3596 case BE_GEN3:
3597 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3598 BEISCSI_MAX_NUM_CPUS : num_cpus;
3599 break;
3600 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003601 /*
3602 * If eqid_count == 1 fall back to
3603 * INTX mechanism
3604 **/
3605 if (phba->fw_config.eqid_count == 1) {
3606 enable_msix = 0;
3607 phba->num_cpus = 1;
3608 return;
3609 }
3610
3611 phba->num_cpus =
3612 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3613 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303614 break;
3615 default:
3616 phba->num_cpus = 1;
3617 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303618}
3619
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303620static void hwi_purge_eq(struct beiscsi_hba *phba)
3621{
3622 struct hwi_controller *phwi_ctrlr;
3623 struct hwi_context_memory *phwi_context;
3624 struct be_queue_info *eq;
3625 struct be_eq_entry *eqe = NULL;
3626 int i, eq_msix;
3627 unsigned int num_processed;
3628
3629 if (beiscsi_hba_in_error(phba))
3630 return;
3631
3632 phwi_ctrlr = phba->phwi_ctrlr;
3633 phwi_context = phwi_ctrlr->phwi_ctxt;
3634 if (phba->msix_enabled)
3635 eq_msix = 1;
3636 else
3637 eq_msix = 0;
3638
3639 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3640 eq = &phwi_context->be_eq[i].q;
3641 eqe = queue_tail_node(eq);
3642 num_processed = 0;
3643 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3644 & EQE_VALID_MASK) {
3645 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3646 queue_tail_inc(eq);
3647 eqe = queue_tail_node(eq);
3648 num_processed++;
3649 }
3650
3651 if (num_processed)
3652 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
3653 }
3654}
3655
3656static void hwi_cleanup_port(struct beiscsi_hba *phba)
3657{
3658 struct be_queue_info *q;
3659 struct be_ctrl_info *ctrl = &phba->ctrl;
3660 struct hwi_controller *phwi_ctrlr;
3661 struct hwi_context_memory *phwi_context;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303662 struct hd_async_context *pasync_ctx;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303663 int i, eq_for_mcc, ulp_num;
3664
3665 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3666 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3667 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
3668
3669 /**
3670 * Purge all EQ entries that may have been left out. This is to
3671 * workaround a problem we've seen occasionally where driver gets an
3672 * interrupt with EQ entry bit set after stopping the controller.
3673 */
3674 hwi_purge_eq(phba);
3675
3676 phwi_ctrlr = phba->phwi_ctrlr;
3677 phwi_context = phwi_ctrlr->phwi_ctxt;
3678
3679 be_cmd_iscsi_remove_template_hdr(ctrl);
3680
3681 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3682 q = &phwi_context->be_wrbq[i];
3683 if (q->created)
3684 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3685 }
3686 kfree(phwi_context->be_wrbq);
3687 free_wrb_handles(phba);
3688
3689 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3690 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3691
3692 q = &phwi_context->be_def_hdrq[ulp_num];
3693 if (q->created)
3694 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3695
3696 q = &phwi_context->be_def_dataq[ulp_num];
3697 if (q->created)
3698 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3699
3700 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3701 }
3702 }
3703
3704 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3705
3706 for (i = 0; i < (phba->num_cpus); i++) {
3707 q = &phwi_context->be_cq[i];
3708 if (q->created) {
3709 be_queue_free(phba, q);
3710 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3711 }
3712 }
3713
3714 be_mcc_queues_destroy(phba);
3715 if (phba->msix_enabled)
3716 eq_for_mcc = 1;
3717 else
3718 eq_for_mcc = 0;
3719 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3720 q = &phwi_context->be_eq[i].q;
3721 if (q->created) {
3722 be_queue_free(phba, q);
3723 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3724 }
3725 }
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303726 /* this ensures complete FW cleanup */
3727 beiscsi_cmd_function_reset(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303728 /* last communication, indicate driver is unloading */
3729 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
3730}
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303731
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303732static int hwi_init_port(struct beiscsi_hba *phba)
3733{
3734 struct hwi_controller *phwi_ctrlr;
3735 struct hwi_context_memory *phwi_context;
3736 unsigned int def_pdu_ring_sz;
3737 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003738 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303739
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303740 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303741 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003742 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303743 phwi_context->min_eqd = 0;
Jitendra Bhivare1b7a7dd2016-08-19 15:20:23 +05303744 phwi_context->cur_eqd = 32;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303745 /* set port optic state to unknown */
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303746 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303747
3748 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303749 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303750 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3751 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303752 goto error;
3753 }
3754
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303755 status = be_mcc_queues_create(phba, phwi_context);
3756 if (status != 0)
3757 goto error;
3758
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303759 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303760 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303761 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3762 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303763 goto error;
3764 }
3765
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303766 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303767 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303768 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3769 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303770 goto error;
3771 }
3772
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003773 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3774 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003775 def_pdu_ring_sz =
3776 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3777 sizeof(struct phys_addr);
3778
3779 status = beiscsi_create_def_hdr(phba, phwi_context,
3780 phwi_ctrlr,
3781 def_pdu_ring_sz,
3782 ulp_num);
3783 if (status != 0) {
3784 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3785 "BM_%d : Default Header not created for ULP : %d\n",
3786 ulp_num);
3787 goto error;
3788 }
3789
3790 status = beiscsi_create_def_data(phba, phwi_context,
3791 phwi_ctrlr,
3792 def_pdu_ring_sz,
3793 ulp_num);
3794 if (status != 0) {
3795 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3796 "BM_%d : Default Data not created for ULP : %d\n",
3797 ulp_num);
3798 goto error;
3799 }
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303800 /**
3801 * Now that the default PDU rings have been created,
3802 * let EP know about it.
3803 * Call beiscsi_cmd_iscsi_cleanup before posting?
3804 */
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303805 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3806 ulp_num);
3807 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3808 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003809 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303810 }
3811
3812 status = beiscsi_post_pages(phba);
3813 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303814 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3815 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303816 goto error;
3817 }
3818
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003819 status = beiscsi_post_template_hdr(phba);
3820 if (status != 0) {
3821 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3822 "BM_%d : Template HDR Posting for CXN Failed\n");
3823 }
3824
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303825 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3826 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303827 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3828 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303829 goto error;
3830 }
3831
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003832 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3833 uint16_t async_arr_idx = 0;
3834
3835 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3836 uint16_t cri = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303837 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003838
3839 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3840 phwi_ctrlr, ulp_num);
3841 for (cri = 0; cri <
3842 phba->params.cxns_per_ctrl; cri++) {
3843 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3844 (phwi_ctrlr, cri))
3845 pasync_ctx->cid_to_async_cri_map[
3846 phwi_ctrlr->wrb_context[cri].cid] =
3847 async_arr_idx++;
3848 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303849 /**
3850 * Now that the default PDU rings have been created,
3851 * let EP know about it.
3852 */
3853 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3854 ulp_num);
3855 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3856 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003857 }
3858 }
3859
John Soni Jose99bc5d52012-08-20 23:00:18 +05303860 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3861 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303862 return 0;
3863
3864error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303865 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3866 "BM_%d : hwi_init_port failed");
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303867 hwi_cleanup_port(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003868 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303869}
3870
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303871static int hwi_init_controller(struct beiscsi_hba *phba)
3872{
3873 struct hwi_controller *phwi_ctrlr;
3874
3875 phwi_ctrlr = phba->phwi_ctrlr;
3876 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3877 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3878 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303879 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3880 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3881 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303882 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303883 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3884 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3885 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303886 return -ENOMEM;
3887 }
3888
3889 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003890 if (beiscsi_init_wrb_handle(phba))
3891 return -ENOMEM;
3892
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003893 if (hwi_init_async_pdu_ctx(phba)) {
3894 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3895 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3896 return -ENOMEM;
3897 }
3898
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303900 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3901 "BM_%d : hwi_init_controller failed\n");
3902
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303903 return -ENOMEM;
3904 }
3905 return 0;
3906}
3907
3908static void beiscsi_free_mem(struct beiscsi_hba *phba)
3909{
3910 struct be_mem_descriptor *mem_descr;
3911 int i, j;
3912
3913 mem_descr = phba->init_mem;
3914 i = 0;
3915 j = 0;
3916 for (i = 0; i < SE_MEM_MAX; i++) {
3917 for (j = mem_descr->num_elements; j > 0; j--) {
3918 pci_free_consistent(phba->pcidev,
3919 mem_descr->mem_array[j - 1].size,
3920 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303921 (unsigned long)mem_descr->mem_array[j - 1].
3922 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303923 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003924
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303925 kfree(mem_descr->mem_array);
3926 mem_descr++;
3927 }
3928 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003929 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303930 kfree(phba->phwi_ctrlr);
3931}
3932
3933static int beiscsi_init_controller(struct beiscsi_hba *phba)
3934{
3935 int ret = -ENOMEM;
3936
3937 ret = beiscsi_get_memory(phba);
3938 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303939 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3940 "BM_%d : beiscsi_dev_probe -"
3941 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303942 return ret;
3943 }
3944
3945 ret = hwi_init_controller(phba);
3946 if (ret)
3947 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303948 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3949 "BM_%d : Return success from beiscsi_init_controller");
3950
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303951 return 0;
3952
3953free_init:
3954 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003955 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303956}
3957
3958static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3959{
3960 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3961 struct sgl_handle *psgl_handle;
3962 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003963 unsigned int arr_index, i, idx;
3964 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303965
3966 phba->io_sgl_hndl_avbl = 0;
3967 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303968
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303969 mem_descr_sglh = phba->init_mem;
3970 mem_descr_sglh += HWI_MEM_SGLH;
3971 if (1 == mem_descr_sglh->num_elements) {
3972 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3973 phba->params.ios_per_ctrl,
3974 GFP_KERNEL);
3975 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303976 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3977 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303978 return -ENOMEM;
3979 }
3980 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3981 (phba->params.icds_per_ctrl -
3982 phba->params.ios_per_ctrl),
3983 GFP_KERNEL);
3984 if (!phba->eh_sgl_hndl_base) {
3985 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303986 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3987 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303988 return -ENOMEM;
3989 }
3990 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303991 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3992 "BM_%d : HWI_MEM_SGLH is more than one element."
3993 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303994 return -ENOMEM;
3995 }
3996
3997 arr_index = 0;
3998 idx = 0;
3999 while (idx < mem_descr_sglh->num_elements) {
4000 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
4001
4002 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
4003 sizeof(struct sgl_handle)); i++) {
4004 if (arr_index < phba->params.ios_per_ctrl) {
4005 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
4006 phba->io_sgl_hndl_avbl++;
4007 arr_index++;
4008 } else {
4009 phba->eh_sgl_hndl_base[arr_index -
4010 phba->params.ios_per_ctrl] =
4011 psgl_handle;
4012 arr_index++;
4013 phba->eh_sgl_hndl_avbl++;
4014 }
4015 psgl_handle++;
4016 }
4017 idx++;
4018 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304019 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4020 "BM_%d : phba->io_sgl_hndl_avbl=%d"
4021 "phba->eh_sgl_hndl_avbl=%d\n",
4022 phba->io_sgl_hndl_avbl,
4023 phba->eh_sgl_hndl_avbl);
4024
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304025 mem_descr_sg = phba->init_mem;
4026 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304027 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4028 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
4029 mem_descr_sg->num_elements);
4030
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004031 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4032 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
4033 break;
4034
4035 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
4036
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304037 arr_index = 0;
4038 idx = 0;
4039 while (idx < mem_descr_sg->num_elements) {
4040 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
4041
4042 for (i = 0;
4043 i < (mem_descr_sg->mem_array[idx].size) /
4044 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
4045 i++) {
4046 if (arr_index < phba->params.ios_per_ctrl)
4047 psgl_handle = phba->io_sgl_hndl_base[arr_index];
4048 else
4049 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
4050 phba->params.ios_per_ctrl];
4051 psgl_handle->pfrag = pfrag;
4052 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4053 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4054 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004055 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304056 }
4057 idx++;
4058 }
4059 phba->io_sgl_free_index = 0;
4060 phba->io_sgl_alloc_index = 0;
4061 phba->eh_sgl_free_index = 0;
4062 phba->eh_sgl_alloc_index = 0;
4063 return 0;
4064}
4065
4066static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4067{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004068 int ret;
4069 uint16_t i, ulp_num;
4070 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304071
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004072 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4073 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4074 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4075 GFP_KERNEL);
4076
4077 if (!ptr_cid_info) {
4078 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4079 "BM_%d : Failed to allocate memory"
4080 "for ULP_CID_INFO for ULP : %d\n",
4081 ulp_num);
4082 ret = -ENOMEM;
4083 goto free_memory;
4084
4085 }
4086
4087 /* Allocate memory for CID array */
Jitendra Bhivarebc9ad172016-12-13 15:56:03 +05304088 ptr_cid_info->cid_array =
4089 kcalloc(BEISCSI_GET_CID_COUNT(phba, ulp_num),
4090 sizeof(*ptr_cid_info->cid_array),
4091 GFP_KERNEL);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004092 if (!ptr_cid_info->cid_array) {
4093 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4094 "BM_%d : Failed to allocate memory"
4095 "for CID_ARRAY for ULP : %d\n",
4096 ulp_num);
4097 kfree(ptr_cid_info);
4098 ptr_cid_info = NULL;
4099 ret = -ENOMEM;
4100
4101 goto free_memory;
4102 }
4103 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4104 phba, ulp_num);
4105
4106 /* Save the cid_info_array ptr */
4107 phba->cid_array_info[ulp_num] = ptr_cid_info;
4108 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304109 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304110 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004111 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304112 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304113 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4114 "BM_%d : Failed to allocate memory in "
4115 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004116 ret = -ENOMEM;
4117
4118 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304119 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004120
4121 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4122 phba->params.cxns_per_ctrl, GFP_KERNEL);
4123 if (!phba->conn_table) {
4124 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4125 "BM_%d : Failed to allocate memory in"
4126 "hba_setup_cid_tbls\n");
4127
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004128 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004129 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004130 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004131
4132 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304133 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004134
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004135 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4136 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004137
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004138 ptr_cid_info = phba->cid_array_info[ulp_num];
4139 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4140 phba->phwi_ctrlr->wrb_context[i].cid;
4141
4142 }
4143
4144 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4145 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4146 ptr_cid_info = phba->cid_array_info[ulp_num];
4147
4148 ptr_cid_info->cid_alloc = 0;
4149 ptr_cid_info->cid_free = 0;
4150 }
4151 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304152 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004153
4154free_memory:
4155 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4156 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4157 ptr_cid_info = phba->cid_array_info[ulp_num];
4158
4159 if (ptr_cid_info) {
4160 kfree(ptr_cid_info->cid_array);
4161 kfree(ptr_cid_info);
4162 phba->cid_array_info[ulp_num] = NULL;
4163 }
4164 }
4165 }
4166
4167 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304168}
4169
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304170static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304171{
4172 struct be_ctrl_info *ctrl = &phba->ctrl;
4173 struct hwi_controller *phwi_ctrlr;
4174 struct hwi_context_memory *phwi_context;
4175 struct be_queue_info *eq;
4176 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304177 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304178 u32 enabled;
4179
4180 phwi_ctrlr = phba->phwi_ctrlr;
4181 phwi_context = phwi_ctrlr->phwi_ctxt;
4182
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304183 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4184 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4185 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304186
4187 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4188 if (!enabled) {
4189 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304190 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4191 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304192 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004193 }
4194
4195 if (!phba->msix_enabled) {
4196 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304197 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4198 "BM_%d : eq->id=%d\n", eq->id);
4199
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004200 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4201 } else {
4202 for (i = 0; i <= phba->num_cpus; i++) {
4203 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304204 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4205 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304206 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4207 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304208 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304209}
4210
4211static void hwi_disable_intr(struct beiscsi_hba *phba)
4212{
4213 struct be_ctrl_info *ctrl = &phba->ctrl;
4214
4215 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4216 u32 reg = ioread32(addr);
4217
4218 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4219 if (enabled) {
4220 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4221 iowrite32(reg, addr);
4222 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304223 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4224 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304225}
4226
4227static int beiscsi_init_port(struct beiscsi_hba *phba)
4228{
4229 int ret;
4230
4231 ret = beiscsi_init_controller(phba);
4232 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304233 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4234 "BM_%d : beiscsi_dev_probe - Failed in"
4235 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304236 return ret;
4237 }
4238 ret = beiscsi_init_sgl_handle(phba);
4239 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304240 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4241 "BM_%d : beiscsi_dev_probe - Failed in"
4242 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304243 goto do_cleanup_ctrlr;
4244 }
4245
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05304246 ret = hba_setup_cid_tbls(phba);
4247 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304248 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4249 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304250 kfree(phba->io_sgl_hndl_base);
4251 kfree(phba->eh_sgl_hndl_base);
4252 goto do_cleanup_ctrlr;
4253 }
4254
4255 return ret;
4256
4257do_cleanup_ctrlr:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304258 hwi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304259 return ret;
4260}
4261
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304262static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304263{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004264 struct ulp_cid_info *ptr_cid_info = NULL;
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304265 int ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304266
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304267 kfree(phba->io_sgl_hndl_base);
4268 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304269 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004270 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004271
4272 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4273 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4274 ptr_cid_info = phba->cid_array_info[ulp_num];
4275
4276 if (ptr_cid_info) {
4277 kfree(ptr_cid_info->cid_array);
4278 kfree(ptr_cid_info);
4279 phba->cid_array_info[ulp_num] = NULL;
4280 }
4281 }
4282 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304283}
4284
John Soni Josed629c472012-10-20 04:42:00 +05304285/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004286 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4287 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004288 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004289 *
4290 * Free driver mgmt resources binded to CXN.
4291 **/
4292void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004293beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4294 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004295{
4296 struct beiscsi_io_task *io_task;
4297 struct beiscsi_hba *phba = beiscsi_conn->phba;
4298 struct hwi_wrb_context *pwrb_context;
4299 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004300 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4301 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004302
4303 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004304 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4305
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004306 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004307
4308 if (io_task->pwrb_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304309 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004310 io_task->pwrb_handle = NULL;
4311 }
4312
4313 if (io_task->psgl_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304314 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004315 io_task->psgl_handle = NULL;
4316 }
4317
John Soni Joseeb1c4692015-04-25 08:17:45 +05304318 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004319 pci_unmap_single(phba->pcidev,
4320 io_task->mtask_addr,
4321 io_task->mtask_data_count,
4322 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304323 io_task->mtask_addr = 0;
4324 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004325}
4326
4327/**
John Soni Josed629c472012-10-20 04:42:00 +05304328 * beiscsi_cleanup_task()- Free driver resources of the task
4329 * @task: ptr to the iscsi task
4330 *
4331 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004332static void beiscsi_cleanup_task(struct iscsi_task *task)
4333{
4334 struct beiscsi_io_task *io_task = task->dd_data;
4335 struct iscsi_conn *conn = task->conn;
4336 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4337 struct beiscsi_hba *phba = beiscsi_conn->phba;
4338 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4339 struct hwi_wrb_context *pwrb_context;
4340 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004341 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4342 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004343
4344 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004345 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004346
4347 if (io_task->cmd_bhs) {
4348 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4349 io_task->bhs_pa.u.a64.address);
4350 io_task->cmd_bhs = NULL;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304351 task->hdr = NULL;
Mike Christie1282ab72012-04-18 03:06:00 -05004352 }
4353
4354 if (task->sc) {
4355 if (io_task->pwrb_handle) {
4356 free_wrb_handle(phba, pwrb_context,
4357 io_task->pwrb_handle);
4358 io_task->pwrb_handle = NULL;
4359 }
4360
4361 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004362 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004363 io_task->psgl_handle = NULL;
4364 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004365
4366 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304367 if (io_task->num_sg)
4368 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004369 io_task->scsi_cmnd = NULL;
4370 }
Mike Christie1282ab72012-04-18 03:06:00 -05004371 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004372 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004373 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004374 }
4375}
4376
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304377void
4378beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4379 struct beiscsi_offload_params *params)
4380{
4381 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304382 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304383 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004384 struct iscsi_task *task = beiscsi_conn->task;
4385 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304386 u32 doorbell = 0;
4387
4388 /*
4389 * We can always use 0 here because it is reserved by libiscsi for
4390 * login/startup related tasks.
4391 */
Mike Christie1282ab72012-04-18 03:06:00 -05004392 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004393 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004394 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004395 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004396
John Soni Jose340c99e2015-08-20 04:44:30 +05304397 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4398 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304399
John Soni Joseacb96932012-10-20 04:44:35 +05304400 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004401 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304402 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304403 phba->init_mem,
4404 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004405 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304406 beiscsi_offload_cxn_v2(params, pwrb_handle,
4407 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304408
John Soni Joseacb96932012-10-20 04:44:35 +05304409 be_dws_le_to_cpu(pwrb_handle->pwrb,
4410 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304411
4412 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304413 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304414 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304415 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004416 iowrite32(doorbell, phba->db_va +
4417 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304418
4419 /*
4420 * There is no completion for CONTEXT_UPDATE. The completion of next
4421 * WRB posted guarantees FW's processing and DMA'ing of it.
4422 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4423 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4424 */
4425 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4426 phba->params.wrbs_per_cxn);
4427 beiscsi_log(phba, KERN_INFO,
4428 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4429 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4430 pwrb_handle, pwrb_context->free_index,
4431 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304432}
4433
4434static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4435 int *index, int *age)
4436{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304437 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304438 if (age)
4439 *age = conn->session->age;
4440}
4441
4442/**
4443 * beiscsi_alloc_pdu - allocates pdu and related resources
4444 * @task: libiscsi task
4445 * @opcode: opcode of pdu for task
4446 *
4447 * This is called with the session lock held. It will allocate
4448 * the wrb and sgl if needed for the command. And it will prep
4449 * the pdu's itt. beiscsi_parse_pdu will later translate
4450 * the pdu itt to the libiscsi task itt.
4451 */
4452static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4453{
4454 struct beiscsi_io_task *io_task = task->dd_data;
4455 struct iscsi_conn *conn = task->conn;
4456 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4457 struct beiscsi_hba *phba = beiscsi_conn->phba;
4458 struct hwi_wrb_context *pwrb_context;
4459 struct hwi_controller *phwi_ctrlr;
4460 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004461 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304462 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4463 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304464
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304465 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004466 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304467 if (!io_task->cmd_bhs)
4468 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304469 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304470 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304471 io_task->conn = beiscsi_conn;
4472
4473 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4474 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304475 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004476 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304477
4478 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304479 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304480 if (!io_task->psgl_handle) {
4481 beiscsi_log(phba, KERN_ERR,
4482 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4483 "BM_%d : Alloc of IO_SGL_ICD Failed"
4484 "for the CID : %d\n",
4485 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304486 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304487 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304488 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304489 beiscsi_conn->beiscsi_conn_cid,
4490 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304491 if (!io_task->pwrb_handle) {
4492 beiscsi_log(phba, KERN_ERR,
4493 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4494 "BM_%d : Alloc of WRB_HANDLE Failed"
4495 "for the CID : %d\n",
4496 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304497 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304498 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304499 } else {
4500 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304501 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004502 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304503 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304504 io_task->psgl_handle = (struct sgl_handle *)
4505 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304506 if (!io_task->psgl_handle) {
4507 beiscsi_log(phba, KERN_ERR,
4508 BEISCSI_LOG_IO |
4509 BEISCSI_LOG_CONFIG,
4510 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4511 "for the CID : %d\n",
4512 beiscsi_conn->
4513 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304514 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304515 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304516
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304517 beiscsi_conn->login_in_progress = 1;
4518 beiscsi_conn->plogin_sgl_handle =
4519 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304520 io_task->pwrb_handle =
4521 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304522 beiscsi_conn->beiscsi_conn_cid,
4523 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304524 if (!io_task->pwrb_handle) {
4525 beiscsi_log(phba, KERN_ERR,
4526 BEISCSI_LOG_IO |
4527 BEISCSI_LOG_CONFIG,
4528 "BM_%d : Alloc of WRB_HANDLE Failed"
4529 "for the CID : %d\n",
4530 beiscsi_conn->
4531 beiscsi_conn_cid);
4532 goto free_mgmt_hndls;
4533 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304534 beiscsi_conn->plogin_wrb_handle =
4535 io_task->pwrb_handle;
4536
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304537 } else {
4538 io_task->psgl_handle =
4539 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304540 io_task->pwrb_handle =
4541 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304542 }
4543 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304544 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304545 if (!io_task->psgl_handle) {
4546 beiscsi_log(phba, KERN_ERR,
4547 BEISCSI_LOG_IO |
4548 BEISCSI_LOG_CONFIG,
4549 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4550 "for the CID : %d\n",
4551 beiscsi_conn->
4552 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304553 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304554 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304555 io_task->pwrb_handle =
4556 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304557 beiscsi_conn->beiscsi_conn_cid,
4558 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304559 if (!io_task->pwrb_handle) {
4560 beiscsi_log(phba, KERN_ERR,
4561 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4562 "BM_%d : Alloc of WRB_HANDLE Failed"
4563 "for the CID : %d\n",
4564 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304565 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304566 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304567
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304568 }
4569 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304570 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4571 wrb_index << 16) | (unsigned int)
4572 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304573 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304574
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304575 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4576 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304577
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304578free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304579 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304580 goto free_hndls;
4581free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304582 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004583 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304584free_hndls:
4585 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004586 cri_index = BE_GET_CRI_FROM_CID(
4587 beiscsi_conn->beiscsi_conn_cid);
4588 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304589 if (io_task->pwrb_handle)
4590 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304591 io_task->pwrb_handle = NULL;
4592 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4593 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004594 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304595 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304596}
Baoyou Xie0825b8e2016-09-26 20:00:59 +08004597static int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
John Soni Jose09a10932012-10-20 04:44:23 +05304598 unsigned int num_sg, unsigned int xferlen,
4599 unsigned int writedir)
4600{
4601
4602 struct beiscsi_io_task *io_task = task->dd_data;
4603 struct iscsi_conn *conn = task->conn;
4604 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4605 struct beiscsi_hba *phba = beiscsi_conn->phba;
4606 struct iscsi_wrb *pwrb = NULL;
4607 unsigned int doorbell = 0;
4608
4609 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304610
John Soni Jose09a10932012-10-20 04:44:23 +05304611 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4612
4613 if (writedir) {
4614 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4615 INI_WR_CMD);
4616 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4617 } else {
4618 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4619 INI_RD_CMD);
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4621 }
4622
4623 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4624 type, pwrb);
4625
4626 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4627 cpu_to_be16(*(unsigned short *)
4628 &io_task->cmd_bhs->iscsi_hdr.lun));
4629 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4630 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4631 io_task->pwrb_handle->wrb_index);
4632 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4633 be32_to_cpu(task->cmdsn));
4634 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4635 io_task->psgl_handle->sgl_index);
4636
4637 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4638 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304639 io_task->pwrb_handle->wrb_index);
4640 if (io_task->pwrb_context->plast_wrb)
4641 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4642 io_task->pwrb_context->plast_wrb,
4643 io_task->pwrb_handle->wrb_index);
4644 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304645
4646 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4647
4648 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4649 doorbell |= (io_task->pwrb_handle->wrb_index &
4650 DB_DEF_PDU_WRB_INDEX_MASK) <<
4651 DB_DEF_PDU_WRB_INDEX_SHIFT;
4652 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004653 iowrite32(doorbell, phba->db_va +
4654 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304655 return 0;
4656}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304657
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304658static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4659 unsigned int num_sg, unsigned int xferlen,
4660 unsigned int writedir)
4661{
4662
4663 struct beiscsi_io_task *io_task = task->dd_data;
4664 struct iscsi_conn *conn = task->conn;
4665 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4666 struct beiscsi_hba *phba = beiscsi_conn->phba;
4667 struct iscsi_wrb *pwrb = NULL;
4668 unsigned int doorbell = 0;
4669
4670 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304671 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4672
4673 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304674 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4675 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304676 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304677 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304678 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4679 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304680 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4681 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304682
John Soni Jose09a10932012-10-20 04:44:23 +05304683 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4684 type, pwrb);
4685
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304686 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004687 cpu_to_be16(*(unsigned short *)
4688 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304689 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4690 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4691 io_task->pwrb_handle->wrb_index);
4692 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4693 be32_to_cpu(task->cmdsn));
4694 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4695 io_task->psgl_handle->sgl_index);
4696
4697 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4698
4699 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304700 io_task->pwrb_handle->wrb_index);
4701 if (io_task->pwrb_context->plast_wrb)
4702 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4703 io_task->pwrb_context->plast_wrb,
4704 io_task->pwrb_handle->wrb_index);
4705 io_task->pwrb_context->plast_wrb = pwrb;
4706
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304707 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4708
4709 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304710 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304711 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4712 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4713
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004714 iowrite32(doorbell, phba->db_va +
4715 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304716 return 0;
4717}
4718
4719static int beiscsi_mtask(struct iscsi_task *task)
4720{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304721 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722 struct iscsi_conn *conn = task->conn;
4723 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4724 struct beiscsi_hba *phba = beiscsi_conn->phba;
4725 struct iscsi_wrb *pwrb = NULL;
4726 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304727 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304728 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004729 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304730
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304731 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304732 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304733
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004734 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304735 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4736 be32_to_cpu(task->cmdsn));
4737 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4738 io_task->pwrb_handle->wrb_index);
4739 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4740 io_task->psgl_handle->sgl_index);
4741 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4742 task->data_count);
4743 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304744 io_task->pwrb_handle->wrb_index);
4745 if (io_task->pwrb_context->plast_wrb)
4746 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4747 io_task->pwrb_context->plast_wrb,
4748 io_task->pwrb_handle->wrb_index);
4749 io_task->pwrb_context->plast_wrb = pwrb;
4750
John Soni Jose09a10932012-10-20 04:44:23 +05304751 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004752 } else {
4753 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4754 be32_to_cpu(task->cmdsn));
4755 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4756 io_task->pwrb_handle->wrb_index);
4757 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4758 io_task->psgl_handle->sgl_index);
4759 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4760 task->data_count);
4761 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304762 io_task->pwrb_handle->wrb_index);
4763 if (io_task->pwrb_context->plast_wrb)
4764 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4765 io_task->pwrb_context->plast_wrb,
4766 io_task->pwrb_handle->wrb_index);
4767 io_task->pwrb_context->plast_wrb = pwrb;
4768
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004769 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304770 }
4771
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304772
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304773 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4774 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304775 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304776 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004777 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304778 break;
4779 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004780 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304781 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004782 if (is_chip_be2_be3r(phba))
4783 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304784 dmsg, pwrb, 1);
4785 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004786 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304787 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004788 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304789 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004790 if (is_chip_be2_be3r(phba))
4791 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304792 dmsg, pwrb, 0);
4793 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004794 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304795 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004796 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004797 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304798 break;
4799 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304800 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004801 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304802 break;
4803 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304804 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004805 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304806 break;
4807 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304808 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004809 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304810 break;
4811
4812 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304813 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4814 "BM_%d : opcode =%d Not supported\n",
4815 task->hdr->opcode & ISCSI_OPCODE_MASK);
4816
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304817 return -EINVAL;
4818 }
4819
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004820 if (ret)
4821 return ret;
4822
John Soni Jose09a10932012-10-20 04:44:23 +05304823 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004824 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4825 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4826 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304827
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304828 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304829 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304830 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4831 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004832 iowrite32(doorbell, phba->db_va +
4833 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304834 return 0;
4835}
4836
4837static int beiscsi_task_xmit(struct iscsi_task *task)
4838{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304839 struct beiscsi_io_task *io_task = task->dd_data;
4840 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304841 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304842 struct scatterlist *sg;
4843 int num_sg;
4844 unsigned int writedir = 0, xferlen = 0;
4845
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304846 phba = io_task->conn->phba;
4847 /**
4848 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4849 * operational if FW still gets heartbeat from EP FW. Is management
4850 * path really needed to continue further?
4851 */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304852 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304853 return -EIO;
4854
Jitendra Bhivare18683792016-02-04 15:49:18 +05304855 if (!io_task->conn->login_in_progress)
4856 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304857
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304858 if (!sc)
4859 return beiscsi_mtask(task);
4860
4861 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304862 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304863 num_sg = scsi_dma_map(sc);
4864 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004865 beiscsi_log(phba, KERN_ERR,
4866 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4867 "BM_%d : scsi_dma_map Failed "
4868 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4869 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4870 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304871
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304872 return num_sg;
4873 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304874 /**
4875 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4876 * For management task, cleanup_task checks mtask_addr before unmapping.
4877 */
4878 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304879 xferlen = scsi_bufflen(sc);
4880 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304881 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304882 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304883 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304884 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304885
John Soni Jose09a10932012-10-20 04:44:23 +05304886 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304887}
4888
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004889/**
4890 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4891 * @job: job to handle
4892 */
4893static int beiscsi_bsg_request(struct bsg_job *job)
4894{
4895 struct Scsi_Host *shost;
4896 struct beiscsi_hba *phba;
4897 struct iscsi_bsg_request *bsg_req = job->request;
4898 int rc = -EINVAL;
4899 unsigned int tag;
4900 struct be_dma_mem nonemb_cmd;
4901 struct be_cmd_resp_hdr *resp;
4902 struct iscsi_bsg_reply *bsg_reply = job->reply;
4903 unsigned short status, extd_status;
4904
4905 shost = iscsi_job_to_shost(job);
4906 phba = iscsi_host_priv(shost);
4907
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304908 if (!beiscsi_hba_is_online(phba)) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304909 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4910 "BM_%d : HBA in error 0x%lx\n", phba->state);
4911 return -ENXIO;
4912 }
4913
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004914 switch (bsg_req->msgcode) {
4915 case ISCSI_BSG_HST_VENDOR:
4916 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4917 job->request_payload.payload_len,
4918 &nonemb_cmd.dma);
4919 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304920 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4921 "BM_%d : Failed to allocate memory for "
4922 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304923 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004924 }
4925 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4926 &nonemb_cmd);
4927 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304928 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304929 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304930
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004931 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4932 nonemb_cmd.va, nonemb_cmd.dma);
4933 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304934 }
4935
4936 rc = wait_event_interruptible_timeout(
4937 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304938 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304939 msecs_to_jiffies(
4940 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304941
4942 if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
4943 clear_bit(MCC_TAG_STATE_RUNNING,
4944 &phba->ctrl.ptag_state[tag].tag_state);
4945 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4946 nonemb_cmd.va, nonemb_cmd.dma);
4947 return -EIO;
4948 }
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304949 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4950 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4951 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304952 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004953 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4954 sg_copy_from_buffer(job->reply_payload.sg_list,
4955 job->reply_payload.sg_cnt,
4956 nonemb_cmd.va, (resp->response_length
4957 + sizeof(*resp)));
4958 bsg_reply->reply_payload_rcv_len = resp->response_length;
4959 bsg_reply->result = status;
4960 bsg_job_done(job, bsg_reply->result,
4961 bsg_reply->reply_payload_rcv_len);
4962 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4963 nonemb_cmd.va, nonemb_cmd.dma);
4964 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304965 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304966 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304967 " status = %d extd_status = %d\n",
4968 status, extd_status);
4969
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004970 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304971 } else {
4972 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004973 }
4974 break;
4975
4976 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304977 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4978 "BM_%d : Unsupported bsg command: 0x%x\n",
4979 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004980 break;
4981 }
4982
4983 return rc;
4984}
4985
Baoyou Xie0825b8e2016-09-26 20:00:59 +08004986static void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304987{
4988 /* Set the logging parameter */
4989 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4990}
4991
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304992void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
4993{
4994 if (phba->boot_struct.boot_kset)
4995 return;
4996
4997 /* skip if boot work is already in progress */
4998 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
4999 return;
5000
5001 phba->boot_struct.retry = 3;
5002 phba->boot_struct.tag = 0;
5003 phba->boot_struct.s_handle = s_handle;
5004 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
5005 schedule_work(&phba->boot_work);
5006}
5007
5008static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
5009{
5010 struct beiscsi_hba *phba = data;
5011 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
5012 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5013 char *str = buf;
5014 int rc = -EPERM;
5015
5016 switch (type) {
5017 case ISCSI_BOOT_TGT_NAME:
5018 rc = sprintf(buf, "%.*s\n",
5019 (int)strlen(boot_sess->target_name),
5020 (char *)&boot_sess->target_name);
5021 break;
5022 case ISCSI_BOOT_TGT_IP_ADDR:
5023 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5024 rc = sprintf(buf, "%pI4\n",
5025 (char *)&boot_conn->dest_ipaddr.addr);
5026 else
5027 rc = sprintf(str, "%pI6\n",
5028 (char *)&boot_conn->dest_ipaddr.addr);
5029 break;
5030 case ISCSI_BOOT_TGT_PORT:
5031 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5032 break;
5033
5034 case ISCSI_BOOT_TGT_CHAP_NAME:
5035 rc = sprintf(str, "%.*s\n",
5036 boot_conn->negotiated_login_options.auth_data.chap.
5037 target_chap_name_length,
5038 (char *)&boot_conn->negotiated_login_options.
5039 auth_data.chap.target_chap_name);
5040 break;
5041 case ISCSI_BOOT_TGT_CHAP_SECRET:
5042 rc = sprintf(str, "%.*s\n",
5043 boot_conn->negotiated_login_options.auth_data.chap.
5044 target_secret_length,
5045 (char *)&boot_conn->negotiated_login_options.
5046 auth_data.chap.target_secret);
5047 break;
5048 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5049 rc = sprintf(str, "%.*s\n",
5050 boot_conn->negotiated_login_options.auth_data.chap.
5051 intr_chap_name_length,
5052 (char *)&boot_conn->negotiated_login_options.
5053 auth_data.chap.intr_chap_name);
5054 break;
5055 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5056 rc = sprintf(str, "%.*s\n",
5057 boot_conn->negotiated_login_options.auth_data.chap.
5058 intr_secret_length,
5059 (char *)&boot_conn->negotiated_login_options.
5060 auth_data.chap.intr_secret);
5061 break;
5062 case ISCSI_BOOT_TGT_FLAGS:
5063 rc = sprintf(str, "2\n");
5064 break;
5065 case ISCSI_BOOT_TGT_NIC_ASSOC:
5066 rc = sprintf(str, "0\n");
5067 break;
5068 }
5069 return rc;
5070}
5071
5072static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5073{
5074 struct beiscsi_hba *phba = data;
5075 char *str = buf;
5076 int rc = -EPERM;
5077
5078 switch (type) {
5079 case ISCSI_BOOT_INI_INITIATOR_NAME:
5080 rc = sprintf(str, "%s\n",
5081 phba->boot_struct.boot_sess.initiator_iscsiname);
5082 break;
5083 }
5084 return rc;
5085}
5086
5087static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5088{
5089 struct beiscsi_hba *phba = data;
5090 char *str = buf;
5091 int rc = -EPERM;
5092
5093 switch (type) {
5094 case ISCSI_BOOT_ETH_FLAGS:
5095 rc = sprintf(str, "2\n");
5096 break;
5097 case ISCSI_BOOT_ETH_INDEX:
5098 rc = sprintf(str, "0\n");
5099 break;
5100 case ISCSI_BOOT_ETH_MAC:
5101 rc = beiscsi_get_macaddr(str, phba);
5102 break;
5103 }
5104 return rc;
5105}
5106
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305107static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5108{
5109 umode_t rc = 0;
5110
5111 switch (type) {
5112 case ISCSI_BOOT_TGT_NAME:
5113 case ISCSI_BOOT_TGT_IP_ADDR:
5114 case ISCSI_BOOT_TGT_PORT:
5115 case ISCSI_BOOT_TGT_CHAP_NAME:
5116 case ISCSI_BOOT_TGT_CHAP_SECRET:
5117 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5118 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5119 case ISCSI_BOOT_TGT_NIC_ASSOC:
5120 case ISCSI_BOOT_TGT_FLAGS:
5121 rc = S_IRUGO;
5122 break;
5123 }
5124 return rc;
5125}
5126
5127static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5128{
5129 umode_t rc = 0;
5130
5131 switch (type) {
5132 case ISCSI_BOOT_INI_INITIATOR_NAME:
5133 rc = S_IRUGO;
5134 break;
5135 }
5136 return rc;
5137}
5138
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305139static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5140{
5141 umode_t rc = 0;
5142
5143 switch (type) {
5144 case ISCSI_BOOT_ETH_FLAGS:
5145 case ISCSI_BOOT_ETH_MAC:
5146 case ISCSI_BOOT_ETH_INDEX:
5147 rc = S_IRUGO;
5148 break;
5149 }
5150 return rc;
5151}
5152
5153static void beiscsi_boot_kobj_release(void *data)
5154{
5155 struct beiscsi_hba *phba = data;
5156
5157 scsi_host_put(phba->shost);
5158}
5159
5160static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5161{
5162 struct boot_struct *bs = &phba->boot_struct;
5163 struct iscsi_boot_kobj *boot_kobj;
5164
5165 if (bs->boot_kset) {
5166 __beiscsi_log(phba, KERN_ERR,
5167 "BM_%d: boot_kset already created\n");
5168 return 0;
5169 }
5170
5171 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5172 if (!bs->boot_kset) {
5173 __beiscsi_log(phba, KERN_ERR,
5174 "BM_%d: boot_kset alloc failed\n");
5175 return -ENOMEM;
5176 }
5177
5178 /* get shost ref because the show function will refer phba */
5179 if (!scsi_host_get(phba->shost))
5180 goto free_kset;
5181
5182 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5183 beiscsi_show_boot_tgt_info,
5184 beiscsi_tgt_get_attr_visibility,
5185 beiscsi_boot_kobj_release);
5186 if (!boot_kobj)
5187 goto put_shost;
5188
5189 if (!scsi_host_get(phba->shost))
5190 goto free_kset;
5191
5192 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5193 beiscsi_show_boot_ini_info,
5194 beiscsi_ini_get_attr_visibility,
5195 beiscsi_boot_kobj_release);
5196 if (!boot_kobj)
5197 goto put_shost;
5198
5199 if (!scsi_host_get(phba->shost))
5200 goto free_kset;
5201
5202 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5203 beiscsi_show_boot_eth_info,
5204 beiscsi_eth_get_attr_visibility,
5205 beiscsi_boot_kobj_release);
5206 if (!boot_kobj)
5207 goto put_shost;
5208
5209 return 0;
5210
5211put_shost:
5212 scsi_host_put(phba->shost);
5213free_kset:
5214 iscsi_boot_destroy_kset(bs->boot_kset);
5215 bs->boot_kset = NULL;
5216 return -ENOMEM;
5217}
5218
5219static void beiscsi_boot_work(struct work_struct *work)
5220{
5221 struct beiscsi_hba *phba =
5222 container_of(work, struct beiscsi_hba, boot_work);
5223 struct boot_struct *bs = &phba->boot_struct;
5224 unsigned int tag = 0;
5225
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305226 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305227 return;
5228
5229 beiscsi_log(phba, KERN_INFO,
5230 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5231 "BM_%d : %s action %d\n",
5232 __func__, phba->boot_struct.action);
5233
5234 switch (phba->boot_struct.action) {
5235 case BEISCSI_BOOT_REOPEN_SESS:
5236 tag = beiscsi_boot_reopen_sess(phba);
5237 break;
5238 case BEISCSI_BOOT_GET_SHANDLE:
5239 tag = __beiscsi_boot_get_shandle(phba, 1);
5240 break;
5241 case BEISCSI_BOOT_GET_SINFO:
5242 tag = beiscsi_boot_get_sinfo(phba);
5243 break;
5244 case BEISCSI_BOOT_LOGOUT_SESS:
5245 tag = beiscsi_boot_logout_sess(phba);
5246 break;
5247 case BEISCSI_BOOT_CREATE_KSET:
5248 beiscsi_boot_create_kset(phba);
5249 /**
5250 * updated boot_kset is made visible to all before
5251 * ending the boot work.
5252 */
5253 mb();
5254 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5255 return;
5256 }
5257 if (!tag) {
5258 if (bs->retry--)
5259 schedule_work(&phba->boot_work);
5260 else
5261 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5262 }
5263}
5264
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305265static void beiscsi_eqd_update_work(struct work_struct *work)
5266{
5267 struct hwi_context_memory *phwi_context;
5268 struct be_set_eqd set_eqd[MAX_CPUS];
5269 struct hwi_controller *phwi_ctrlr;
5270 struct be_eq_obj *pbe_eq;
5271 struct beiscsi_hba *phba;
5272 unsigned int pps, delta;
5273 struct be_aic_obj *aic;
5274 int eqd, i, num = 0;
5275 unsigned long now;
5276
5277 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305278 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305279 return;
5280
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005281 phwi_ctrlr = phba->phwi_ctrlr;
5282 phwi_context = phwi_ctrlr->phwi_ctxt;
5283
5284 for (i = 0; i <= phba->num_cpus; i++) {
5285 aic = &phba->aic_obj[i];
5286 pbe_eq = &phwi_context->be_eq[i];
5287 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305288 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005289 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305290 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005291 aic->eq_prev = pbe_eq->cq_count;
5292 continue;
5293 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305294 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005295 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5296 eqd = (pps / 1500) << 2;
5297
5298 if (eqd < 8)
5299 eqd = 0;
5300 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5301 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5302
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305303 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005304 aic->eq_prev = pbe_eq->cq_count;
5305
5306 if (eqd != aic->prev_eqd) {
5307 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5308 set_eqd[num].eq_id = pbe_eq->q.id;
5309 aic->prev_eqd = eqd;
5310 num++;
5311 }
5312 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305313 if (num)
5314 /* completion of this is ignored */
5315 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005316
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305317 schedule_delayed_work(&phba->eqd_update,
5318 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305319}
5320
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305321static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5322{
5323 int i, status;
5324
5325 for (i = 0; i <= phba->num_cpus; i++)
5326 phba->msix_entries[i].entry = i;
5327
5328 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5329 phba->num_cpus + 1, phba->num_cpus + 1);
5330 if (status > 0)
5331 phba->msix_enabled = true;
5332}
5333
5334static void beiscsi_hw_tpe_check(unsigned long ptr)
5335{
5336 struct beiscsi_hba *phba;
5337 u32 wait;
5338
5339 phba = (struct beiscsi_hba *)ptr;
5340 /* if not TPE, do nothing */
5341 if (!beiscsi_detect_tpe(phba))
5342 return;
5343
5344 /* wait default 4000ms before recovering */
5345 wait = 4000;
5346 if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
5347 wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
5348 queue_delayed_work(phba->wq, &phba->recover_port,
5349 msecs_to_jiffies(wait));
5350}
5351
5352static void beiscsi_hw_health_check(unsigned long ptr)
5353{
5354 struct beiscsi_hba *phba;
5355
5356 phba = (struct beiscsi_hba *)ptr;
5357 beiscsi_detect_ue(phba);
5358 if (beiscsi_detect_ue(phba)) {
5359 __beiscsi_log(phba, KERN_ERR,
5360 "BM_%d : port in error: %lx\n", phba->state);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305361 /* sessions are no longer valid, so first fail the sessions */
5362 queue_work(phba->wq, &phba->sess_work);
5363
5364 /* detect UER supported */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305365 if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
5366 return;
5367 /* modify this timer to check TPE */
5368 phba->hw_check.function = beiscsi_hw_tpe_check;
5369 }
5370
5371 mod_timer(&phba->hw_check,
5372 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5373}
5374
5375/*
5376 * beiscsi_enable_port()- Enables the disabled port.
5377 * Only port resources freed in disable function are reallocated.
5378 * This is called in HBA error handling path.
5379 *
5380 * @phba: Instance of driver private structure
5381 *
5382 **/
5383static int beiscsi_enable_port(struct beiscsi_hba *phba)
5384{
5385 struct hwi_context_memory *phwi_context;
5386 struct hwi_controller *phwi_ctrlr;
5387 struct be_eq_obj *pbe_eq;
5388 int ret, i;
5389
5390 if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
5391 __beiscsi_log(phba, KERN_ERR,
5392 "BM_%d : %s : port is online %lx\n",
5393 __func__, phba->state);
5394 return 0;
5395 }
5396
5397 ret = beiscsi_init_sliport(phba);
5398 if (ret)
5399 return ret;
5400
5401 if (enable_msix)
5402 find_num_cpus(phba);
5403 else
5404 phba->num_cpus = 1;
5405 if (enable_msix) {
5406 beiscsi_msix_enable(phba);
5407 if (!phba->msix_enabled)
5408 phba->num_cpus = 1;
5409 }
5410
5411 beiscsi_get_params(phba);
5412 /* Re-enable UER. If different TPE occurs then it is recoverable. */
5413 beiscsi_set_uer_feature(phba);
5414
5415 phba->shost->max_id = phba->params.cxns_per_ctrl;
5416 phba->shost->can_queue = phba->params.ios_per_ctrl;
5417 ret = hwi_init_controller(phba);
5418 if (ret) {
5419 __beiscsi_log(phba, KERN_ERR,
5420 "BM_%d : init controller failed %d\n", ret);
5421 goto disable_msix;
5422 }
5423
5424 for (i = 0; i < MAX_MCC_CMD; i++) {
5425 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5426 phba->ctrl.mcc_tag[i] = i + 1;
5427 phba->ctrl.mcc_tag_status[i + 1] = 0;
5428 phba->ctrl.mcc_tag_available++;
5429 }
5430
5431 phwi_ctrlr = phba->phwi_ctrlr;
5432 phwi_context = phwi_ctrlr->phwi_ctxt;
5433 for (i = 0; i < phba->num_cpus; i++) {
5434 pbe_eq = &phwi_context->be_eq[i];
5435 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5436 }
5437
5438 i = (phba->msix_enabled) ? i : 0;
5439 /* Work item for MCC handling */
5440 pbe_eq = &phwi_context->be_eq[i];
5441 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5442
5443 ret = beiscsi_init_irqs(phba);
5444 if (ret < 0) {
5445 __beiscsi_log(phba, KERN_ERR,
5446 "BM_%d : setup IRQs failed %d\n", ret);
5447 goto cleanup_port;
5448 }
5449 hwi_enable_intr(phba);
5450 /* port operational: clear all error bits */
5451 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5452 __beiscsi_log(phba, KERN_INFO,
5453 "BM_%d : port online: 0x%lx\n", phba->state);
5454
5455 /* start hw_check timer and eqd_update work */
5456 schedule_delayed_work(&phba->eqd_update,
5457 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5458
5459 /**
5460 * Timer function gets modified for TPE detection.
5461 * Always reinit to do health check first.
5462 */
5463 phba->hw_check.function = beiscsi_hw_health_check;
5464 mod_timer(&phba->hw_check,
5465 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5466 return 0;
5467
5468cleanup_port:
5469 for (i = 0; i < phba->num_cpus; i++) {
5470 pbe_eq = &phwi_context->be_eq[i];
5471 irq_poll_disable(&pbe_eq->iopoll);
5472 }
5473 hwi_cleanup_port(phba);
5474
5475disable_msix:
5476 if (phba->msix_enabled)
5477 pci_disable_msix(phba->pcidev);
5478
5479 return ret;
5480}
5481
5482/*
5483 * beiscsi_disable_port()- Disable port and cleanup driver resources.
5484 * This is called in HBA error handling and driver removal.
5485 * @phba: Instance Priv structure
5486 * @unload: indicate driver is unloading
5487 *
5488 * Free the OS and HW resources held by the driver
5489 **/
5490static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
5491{
5492 struct hwi_context_memory *phwi_context;
5493 struct hwi_controller *phwi_ctrlr;
5494 struct be_eq_obj *pbe_eq;
5495 unsigned int i, msix_vec;
5496
5497 if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
5498 return;
5499
5500 phwi_ctrlr = phba->phwi_ctrlr;
5501 phwi_context = phwi_ctrlr->phwi_ctxt;
5502 hwi_disable_intr(phba);
5503 if (phba->msix_enabled) {
5504 for (i = 0; i <= phba->num_cpus; i++) {
5505 msix_vec = phba->msix_entries[i].vector;
5506 free_irq(msix_vec, &phwi_context->be_eq[i]);
5507 kfree(phba->msi_name[i]);
5508 }
5509 } else
5510 if (phba->pcidev->irq)
5511 free_irq(phba->pcidev->irq, phba);
5512 pci_disable_msix(phba->pcidev);
5513
5514 for (i = 0; i < phba->num_cpus; i++) {
5515 pbe_eq = &phwi_context->be_eq[i];
5516 irq_poll_disable(&pbe_eq->iopoll);
5517 }
5518 cancel_delayed_work_sync(&phba->eqd_update);
5519 cancel_work_sync(&phba->boot_work);
5520 /* WQ might be running cancel queued mcc_work if we are not exiting */
5521 if (!unload && beiscsi_hba_in_error(phba)) {
5522 pbe_eq = &phwi_context->be_eq[i];
5523 cancel_work_sync(&pbe_eq->mcc_work);
5524 }
5525 hwi_cleanup_port(phba);
5526}
5527
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305528static void beiscsi_sess_work(struct work_struct *work)
5529{
5530 struct beiscsi_hba *phba;
5531
5532 phba = container_of(work, struct beiscsi_hba, sess_work);
5533 /*
5534 * This work gets scheduled only in case of HBA error.
5535 * Old sessions are gone so need to be re-established.
5536 * iscsi_session_failure needs process context hence this work.
5537 */
5538 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5539}
5540
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305541static void beiscsi_recover_port(struct work_struct *work)
5542{
5543 struct beiscsi_hba *phba;
5544
5545 phba = container_of(work, struct beiscsi_hba, recover_port.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305546 beiscsi_disable_port(phba, 0);
5547 beiscsi_enable_port(phba);
5548}
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005549
5550static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5551 pci_channel_state_t state)
5552{
5553 struct beiscsi_hba *phba = NULL;
5554
5555 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305556 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005557
5558 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5559 "BM_%d : EEH error detected\n");
5560
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305561 /* first stop UE detection when PCI error detected */
5562 del_timer_sync(&phba->hw_check);
5563 cancel_delayed_work_sync(&phba->recover_port);
5564
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305565 /* sessions are no longer valid, so first fail the sessions */
5566 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305567 beiscsi_disable_port(phba, 0);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005568
5569 if (state == pci_channel_io_perm_failure) {
5570 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5571 "BM_%d : EEH : State PERM Failure");
5572 return PCI_ERS_RESULT_DISCONNECT;
5573 }
5574
5575 pci_disable_device(pdev);
5576
5577 /* The error could cause the FW to trigger a flash debug dump.
5578 * Resetting the card while flash dump is in progress
5579 * can cause it not to recover; wait for it to finish.
5580 * Wait only for first function as it is needed only once per
5581 * adapter.
5582 **/
5583 if (pdev->devfn == 0)
5584 ssleep(30);
5585
5586 return PCI_ERS_RESULT_NEED_RESET;
5587}
5588
5589static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5590{
5591 struct beiscsi_hba *phba = NULL;
5592 int status = 0;
5593
5594 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5595
5596 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5597 "BM_%d : EEH Reset\n");
5598
5599 status = pci_enable_device(pdev);
5600 if (status)
5601 return PCI_ERS_RESULT_DISCONNECT;
5602
5603 pci_set_master(pdev);
5604 pci_set_power_state(pdev, PCI_D0);
5605 pci_restore_state(pdev);
5606
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305607 status = beiscsi_check_fw_rdy(phba);
5608 if (status) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005609 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5610 "BM_%d : EEH Reset Completed\n");
5611 } else {
5612 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5613 "BM_%d : EEH Reset Completion Failure\n");
5614 return PCI_ERS_RESULT_DISCONNECT;
5615 }
5616
5617 pci_cleanup_aer_uncorrect_error_status(pdev);
5618 return PCI_ERS_RESULT_RECOVERED;
5619}
5620
5621static void beiscsi_eeh_resume(struct pci_dev *pdev)
5622{
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305623 struct beiscsi_hba *phba;
5624 int ret;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005625
5626 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5627 pci_save_state(pdev);
5628
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305629 ret = beiscsi_enable_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305630 if (ret)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305631 __beiscsi_log(phba, KERN_ERR,
5632 "BM_%d : AER EEH resume failed\n");
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005633}
5634
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005635static int beiscsi_dev_probe(struct pci_dev *pcidev,
5636 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305637{
5638 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305639 struct hwi_controller *phwi_ctrlr;
5640 struct hwi_context_memory *phwi_context;
5641 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305642 unsigned int s_handle;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305643 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305644
5645 ret = beiscsi_enable_pci(pcidev);
5646 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305647 dev_err(&pcidev->dev,
5648 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305649 return ret;
5650 }
5651
5652 phba = beiscsi_hba_alloc(pcidev);
5653 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305654 dev_err(&pcidev->dev,
5655 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305656 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305657 goto disable_pci;
5658 }
5659
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005660 /* Enable EEH reporting */
5661 ret = pci_enable_pcie_error_reporting(pcidev);
5662 if (ret)
5663 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5664 "BM_%d : PCIe Error Reporting "
5665 "Enabling Failed\n");
5666
5667 pci_save_state(pcidev);
5668
John Soni Jose99bc5d52012-08-20 23:00:18 +05305669 /* Initialize Driver configuration Paramters */
5670 beiscsi_hba_attrs_init(phba);
5671
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005672 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305673
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305674 switch (pcidev->device) {
5675 case BE_DEVICE_ID1:
5676 case OC_DEVICE_ID1:
5677 case OC_DEVICE_ID2:
5678 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305679 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305680 break;
5681 case BE_DEVICE_ID2:
5682 case OC_DEVICE_ID3:
5683 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305684 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305685 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305686 case OC_SKH_ID1:
5687 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305688 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005689 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305690 default:
5691 phba->generation = 0;
5692 }
5693
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305694 ret = be_ctrl_init(phba, pcidev);
5695 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305696 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305697 "BM_%d : be_ctrl_init failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305698 goto hba_free;
5699 }
5700
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305701 ret = beiscsi_init_sliport(phba);
5702 if (ret)
5703 goto hba_free;
5704
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305705 spin_lock_init(&phba->io_sgl_lock);
5706 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005707 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305708 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305709 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305710 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5711 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305712 goto free_port;
5713 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305714 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305715 beiscsi_get_params(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305716 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005717
5718 if (enable_msix)
5719 find_num_cpus(phba);
5720 else
5721 phba->num_cpus = 1;
5722
5723 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5724 "BM_%d : num_cpus = %d\n",
5725 phba->num_cpus);
5726
5727 if (enable_msix) {
5728 beiscsi_msix_enable(phba);
5729 if (!phba->msix_enabled)
5730 phba->num_cpus = 1;
5731 }
5732
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005733 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305734 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305735 ret = beiscsi_init_port(phba);
5736 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305737 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5738 "BM_%d : beiscsi_dev_probe-"
5739 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305740 goto free_port;
5741 }
5742
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005743 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305744 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5745 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305746 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305747 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005748 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005749 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305750 }
5751
5752 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5753
John Soni Jose72fb46a2012-10-20 04:42:49 +05305754 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305755 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005756 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305757 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305758 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5759 "BM_%d : beiscsi_dev_probe-"
5760 "Failed to allocate work queue\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305761 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305762 goto free_twq;
5763 }
5764
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305765 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305766
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305767 phwi_ctrlr = phba->phwi_ctrlr;
5768 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305769
Jens Axboe89f8b332014-03-13 09:38:42 -06005770 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305771 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305772 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305773 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305774
Jens Axboe89f8b332014-03-13 09:38:42 -06005775 i = (phba->msix_enabled) ? i : 0;
5776 /* Work item for MCC handling */
5777 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305778 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005779
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305780 ret = beiscsi_init_irqs(phba);
5781 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305782 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5783 "BM_%d : beiscsi_dev_probe-"
5784 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305785 goto free_blkenbld;
5786 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305787 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005788
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305789 ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
5790 if (ret)
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005791 goto free_blkenbld;
5792
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305793 /* set online bit after port is operational */
5794 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5795 __beiscsi_log(phba, KERN_INFO,
5796 "BM_%d : port online: 0x%lx\n", phba->state);
5797
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305798 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5799 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5800 if (ret > 0) {
5801 beiscsi_start_boot_work(phba, s_handle);
5802 /**
5803 * Set this bit after starting the work to let
5804 * probe handle it first.
5805 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005806 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305807 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5808 }
Mike Christief457a462011-06-24 15:11:53 -05005809
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305810 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305811 schedule_delayed_work(&phba->eqd_update,
5812 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305813
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305814 INIT_WORK(&phba->sess_work, beiscsi_sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305815 INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305816 /**
5817 * Start UE detection here. UE before this will cause stall in probe
5818 * and eventually fail the probe.
5819 */
5820 init_timer(&phba->hw_check);
5821 phba->hw_check.function = beiscsi_hw_health_check;
5822 phba->hw_check.data = (unsigned long)phba;
5823 mod_timer(&phba->hw_check,
5824 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose99bc5d52012-08-20 23:00:18 +05305825 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5826 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305827 return 0;
5828
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305829free_blkenbld:
5830 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005831 for (i = 0; i < phba->num_cpus; i++) {
5832 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005833 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005834 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305835free_twq:
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305836 hwi_cleanup_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305837 beiscsi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305838 beiscsi_free_mem(phba);
5839free_port:
5840 pci_free_consistent(phba->pcidev,
5841 phba->ctrl.mbox_mem_alloced.size,
5842 phba->ctrl.mbox_mem_alloced.va,
5843 phba->ctrl.mbox_mem_alloced.dma);
5844 beiscsi_unmap_pci_function(phba);
5845hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305846 if (phba->msix_enabled)
5847 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305848 pci_dev_put(phba->pcidev);
5849 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305850 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305851disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305852 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305853 pci_disable_device(pcidev);
5854 return ret;
5855}
5856
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305857static void beiscsi_remove(struct pci_dev *pcidev)
5858{
5859 struct beiscsi_hba *phba = NULL;
5860
5861 phba = pci_get_drvdata(pcidev);
5862 if (!phba) {
5863 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5864 return;
5865 }
5866
5867 /* first stop UE detection before unloading */
5868 del_timer_sync(&phba->hw_check);
5869 cancel_delayed_work_sync(&phba->recover_port);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305870 cancel_work_sync(&phba->sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305871
5872 beiscsi_iface_destroy_default(phba);
5873 iscsi_host_remove(phba->shost);
5874 beiscsi_disable_port(phba, 1);
5875
5876 /* after cancelling boot_work */
5877 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5878
5879 /* free all resources */
5880 destroy_workqueue(phba->wq);
5881 beiscsi_cleanup_port(phba);
5882 beiscsi_free_mem(phba);
5883
5884 /* ctrl uninit */
5885 beiscsi_unmap_pci_function(phba);
5886 pci_free_consistent(phba->pcidev,
5887 phba->ctrl.mbox_mem_alloced.size,
5888 phba->ctrl.mbox_mem_alloced.va,
5889 phba->ctrl.mbox_mem_alloced.dma);
5890
5891 pci_dev_put(phba->pcidev);
5892 iscsi_host_free(phba->shost);
5893 pci_disable_pcie_error_reporting(pcidev);
5894 pci_set_drvdata(pcidev, NULL);
5895 pci_release_regions(pcidev);
5896 pci_disable_device(pcidev);
5897}
5898
5899
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005900static struct pci_error_handlers beiscsi_eeh_handlers = {
5901 .error_detected = beiscsi_eeh_err_detected,
5902 .slot_reset = beiscsi_eeh_reset,
5903 .resume = beiscsi_eeh_resume,
5904};
5905
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305906struct iscsi_transport beiscsi_iscsi_transport = {
5907 .owner = THIS_MODULE,
5908 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305909 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305910 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305911 .create_session = beiscsi_session_create,
5912 .destroy_session = beiscsi_session_destroy,
5913 .create_conn = beiscsi_conn_create,
5914 .bind_conn = beiscsi_conn_bind,
5915 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305916 .attr_is_visible = beiscsi_attr_is_visible,
5917 .set_iface_param = beiscsi_iface_set_param,
5918 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305919 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005920 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305921 .get_session_param = iscsi_session_get_param,
5922 .get_host_param = beiscsi_get_host_param,
5923 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005924 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305925 .send_pdu = iscsi_conn_send_pdu,
5926 .xmit_task = beiscsi_task_xmit,
5927 .cleanup_task = beiscsi_cleanup_task,
5928 .alloc_pdu = beiscsi_alloc_pdu,
5929 .parse_pdu_itt = beiscsi_parse_pdu,
5930 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005931 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305932 .ep_connect = beiscsi_ep_connect,
5933 .ep_poll = beiscsi_ep_poll,
5934 .ep_disconnect = beiscsi_ep_disconnect,
5935 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005936 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305937};
5938
5939static struct pci_driver beiscsi_pci_driver = {
5940 .name = DRV_NAME,
5941 .probe = beiscsi_dev_probe,
5942 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005943 .id_table = beiscsi_pci_id_table,
5944 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305945};
5946
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305947static int __init beiscsi_module_init(void)
5948{
5949 int ret;
5950
5951 beiscsi_scsi_transport =
5952 iscsi_register_transport(&beiscsi_iscsi_transport);
5953 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305954 printk(KERN_ERR
5955 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305956 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305957 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305958 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5959 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305960
5961 ret = pci_register_driver(&beiscsi_pci_driver);
5962 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305963 printk(KERN_ERR
5964 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305965 goto unregister_iscsi_transport;
5966 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305967 return 0;
5968
5969unregister_iscsi_transport:
5970 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5971 return ret;
5972}
5973
5974static void __exit beiscsi_module_exit(void)
5975{
5976 pci_unregister_driver(&beiscsi_pci_driver);
5977 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5978}
5979
5980module_init(beiscsi_module_init);
5981module_exit(beiscsi_module_exit);