blob: 6a6906f847dbba07ef38149a9912563d48ba3d82 [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)\
64ssize_t \
65beiscsi_##_name##_disp(struct device *dev,\
66 struct device_attribute *attrib, char *buf) \
67{ \
68 struct Scsi_Host *shost = class_to_shost(dev);\
69 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
70 uint32_t param_val = 0; \
71 param_val = phba->attr_##_name;\
72 return snprintf(buf, PAGE_SIZE, "%d\n",\
73 phba->attr_##_name);\
74}
75
76#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
77int \
78beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
79{\
80 if (val >= _minval && val <= _maxval) {\
81 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
82 "BA_%d : beiscsi_"#_name" updated "\
83 "from 0x%x ==> 0x%x\n",\
84 phba->attr_##_name, val); \
85 phba->attr_##_name = val;\
86 return 0;\
87 } \
88 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
89 "BA_%d beiscsi_"#_name" attribute "\
90 "cannot be updated to 0x%x, "\
91 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
92 return -EINVAL;\
93}
94
95#define beiscsi_store_param(_name) \
96ssize_t \
97beiscsi_##_name##_store(struct device *dev,\
98 struct device_attribute *attr, const char *buf,\
99 size_t count) \
100{ \
101 struct Scsi_Host *shost = class_to_shost(dev);\
102 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
103 uint32_t param_val = 0;\
104 if (!isdigit(buf[0]))\
105 return -EINVAL;\
106 if (sscanf(buf, "%i", &param_val) != 1)\
107 return -EINVAL;\
108 if (beiscsi_##_name##_change(phba, param_val) == 0) \
109 return strlen(buf);\
110 else \
111 return -EINVAL;\
112}
113
114#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
115int \
116beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
117{ \
118 if (val >= _minval && val <= _maxval) {\
119 phba->attr_##_name = val;\
120 return 0;\
121 } \
122 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
123 "BA_%d beiscsi_"#_name" attribute " \
124 "cannot be updated to 0x%x, "\
125 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
126 phba->attr_##_name = _defval;\
127 return -EINVAL;\
128}
129
130#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
131static uint beiscsi_##_name = _defval;\
132module_param(beiscsi_##_name, uint, S_IRUGO);\
133MODULE_PARM_DESC(beiscsi_##_name, _descp);\
134beiscsi_disp_param(_name)\
135beiscsi_change_param(_name, _minval, _maxval, _defval)\
136beiscsi_store_param(_name)\
137beiscsi_init_param(_name, _minval, _maxval, _defval)\
138DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
139 beiscsi_##_name##_disp, beiscsi_##_name##_store)
140
141/*
142 * When new log level added update the
143 * the MAX allowed value for log_enable
144 */
145BEISCSI_RW_ATTR(log_enable, 0x00,
146 0xFF, 0x00, "Enable logging Bit Mask\n"
147 "\t\t\t\tInitialization Events : 0x01\n"
148 "\t\t\t\tMailbox Events : 0x02\n"
149 "\t\t\t\tMiscellaneous Events : 0x04\n"
150 "\t\t\t\tError Handling : 0x08\n"
151 "\t\t\t\tIO Path Events : 0x10\n"
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 Kallickal6c57625b2014-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 Kallickal6c57625b2014-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;
903
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530904 spin_lock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530905 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530906 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
907 "BM_%d : In alloc_io_sgl_handle,"
908 " io_sgl_alloc_index=%d\n",
909 phba->io_sgl_alloc_index);
910
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530911 psgl_handle = phba->io_sgl_hndl_base[phba->
912 io_sgl_alloc_index];
913 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
914 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530915 if (phba->io_sgl_alloc_index == (phba->params.
916 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530917 phba->io_sgl_alloc_index = 0;
918 else
919 phba->io_sgl_alloc_index++;
920 } else
921 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530922 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530923 return psgl_handle;
924}
925
926static void
927free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
928{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530929 spin_lock_bh(&phba->io_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530930 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
931 "BM_%d : In free_,io_sgl_free_index=%d\n",
932 phba->io_sgl_free_index);
933
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530934 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
935 /*
936 * this can happen if clean_task is called on a task that
937 * failed in xmit_task or alloc_pdu.
938 */
John Soni Jose99bc5d52012-08-20 23:00:18 +0530939 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
940 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
941 "value there=%p\n", phba->io_sgl_free_index,
942 phba->io_sgl_hndl_base
943 [phba->io_sgl_free_index]);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530944 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530945 return;
946 }
947 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
948 phba->io_sgl_hndl_avbl++;
949 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
950 phba->io_sgl_free_index = 0;
951 else
952 phba->io_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530953 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530954}
955
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530956static inline struct wrb_handle *
957beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
958 unsigned int wrbs_per_cxn)
959{
960 struct wrb_handle *pwrb_handle;
961
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +0530962 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530963 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
964 pwrb_context->wrb_handles_available--;
965 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
966 pwrb_context->alloc_index = 0;
967 else
968 pwrb_context->alloc_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +0530969 spin_unlock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarebf9b7552016-08-26 15:09:07 +0530970
971 if (pwrb_handle)
972 memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530973
974 return pwrb_handle;
975}
976
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530977/**
978 * alloc_wrb_handle - To allocate a wrb handle
979 * @phba: The hba pointer
980 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +0530981 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530982 *
983 * This happens under session_lock until submission to chip
984 */
John Soni Jose340c99e2015-08-20 04:44:30 +0530985struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530986 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530987{
988 struct hwi_wrb_context *pwrb_context;
989 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700990 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530991
992 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700993 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530994 /* return the context address */
995 *pcontext = pwrb_context;
996 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
997}
John Soni Jose340c99e2015-08-20 04:44:30 +0530998
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530999static inline void
1000beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1001 struct wrb_handle *pwrb_handle,
1002 unsigned int wrbs_per_cxn)
1003{
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301004 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301005 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1006 pwrb_context->wrb_handles_available++;
1007 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1008 pwrb_context->free_index = 0;
1009 else
1010 pwrb_context->free_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301011 spin_unlock_bh(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301012}
1013
1014/**
1015 * free_wrb_handle - To free the wrb handle back to pool
1016 * @phba: The hba pointer
1017 * @pwrb_context: The context to free from
1018 * @pwrb_handle: The wrb_handle to free
1019 *
1020 * This happens under session_lock until submission to chip
1021 */
1022static void
1023free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1024 struct wrb_handle *pwrb_handle)
1025{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301026 beiscsi_put_wrb_handle(pwrb_context,
1027 pwrb_handle,
1028 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301029 beiscsi_log(phba, KERN_INFO,
1030 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1031 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1032 "wrb_handles_available=%d\n",
1033 pwrb_handle, pwrb_context->free_index,
1034 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301035}
1036
1037static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1038{
1039 struct sgl_handle *psgl_handle;
1040
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301041 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301042 if (phba->eh_sgl_hndl_avbl) {
1043 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1044 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301045 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1046 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1047 phba->eh_sgl_alloc_index,
1048 phba->eh_sgl_alloc_index);
1049
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301050 phba->eh_sgl_hndl_avbl--;
1051 if (phba->eh_sgl_alloc_index ==
1052 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1053 1))
1054 phba->eh_sgl_alloc_index = 0;
1055 else
1056 phba->eh_sgl_alloc_index++;
1057 } else
1058 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301059 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301060 return psgl_handle;
1061}
1062
1063void
1064free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1065{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301066 spin_lock_bh(&phba->mgmt_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301067 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1068 "BM_%d : In free_mgmt_sgl_handle,"
1069 "eh_sgl_free_index=%d\n",
1070 phba->eh_sgl_free_index);
1071
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301072 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1073 /*
1074 * this can happen if clean_task is called on a task that
1075 * failed in xmit_task or alloc_pdu.
1076 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301077 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1078 "BM_%d : Double Free in eh SGL ,"
1079 "eh_sgl_free_index=%d\n",
1080 phba->eh_sgl_free_index);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301081 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301082 return;
1083 }
1084 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1085 phba->eh_sgl_hndl_avbl++;
1086 if (phba->eh_sgl_free_index ==
1087 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1088 phba->eh_sgl_free_index = 0;
1089 else
1090 phba->eh_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301091 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301092}
1093
1094static void
1095be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301096 struct iscsi_task *task,
1097 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301098{
1099 struct beiscsi_io_task *io_task = task->dd_data;
1100 struct be_status_bhs *sts_bhs =
1101 (struct be_status_bhs *)io_task->cmd_bhs;
1102 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301103 unsigned char *sense;
1104 u32 resid = 0, exp_cmdsn, max_cmdsn;
1105 u8 rsp, status, flags;
1106
John Soni Jose73133262012-10-20 04:44:49 +05301107 exp_cmdsn = csol_cqe->exp_cmdsn;
1108 max_cmdsn = (csol_cqe->exp_cmdsn +
1109 csol_cqe->cmd_wnd - 1);
1110 rsp = csol_cqe->i_resp;
1111 status = csol_cqe->i_sts;
1112 flags = csol_cqe->i_flags;
1113 resid = csol_cqe->res_cnt;
1114
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001115 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001116 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001117 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001118 io_task->scsi_cmnd = NULL;
1119 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301120
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001121 return;
1122 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301123 task->sc->result = (DID_OK << 16) | status;
1124 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1125 task->sc->result = DID_ERROR << 16;
1126 goto unmap;
1127 }
1128
1129 /* bidi not initially supported */
1130 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301131 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1132 task->sc->result = DID_ERROR << 16;
1133
1134 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1135 scsi_set_resid(task->sc, resid);
1136 if (!status && (scsi_bufflen(task->sc) - resid <
1137 task->sc->underflow))
1138 task->sc->result = DID_ERROR << 16;
1139 }
1140 }
1141
1142 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001143 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301144 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001145
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301146 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001147 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301148 memcpy(task->sc->sense_buffer, sense,
1149 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1150 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301151
John Soni Jose73133262012-10-20 04:44:49 +05301152 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1153 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301154unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301155 if (io_task->scsi_cmnd) {
1156 scsi_dma_unmap(io_task->scsi_cmnd);
1157 io_task->scsi_cmnd = NULL;
1158 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301159 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1160}
1161
1162static void
1163be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301164 struct iscsi_task *task,
1165 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301166{
1167 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301168 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301169 struct iscsi_conn *conn = beiscsi_conn->conn;
1170
1171 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301172 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301173 hdr->t2wait = 5;
1174 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301175 hdr->flags = csol_cqe->i_flags;
1176 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001177 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1178 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1179 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301180
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301181 hdr->dlength[0] = 0;
1182 hdr->dlength[1] = 0;
1183 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301184 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301185 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301186 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1187}
1188
1189static void
1190be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301191 struct iscsi_task *task,
1192 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301193{
1194 struct iscsi_tm_rsp *hdr;
1195 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301196 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301197
1198 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301199 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301200 hdr->flags = csol_cqe->i_flags;
1201 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001202 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1203 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1204 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301205
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301206 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301207 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1208}
1209
1210static void
1211hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1212 struct beiscsi_hba *phba, struct sol_cqe *psol)
1213{
1214 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001215 uint16_t wrb_index, cid, cri_index;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05301216 struct hwi_controller *phwi_ctrlr;
1217 struct wrb_handle *pwrb_handle;
1218 struct iscsi_task *task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301219
1220 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001221 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301222 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1223 wrb_idx, psol);
1224 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1225 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001226 } else {
1227 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1228 wrb_idx, psol);
1229 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1230 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301231 }
1232
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001233 cri_index = BE_GET_CRI_FROM_CID(cid);
1234 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301235 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301236 task = pwrb_handle->pio_handle;
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07001237 iscsi_put_task(task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301238}
1239
1240static void
1241be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301242 struct iscsi_task *task,
1243 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301244{
1245 struct iscsi_nopin *hdr;
1246 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301247 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301248
1249 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301250 hdr->flags = csol_cqe->i_flags;
1251 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001252 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1253 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301254
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301255 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301256 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301257 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1258}
1259
John Soni Jose73133262012-10-20 04:44:49 +05301260static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1261 struct sol_cqe *psol,
1262 struct common_sol_cqe *csol_cqe)
1263{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001264 if (is_chip_be2_be3r(phba)) {
1265 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1266 i_exp_cmd_sn, psol);
1267 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1268 i_res_cnt, psol);
1269 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1270 i_cmd_wnd, psol);
1271 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1272 wrb_index, psol);
1273 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1274 cid, psol);
1275 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1276 hw_sts, psol);
1277 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1278 i_resp, psol);
1279 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1280 i_sts, psol);
1281 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1282 i_flags, psol);
1283 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301284 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1285 i_exp_cmd_sn, psol);
1286 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1287 i_res_cnt, psol);
1288 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1289 wrb_index, psol);
1290 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1291 cid, psol);
1292 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1293 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001294 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301295 i_cmd_wnd, psol);
1296 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1297 cmd_cmpl, psol))
1298 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1299 i_sts, psol);
1300 else
1301 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1302 i_sts, psol);
1303 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1304 u, psol))
1305 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1306
1307 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1308 o, psol))
1309 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301310 }
1311}
1312
1313
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301314static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1315 struct beiscsi_hba *phba, struct sol_cqe *psol)
1316{
1317 struct hwi_wrb_context *pwrb_context;
1318 struct wrb_handle *pwrb_handle;
1319 struct iscsi_wrb *pwrb = NULL;
1320 struct hwi_controller *phwi_ctrlr;
1321 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301322 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301323 struct iscsi_conn *conn = beiscsi_conn->conn;
1324 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301325 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001326 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301327
1328 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301329
1330 /* Copy the elements to a common structure */
1331 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1332
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001333 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1334 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301335
1336 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1337 csol_cqe.wrb_index];
1338
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301339 task = pwrb_handle->pio_handle;
1340 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301341 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301342
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001343 spin_lock_bh(&session->back_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301344 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301345 case HWH_TYPE_IO:
1346 case HWH_TYPE_IO_RD:
1347 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301348 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301349 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301350 else
John Soni Jose73133262012-10-20 04:44:49 +05301351 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301352 break;
1353
1354 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301355 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301356 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301357 else
John Soni Jose73133262012-10-20 04:44:49 +05301358 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301359 break;
1360
1361 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301362 beiscsi_log(phba, KERN_ERR,
1363 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1364 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1365 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301366 break;
1367
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301368 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301369 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301370 break;
1371
1372 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301373 beiscsi_log(phba, KERN_WARNING,
1374 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1375 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1376 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301377 csol_cqe.wrb_index,
1378 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301379 break;
1380 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301381
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001382 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301383}
1384
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301385/**
1386 * ASYNC PDUs include
1387 * a. Unsolicited NOP-In (target initiated NOP-In)
1388 * b. ASYNC Messages
1389 * c. Reject PDU
1390 * d. Login response
1391 * These headers arrive unprocessed by the EP firmware.
1392 * iSCSI layer processes them.
1393 */
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394static unsigned int
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301395beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
1396 struct pdu_base *phdr, void *pdata, unsigned int dlen)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301397{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301398 struct beiscsi_hba *phba = beiscsi_conn->phba;
1399 struct iscsi_conn *conn = beiscsi_conn->conn;
1400 struct beiscsi_io_task *io_task;
1401 struct iscsi_hdr *login_hdr;
1402 struct iscsi_task *task;
1403 u8 code;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301404
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301405 code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
1406 switch (code) {
1407 case ISCSI_OP_NOOP_IN:
1408 pdata = NULL;
1409 dlen = 0;
1410 break;
1411 case ISCSI_OP_ASYNC_EVENT:
1412 break;
1413 case ISCSI_OP_REJECT:
1414 WARN_ON(!pdata);
1415 WARN_ON(!(dlen == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301416 beiscsi_log(phba, KERN_ERR,
1417 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301418 "BM_%d : In ISCSI_OP_REJECT\n");
1419 break;
1420 case ISCSI_OP_LOGIN_RSP:
1421 case ISCSI_OP_TEXT_RSP:
1422 task = conn->login_task;
1423 io_task = task->dd_data;
1424 login_hdr = (struct iscsi_hdr *)phdr;
1425 login_hdr->itt = io_task->libiscsi_itt;
1426 break;
1427 default:
1428 beiscsi_log(phba, KERN_WARNING,
1429 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1430 "BM_%d : unrecognized async PDU opcode 0x%x\n",
1431 code);
1432 return 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301433 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301434 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301435 return 0;
1436}
1437
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301438static inline void
1439beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
1440 struct hd_async_handle *pasync_handle)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301441{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301442 if (pasync_handle->is_header) {
1443 list_add_tail(&pasync_handle->link,
1444 &pasync_ctx->async_header.free_list);
1445 pasync_ctx->async_header.free_entries++;
1446 } else {
1447 list_add_tail(&pasync_handle->link,
1448 &pasync_ctx->async_data.free_list);
1449 pasync_ctx->async_data.free_entries++;
1450 }
1451}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301452
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301453static struct hd_async_handle *
1454beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
1455 struct hd_async_context *pasync_ctx,
1456 struct i_t_dpdu_cqe *pdpdu_cqe)
1457{
1458 struct beiscsi_hba *phba = beiscsi_conn->phba;
1459 struct hd_async_handle *pasync_handle;
1460 struct be_bus_address phys_addr;
1461 u8 final, error = 0;
1462 u16 cid, code, ci;
1463 u32 dpl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301464
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301465 cid = beiscsi_conn->beiscsi_conn_cid;
1466 /**
1467 * This function is invoked to get the right async_handle structure
1468 * from a given DEF PDU CQ entry.
1469 *
1470 * - index in CQ entry gives the vertical index
1471 * - address in CQ entry is the offset where the DMA last ended
1472 * - final - no more notifications for this PDU
1473 */
1474 if (is_chip_be2_be3r(phba)) {
1475 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1476 dpl, pdpdu_cqe);
1477 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1478 index, pdpdu_cqe);
1479 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1480 final, pdpdu_cqe);
1481 } else {
1482 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1483 dpl, pdpdu_cqe);
1484 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1485 index, pdpdu_cqe);
1486 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1487 final, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301488 }
1489
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301490 /**
1491 * DB addr Hi/Lo is same for BE and SKH.
1492 * Subtract the dataplacementlength to get to the base.
1493 */
1494 phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1495 db_addr_lo, pdpdu_cqe);
1496 phys_addr.u.a32.address_lo -= dpl;
1497 phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1498 db_addr_hi, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301499
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301500 code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
1501 switch (code) {
1502 case UNSOL_HDR_NOTIFY:
1503 pasync_handle = pasync_ctx->async_entry[ci].header;
1504 break;
1505 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1506 error = 1;
1507 case UNSOL_DATA_NOTIFY:
1508 pasync_handle = pasync_ctx->async_entry[ci].data;
1509 break;
1510 /* called only for above codes */
1511 default:
1512 pasync_handle = NULL;
1513 break;
1514 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301515
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301516 if (!pasync_handle) {
1517 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1518 "BM_%d : cid %d async PDU handle not found - code %d ci %d addr %llx\n",
1519 cid, code, ci, phys_addr.u.a64.address);
1520 return pasync_handle;
1521 }
1522
1523 if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
1524 pasync_handle->index != ci) {
1525 /* driver bug - if ci does not match async handle index */
1526 error = 1;
1527 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1528 "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
1529 cid, pasync_handle->is_header ? 'H' : 'D',
1530 pasync_handle->pa.u.a64.address,
1531 pasync_handle->index,
1532 phys_addr.u.a64.address, ci);
1533 /* FW has stale address - attempt continuing by dropping */
1534 }
1535
1536 /**
1537 * Each CID is associated with unique CRI.
1538 * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
1539 **/
1540 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
1541 pasync_handle->is_final = final;
1542 pasync_handle->buffer_len = dpl;
1543 /* empty the slot */
1544 if (pasync_handle->is_header)
1545 pasync_ctx->async_entry[ci].header = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301546 else
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301547 pasync_ctx->async_entry[ci].data = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301548
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301549 /**
1550 * DEF PDU header and data buffers with errors should be simply
1551 * dropped as there are no consumers for it.
1552 */
1553 if (error) {
1554 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1555 pasync_handle = NULL;
1556 }
1557 return pasync_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301558}
1559
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301560static void
1561beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
1562 struct hd_async_context *pasync_ctx,
1563 u16 cri)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301564{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301565 struct hd_async_handle *pasync_handle, *tmp_handle;
1566 struct list_head *plist;
1567
1568 plist = &pasync_ctx->async_entry[cri].wq.list;
1569 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1570 list_del(&pasync_handle->link);
1571 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1572 }
1573
1574 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
1575 pasync_ctx->async_entry[cri].wq.hdr_len = 0;
1576 pasync_ctx->async_entry[cri].wq.bytes_received = 0;
1577 pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
1578}
1579
1580static unsigned int
1581beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
1582 struct hd_async_context *pasync_ctx,
1583 u16 cri)
1584{
1585 struct iscsi_session *session = beiscsi_conn->conn->session;
1586 struct hd_async_handle *pasync_handle, *plast_handle;
1587 struct beiscsi_hba *phba = beiscsi_conn->phba;
1588 void *phdr = NULL, *pdata = NULL;
1589 u32 dlen = 0, status = 0;
1590 struct list_head *plist;
1591
1592 plist = &pasync_ctx->async_entry[cri].wq.list;
1593 plast_handle = NULL;
1594 list_for_each_entry(pasync_handle, plist, link) {
1595 plast_handle = pasync_handle;
1596 /* get the header, the first entry */
1597 if (!phdr) {
1598 phdr = pasync_handle->pbuffer;
1599 continue;
1600 }
1601 /* use first buffer to collect all the data */
1602 if (!pdata) {
1603 pdata = pasync_handle->pbuffer;
1604 dlen = pasync_handle->buffer_len;
1605 continue;
1606 }
1607 memcpy(pdata + dlen, pasync_handle->pbuffer,
1608 pasync_handle->buffer_len);
1609 dlen += pasync_handle->buffer_len;
1610 }
1611
1612 if (!plast_handle->is_final) {
1613 /* last handle should have final PDU notification from FW */
1614 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1615 "BM_%d : cid %u %p fwd async PDU with last handle missing - HL%u:DN%u:DR%u\n",
1616 beiscsi_conn->beiscsi_conn_cid, plast_handle,
1617 pasync_ctx->async_entry[cri].wq.hdr_len,
1618 pasync_ctx->async_entry[cri].wq.bytes_needed,
1619 pasync_ctx->async_entry[cri].wq.bytes_received);
1620 }
1621 spin_lock_bh(&session->back_lock);
1622 status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
1623 spin_unlock_bh(&session->back_lock);
1624 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1625 return status;
1626}
1627
1628static unsigned int
1629beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
1630 struct hd_async_context *pasync_ctx,
1631 struct hd_async_handle *pasync_handle)
1632{
1633 unsigned int bytes_needed = 0, status = 0;
1634 u16 cri = pasync_handle->cri;
1635 struct cri_wait_queue *wq;
1636 struct beiscsi_hba *phba;
1637 struct pdu_base *ppdu;
1638 char *err = "";
1639
1640 phba = beiscsi_conn->phba;
1641 wq = &pasync_ctx->async_entry[cri].wq;
1642 if (pasync_handle->is_header) {
1643 /* check if PDU hdr is rcv'd when old hdr not completed */
1644 if (wq->hdr_len) {
1645 err = "incomplete";
1646 goto drop_pdu;
1647 }
1648 ppdu = pasync_handle->pbuffer;
1649 bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
1650 data_len_hi, ppdu);
1651 bytes_needed <<= 16;
1652 bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
1653 data_len_lo, ppdu));
1654 wq->hdr_len = pasync_handle->buffer_len;
1655 wq->bytes_received = 0;
1656 wq->bytes_needed = bytes_needed;
1657 list_add_tail(&pasync_handle->link, &wq->list);
1658 if (!bytes_needed)
1659 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1660 pasync_ctx, cri);
1661 } else {
1662 /* check if data received has header and is needed */
1663 if (!wq->hdr_len || !wq->bytes_needed) {
1664 err = "header less";
1665 goto drop_pdu;
1666 }
1667 wq->bytes_received += pasync_handle->buffer_len;
1668 /* Something got overwritten? Better catch it here. */
1669 if (wq->bytes_received > wq->bytes_needed) {
1670 err = "overflow";
1671 goto drop_pdu;
1672 }
1673 list_add_tail(&pasync_handle->link, &wq->list);
1674 if (wq->bytes_received == wq->bytes_needed)
1675 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1676 pasync_ctx, cri);
1677 }
1678 return status;
1679
1680drop_pdu:
1681 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1682 "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
1683 beiscsi_conn->beiscsi_conn_cid, err,
1684 pasync_handle->is_header ? 'H' : 'D',
1685 wq->hdr_len, wq->bytes_needed,
1686 pasync_handle->buffer_len);
1687 /* discard this handle */
1688 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1689 /* free all the other handles in cri_wait_queue */
1690 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1691 /* try continuing */
1692 return status;
1693}
1694
1695static void
1696beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
1697 u8 header, u8 ulp_num)
1698{
1699 struct hd_async_handle *pasync_handle, *tmp, **slot;
1700 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301701 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301702 struct list_head *hfree_list;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301703 struct phys_addr *pasync_sge;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301704 u32 ring_id, doorbell = 0;
1705 u16 index, num_entries;
1706 u32 doorbell_offset;
1707 u16 prod = 0, cons;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301708
1709 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001710 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001711 num_entries = pasync_ctx->num_entries;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301712 if (header) {
1713 cons = pasync_ctx->async_header.free_entries;
1714 hfree_list = &pasync_ctx->async_header.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001715 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1716 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301717 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301718 } else {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301719 cons = pasync_ctx->async_data.free_entries;
1720 hfree_list = &pasync_ctx->async_data.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001721 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1722 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301723 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301724 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301725 /* number of entries posted must be in multiples of 8 */
1726 if (cons % 8)
1727 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301728
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301729 list_for_each_entry_safe(pasync_handle, tmp, hfree_list, link) {
1730 list_del_init(&pasync_handle->link);
1731 pasync_handle->is_final = 0;
1732 pasync_handle->buffer_len = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301733
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301734 /* handles can be consumed out of order, use index in handle */
1735 index = pasync_handle->index;
1736 WARN_ON(pasync_handle->is_header != header);
1737 if (header)
1738 slot = &pasync_ctx->async_entry[index].header;
1739 else
1740 slot = &pasync_ctx->async_entry[index].data;
1741 /**
1742 * The slot just tracks handle's hold and release, so
1743 * overwriting at the same index won't do any harm but
1744 * needs to be caught.
1745 */
1746 if (*slot != NULL) {
1747 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1748 "BM_%d : async PDU %s slot at %u not empty\n",
1749 header ? "header" : "data", index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301750 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301751 /**
1752 * We use same freed index as in completion to post so this
1753 * operation is not required for refills. Its required only
1754 * for ring creation.
1755 */
1756 if (header)
1757 pasync_sge = pasync_ctx->async_header.ring_base;
1758 else
1759 pasync_sge = pasync_ctx->async_data.ring_base;
1760 pasync_sge += index;
1761 /* if its a refill then address is same; hi is lo */
1762 WARN_ON(pasync_sge->hi &&
1763 pasync_sge->hi != pasync_handle->pa.u.a32.address_lo);
1764 WARN_ON(pasync_sge->lo &&
1765 pasync_sge->lo != pasync_handle->pa.u.a32.address_hi);
1766 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1767 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301768
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301769 *slot = pasync_handle;
1770 if (++prod == cons)
1771 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301772 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301773 if (header)
1774 pasync_ctx->async_header.free_entries -= prod;
1775 else
1776 pasync_ctx->async_data.free_entries -= prod;
1777
1778 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1779 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1780 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1781 doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
1782 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301783}
1784
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301785static void
1786beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
1787 struct i_t_dpdu_cqe *pdpdu_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301788{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301789 struct beiscsi_hba *phba = beiscsi_conn->phba;
1790 struct hd_async_handle *pasync_handle = NULL;
1791 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301792 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301793 u16 cid_cri;
1794 u8 ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301795
1796 phwi_ctrlr = phba->phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301797 cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
1798 ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
1799 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1800 pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
1801 pdpdu_cqe);
1802 if (!pasync_handle)
1803 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301804
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301805 beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
1806 beiscsi_hdq_post_handles(phba, pasync_handle->is_header, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301807}
1808
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301809void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301810{
1811 struct be_queue_info *mcc_cq;
1812 struct be_mcc_compl *mcc_compl;
1813 unsigned int num_processed = 0;
1814
1815 mcc_cq = &phba->ctrl.mcc_obj.cq;
1816 mcc_compl = queue_tail_node(mcc_cq);
1817 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1818 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301819 if (beiscsi_hba_in_error(phba))
1820 return;
1821
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301822 if (num_processed >= 32) {
1823 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301824 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301825 num_processed = 0;
1826 }
1827 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05301828 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301829 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301830 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301831 }
1832
1833 mcc_compl->flags = 0;
1834 queue_tail_inc(mcc_cq);
1835 mcc_compl = queue_tail_node(mcc_cq);
1836 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1837 num_processed++;
1838 }
1839
1840 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301841 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301842}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301843
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301844static void beiscsi_mcc_work(struct work_struct *work)
1845{
1846 struct be_eq_obj *pbe_eq;
1847 struct beiscsi_hba *phba;
1848
1849 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1850 phba = pbe_eq->phba;
1851 beiscsi_process_mcc_cq(phba);
1852 /* rearm EQ for further interrupts */
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301853 if (!beiscsi_hba_in_error(phba))
1854 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301855}
1856
John Soni Jose6763daa2012-10-20 04:41:45 +05301857/**
1858 * beiscsi_process_cq()- Process the Completion Queue
1859 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301860 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05301861 *
1862 * return
1863 * Number of Completion Entries processed.
1864 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301865unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301866{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867 struct be_queue_info *cq;
1868 struct sol_cqe *sol;
1869 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301870 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301871 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301872 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001873 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301874 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05301875 struct beiscsi_endpoint *beiscsi_ep;
1876 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301877 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301878
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301879 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301880 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301881 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301882
1883 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1884 CQE_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301885 if (beiscsi_hba_in_error(phba))
1886 return 0;
1887
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301888 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1889
John Soni Jose73133262012-10-20 04:44:49 +05301890 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
1891 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301892
John Soni Jose73133262012-10-20 04:44:49 +05301893 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001894 if (is_chip_be2_be3r(phba)) {
1895 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1896 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301897 if ((code == DRIVERMSG_NOTIFY) ||
1898 (code == UNSOL_HDR_NOTIFY) ||
1899 (code == UNSOL_DATA_NOTIFY))
1900 cid = AMAP_GET_BITS(
1901 struct amap_i_t_dpdu_cqe_v2,
1902 cid, sol);
1903 else
1904 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1905 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001906 }
John Soni Jose73133262012-10-20 04:44:49 +05301907
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001908 cri_index = BE_GET_CRI_FROM_CID(cid);
1909 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04001910
1911 if (ep == NULL) {
1912 /* connection has already been freed
1913 * just move on to next one
1914 */
1915 beiscsi_log(phba, KERN_WARNING,
1916 BEISCSI_LOG_INIT,
1917 "BM_%d : proc cqe of disconn ep: cid %d\n",
1918 cid);
1919 goto proc_next_cqe;
1920 }
1921
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05301922 beiscsi_ep = ep->dd_data;
1923 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301924
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301925 /* replenish cq */
1926 if (num_processed == 32) {
1927 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301928 num_processed = 0;
1929 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301930 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301931
John Soni Jose0a513dd2012-08-20 23:00:55 +05301932 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301933 case SOL_CMD_COMPLETE:
1934 hwi_complete_cmd(beiscsi_conn, phba, sol);
1935 break;
1936 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301937 beiscsi_log(phba, KERN_INFO,
1938 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301939 "BM_%d : Received %s[%d] on CID : %d\n",
1940 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301941
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301942 dmsg = (struct dmsg_cqe *)sol;
1943 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1944 break;
1945 case UNSOL_HDR_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 Kallickal8f09a3b2013-09-28 15:35:42 -07001951 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301952 beiscsi_hdq_process_compl(beiscsi_conn,
1953 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001954 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301955 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301956 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301957 beiscsi_log(phba, KERN_INFO,
1958 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301959 "BM_%d : Received %s[%d] on CID : %d\n",
1960 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301961
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001962 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301963 beiscsi_hdq_process_compl(beiscsi_conn,
1964 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001965 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301966 break;
1967 case CXN_INVALIDATE_INDEX_NOTIFY:
1968 case CMD_INVALIDATED_NOTIFY:
1969 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301970 beiscsi_log(phba, KERN_ERR,
1971 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301972 "BM_%d : Ignoring %s[%d] on CID : %d\n",
1973 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301974 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301975 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301976 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301977 beiscsi_log(phba, KERN_ERR,
1978 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1979 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1980 cqe_desc[code], code, cid);
1981 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301982 case CMD_KILLED_INVALID_STATSN_RCVD:
1983 case CMD_KILLED_INVALID_R2T_RCVD:
1984 case CMD_CXN_KILLED_LUN_INVALID:
1985 case CMD_CXN_KILLED_ICD_INVALID:
1986 case CMD_CXN_KILLED_ITT_INVALID:
1987 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1988 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301989 beiscsi_log(phba, KERN_ERR,
1990 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301991 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1992 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301993 break;
1994 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301995 beiscsi_log(phba, KERN_ERR,
1996 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301997 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
1998 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001999 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302000 /* driver consumes the entry and drops the contents */
2001 beiscsi_hdq_process_compl(beiscsi_conn,
2002 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002003 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302004 break;
2005 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2006 case CXN_KILLED_BURST_LEN_MISMATCH:
2007 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302008 case CXN_KILLED_UNKNOWN_HDR:
2009 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2010 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2011 case CXN_KILLED_TIMED_OUT:
2012 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302013 case CXN_KILLED_RST_SENT:
2014 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302015 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2016 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2017 case CXN_KILLED_OVER_RUN_RESIDUAL:
2018 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2019 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302020 beiscsi_log(phba, KERN_ERR,
2021 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302022 "BM_%d : Event %s[%d] received on CID : %d\n",
2023 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302024 if (beiscsi_conn)
2025 iscsi_conn_failure(beiscsi_conn->conn,
2026 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302027 break;
2028 default:
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 : Invalid CQE Event Received Code : %d"
2032 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302033 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302034 break;
2035 }
2036
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002037proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302038 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2039 queue_tail_inc(cq);
2040 sol = queue_tail_node(cq);
2041 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302042 if (total == budget)
2043 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302044 }
2045
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302046 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2047 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302048}
2049
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002050static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302051{
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302052 unsigned int ret, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302053 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302054 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302055 struct be_eq_entry *eqe = NULL;
2056 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302057
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302058 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302059 phba = pbe_eq->phba;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302060 if (beiscsi_hba_in_error(phba)) {
2061 irq_poll_complete(iop);
2062 return 0;
2063 }
2064
2065 io_events = 0;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302066 eq = &pbe_eq->q;
2067 eqe = queue_tail_node(eq);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302068 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2069 EQE_VALID_MASK) {
2070 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2071 queue_tail_inc(eq);
2072 eqe = queue_tail_node(eq);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302073 io_events++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302074 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302075 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302076
2077 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002078 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302079 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002080 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302081 beiscsi_log(phba, KERN_INFO,
2082 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302083 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2084 pbe_eq->q.id, ret);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302085 if (!beiscsi_hba_in_error(phba))
2086 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302087 }
2088 return ret;
2089}
2090
2091static void
John Soni Jose09a10932012-10-20 04:44:23 +05302092hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2093 unsigned int num_sg, struct beiscsi_io_task *io_task)
2094{
2095 struct iscsi_sge *psgl;
2096 unsigned int sg_len, index;
2097 unsigned int sge_len = 0;
2098 unsigned long long addr;
2099 struct scatterlist *l_sg;
2100 unsigned int offset;
2101
2102 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2103 io_task->bhs_pa.u.a32.address_lo);
2104 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2105 io_task->bhs_pa.u.a32.address_hi);
2106
2107 l_sg = sg;
2108 for (index = 0; (index < num_sg) && (index < 2); index++,
2109 sg = sg_next(sg)) {
2110 if (index == 0) {
2111 sg_len = sg_dma_len(sg);
2112 addr = (u64) sg_dma_address(sg);
2113 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2114 sge0_addr_lo, pwrb,
2115 lower_32_bits(addr));
2116 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2117 sge0_addr_hi, pwrb,
2118 upper_32_bits(addr));
2119 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2120 sge0_len, pwrb,
2121 sg_len);
2122 sge_len = sg_len;
2123 } else {
2124 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2125 pwrb, sge_len);
2126 sg_len = sg_dma_len(sg);
2127 addr = (u64) sg_dma_address(sg);
2128 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2129 sge1_addr_lo, pwrb,
2130 lower_32_bits(addr));
2131 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2132 sge1_addr_hi, pwrb,
2133 upper_32_bits(addr));
2134 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2135 sge1_len, pwrb,
2136 sg_len);
2137 }
2138 }
2139 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2140 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2141
2142 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2143
2144 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2145 io_task->bhs_pa.u.a32.address_hi);
2146 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2147 io_task->bhs_pa.u.a32.address_lo);
2148
2149 if (num_sg == 1) {
2150 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2151 1);
2152 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2153 0);
2154 } else if (num_sg == 2) {
2155 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2156 0);
2157 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2158 1);
2159 } else {
2160 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2161 0);
2162 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2163 0);
2164 }
2165
2166 sg = l_sg;
2167 psgl++;
2168 psgl++;
2169 offset = 0;
2170 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2171 sg_len = sg_dma_len(sg);
2172 addr = (u64) sg_dma_address(sg);
2173 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2174 lower_32_bits(addr));
2175 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2176 upper_32_bits(addr));
2177 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2178 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2179 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2180 offset += sg_len;
2181 }
2182 psgl--;
2183 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2184}
2185
2186static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302187hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2188 unsigned int num_sg, struct beiscsi_io_task *io_task)
2189{
2190 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302191 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302192 unsigned int sge_len = 0;
2193 unsigned long long addr;
2194 struct scatterlist *l_sg;
2195 unsigned int offset;
2196
2197 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2198 io_task->bhs_pa.u.a32.address_lo);
2199 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2200 io_task->bhs_pa.u.a32.address_hi);
2201
2202 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302203 for (index = 0; (index < num_sg) && (index < 2); index++,
2204 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302205 if (index == 0) {
2206 sg_len = sg_dma_len(sg);
2207 addr = (u64) sg_dma_address(sg);
2208 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302209 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302210 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302211 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302212 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2213 sg_len);
2214 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302215 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302216 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2217 pwrb, sge_len);
2218 sg_len = sg_dma_len(sg);
2219 addr = (u64) sg_dma_address(sg);
2220 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302221 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302222 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302223 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302224 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2225 sg_len);
2226 }
2227 }
2228 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2229 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2230
2231 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2232
2233 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2234 io_task->bhs_pa.u.a32.address_hi);
2235 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2236 io_task->bhs_pa.u.a32.address_lo);
2237
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302238 if (num_sg == 1) {
2239 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2240 1);
2241 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2242 0);
2243 } else if (num_sg == 2) {
2244 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2245 0);
2246 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2247 1);
2248 } else {
2249 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2250 0);
2251 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2252 0);
2253 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302254 sg = l_sg;
2255 psgl++;
2256 psgl++;
2257 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302258 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302259 sg_len = sg_dma_len(sg);
2260 addr = (u64) sg_dma_address(sg);
2261 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2262 (addr & 0xFFFFFFFF));
2263 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2264 (addr >> 32));
2265 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2266 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2267 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2268 offset += sg_len;
2269 }
2270 psgl--;
2271 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2272}
2273
John Soni Josed629c472012-10-20 04:42:00 +05302274/**
2275 * hwi_write_buffer()- Populate the WRB with task info
2276 * @pwrb: ptr to the WRB entry
2277 * @task: iscsi task which is to be executed
2278 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002279static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302280{
2281 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302282 struct beiscsi_io_task *io_task = task->dd_data;
2283 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2284 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302285 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302286
2287 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2288 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2289 io_task->bhs_pa.u.a32.address_lo);
2290 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2291 io_task->bhs_pa.u.a32.address_hi);
2292
2293 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302294
2295 /* Check for the data_count */
2296 dsp_value = (task->data_count) ? 1 : 0;
2297
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002298 if (is_chip_be2_be3r(phba))
2299 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302300 pwrb, dsp_value);
2301 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002302 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302303 pwrb, dsp_value);
2304
2305 /* Map addr only if there is data_count */
2306 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302307 io_task->mtask_addr = pci_map_single(phba->pcidev,
2308 task->data,
2309 task->data_count,
2310 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002311 if (pci_dma_mapping_error(phba->pcidev,
2312 io_task->mtask_addr))
2313 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302314 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302315 } else
John Soni Josed629c472012-10-20 04:42:00 +05302316 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302317
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302318 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302319 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302320 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302321 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302322 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2323 task->data_count);
2324
2325 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2326 } else {
2327 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302328 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302329 }
2330
2331 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2332
2333 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2334
2335 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2336 io_task->bhs_pa.u.a32.address_hi);
2337 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2338 io_task->bhs_pa.u.a32.address_lo);
2339 if (task->data) {
2340 psgl++;
2341 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2342 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2343 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2344 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2345 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2346 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2347
2348 psgl++;
2349 if (task->data) {
2350 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302351 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302352 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302353 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302354 }
2355 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2356 }
2357 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002358 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302359}
2360
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002361/**
2362 * beiscsi_find_mem_req()- Find mem needed
2363 * @phba: ptr to HBA struct
2364 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302365static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2366{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002367 uint8_t mem_descr_index, ulp_num;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302368 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302369 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2370 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2371
2372 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2373 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302374
2375 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2376
2377 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2378 BE_ISCSI_PDU_HEADER_SIZE;
2379 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2380 sizeof(struct hwi_context_memory);
2381
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302382
2383 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2384 * (phba->params.wrbs_per_cxn)
2385 * phba->params.cxns_per_ctrl;
2386 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2387 (phba->params.wrbs_per_cxn);
2388 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2389 phba->params.cxns_per_ctrl);
2390
2391 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2392 phba->params.icds_per_ctrl;
2393 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2394 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002395 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2396 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302397
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002398 num_async_pdu_buf_sgl_pages =
2399 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2400 phba, ulp_num) *
2401 sizeof(struct phys_addr));
2402
2403 num_async_pdu_buf_pages =
2404 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2405 phba, ulp_num) *
2406 phba->params.defpdu_hdr_sz);
2407
2408 num_async_pdu_data_pages =
2409 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2410 phba, ulp_num) *
2411 phba->params.defpdu_data_sz);
2412
2413 num_async_pdu_data_sgl_pages =
2414 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2415 phba, ulp_num) *
2416 sizeof(struct phys_addr));
2417
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002418 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2419 (ulp_num * MEM_DESCR_OFFSET));
2420 phba->mem_req[mem_descr_index] =
2421 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2422 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2423
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002424 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2425 (ulp_num * MEM_DESCR_OFFSET));
2426 phba->mem_req[mem_descr_index] =
2427 num_async_pdu_buf_pages *
2428 PAGE_SIZE;
2429
2430 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2431 (ulp_num * MEM_DESCR_OFFSET));
2432 phba->mem_req[mem_descr_index] =
2433 num_async_pdu_data_pages *
2434 PAGE_SIZE;
2435
2436 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2437 (ulp_num * MEM_DESCR_OFFSET));
2438 phba->mem_req[mem_descr_index] =
2439 num_async_pdu_buf_sgl_pages *
2440 PAGE_SIZE;
2441
2442 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2443 (ulp_num * MEM_DESCR_OFFSET));
2444 phba->mem_req[mem_descr_index] =
2445 num_async_pdu_data_sgl_pages *
2446 PAGE_SIZE;
2447
2448 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2449 (ulp_num * MEM_DESCR_OFFSET));
2450 phba->mem_req[mem_descr_index] =
2451 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302452 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002453
2454 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2455 (ulp_num * MEM_DESCR_OFFSET));
2456 phba->mem_req[mem_descr_index] =
2457 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302458 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002459
2460 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2461 (ulp_num * MEM_DESCR_OFFSET));
2462 phba->mem_req[mem_descr_index] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302463 sizeof(struct hd_async_context) +
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002464 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302465 sizeof(struct hd_async_entry));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002466 }
2467 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302468}
2469
2470static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2471{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302472 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002473 struct hwi_controller *phwi_ctrlr;
2474 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302475 struct mem_array *mem_arr, *mem_arr_orig;
2476 unsigned int i, j, alloc_size, curr_alloc_size;
2477
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002478 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302479 if (!phba->phwi_ctrlr)
2480 return -ENOMEM;
2481
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002482 /* Allocate memory for wrb_context */
2483 phwi_ctrlr = phba->phwi_ctrlr;
2484 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2485 phba->params.cxns_per_ctrl,
2486 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002487 if (!phwi_ctrlr->wrb_context) {
2488 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002489 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002490 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002491
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302492 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2493 GFP_KERNEL);
2494 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002495 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302496 kfree(phba->phwi_ctrlr);
2497 return -ENOMEM;
2498 }
2499
2500 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2501 GFP_KERNEL);
2502 if (!mem_arr_orig) {
2503 kfree(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_descr = phba->init_mem;
2510 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002511 if (!phba->mem_req[i]) {
2512 mem_descr->mem_array = NULL;
2513 mem_descr++;
2514 continue;
2515 }
2516
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302517 j = 0;
2518 mem_arr = mem_arr_orig;
2519 alloc_size = phba->mem_req[i];
2520 memset(mem_arr, 0, sizeof(struct mem_array) *
2521 BEISCSI_MAX_FRAGS_INIT);
2522 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2523 do {
2524 mem_arr->virtual_address = pci_alloc_consistent(
2525 phba->pcidev,
2526 curr_alloc_size,
2527 &bus_add);
2528 if (!mem_arr->virtual_address) {
2529 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2530 goto free_mem;
2531 if (curr_alloc_size -
2532 rounddown_pow_of_two(curr_alloc_size))
2533 curr_alloc_size = rounddown_pow_of_two
2534 (curr_alloc_size);
2535 else
2536 curr_alloc_size = curr_alloc_size / 2;
2537 } else {
2538 mem_arr->bus_address.u.
2539 a64.address = (__u64) bus_add;
2540 mem_arr->size = curr_alloc_size;
2541 alloc_size -= curr_alloc_size;
2542 curr_alloc_size = min(be_max_phys_size *
2543 1024, alloc_size);
2544 j++;
2545 mem_arr++;
2546 }
2547 } while (alloc_size);
2548 mem_descr->num_elements = j;
2549 mem_descr->size_in_bytes = phba->mem_req[i];
2550 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2551 GFP_KERNEL);
2552 if (!mem_descr->mem_array)
2553 goto free_mem;
2554
2555 memcpy(mem_descr->mem_array, mem_arr_orig,
2556 sizeof(struct mem_array) * j);
2557 mem_descr++;
2558 }
2559 kfree(mem_arr_orig);
2560 return 0;
2561free_mem:
2562 mem_descr->num_elements = j;
2563 while ((i) || (j)) {
2564 for (j = mem_descr->num_elements; j > 0; j--) {
2565 pci_free_consistent(phba->pcidev,
2566 mem_descr->mem_array[j - 1].size,
2567 mem_descr->mem_array[j - 1].
2568 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302569 (unsigned long)mem_descr->
2570 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302571 bus_address.u.a64.address);
2572 }
2573 if (i) {
2574 i--;
2575 kfree(mem_descr->mem_array);
2576 mem_descr--;
2577 }
2578 }
2579 kfree(mem_arr_orig);
2580 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002581 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302582 kfree(phba->phwi_ctrlr);
2583 return -ENOMEM;
2584}
2585
2586static int beiscsi_get_memory(struct beiscsi_hba *phba)
2587{
2588 beiscsi_find_mem_req(phba);
2589 return beiscsi_alloc_mem(phba);
2590}
2591
2592static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2593{
2594 struct pdu_data_out *pdata_out;
2595 struct pdu_nop_out *pnop_out;
2596 struct be_mem_descriptor *mem_descr;
2597
2598 mem_descr = phba->init_mem;
2599 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2600 pdata_out =
2601 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2602 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2603
2604 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2605 IIOC_SCSI_DATA);
2606
2607 pnop_out =
2608 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2609 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2610
2611 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2612 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2613 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2614 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2615}
2616
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002617static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302618{
2619 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002620 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002621 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302622 struct hwi_controller *phwi_ctrlr;
2623 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002624 struct iscsi_wrb *pwrb = NULL;
2625 unsigned int num_cxn_wrbh = 0;
2626 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302627
2628 mem_descr_wrbh = phba->init_mem;
2629 mem_descr_wrbh += HWI_MEM_WRBH;
2630
2631 mem_descr_wrb = phba->init_mem;
2632 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302633 phwi_ctrlr = phba->phwi_ctrlr;
2634
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002635 /* Allocate memory for WRBQ */
2636 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2637 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002638 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002639 GFP_KERNEL);
2640 if (!phwi_ctxt->be_wrbq) {
2641 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2642 "BM_%d : WRBQ Mem Alloc Failed\n");
2643 return -ENOMEM;
2644 }
2645
2646 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302647 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302648 pwrb_context->pwrb_handle_base =
2649 kzalloc(sizeof(struct wrb_handle *) *
2650 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002651 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302652 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2653 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002654 goto init_wrb_hndl_failed;
2655 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302656 pwrb_context->pwrb_handle_basestd =
2657 kzalloc(sizeof(struct wrb_handle *) *
2658 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002659 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302660 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2661 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002662 goto init_wrb_hndl_failed;
2663 }
2664 if (!num_cxn_wrbh) {
2665 pwrb_handle =
2666 mem_descr_wrbh->mem_array[idx].virtual_address;
2667 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2668 ((sizeof(struct wrb_handle)) *
2669 phba->params.wrbs_per_cxn));
2670 idx++;
2671 }
2672 pwrb_context->alloc_index = 0;
2673 pwrb_context->wrb_handles_available = 0;
2674 pwrb_context->free_index = 0;
2675
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302676 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302677 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2678 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2679 pwrb_context->pwrb_handle_basestd[j] =
2680 pwrb_handle;
2681 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302682 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302683 pwrb_handle++;
2684 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302685 num_cxn_wrbh--;
2686 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302687 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302688 }
2689 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002690 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302691 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002692 if (!num_cxn_wrb) {
2693 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2694 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2695 ((sizeof(struct iscsi_wrb) *
2696 phba->params.wrbs_per_cxn));
2697 idx++;
2698 }
2699
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302700 if (num_cxn_wrb) {
2701 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2702 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2703 pwrb_handle->pwrb = pwrb;
2704 pwrb++;
2705 }
2706 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302707 }
2708 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002709 return 0;
2710init_wrb_hndl_failed:
2711 for (j = index; j > 0; j--) {
2712 pwrb_context = &phwi_ctrlr->wrb_context[j];
2713 kfree(pwrb_context->pwrb_handle_base);
2714 kfree(pwrb_context->pwrb_handle_basestd);
2715 }
2716 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302717}
2718
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002719static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302720{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002721 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302722 struct hwi_controller *phwi_ctrlr;
2723 struct hba_parameters *p = &phba->params;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302724 struct hd_async_context *pasync_ctx;
2725 struct hd_async_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002726 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302727 struct be_mem_descriptor *mem_descr;
2728
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002729 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2730 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302731 /* get async_ctx for each ULP */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002732 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2733 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2734 (ulp_num * MEM_DESCR_OFFSET));
2735
2736 phwi_ctrlr = phba->phwi_ctrlr;
2737 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302738 (struct hd_async_context *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002739 mem_descr->mem_array[0].virtual_address;
2740
2741 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2742 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2743
2744 pasync_ctx->async_entry =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302745 (struct hd_async_entry *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002746 ((long unsigned int)pasync_ctx +
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302747 sizeof(struct hd_async_context));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002748
2749 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2750 ulp_num);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302751 /* setup header buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002752 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2753 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2754 (ulp_num * MEM_DESCR_OFFSET);
2755 if (mem_descr->mem_array[0].virtual_address) {
2756 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2757 "BM_%d : hwi_init_async_pdu_ctx"
2758 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2759 ulp_num,
2760 mem_descr->mem_array[0].
2761 virtual_address);
2762 } else
2763 beiscsi_log(phba, KERN_WARNING,
2764 BEISCSI_LOG_INIT,
2765 "BM_%d : No Virtual address for ULP : %d\n",
2766 ulp_num);
2767
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302768 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002769 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302770 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302771
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002772 pasync_ctx->async_header.pa_base.u.a64.address =
2773 mem_descr->mem_array[0].
2774 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002775
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302776 /* setup header buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002777 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2778 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2779 (ulp_num * MEM_DESCR_OFFSET);
2780 if (mem_descr->mem_array[0].virtual_address) {
2781 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2782 "BM_%d : hwi_init_async_pdu_ctx"
2783 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2784 ulp_num,
2785 mem_descr->mem_array[0].
2786 virtual_address);
2787 } else
2788 beiscsi_log(phba, KERN_WARNING,
2789 BEISCSI_LOG_INIT,
2790 "BM_%d : No Virtual address for ULP : %d\n",
2791 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302792
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002793 pasync_ctx->async_header.ring_base =
2794 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302795
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302796 /* setup header buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002797 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2798 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2799 (ulp_num * MEM_DESCR_OFFSET);
2800 if (mem_descr->mem_array[0].virtual_address) {
2801 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2802 "BM_%d : hwi_init_async_pdu_ctx"
2803 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2804 ulp_num,
2805 mem_descr->mem_array[0].
2806 virtual_address);
2807 } else
2808 beiscsi_log(phba, KERN_WARNING,
2809 BEISCSI_LOG_INIT,
2810 "BM_%d : No Virtual address for ULP : %d\n",
2811 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302812
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002813 pasync_ctx->async_header.handle_base =
2814 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002815 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302816
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302817 /* setup data buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002818 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2819 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2820 (ulp_num * MEM_DESCR_OFFSET);
2821 if (mem_descr->mem_array[0].virtual_address) {
2822 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2823 "BM_%d : hwi_init_async_pdu_ctx"
2824 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2825 ulp_num,
2826 mem_descr->mem_array[0].
2827 virtual_address);
2828 } else
2829 beiscsi_log(phba, KERN_WARNING,
2830 BEISCSI_LOG_INIT,
2831 "BM_%d : No Virtual address for ULP : %d\n",
2832 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302833
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002834 pasync_ctx->async_data.ring_base =
2835 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302836
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302837 /* setup data buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002838 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2839 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2840 (ulp_num * MEM_DESCR_OFFSET);
2841 if (!mem_descr->mem_array[0].virtual_address)
2842 beiscsi_log(phba, KERN_WARNING,
2843 BEISCSI_LOG_INIT,
2844 "BM_%d : No Virtual address for ULP : %d\n",
2845 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302846
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002847 pasync_ctx->async_data.handle_base =
2848 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002849 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302850
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002851 pasync_header_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302852 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002853 pasync_ctx->async_header.handle_base;
2854 pasync_data_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302855 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002856 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302857
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302858 /* setup data buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002859 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2860 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2861 (ulp_num * MEM_DESCR_OFFSET);
2862 if (mem_descr->mem_array[0].virtual_address) {
2863 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2864 "BM_%d : hwi_init_async_pdu_ctx"
2865 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2866 ulp_num,
2867 mem_descr->mem_array[0].
2868 virtual_address);
2869 } else
2870 beiscsi_log(phba, KERN_WARNING,
2871 BEISCSI_LOG_INIT,
2872 "BM_%d : No Virtual address for ULP : %d\n",
2873 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302874
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002875 idx = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302876 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002877 pasync_ctx->async_data.va_base =
2878 mem_descr->mem_array[idx].virtual_address;
2879 pasync_ctx->async_data.pa_base.u.a64.address =
2880 mem_descr->mem_array[idx].
2881 bus_address.u.a64.address;
2882
2883 num_async_data = ((mem_descr->mem_array[idx].size) /
2884 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002885 num_per_mem = 0;
2886
2887 for (index = 0; index < BEISCSI_GET_CID_COUNT
2888 (phba, ulp_num); index++) {
2889 pasync_header_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302890 pasync_header_h->is_header = 1;
2891 pasync_header_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002892 INIT_LIST_HEAD(&pasync_header_h->link);
2893 pasync_header_h->pbuffer =
2894 (void *)((unsigned long)
2895 (pasync_ctx->
2896 async_header.va_base) +
2897 (p->defpdu_hdr_sz * index));
2898
2899 pasync_header_h->pa.u.a64.address =
2900 pasync_ctx->async_header.pa_base.u.a64.
2901 address + (p->defpdu_hdr_sz * index);
2902
2903 list_add_tail(&pasync_header_h->link,
2904 &pasync_ctx->async_header.
2905 free_list);
2906 pasync_header_h++;
2907 pasync_ctx->async_header.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302908 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2909 wq.list);
2910 pasync_ctx->async_entry[index].header = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002911
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002912 pasync_data_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302913 pasync_data_h->is_header = 0;
2914 pasync_data_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002915 INIT_LIST_HEAD(&pasync_data_h->link);
2916
2917 if (!num_async_data) {
2918 num_per_mem = 0;
2919 idx++;
2920 pasync_ctx->async_data.va_base =
2921 mem_descr->mem_array[idx].
2922 virtual_address;
2923 pasync_ctx->async_data.pa_base.u.
2924 a64.address =
2925 mem_descr->mem_array[idx].
2926 bus_address.u.a64.address;
2927 num_async_data =
2928 ((mem_descr->mem_array[idx].
2929 size) /
2930 phba->params.defpdu_data_sz);
2931 }
2932 pasync_data_h->pbuffer =
2933 (void *)((unsigned long)
2934 (pasync_ctx->async_data.va_base) +
2935 (p->defpdu_data_sz * num_per_mem));
2936
2937 pasync_data_h->pa.u.a64.address =
2938 pasync_ctx->async_data.pa_base.u.a64.
2939 address + (p->defpdu_data_sz *
2940 num_per_mem);
2941 num_per_mem++;
2942 num_async_data--;
2943
2944 list_add_tail(&pasync_data_h->link,
2945 &pasync_ctx->async_data.
2946 free_list);
2947 pasync_data_h++;
2948 pasync_ctx->async_data.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302949 pasync_ctx->async_entry[index].data = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002950 }
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002951 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302952 }
2953
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002954 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302955}
2956
2957static int
2958be_sgl_create_contiguous(void *virtual_address,
2959 u64 physical_address, u32 length,
2960 struct be_dma_mem *sgl)
2961{
2962 WARN_ON(!virtual_address);
2963 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06002964 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302965 WARN_ON(!sgl);
2966
2967 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302968 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302969 sgl->size = length;
2970
2971 return 0;
2972}
2973
2974static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2975{
2976 memset(sgl, 0, sizeof(*sgl));
2977}
2978
2979static void
2980hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2981 struct mem_array *pmem, struct be_dma_mem *sgl)
2982{
2983 if (sgl->va)
2984 be_sgl_destroy_contiguous(sgl);
2985
2986 be_sgl_create_contiguous(pmem->virtual_address,
2987 pmem->bus_address.u.a64.address,
2988 pmem->size, sgl);
2989}
2990
2991static void
2992hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2993 struct mem_array *pmem, struct be_dma_mem *sgl)
2994{
2995 if (sgl->va)
2996 be_sgl_destroy_contiguous(sgl);
2997
2998 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2999 pmem->bus_address.u.a64.address,
3000 pmem->size, sgl);
3001}
3002
3003static int be_fill_queue(struct be_queue_info *q,
3004 u16 len, u16 entry_size, void *vaddress)
3005{
3006 struct be_dma_mem *mem = &q->dma_mem;
3007
3008 memset(q, 0, sizeof(*q));
3009 q->len = len;
3010 q->entry_size = entry_size;
3011 mem->size = len * entry_size;
3012 mem->va = vaddress;
3013 if (!mem->va)
3014 return -ENOMEM;
3015 memset(mem->va, 0, mem->size);
3016 return 0;
3017}
3018
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303019static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303020 struct hwi_context_memory *phwi_context)
3021{
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303022 int ret = -ENOMEM, eq_for_mcc;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303023 unsigned int i, num_eq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303024 struct be_queue_info *eq;
3025 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303026 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303027 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303028
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303029 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3030 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303031
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303032 if (phba->msix_enabled)
3033 eq_for_mcc = 1;
3034 else
3035 eq_for_mcc = 0;
3036 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3037 eq = &phwi_context->be_eq[i].q;
3038 mem = &eq->dma_mem;
3039 phwi_context->be_eq[i].phba = phba;
3040 eq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303041 num_eq_pages * PAGE_SIZE,
3042 &paddr);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303043 if (!eq_vaddress)
3044 goto create_eq_error;
3045
3046 mem->va = eq_vaddress;
3047 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3048 sizeof(struct be_eq_entry), eq_vaddress);
3049 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303050 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3051 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303052 goto create_eq_error;
3053 }
3054
3055 mem->dma = paddr;
3056 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3057 phwi_context->cur_eqd);
3058 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303059 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3060 "BM_%d : beiscsi_cmd_eq_create"
3061 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303062 goto create_eq_error;
3063 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303064
3065 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3066 "BM_%d : eqid = %d\n",
3067 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303068 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303069 return 0;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303070
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303071create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303072 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303073 eq = &phwi_context->be_eq[i].q;
3074 mem = &eq->dma_mem;
3075 if (mem->va)
3076 pci_free_consistent(phba->pcidev, num_eq_pages
3077 * PAGE_SIZE,
3078 mem->va, mem->dma);
3079 }
3080 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303081}
3082
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303083static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303084 struct hwi_context_memory *phwi_context)
3085{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303086 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303087 struct be_queue_info *cq, *eq;
3088 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303089 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303090 void *cq_vaddress;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303091 int ret = -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303092 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303093
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303094 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3095 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303096
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303097 for (i = 0; i < phba->num_cpus; i++) {
3098 cq = &phwi_context->be_cq[i];
3099 eq = &phwi_context->be_eq[i].q;
3100 pbe_eq = &phwi_context->be_eq[i];
3101 pbe_eq->cq = cq;
3102 pbe_eq->phba = phba;
3103 mem = &cq->dma_mem;
3104 cq_vaddress = pci_alloc_consistent(phba->pcidev,
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303105 num_cq_pages * PAGE_SIZE,
3106 &paddr);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303107 if (!cq_vaddress)
3108 goto create_cq_error;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05303109
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303110 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303111 sizeof(struct sol_cqe), cq_vaddress);
3112 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303113 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3114 "BM_%d : be_fill_queue Failed "
3115 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303116 goto create_cq_error;
3117 }
3118
3119 mem->dma = paddr;
3120 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3121 false, 0);
3122 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303123 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3124 "BM_%d : beiscsi_cmd_eq_create"
3125 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303126 goto create_cq_error;
3127 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303128 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3129 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3130 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303131 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303132 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303133
3134create_cq_error:
3135 for (i = 0; i < phba->num_cpus; i++) {
3136 cq = &phwi_context->be_cq[i];
3137 mem = &cq->dma_mem;
3138 if (mem->va)
3139 pci_free_consistent(phba->pcidev, num_cq_pages
3140 * PAGE_SIZE,
3141 mem->va, mem->dma);
3142 }
3143 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303144}
3145
3146static int
3147beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3148 struct hwi_context_memory *phwi_context,
3149 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003150 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303151{
3152 unsigned int idx;
3153 int ret;
3154 struct be_queue_info *dq, *cq;
3155 struct be_dma_mem *mem;
3156 struct be_mem_descriptor *mem_descr;
3157 void *dq_vaddress;
3158
3159 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003160 dq = &phwi_context->be_def_hdrq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303161 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303162 mem = &dq->dma_mem;
3163 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003164 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3165 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303166 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3167 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3168 sizeof(struct phys_addr),
3169 sizeof(struct phys_addr), dq_vaddress);
3170 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303171 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003172 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3173 ulp_num);
3174
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303175 return ret;
3176 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303177 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3178 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303179 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3180 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003181 phba->params.defpdu_hdr_sz,
3182 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303183 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303184 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003185 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3186 ulp_num);
3187
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303188 return ret;
3189 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303190
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003191 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3192 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3193 ulp_num,
3194 phwi_context->be_def_hdrq[ulp_num].id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303195 return 0;
3196}
3197
3198static int
3199beiscsi_create_def_data(struct beiscsi_hba *phba,
3200 struct hwi_context_memory *phwi_context,
3201 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003202 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303203{
3204 unsigned int idx;
3205 int ret;
3206 struct be_queue_info *dataq, *cq;
3207 struct be_dma_mem *mem;
3208 struct be_mem_descriptor *mem_descr;
3209 void *dq_vaddress;
3210
3211 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003212 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303213 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303214 mem = &dataq->dma_mem;
3215 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003216 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3217 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303218 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3219 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3220 sizeof(struct phys_addr),
3221 sizeof(struct phys_addr), dq_vaddress);
3222 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303223 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003224 "BM_%d : be_fill_queue Failed for DEF PDU "
3225 "DATA on ULP : %d\n",
3226 ulp_num);
3227
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303228 return ret;
3229 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303230 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3231 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303232 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3233 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003234 phba->params.defpdu_data_sz,
3235 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303236 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303237 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3238 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003239 " Failed for DEF PDU DATA on ULP : %d\n",
3240 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303241 return ret;
3242 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303243
John Soni Jose99bc5d52012-08-20 23:00:18 +05303244 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003245 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3246 ulp_num,
3247 phwi_context->be_def_dataq[ulp_num].id);
3248
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003249 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3250 "BM_%d : DEFAULT PDU DATA RING CREATED"
3251 "on ULP : %d\n", ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303252 return 0;
3253}
3254
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003255
3256static int
3257beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3258{
3259 struct be_mem_descriptor *mem_descr;
3260 struct mem_array *pm_arr;
3261 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003262 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003263
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003264 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3265 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3266 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3267 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3268 (ulp_num * MEM_DESCR_OFFSET);
3269 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003270
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003271 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3272 status = be_cmd_iscsi_post_template_hdr(
3273 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003274
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003275 if (status != 0) {
3276 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3277 "BM_%d : Post Template HDR Failed for"
3278 "ULP_%d\n", ulp_num);
3279 return status;
3280 }
3281
3282 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3283 "BM_%d : Template HDR Pages Posted for"
3284 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003285 }
3286 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003287 return 0;
3288}
3289
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303290static int
3291beiscsi_post_pages(struct beiscsi_hba *phba)
3292{
3293 struct be_mem_descriptor *mem_descr;
3294 struct mem_array *pm_arr;
3295 unsigned int page_offset, i;
3296 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003297 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303298
3299 mem_descr = phba->init_mem;
3300 mem_descr += HWI_MEM_SGE;
3301 pm_arr = mem_descr->mem_array;
3302
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003303 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3304 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3305 break;
3306
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303307 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003308 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303309 for (i = 0; i < mem_descr->num_elements; i++) {
3310 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3311 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3312 page_offset,
3313 (pm_arr->size / PAGE_SIZE));
3314 page_offset += pm_arr->size / PAGE_SIZE;
3315 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303316 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3317 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303318 return status;
3319 }
3320 pm_arr++;
3321 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303322 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3323 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303324 return 0;
3325}
3326
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303327static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3328{
3329 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003330 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303331 pci_free_consistent(phba->pcidev, mem->size,
3332 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003333 mem->va = NULL;
3334 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303335}
3336
3337static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3338 u16 len, u16 entry_size)
3339{
3340 struct be_dma_mem *mem = &q->dma_mem;
3341
3342 memset(q, 0, sizeof(*q));
3343 q->len = len;
3344 q->entry_size = entry_size;
3345 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003346 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303347 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303348 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303349 return 0;
3350}
3351
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303352static int
3353beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3354 struct hwi_context_memory *phwi_context,
3355 struct hwi_controller *phwi_ctrlr)
3356{
3357 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3358 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003359 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303360 struct mem_array *pwrb_arr;
3361 void *wrb_vaddr;
3362 struct be_dma_mem sgl;
3363 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003364 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303365 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003366 uint8_t ulp_count = 0, ulp_base_num = 0;
3367 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303368
3369 idx = 0;
3370 mem_descr = phba->init_mem;
3371 mem_descr += HWI_MEM_WRB;
3372 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3373 GFP_KERNEL);
3374 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303375 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3376 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303377 return -ENOMEM;
3378 }
3379 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3380 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3381 num_wrb_rings = mem_descr->mem_array[idx].size /
3382 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3383
3384 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3385 if (num_wrb_rings) {
3386 pwrb_arr[num].virtual_address = wrb_vaddr;
3387 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3388 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3389 sizeof(struct iscsi_wrb);
3390 wrb_vaddr += pwrb_arr[num].size;
3391 pa_addr_lo += pwrb_arr[num].size;
3392 num_wrb_rings--;
3393 } else {
3394 idx++;
3395 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3396 pa_addr_lo = mem_descr->mem_array[idx].\
3397 bus_address.u.a64.address;
3398 num_wrb_rings = mem_descr->mem_array[idx].size /
3399 (phba->params.wrbs_per_cxn *
3400 sizeof(struct iscsi_wrb));
3401 pwrb_arr[num].virtual_address = wrb_vaddr;
3402 pwrb_arr[num].bus_address.u.a64.address\
3403 = pa_addr_lo;
3404 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3405 sizeof(struct iscsi_wrb);
3406 wrb_vaddr += pwrb_arr[num].size;
3407 pa_addr_lo += pwrb_arr[num].size;
3408 num_wrb_rings--;
3409 }
3410 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003411
3412 /* Get the ULP Count */
3413 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3414 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3415 ulp_count++;
3416 ulp_base_num = ulp_num;
3417 cid_count_ulp[ulp_num] =
3418 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3419 }
3420
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303421 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3422 wrb_mem_index = 0;
3423 offset = 0;
3424 size = 0;
3425
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003426 if (ulp_count > 1) {
3427 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3428
3429 if (!cid_count_ulp[ulp_base_num])
3430 ulp_base_num = (ulp_base_num + 1) %
3431 BEISCSI_ULP_COUNT;
3432
3433 cid_count_ulp[ulp_base_num]--;
3434 }
3435
3436
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303437 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3438 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003439 &phwi_context->be_wrbq[i],
3440 &phwi_ctrlr->wrb_context[i],
3441 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303442 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303443 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3444 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003445 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303446 return status;
3447 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003448 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003449 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303450 }
3451 kfree(pwrb_arr);
3452 return 0;
3453}
3454
3455static void free_wrb_handles(struct beiscsi_hba *phba)
3456{
3457 unsigned int index;
3458 struct hwi_controller *phwi_ctrlr;
3459 struct hwi_wrb_context *pwrb_context;
3460
3461 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003462 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303463 pwrb_context = &phwi_ctrlr->wrb_context[index];
3464 kfree(pwrb_context->pwrb_handle_base);
3465 kfree(pwrb_context->pwrb_handle_basestd);
3466 }
3467}
3468
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303469static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3470{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303471 struct be_ctrl_info *ctrl = &phba->ctrl;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303472 struct be_dma_mem *ptag_mem;
3473 struct be_queue_info *q;
3474 int i, tag;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303475
3476 q = &phba->ctrl.mcc_obj.q;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303477 for (i = 0; i < MAX_MCC_CMD; i++) {
3478 tag = i + 1;
3479 if (!test_bit(MCC_TAG_STATE_RUNNING,
3480 &ctrl->ptag_state[tag].tag_state))
3481 continue;
3482
3483 if (test_bit(MCC_TAG_STATE_TIMEOUT,
3484 &ctrl->ptag_state[tag].tag_state)) {
3485 ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
3486 if (ptag_mem->size) {
3487 pci_free_consistent(ctrl->pdev,
3488 ptag_mem->size,
3489 ptag_mem->va,
3490 ptag_mem->dma);
3491 ptag_mem->size = 0;
3492 }
3493 continue;
3494 }
3495 /**
3496 * If MCC is still active and waiting then wake up the process.
3497 * We are here only because port is going offline. The process
3498 * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
3499 * returned for the operation and allocated memory cleaned up.
3500 */
3501 if (waitqueue_active(&ctrl->mcc_wait[tag])) {
3502 ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
3503 ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
3504 wake_up_interruptible(&ctrl->mcc_wait[tag]);
3505 /*
3506 * Control tag info gets reinitialized in enable
3507 * so wait for the process to clear running state.
3508 */
3509 while (test_bit(MCC_TAG_STATE_RUNNING,
3510 &ctrl->ptag_state[tag].tag_state))
3511 schedule_timeout_uninterruptible(HZ);
3512 }
3513 /**
3514 * For MCC with tag_states MCC_TAG_STATE_ASYNC and
3515 * MCC_TAG_STATE_IGNORE nothing needs to done.
3516 */
3517 }
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303518 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303519 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303520 be_queue_free(phba, q);
3521 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303522
3523 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303524 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303525 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303526 be_queue_free(phba, q);
3527 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303528}
3529
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303530static int be_mcc_queues_create(struct beiscsi_hba *phba,
3531 struct hwi_context_memory *phwi_context)
3532{
3533 struct be_queue_info *q, *cq;
3534 struct be_ctrl_info *ctrl = &phba->ctrl;
3535
3536 /* Alloc MCC compl queue */
3537 cq = &phba->ctrl.mcc_obj.cq;
3538 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3539 sizeof(struct be_mcc_compl)))
3540 goto err;
3541 /* Ask BE to create MCC compl queue; */
3542 if (phba->msix_enabled) {
3543 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3544 [phba->num_cpus].q, false, true, 0))
3545 goto mcc_cq_free;
3546 } else {
3547 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3548 false, true, 0))
3549 goto mcc_cq_free;
3550 }
3551
3552 /* Alloc MCC queue */
3553 q = &phba->ctrl.mcc_obj.q;
3554 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3555 goto mcc_cq_destroy;
3556
3557 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303558 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303559 goto mcc_q_free;
3560
3561 return 0;
3562
3563mcc_q_free:
3564 be_queue_free(phba, q);
3565mcc_cq_destroy:
3566 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3567mcc_cq_free:
3568 be_queue_free(phba, cq);
3569err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303570 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303571}
3572
John Soni Jose107dfcb2012-10-20 04:42:13 +05303573/**
3574 * find_num_cpus()- Get the CPU online count
3575 * @phba: ptr to priv structure
3576 *
3577 * CPU count is used for creating EQ.
3578 **/
3579static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303580{
3581 int num_cpus = 0;
3582
3583 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303584
John Soni Jose22abeef2012-10-20 04:43:32 +05303585 switch (phba->generation) {
3586 case BE_GEN2:
3587 case BE_GEN3:
3588 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3589 BEISCSI_MAX_NUM_CPUS : num_cpus;
3590 break;
3591 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003592 /*
3593 * If eqid_count == 1 fall back to
3594 * INTX mechanism
3595 **/
3596 if (phba->fw_config.eqid_count == 1) {
3597 enable_msix = 0;
3598 phba->num_cpus = 1;
3599 return;
3600 }
3601
3602 phba->num_cpus =
3603 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3604 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303605 break;
3606 default:
3607 phba->num_cpus = 1;
3608 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303609}
3610
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303611static void hwi_purge_eq(struct beiscsi_hba *phba)
3612{
3613 struct hwi_controller *phwi_ctrlr;
3614 struct hwi_context_memory *phwi_context;
3615 struct be_queue_info *eq;
3616 struct be_eq_entry *eqe = NULL;
3617 int i, eq_msix;
3618 unsigned int num_processed;
3619
3620 if (beiscsi_hba_in_error(phba))
3621 return;
3622
3623 phwi_ctrlr = phba->phwi_ctrlr;
3624 phwi_context = phwi_ctrlr->phwi_ctxt;
3625 if (phba->msix_enabled)
3626 eq_msix = 1;
3627 else
3628 eq_msix = 0;
3629
3630 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3631 eq = &phwi_context->be_eq[i].q;
3632 eqe = queue_tail_node(eq);
3633 num_processed = 0;
3634 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3635 & EQE_VALID_MASK) {
3636 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3637 queue_tail_inc(eq);
3638 eqe = queue_tail_node(eq);
3639 num_processed++;
3640 }
3641
3642 if (num_processed)
3643 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
3644 }
3645}
3646
3647static void hwi_cleanup_port(struct beiscsi_hba *phba)
3648{
3649 struct be_queue_info *q;
3650 struct be_ctrl_info *ctrl = &phba->ctrl;
3651 struct hwi_controller *phwi_ctrlr;
3652 struct hwi_context_memory *phwi_context;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303653 struct hd_async_context *pasync_ctx;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303654 int i, eq_for_mcc, ulp_num;
3655
3656 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3657 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3658 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
3659
3660 /**
3661 * Purge all EQ entries that may have been left out. This is to
3662 * workaround a problem we've seen occasionally where driver gets an
3663 * interrupt with EQ entry bit set after stopping the controller.
3664 */
3665 hwi_purge_eq(phba);
3666
3667 phwi_ctrlr = phba->phwi_ctrlr;
3668 phwi_context = phwi_ctrlr->phwi_ctxt;
3669
3670 be_cmd_iscsi_remove_template_hdr(ctrl);
3671
3672 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3673 q = &phwi_context->be_wrbq[i];
3674 if (q->created)
3675 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3676 }
3677 kfree(phwi_context->be_wrbq);
3678 free_wrb_handles(phba);
3679
3680 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3681 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3682
3683 q = &phwi_context->be_def_hdrq[ulp_num];
3684 if (q->created)
3685 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3686
3687 q = &phwi_context->be_def_dataq[ulp_num];
3688 if (q->created)
3689 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3690
3691 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3692 }
3693 }
3694
3695 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3696
3697 for (i = 0; i < (phba->num_cpus); i++) {
3698 q = &phwi_context->be_cq[i];
3699 if (q->created) {
3700 be_queue_free(phba, q);
3701 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3702 }
3703 }
3704
3705 be_mcc_queues_destroy(phba);
3706 if (phba->msix_enabled)
3707 eq_for_mcc = 1;
3708 else
3709 eq_for_mcc = 0;
3710 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3711 q = &phwi_context->be_eq[i].q;
3712 if (q->created) {
3713 be_queue_free(phba, q);
3714 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3715 }
3716 }
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303717 /* this ensures complete FW cleanup */
3718 beiscsi_cmd_function_reset(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303719 /* last communication, indicate driver is unloading */
3720 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
3721}
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303722
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303723static int hwi_init_port(struct beiscsi_hba *phba)
3724{
3725 struct hwi_controller *phwi_ctrlr;
3726 struct hwi_context_memory *phwi_context;
3727 unsigned int def_pdu_ring_sz;
3728 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003729 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303730
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303731 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303732 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003733 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303734 phwi_context->min_eqd = 0;
Jitendra Bhivare1b7a7dd2016-08-19 15:20:23 +05303735 phwi_context->cur_eqd = 32;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303736 /* set port optic state to unknown */
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303737 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303738
3739 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303740 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303741 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3742 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303743 goto error;
3744 }
3745
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303746 status = be_mcc_queues_create(phba, phwi_context);
3747 if (status != 0)
3748 goto error;
3749
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303750 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303751 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303752 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3753 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303754 goto error;
3755 }
3756
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303757 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303758 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303759 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3760 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303761 goto error;
3762 }
3763
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003764 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3765 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003766 def_pdu_ring_sz =
3767 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3768 sizeof(struct phys_addr);
3769
3770 status = beiscsi_create_def_hdr(phba, phwi_context,
3771 phwi_ctrlr,
3772 def_pdu_ring_sz,
3773 ulp_num);
3774 if (status != 0) {
3775 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3776 "BM_%d : Default Header not created for ULP : %d\n",
3777 ulp_num);
3778 goto error;
3779 }
3780
3781 status = beiscsi_create_def_data(phba, phwi_context,
3782 phwi_ctrlr,
3783 def_pdu_ring_sz,
3784 ulp_num);
3785 if (status != 0) {
3786 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3787 "BM_%d : Default Data not created for ULP : %d\n",
3788 ulp_num);
3789 goto error;
3790 }
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303791 /**
3792 * Now that the default PDU rings have been created,
3793 * let EP know about it.
3794 * Call beiscsi_cmd_iscsi_cleanup before posting?
3795 */
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303796 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3797 ulp_num);
3798 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3799 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003800 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303801 }
3802
3803 status = beiscsi_post_pages(phba);
3804 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303805 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3806 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303807 goto error;
3808 }
3809
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003810 status = beiscsi_post_template_hdr(phba);
3811 if (status != 0) {
3812 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3813 "BM_%d : Template HDR Posting for CXN Failed\n");
3814 }
3815
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303816 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3817 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303818 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3819 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303820 goto error;
3821 }
3822
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003823 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3824 uint16_t async_arr_idx = 0;
3825
3826 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3827 uint16_t cri = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303828 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003829
3830 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3831 phwi_ctrlr, ulp_num);
3832 for (cri = 0; cri <
3833 phba->params.cxns_per_ctrl; cri++) {
3834 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3835 (phwi_ctrlr, cri))
3836 pasync_ctx->cid_to_async_cri_map[
3837 phwi_ctrlr->wrb_context[cri].cid] =
3838 async_arr_idx++;
3839 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303840 /**
3841 * Now that the default PDU rings have been created,
3842 * let EP know about it.
3843 */
3844 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3845 ulp_num);
3846 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3847 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003848 }
3849 }
3850
John Soni Jose99bc5d52012-08-20 23:00:18 +05303851 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3852 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303853 return 0;
3854
3855error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303856 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3857 "BM_%d : hwi_init_port failed");
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303858 hwi_cleanup_port(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003859 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303860}
3861
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303862static int hwi_init_controller(struct beiscsi_hba *phba)
3863{
3864 struct hwi_controller *phwi_ctrlr;
3865
3866 phwi_ctrlr = phba->phwi_ctrlr;
3867 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3868 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3869 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303870 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3871 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3872 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303873 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303874 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3875 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3876 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303877 return -ENOMEM;
3878 }
3879
3880 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003881 if (beiscsi_init_wrb_handle(phba))
3882 return -ENOMEM;
3883
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003884 if (hwi_init_async_pdu_ctx(phba)) {
3885 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3886 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3887 return -ENOMEM;
3888 }
3889
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303890 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303891 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3892 "BM_%d : hwi_init_controller failed\n");
3893
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303894 return -ENOMEM;
3895 }
3896 return 0;
3897}
3898
3899static void beiscsi_free_mem(struct beiscsi_hba *phba)
3900{
3901 struct be_mem_descriptor *mem_descr;
3902 int i, j;
3903
3904 mem_descr = phba->init_mem;
3905 i = 0;
3906 j = 0;
3907 for (i = 0; i < SE_MEM_MAX; i++) {
3908 for (j = mem_descr->num_elements; j > 0; j--) {
3909 pci_free_consistent(phba->pcidev,
3910 mem_descr->mem_array[j - 1].size,
3911 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303912 (unsigned long)mem_descr->mem_array[j - 1].
3913 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303914 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003915
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303916 kfree(mem_descr->mem_array);
3917 mem_descr++;
3918 }
3919 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003920 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303921 kfree(phba->phwi_ctrlr);
3922}
3923
3924static int beiscsi_init_controller(struct beiscsi_hba *phba)
3925{
3926 int ret = -ENOMEM;
3927
3928 ret = beiscsi_get_memory(phba);
3929 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303930 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3931 "BM_%d : beiscsi_dev_probe -"
3932 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303933 return ret;
3934 }
3935
3936 ret = hwi_init_controller(phba);
3937 if (ret)
3938 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303939 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3940 "BM_%d : Return success from beiscsi_init_controller");
3941
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303942 return 0;
3943
3944free_init:
3945 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003946 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303947}
3948
3949static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3950{
3951 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3952 struct sgl_handle *psgl_handle;
3953 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003954 unsigned int arr_index, i, idx;
3955 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303956
3957 phba->io_sgl_hndl_avbl = 0;
3958 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303959
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303960 mem_descr_sglh = phba->init_mem;
3961 mem_descr_sglh += HWI_MEM_SGLH;
3962 if (1 == mem_descr_sglh->num_elements) {
3963 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3964 phba->params.ios_per_ctrl,
3965 GFP_KERNEL);
3966 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303967 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3968 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303969 return -ENOMEM;
3970 }
3971 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3972 (phba->params.icds_per_ctrl -
3973 phba->params.ios_per_ctrl),
3974 GFP_KERNEL);
3975 if (!phba->eh_sgl_hndl_base) {
3976 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303977 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3978 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303979 return -ENOMEM;
3980 }
3981 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303982 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3983 "BM_%d : HWI_MEM_SGLH is more than one element."
3984 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303985 return -ENOMEM;
3986 }
3987
3988 arr_index = 0;
3989 idx = 0;
3990 while (idx < mem_descr_sglh->num_elements) {
3991 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3992
3993 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3994 sizeof(struct sgl_handle)); i++) {
3995 if (arr_index < phba->params.ios_per_ctrl) {
3996 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3997 phba->io_sgl_hndl_avbl++;
3998 arr_index++;
3999 } else {
4000 phba->eh_sgl_hndl_base[arr_index -
4001 phba->params.ios_per_ctrl] =
4002 psgl_handle;
4003 arr_index++;
4004 phba->eh_sgl_hndl_avbl++;
4005 }
4006 psgl_handle++;
4007 }
4008 idx++;
4009 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304010 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4011 "BM_%d : phba->io_sgl_hndl_avbl=%d"
4012 "phba->eh_sgl_hndl_avbl=%d\n",
4013 phba->io_sgl_hndl_avbl,
4014 phba->eh_sgl_hndl_avbl);
4015
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304016 mem_descr_sg = phba->init_mem;
4017 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304018 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4019 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
4020 mem_descr_sg->num_elements);
4021
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004022 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4023 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
4024 break;
4025
4026 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
4027
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304028 arr_index = 0;
4029 idx = 0;
4030 while (idx < mem_descr_sg->num_elements) {
4031 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
4032
4033 for (i = 0;
4034 i < (mem_descr_sg->mem_array[idx].size) /
4035 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
4036 i++) {
4037 if (arr_index < phba->params.ios_per_ctrl)
4038 psgl_handle = phba->io_sgl_hndl_base[arr_index];
4039 else
4040 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
4041 phba->params.ios_per_ctrl];
4042 psgl_handle->pfrag = pfrag;
4043 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4044 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4045 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004046 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304047 }
4048 idx++;
4049 }
4050 phba->io_sgl_free_index = 0;
4051 phba->io_sgl_alloc_index = 0;
4052 phba->eh_sgl_free_index = 0;
4053 phba->eh_sgl_alloc_index = 0;
4054 return 0;
4055}
4056
4057static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4058{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004059 int ret;
4060 uint16_t i, ulp_num;
4061 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304062
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004063 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4064 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4065 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4066 GFP_KERNEL);
4067
4068 if (!ptr_cid_info) {
4069 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4070 "BM_%d : Failed to allocate memory"
4071 "for ULP_CID_INFO for ULP : %d\n",
4072 ulp_num);
4073 ret = -ENOMEM;
4074 goto free_memory;
4075
4076 }
4077
4078 /* Allocate memory for CID array */
4079 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4080 BEISCSI_GET_CID_COUNT(phba,
4081 ulp_num), GFP_KERNEL);
4082 if (!ptr_cid_info->cid_array) {
4083 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4084 "BM_%d : Failed to allocate memory"
4085 "for CID_ARRAY for ULP : %d\n",
4086 ulp_num);
4087 kfree(ptr_cid_info);
4088 ptr_cid_info = NULL;
4089 ret = -ENOMEM;
4090
4091 goto free_memory;
4092 }
4093 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4094 phba, ulp_num);
4095
4096 /* Save the cid_info_array ptr */
4097 phba->cid_array_info[ulp_num] = ptr_cid_info;
4098 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304099 }
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05304100 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004101 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304102 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304103 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4104 "BM_%d : Failed to allocate memory in "
4105 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004106 ret = -ENOMEM;
4107
4108 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304109 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004110
4111 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4112 phba->params.cxns_per_ctrl, GFP_KERNEL);
4113 if (!phba->conn_table) {
4114 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4115 "BM_%d : Failed to allocate memory in"
4116 "hba_setup_cid_tbls\n");
4117
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004118 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004119 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004120 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004121
4122 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304123 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004124
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004125 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4126 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004127
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004128 ptr_cid_info = phba->cid_array_info[ulp_num];
4129 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4130 phba->phwi_ctrlr->wrb_context[i].cid;
4131
4132 }
4133
4134 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4135 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4136 ptr_cid_info = phba->cid_array_info[ulp_num];
4137
4138 ptr_cid_info->cid_alloc = 0;
4139 ptr_cid_info->cid_free = 0;
4140 }
4141 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304142 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004143
4144free_memory:
4145 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4146 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4147 ptr_cid_info = phba->cid_array_info[ulp_num];
4148
4149 if (ptr_cid_info) {
4150 kfree(ptr_cid_info->cid_array);
4151 kfree(ptr_cid_info);
4152 phba->cid_array_info[ulp_num] = NULL;
4153 }
4154 }
4155 }
4156
4157 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304158}
4159
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304160static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304161{
4162 struct be_ctrl_info *ctrl = &phba->ctrl;
4163 struct hwi_controller *phwi_ctrlr;
4164 struct hwi_context_memory *phwi_context;
4165 struct be_queue_info *eq;
4166 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304167 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304168 u32 enabled;
4169
4170 phwi_ctrlr = phba->phwi_ctrlr;
4171 phwi_context = phwi_ctrlr->phwi_ctxt;
4172
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304173 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4174 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4175 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304176
4177 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4178 if (!enabled) {
4179 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304180 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4181 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304182 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004183 }
4184
4185 if (!phba->msix_enabled) {
4186 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304187 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4188 "BM_%d : eq->id=%d\n", eq->id);
4189
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004190 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4191 } else {
4192 for (i = 0; i <= phba->num_cpus; i++) {
4193 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304194 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4195 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304196 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4197 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304198 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304199}
4200
4201static void hwi_disable_intr(struct beiscsi_hba *phba)
4202{
4203 struct be_ctrl_info *ctrl = &phba->ctrl;
4204
4205 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4206 u32 reg = ioread32(addr);
4207
4208 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4209 if (enabled) {
4210 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4211 iowrite32(reg, addr);
4212 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304213 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4214 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304215}
4216
4217static int beiscsi_init_port(struct beiscsi_hba *phba)
4218{
4219 int ret;
4220
4221 ret = beiscsi_init_controller(phba);
4222 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304223 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4224 "BM_%d : beiscsi_dev_probe - Failed in"
4225 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304226 return ret;
4227 }
4228 ret = beiscsi_init_sgl_handle(phba);
4229 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304230 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4231 "BM_%d : beiscsi_dev_probe - Failed in"
4232 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304233 goto do_cleanup_ctrlr;
4234 }
4235
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05304236 ret = hba_setup_cid_tbls(phba);
4237 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304238 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4239 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304240 kfree(phba->io_sgl_hndl_base);
4241 kfree(phba->eh_sgl_hndl_base);
4242 goto do_cleanup_ctrlr;
4243 }
4244
4245 return ret;
4246
4247do_cleanup_ctrlr:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304248 hwi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304249 return ret;
4250}
4251
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304252static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304253{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004254 struct ulp_cid_info *ptr_cid_info = NULL;
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304255 int ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304256
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304257 kfree(phba->io_sgl_hndl_base);
4258 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304259 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004260 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004261
4262 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4263 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4264 ptr_cid_info = phba->cid_array_info[ulp_num];
4265
4266 if (ptr_cid_info) {
4267 kfree(ptr_cid_info->cid_array);
4268 kfree(ptr_cid_info);
4269 phba->cid_array_info[ulp_num] = NULL;
4270 }
4271 }
4272 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304273}
4274
John Soni Josed629c472012-10-20 04:42:00 +05304275/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004276 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4277 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004278 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004279 *
4280 * Free driver mgmt resources binded to CXN.
4281 **/
4282void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004283beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4284 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004285{
4286 struct beiscsi_io_task *io_task;
4287 struct beiscsi_hba *phba = beiscsi_conn->phba;
4288 struct hwi_wrb_context *pwrb_context;
4289 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004290 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4291 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004292
4293 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004294 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4295
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004296 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004297
4298 if (io_task->pwrb_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304299 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004300 io_task->pwrb_handle = NULL;
4301 }
4302
4303 if (io_task->psgl_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304304 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004305 io_task->psgl_handle = NULL;
4306 }
4307
John Soni Joseeb1c4692015-04-25 08:17:45 +05304308 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004309 pci_unmap_single(phba->pcidev,
4310 io_task->mtask_addr,
4311 io_task->mtask_data_count,
4312 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304313 io_task->mtask_addr = 0;
4314 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004315}
4316
4317/**
John Soni Josed629c472012-10-20 04:42:00 +05304318 * beiscsi_cleanup_task()- Free driver resources of the task
4319 * @task: ptr to the iscsi task
4320 *
4321 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004322static void beiscsi_cleanup_task(struct iscsi_task *task)
4323{
4324 struct beiscsi_io_task *io_task = task->dd_data;
4325 struct iscsi_conn *conn = task->conn;
4326 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4327 struct beiscsi_hba *phba = beiscsi_conn->phba;
4328 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4329 struct hwi_wrb_context *pwrb_context;
4330 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004331 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4332 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004333
4334 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004335 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004336
4337 if (io_task->cmd_bhs) {
4338 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4339 io_task->bhs_pa.u.a64.address);
4340 io_task->cmd_bhs = NULL;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304341 task->hdr = NULL;
Mike Christie1282ab72012-04-18 03:06:00 -05004342 }
4343
4344 if (task->sc) {
4345 if (io_task->pwrb_handle) {
4346 free_wrb_handle(phba, pwrb_context,
4347 io_task->pwrb_handle);
4348 io_task->pwrb_handle = NULL;
4349 }
4350
4351 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004352 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004353 io_task->psgl_handle = NULL;
4354 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004355
4356 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304357 if (io_task->num_sg)
4358 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004359 io_task->scsi_cmnd = NULL;
4360 }
Mike Christie1282ab72012-04-18 03:06:00 -05004361 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004362 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004363 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004364 }
4365}
4366
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304367void
4368beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4369 struct beiscsi_offload_params *params)
4370{
4371 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304372 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304373 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004374 struct iscsi_task *task = beiscsi_conn->task;
4375 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304376 u32 doorbell = 0;
4377
4378 /*
4379 * We can always use 0 here because it is reserved by libiscsi for
4380 * login/startup related tasks.
4381 */
Mike Christie1282ab72012-04-18 03:06:00 -05004382 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004383 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004384 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004385 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004386
John Soni Jose340c99e2015-08-20 04:44:30 +05304387 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4388 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304389
John Soni Joseacb96932012-10-20 04:44:35 +05304390 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004391 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304392 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304393 phba->init_mem,
4394 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004395 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304396 beiscsi_offload_cxn_v2(params, pwrb_handle,
4397 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304398
John Soni Joseacb96932012-10-20 04:44:35 +05304399 be_dws_le_to_cpu(pwrb_handle->pwrb,
4400 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304401
4402 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304403 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304404 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304405 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004406 iowrite32(doorbell, phba->db_va +
4407 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304408
4409 /*
4410 * There is no completion for CONTEXT_UPDATE. The completion of next
4411 * WRB posted guarantees FW's processing and DMA'ing of it.
4412 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4413 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4414 */
4415 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4416 phba->params.wrbs_per_cxn);
4417 beiscsi_log(phba, KERN_INFO,
4418 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4419 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4420 pwrb_handle, pwrb_context->free_index,
4421 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304422}
4423
4424static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4425 int *index, int *age)
4426{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304427 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304428 if (age)
4429 *age = conn->session->age;
4430}
4431
4432/**
4433 * beiscsi_alloc_pdu - allocates pdu and related resources
4434 * @task: libiscsi task
4435 * @opcode: opcode of pdu for task
4436 *
4437 * This is called with the session lock held. It will allocate
4438 * the wrb and sgl if needed for the command. And it will prep
4439 * the pdu's itt. beiscsi_parse_pdu will later translate
4440 * the pdu itt to the libiscsi task itt.
4441 */
4442static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4443{
4444 struct beiscsi_io_task *io_task = task->dd_data;
4445 struct iscsi_conn *conn = task->conn;
4446 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4447 struct beiscsi_hba *phba = beiscsi_conn->phba;
4448 struct hwi_wrb_context *pwrb_context;
4449 struct hwi_controller *phwi_ctrlr;
4450 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004451 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304452 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4453 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304454
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304455 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004456 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304457 if (!io_task->cmd_bhs)
4458 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304459 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304460 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304461 io_task->conn = beiscsi_conn;
4462
4463 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4464 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304465 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004466 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304467
4468 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304469 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304470 if (!io_task->psgl_handle) {
4471 beiscsi_log(phba, KERN_ERR,
4472 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4473 "BM_%d : Alloc of IO_SGL_ICD Failed"
4474 "for the CID : %d\n",
4475 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304476 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304477 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304478 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304479 beiscsi_conn->beiscsi_conn_cid,
4480 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304481 if (!io_task->pwrb_handle) {
4482 beiscsi_log(phba, KERN_ERR,
4483 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4484 "BM_%d : Alloc of WRB_HANDLE Failed"
4485 "for the CID : %d\n",
4486 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304487 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304488 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304489 } else {
4490 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304491 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004492 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304493 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304494 io_task->psgl_handle = (struct sgl_handle *)
4495 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304496 if (!io_task->psgl_handle) {
4497 beiscsi_log(phba, KERN_ERR,
4498 BEISCSI_LOG_IO |
4499 BEISCSI_LOG_CONFIG,
4500 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4501 "for the CID : %d\n",
4502 beiscsi_conn->
4503 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304504 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304505 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304506
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304507 beiscsi_conn->login_in_progress = 1;
4508 beiscsi_conn->plogin_sgl_handle =
4509 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304510 io_task->pwrb_handle =
4511 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304512 beiscsi_conn->beiscsi_conn_cid,
4513 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304514 if (!io_task->pwrb_handle) {
4515 beiscsi_log(phba, KERN_ERR,
4516 BEISCSI_LOG_IO |
4517 BEISCSI_LOG_CONFIG,
4518 "BM_%d : Alloc of WRB_HANDLE Failed"
4519 "for the CID : %d\n",
4520 beiscsi_conn->
4521 beiscsi_conn_cid);
4522 goto free_mgmt_hndls;
4523 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304524 beiscsi_conn->plogin_wrb_handle =
4525 io_task->pwrb_handle;
4526
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304527 } else {
4528 io_task->psgl_handle =
4529 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304530 io_task->pwrb_handle =
4531 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304532 }
4533 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304534 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304535 if (!io_task->psgl_handle) {
4536 beiscsi_log(phba, KERN_ERR,
4537 BEISCSI_LOG_IO |
4538 BEISCSI_LOG_CONFIG,
4539 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4540 "for the CID : %d\n",
4541 beiscsi_conn->
4542 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304543 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304544 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304545 io_task->pwrb_handle =
4546 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304547 beiscsi_conn->beiscsi_conn_cid,
4548 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304549 if (!io_task->pwrb_handle) {
4550 beiscsi_log(phba, KERN_ERR,
4551 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4552 "BM_%d : Alloc of WRB_HANDLE Failed"
4553 "for the CID : %d\n",
4554 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304555 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304556 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304557
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304558 }
4559 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304560 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4561 wrb_index << 16) | (unsigned int)
4562 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304563 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304564
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304565 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4566 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304567
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304568free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304569 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304570 goto free_hndls;
4571free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304572 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004573 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304574free_hndls:
4575 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004576 cri_index = BE_GET_CRI_FROM_CID(
4577 beiscsi_conn->beiscsi_conn_cid);
4578 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304579 if (io_task->pwrb_handle)
4580 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304581 io_task->pwrb_handle = NULL;
4582 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4583 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004584 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304585 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304586}
John Soni Jose09a10932012-10-20 04:44:23 +05304587int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4588 unsigned int num_sg, unsigned int xferlen,
4589 unsigned int writedir)
4590{
4591
4592 struct beiscsi_io_task *io_task = task->dd_data;
4593 struct iscsi_conn *conn = task->conn;
4594 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4595 struct beiscsi_hba *phba = beiscsi_conn->phba;
4596 struct iscsi_wrb *pwrb = NULL;
4597 unsigned int doorbell = 0;
4598
4599 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304600
John Soni Jose09a10932012-10-20 04:44:23 +05304601 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4602
4603 if (writedir) {
4604 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4605 INI_WR_CMD);
4606 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4607 } else {
4608 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4609 INI_RD_CMD);
4610 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4611 }
4612
4613 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4614 type, pwrb);
4615
4616 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4617 cpu_to_be16(*(unsigned short *)
4618 &io_task->cmd_bhs->iscsi_hdr.lun));
4619 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4621 io_task->pwrb_handle->wrb_index);
4622 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4623 be32_to_cpu(task->cmdsn));
4624 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4625 io_task->psgl_handle->sgl_index);
4626
4627 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4628 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304629 io_task->pwrb_handle->wrb_index);
4630 if (io_task->pwrb_context->plast_wrb)
4631 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4632 io_task->pwrb_context->plast_wrb,
4633 io_task->pwrb_handle->wrb_index);
4634 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304635
4636 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4637
4638 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4639 doorbell |= (io_task->pwrb_handle->wrb_index &
4640 DB_DEF_PDU_WRB_INDEX_MASK) <<
4641 DB_DEF_PDU_WRB_INDEX_SHIFT;
4642 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004643 iowrite32(doorbell, phba->db_va +
4644 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304645 return 0;
4646}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304647
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304648static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4649 unsigned int num_sg, unsigned int xferlen,
4650 unsigned int writedir)
4651{
4652
4653 struct beiscsi_io_task *io_task = task->dd_data;
4654 struct iscsi_conn *conn = task->conn;
4655 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4656 struct beiscsi_hba *phba = beiscsi_conn->phba;
4657 struct iscsi_wrb *pwrb = NULL;
4658 unsigned int doorbell = 0;
4659
4660 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304661 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4662
4663 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304664 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4665 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304666 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304667 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304668 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4669 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304670 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4671 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304672
John Soni Jose09a10932012-10-20 04:44:23 +05304673 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4674 type, pwrb);
4675
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304676 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004677 cpu_to_be16(*(unsigned short *)
4678 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304679 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4680 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4681 io_task->pwrb_handle->wrb_index);
4682 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4683 be32_to_cpu(task->cmdsn));
4684 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4685 io_task->psgl_handle->sgl_index);
4686
4687 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4688
4689 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304690 io_task->pwrb_handle->wrb_index);
4691 if (io_task->pwrb_context->plast_wrb)
4692 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4693 io_task->pwrb_context->plast_wrb,
4694 io_task->pwrb_handle->wrb_index);
4695 io_task->pwrb_context->plast_wrb = pwrb;
4696
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304697 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4698
4699 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304700 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304701 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4702 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4703
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004704 iowrite32(doorbell, phba->db_va +
4705 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304706 return 0;
4707}
4708
4709static int beiscsi_mtask(struct iscsi_task *task)
4710{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304711 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304712 struct iscsi_conn *conn = task->conn;
4713 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4714 struct beiscsi_hba *phba = beiscsi_conn->phba;
4715 struct iscsi_wrb *pwrb = NULL;
4716 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304717 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304718 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004719 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304720
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304721 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304723
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004724 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304725 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4726 be32_to_cpu(task->cmdsn));
4727 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4728 io_task->pwrb_handle->wrb_index);
4729 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4730 io_task->psgl_handle->sgl_index);
4731 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4732 task->data_count);
4733 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304734 io_task->pwrb_handle->wrb_index);
4735 if (io_task->pwrb_context->plast_wrb)
4736 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4737 io_task->pwrb_context->plast_wrb,
4738 io_task->pwrb_handle->wrb_index);
4739 io_task->pwrb_context->plast_wrb = pwrb;
4740
John Soni Jose09a10932012-10-20 04:44:23 +05304741 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004742 } else {
4743 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4744 be32_to_cpu(task->cmdsn));
4745 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4746 io_task->pwrb_handle->wrb_index);
4747 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4748 io_task->psgl_handle->sgl_index);
4749 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4750 task->data_count);
4751 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304752 io_task->pwrb_handle->wrb_index);
4753 if (io_task->pwrb_context->plast_wrb)
4754 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4755 io_task->pwrb_context->plast_wrb,
4756 io_task->pwrb_handle->wrb_index);
4757 io_task->pwrb_context->plast_wrb = pwrb;
4758
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004759 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304760 }
4761
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304762
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304763 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4764 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304765 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304766 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004767 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304768 break;
4769 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004770 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304771 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004772 if (is_chip_be2_be3r(phba))
4773 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304774 dmsg, pwrb, 1);
4775 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004776 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304777 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004778 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304779 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004780 if (is_chip_be2_be3r(phba))
4781 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304782 dmsg, pwrb, 0);
4783 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004784 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304785 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004786 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004787 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304788 break;
4789 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304790 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004791 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304792 break;
4793 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304794 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004795 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304796 break;
4797 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304798 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004799 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304800 break;
4801
4802 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304803 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4804 "BM_%d : opcode =%d Not supported\n",
4805 task->hdr->opcode & ISCSI_OPCODE_MASK);
4806
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304807 return -EINVAL;
4808 }
4809
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004810 if (ret)
4811 return ret;
4812
John Soni Jose09a10932012-10-20 04:44:23 +05304813 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004814 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4815 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4816 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304817
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304818 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304819 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304820 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4821 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004822 iowrite32(doorbell, phba->db_va +
4823 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304824 return 0;
4825}
4826
4827static int beiscsi_task_xmit(struct iscsi_task *task)
4828{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304829 struct beiscsi_io_task *io_task = task->dd_data;
4830 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304831 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304832 struct scatterlist *sg;
4833 int num_sg;
4834 unsigned int writedir = 0, xferlen = 0;
4835
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304836 phba = io_task->conn->phba;
4837 /**
4838 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4839 * operational if FW still gets heartbeat from EP FW. Is management
4840 * path really needed to continue further?
4841 */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304842 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304843 return -EIO;
4844
Jitendra Bhivare18683792016-02-04 15:49:18 +05304845 if (!io_task->conn->login_in_progress)
4846 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304847
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304848 if (!sc)
4849 return beiscsi_mtask(task);
4850
4851 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304852 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304853 num_sg = scsi_dma_map(sc);
4854 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004855 beiscsi_log(phba, KERN_ERR,
4856 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4857 "BM_%d : scsi_dma_map Failed "
4858 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4859 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4860 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304861
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304862 return num_sg;
4863 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304864 /**
4865 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4866 * For management task, cleanup_task checks mtask_addr before unmapping.
4867 */
4868 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304869 xferlen = scsi_bufflen(sc);
4870 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304871 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304872 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304873 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304874 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304875
John Soni Jose09a10932012-10-20 04:44:23 +05304876 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304877}
4878
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004879/**
4880 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4881 * @job: job to handle
4882 */
4883static int beiscsi_bsg_request(struct bsg_job *job)
4884{
4885 struct Scsi_Host *shost;
4886 struct beiscsi_hba *phba;
4887 struct iscsi_bsg_request *bsg_req = job->request;
4888 int rc = -EINVAL;
4889 unsigned int tag;
4890 struct be_dma_mem nonemb_cmd;
4891 struct be_cmd_resp_hdr *resp;
4892 struct iscsi_bsg_reply *bsg_reply = job->reply;
4893 unsigned short status, extd_status;
4894
4895 shost = iscsi_job_to_shost(job);
4896 phba = iscsi_host_priv(shost);
4897
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304898 if (!beiscsi_hba_is_online(phba)) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304899 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4900 "BM_%d : HBA in error 0x%lx\n", phba->state);
4901 return -ENXIO;
4902 }
4903
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004904 switch (bsg_req->msgcode) {
4905 case ISCSI_BSG_HST_VENDOR:
4906 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4907 job->request_payload.payload_len,
4908 &nonemb_cmd.dma);
4909 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304910 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4911 "BM_%d : Failed to allocate memory for "
4912 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304913 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004914 }
4915 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4916 &nonemb_cmd);
4917 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304918 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304919 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304920
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004921 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4922 nonemb_cmd.va, nonemb_cmd.dma);
4923 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304924 }
4925
4926 rc = wait_event_interruptible_timeout(
4927 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304928 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304929 msecs_to_jiffies(
4930 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304931
4932 if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
4933 clear_bit(MCC_TAG_STATE_RUNNING,
4934 &phba->ctrl.ptag_state[tag].tag_state);
4935 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4936 nonemb_cmd.va, nonemb_cmd.dma);
4937 return -EIO;
4938 }
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304939 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4940 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4941 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304942 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004943 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4944 sg_copy_from_buffer(job->reply_payload.sg_list,
4945 job->reply_payload.sg_cnt,
4946 nonemb_cmd.va, (resp->response_length
4947 + sizeof(*resp)));
4948 bsg_reply->reply_payload_rcv_len = resp->response_length;
4949 bsg_reply->result = status;
4950 bsg_job_done(job, bsg_reply->result,
4951 bsg_reply->reply_payload_rcv_len);
4952 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4953 nonemb_cmd.va, nonemb_cmd.dma);
4954 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304955 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304956 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304957 " status = %d extd_status = %d\n",
4958 status, extd_status);
4959
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004960 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304961 } else {
4962 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004963 }
4964 break;
4965
4966 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304967 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4968 "BM_%d : Unsupported bsg command: 0x%x\n",
4969 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004970 break;
4971 }
4972
4973 return rc;
4974}
4975
John Soni Jose99bc5d52012-08-20 23:00:18 +05304976void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4977{
4978 /* Set the logging parameter */
4979 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4980}
4981
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304982void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
4983{
4984 if (phba->boot_struct.boot_kset)
4985 return;
4986
4987 /* skip if boot work is already in progress */
4988 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
4989 return;
4990
4991 phba->boot_struct.retry = 3;
4992 phba->boot_struct.tag = 0;
4993 phba->boot_struct.s_handle = s_handle;
4994 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
4995 schedule_work(&phba->boot_work);
4996}
4997
4998static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
4999{
5000 struct beiscsi_hba *phba = data;
5001 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
5002 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5003 char *str = buf;
5004 int rc = -EPERM;
5005
5006 switch (type) {
5007 case ISCSI_BOOT_TGT_NAME:
5008 rc = sprintf(buf, "%.*s\n",
5009 (int)strlen(boot_sess->target_name),
5010 (char *)&boot_sess->target_name);
5011 break;
5012 case ISCSI_BOOT_TGT_IP_ADDR:
5013 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5014 rc = sprintf(buf, "%pI4\n",
5015 (char *)&boot_conn->dest_ipaddr.addr);
5016 else
5017 rc = sprintf(str, "%pI6\n",
5018 (char *)&boot_conn->dest_ipaddr.addr);
5019 break;
5020 case ISCSI_BOOT_TGT_PORT:
5021 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5022 break;
5023
5024 case ISCSI_BOOT_TGT_CHAP_NAME:
5025 rc = sprintf(str, "%.*s\n",
5026 boot_conn->negotiated_login_options.auth_data.chap.
5027 target_chap_name_length,
5028 (char *)&boot_conn->negotiated_login_options.
5029 auth_data.chap.target_chap_name);
5030 break;
5031 case ISCSI_BOOT_TGT_CHAP_SECRET:
5032 rc = sprintf(str, "%.*s\n",
5033 boot_conn->negotiated_login_options.auth_data.chap.
5034 target_secret_length,
5035 (char *)&boot_conn->negotiated_login_options.
5036 auth_data.chap.target_secret);
5037 break;
5038 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5039 rc = sprintf(str, "%.*s\n",
5040 boot_conn->negotiated_login_options.auth_data.chap.
5041 intr_chap_name_length,
5042 (char *)&boot_conn->negotiated_login_options.
5043 auth_data.chap.intr_chap_name);
5044 break;
5045 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5046 rc = sprintf(str, "%.*s\n",
5047 boot_conn->negotiated_login_options.auth_data.chap.
5048 intr_secret_length,
5049 (char *)&boot_conn->negotiated_login_options.
5050 auth_data.chap.intr_secret);
5051 break;
5052 case ISCSI_BOOT_TGT_FLAGS:
5053 rc = sprintf(str, "2\n");
5054 break;
5055 case ISCSI_BOOT_TGT_NIC_ASSOC:
5056 rc = sprintf(str, "0\n");
5057 break;
5058 }
5059 return rc;
5060}
5061
5062static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5063{
5064 struct beiscsi_hba *phba = data;
5065 char *str = buf;
5066 int rc = -EPERM;
5067
5068 switch (type) {
5069 case ISCSI_BOOT_INI_INITIATOR_NAME:
5070 rc = sprintf(str, "%s\n",
5071 phba->boot_struct.boot_sess.initiator_iscsiname);
5072 break;
5073 }
5074 return rc;
5075}
5076
5077static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5078{
5079 struct beiscsi_hba *phba = data;
5080 char *str = buf;
5081 int rc = -EPERM;
5082
5083 switch (type) {
5084 case ISCSI_BOOT_ETH_FLAGS:
5085 rc = sprintf(str, "2\n");
5086 break;
5087 case ISCSI_BOOT_ETH_INDEX:
5088 rc = sprintf(str, "0\n");
5089 break;
5090 case ISCSI_BOOT_ETH_MAC:
5091 rc = beiscsi_get_macaddr(str, phba);
5092 break;
5093 }
5094 return rc;
5095}
5096
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305097static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5098{
5099 umode_t rc = 0;
5100
5101 switch (type) {
5102 case ISCSI_BOOT_TGT_NAME:
5103 case ISCSI_BOOT_TGT_IP_ADDR:
5104 case ISCSI_BOOT_TGT_PORT:
5105 case ISCSI_BOOT_TGT_CHAP_NAME:
5106 case ISCSI_BOOT_TGT_CHAP_SECRET:
5107 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5108 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5109 case ISCSI_BOOT_TGT_NIC_ASSOC:
5110 case ISCSI_BOOT_TGT_FLAGS:
5111 rc = S_IRUGO;
5112 break;
5113 }
5114 return rc;
5115}
5116
5117static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5118{
5119 umode_t rc = 0;
5120
5121 switch (type) {
5122 case ISCSI_BOOT_INI_INITIATOR_NAME:
5123 rc = S_IRUGO;
5124 break;
5125 }
5126 return rc;
5127}
5128
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305129static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5130{
5131 umode_t rc = 0;
5132
5133 switch (type) {
5134 case ISCSI_BOOT_ETH_FLAGS:
5135 case ISCSI_BOOT_ETH_MAC:
5136 case ISCSI_BOOT_ETH_INDEX:
5137 rc = S_IRUGO;
5138 break;
5139 }
5140 return rc;
5141}
5142
5143static void beiscsi_boot_kobj_release(void *data)
5144{
5145 struct beiscsi_hba *phba = data;
5146
5147 scsi_host_put(phba->shost);
5148}
5149
5150static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5151{
5152 struct boot_struct *bs = &phba->boot_struct;
5153 struct iscsi_boot_kobj *boot_kobj;
5154
5155 if (bs->boot_kset) {
5156 __beiscsi_log(phba, KERN_ERR,
5157 "BM_%d: boot_kset already created\n");
5158 return 0;
5159 }
5160
5161 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5162 if (!bs->boot_kset) {
5163 __beiscsi_log(phba, KERN_ERR,
5164 "BM_%d: boot_kset alloc failed\n");
5165 return -ENOMEM;
5166 }
5167
5168 /* get shost ref because the show function will refer phba */
5169 if (!scsi_host_get(phba->shost))
5170 goto free_kset;
5171
5172 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5173 beiscsi_show_boot_tgt_info,
5174 beiscsi_tgt_get_attr_visibility,
5175 beiscsi_boot_kobj_release);
5176 if (!boot_kobj)
5177 goto put_shost;
5178
5179 if (!scsi_host_get(phba->shost))
5180 goto free_kset;
5181
5182 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5183 beiscsi_show_boot_ini_info,
5184 beiscsi_ini_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_ethernet(bs->boot_kset, 0, phba,
5193 beiscsi_show_boot_eth_info,
5194 beiscsi_eth_get_attr_visibility,
5195 beiscsi_boot_kobj_release);
5196 if (!boot_kobj)
5197 goto put_shost;
5198
5199 return 0;
5200
5201put_shost:
5202 scsi_host_put(phba->shost);
5203free_kset:
5204 iscsi_boot_destroy_kset(bs->boot_kset);
5205 bs->boot_kset = NULL;
5206 return -ENOMEM;
5207}
5208
5209static void beiscsi_boot_work(struct work_struct *work)
5210{
5211 struct beiscsi_hba *phba =
5212 container_of(work, struct beiscsi_hba, boot_work);
5213 struct boot_struct *bs = &phba->boot_struct;
5214 unsigned int tag = 0;
5215
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305216 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305217 return;
5218
5219 beiscsi_log(phba, KERN_INFO,
5220 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5221 "BM_%d : %s action %d\n",
5222 __func__, phba->boot_struct.action);
5223
5224 switch (phba->boot_struct.action) {
5225 case BEISCSI_BOOT_REOPEN_SESS:
5226 tag = beiscsi_boot_reopen_sess(phba);
5227 break;
5228 case BEISCSI_BOOT_GET_SHANDLE:
5229 tag = __beiscsi_boot_get_shandle(phba, 1);
5230 break;
5231 case BEISCSI_BOOT_GET_SINFO:
5232 tag = beiscsi_boot_get_sinfo(phba);
5233 break;
5234 case BEISCSI_BOOT_LOGOUT_SESS:
5235 tag = beiscsi_boot_logout_sess(phba);
5236 break;
5237 case BEISCSI_BOOT_CREATE_KSET:
5238 beiscsi_boot_create_kset(phba);
5239 /**
5240 * updated boot_kset is made visible to all before
5241 * ending the boot work.
5242 */
5243 mb();
5244 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5245 return;
5246 }
5247 if (!tag) {
5248 if (bs->retry--)
5249 schedule_work(&phba->boot_work);
5250 else
5251 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5252 }
5253}
5254
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305255static void beiscsi_eqd_update_work(struct work_struct *work)
5256{
5257 struct hwi_context_memory *phwi_context;
5258 struct be_set_eqd set_eqd[MAX_CPUS];
5259 struct hwi_controller *phwi_ctrlr;
5260 struct be_eq_obj *pbe_eq;
5261 struct beiscsi_hba *phba;
5262 unsigned int pps, delta;
5263 struct be_aic_obj *aic;
5264 int eqd, i, num = 0;
5265 unsigned long now;
5266
5267 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305268 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305269 return;
5270
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005271 phwi_ctrlr = phba->phwi_ctrlr;
5272 phwi_context = phwi_ctrlr->phwi_ctxt;
5273
5274 for (i = 0; i <= phba->num_cpus; i++) {
5275 aic = &phba->aic_obj[i];
5276 pbe_eq = &phwi_context->be_eq[i];
5277 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305278 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005279 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305280 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005281 aic->eq_prev = pbe_eq->cq_count;
5282 continue;
5283 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305284 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005285 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5286 eqd = (pps / 1500) << 2;
5287
5288 if (eqd < 8)
5289 eqd = 0;
5290 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5291 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5292
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305293 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005294 aic->eq_prev = pbe_eq->cq_count;
5295
5296 if (eqd != aic->prev_eqd) {
5297 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5298 set_eqd[num].eq_id = pbe_eq->q.id;
5299 aic->prev_eqd = eqd;
5300 num++;
5301 }
5302 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305303 if (num)
5304 /* completion of this is ignored */
5305 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005306
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305307 schedule_delayed_work(&phba->eqd_update,
5308 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305309}
5310
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305311static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5312{
5313 int i, status;
5314
5315 for (i = 0; i <= phba->num_cpus; i++)
5316 phba->msix_entries[i].entry = i;
5317
5318 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5319 phba->num_cpus + 1, phba->num_cpus + 1);
5320 if (status > 0)
5321 phba->msix_enabled = true;
5322}
5323
5324static void beiscsi_hw_tpe_check(unsigned long ptr)
5325{
5326 struct beiscsi_hba *phba;
5327 u32 wait;
5328
5329 phba = (struct beiscsi_hba *)ptr;
5330 /* if not TPE, do nothing */
5331 if (!beiscsi_detect_tpe(phba))
5332 return;
5333
5334 /* wait default 4000ms before recovering */
5335 wait = 4000;
5336 if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
5337 wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
5338 queue_delayed_work(phba->wq, &phba->recover_port,
5339 msecs_to_jiffies(wait));
5340}
5341
5342static void beiscsi_hw_health_check(unsigned long ptr)
5343{
5344 struct beiscsi_hba *phba;
5345
5346 phba = (struct beiscsi_hba *)ptr;
5347 beiscsi_detect_ue(phba);
5348 if (beiscsi_detect_ue(phba)) {
5349 __beiscsi_log(phba, KERN_ERR,
5350 "BM_%d : port in error: %lx\n", phba->state);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305351 /* sessions are no longer valid, so first fail the sessions */
5352 queue_work(phba->wq, &phba->sess_work);
5353
5354 /* detect UER supported */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305355 if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
5356 return;
5357 /* modify this timer to check TPE */
5358 phba->hw_check.function = beiscsi_hw_tpe_check;
5359 }
5360
5361 mod_timer(&phba->hw_check,
5362 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5363}
5364
5365/*
5366 * beiscsi_enable_port()- Enables the disabled port.
5367 * Only port resources freed in disable function are reallocated.
5368 * This is called in HBA error handling path.
5369 *
5370 * @phba: Instance of driver private structure
5371 *
5372 **/
5373static int beiscsi_enable_port(struct beiscsi_hba *phba)
5374{
5375 struct hwi_context_memory *phwi_context;
5376 struct hwi_controller *phwi_ctrlr;
5377 struct be_eq_obj *pbe_eq;
5378 int ret, i;
5379
5380 if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
5381 __beiscsi_log(phba, KERN_ERR,
5382 "BM_%d : %s : port is online %lx\n",
5383 __func__, phba->state);
5384 return 0;
5385 }
5386
5387 ret = beiscsi_init_sliport(phba);
5388 if (ret)
5389 return ret;
5390
5391 if (enable_msix)
5392 find_num_cpus(phba);
5393 else
5394 phba->num_cpus = 1;
5395 if (enable_msix) {
5396 beiscsi_msix_enable(phba);
5397 if (!phba->msix_enabled)
5398 phba->num_cpus = 1;
5399 }
5400
5401 beiscsi_get_params(phba);
5402 /* Re-enable UER. If different TPE occurs then it is recoverable. */
5403 beiscsi_set_uer_feature(phba);
5404
5405 phba->shost->max_id = phba->params.cxns_per_ctrl;
5406 phba->shost->can_queue = phba->params.ios_per_ctrl;
5407 ret = hwi_init_controller(phba);
5408 if (ret) {
5409 __beiscsi_log(phba, KERN_ERR,
5410 "BM_%d : init controller failed %d\n", ret);
5411 goto disable_msix;
5412 }
5413
5414 for (i = 0; i < MAX_MCC_CMD; i++) {
5415 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5416 phba->ctrl.mcc_tag[i] = i + 1;
5417 phba->ctrl.mcc_tag_status[i + 1] = 0;
5418 phba->ctrl.mcc_tag_available++;
5419 }
5420
5421 phwi_ctrlr = phba->phwi_ctrlr;
5422 phwi_context = phwi_ctrlr->phwi_ctxt;
5423 for (i = 0; i < phba->num_cpus; i++) {
5424 pbe_eq = &phwi_context->be_eq[i];
5425 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5426 }
5427
5428 i = (phba->msix_enabled) ? i : 0;
5429 /* Work item for MCC handling */
5430 pbe_eq = &phwi_context->be_eq[i];
5431 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5432
5433 ret = beiscsi_init_irqs(phba);
5434 if (ret < 0) {
5435 __beiscsi_log(phba, KERN_ERR,
5436 "BM_%d : setup IRQs failed %d\n", ret);
5437 goto cleanup_port;
5438 }
5439 hwi_enable_intr(phba);
5440 /* port operational: clear all error bits */
5441 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5442 __beiscsi_log(phba, KERN_INFO,
5443 "BM_%d : port online: 0x%lx\n", phba->state);
5444
5445 /* start hw_check timer and eqd_update work */
5446 schedule_delayed_work(&phba->eqd_update,
5447 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5448
5449 /**
5450 * Timer function gets modified for TPE detection.
5451 * Always reinit to do health check first.
5452 */
5453 phba->hw_check.function = beiscsi_hw_health_check;
5454 mod_timer(&phba->hw_check,
5455 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5456 return 0;
5457
5458cleanup_port:
5459 for (i = 0; i < phba->num_cpus; i++) {
5460 pbe_eq = &phwi_context->be_eq[i];
5461 irq_poll_disable(&pbe_eq->iopoll);
5462 }
5463 hwi_cleanup_port(phba);
5464
5465disable_msix:
5466 if (phba->msix_enabled)
5467 pci_disable_msix(phba->pcidev);
5468
5469 return ret;
5470}
5471
5472/*
5473 * beiscsi_disable_port()- Disable port and cleanup driver resources.
5474 * This is called in HBA error handling and driver removal.
5475 * @phba: Instance Priv structure
5476 * @unload: indicate driver is unloading
5477 *
5478 * Free the OS and HW resources held by the driver
5479 **/
5480static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
5481{
5482 struct hwi_context_memory *phwi_context;
5483 struct hwi_controller *phwi_ctrlr;
5484 struct be_eq_obj *pbe_eq;
5485 unsigned int i, msix_vec;
5486
5487 if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
5488 return;
5489
5490 phwi_ctrlr = phba->phwi_ctrlr;
5491 phwi_context = phwi_ctrlr->phwi_ctxt;
5492 hwi_disable_intr(phba);
5493 if (phba->msix_enabled) {
5494 for (i = 0; i <= phba->num_cpus; i++) {
5495 msix_vec = phba->msix_entries[i].vector;
5496 free_irq(msix_vec, &phwi_context->be_eq[i]);
5497 kfree(phba->msi_name[i]);
5498 }
5499 } else
5500 if (phba->pcidev->irq)
5501 free_irq(phba->pcidev->irq, phba);
5502 pci_disable_msix(phba->pcidev);
5503
5504 for (i = 0; i < phba->num_cpus; i++) {
5505 pbe_eq = &phwi_context->be_eq[i];
5506 irq_poll_disable(&pbe_eq->iopoll);
5507 }
5508 cancel_delayed_work_sync(&phba->eqd_update);
5509 cancel_work_sync(&phba->boot_work);
5510 /* WQ might be running cancel queued mcc_work if we are not exiting */
5511 if (!unload && beiscsi_hba_in_error(phba)) {
5512 pbe_eq = &phwi_context->be_eq[i];
5513 cancel_work_sync(&pbe_eq->mcc_work);
5514 }
5515 hwi_cleanup_port(phba);
5516}
5517
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305518static void beiscsi_sess_work(struct work_struct *work)
5519{
5520 struct beiscsi_hba *phba;
5521
5522 phba = container_of(work, struct beiscsi_hba, sess_work);
5523 /*
5524 * This work gets scheduled only in case of HBA error.
5525 * Old sessions are gone so need to be re-established.
5526 * iscsi_session_failure needs process context hence this work.
5527 */
5528 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5529}
5530
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305531static void beiscsi_recover_port(struct work_struct *work)
5532{
5533 struct beiscsi_hba *phba;
5534
5535 phba = container_of(work, struct beiscsi_hba, recover_port.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305536 beiscsi_disable_port(phba, 0);
5537 beiscsi_enable_port(phba);
5538}
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005539
5540static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5541 pci_channel_state_t state)
5542{
5543 struct beiscsi_hba *phba = NULL;
5544
5545 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305546 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005547
5548 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5549 "BM_%d : EEH error detected\n");
5550
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305551 /* first stop UE detection when PCI error detected */
5552 del_timer_sync(&phba->hw_check);
5553 cancel_delayed_work_sync(&phba->recover_port);
5554
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305555 /* sessions are no longer valid, so first fail the sessions */
5556 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305557 beiscsi_disable_port(phba, 0);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005558
5559 if (state == pci_channel_io_perm_failure) {
5560 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5561 "BM_%d : EEH : State PERM Failure");
5562 return PCI_ERS_RESULT_DISCONNECT;
5563 }
5564
5565 pci_disable_device(pdev);
5566
5567 /* The error could cause the FW to trigger a flash debug dump.
5568 * Resetting the card while flash dump is in progress
5569 * can cause it not to recover; wait for it to finish.
5570 * Wait only for first function as it is needed only once per
5571 * adapter.
5572 **/
5573 if (pdev->devfn == 0)
5574 ssleep(30);
5575
5576 return PCI_ERS_RESULT_NEED_RESET;
5577}
5578
5579static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5580{
5581 struct beiscsi_hba *phba = NULL;
5582 int status = 0;
5583
5584 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5585
5586 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5587 "BM_%d : EEH Reset\n");
5588
5589 status = pci_enable_device(pdev);
5590 if (status)
5591 return PCI_ERS_RESULT_DISCONNECT;
5592
5593 pci_set_master(pdev);
5594 pci_set_power_state(pdev, PCI_D0);
5595 pci_restore_state(pdev);
5596
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305597 status = beiscsi_check_fw_rdy(phba);
5598 if (status) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005599 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5600 "BM_%d : EEH Reset Completed\n");
5601 } else {
5602 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5603 "BM_%d : EEH Reset Completion Failure\n");
5604 return PCI_ERS_RESULT_DISCONNECT;
5605 }
5606
5607 pci_cleanup_aer_uncorrect_error_status(pdev);
5608 return PCI_ERS_RESULT_RECOVERED;
5609}
5610
5611static void beiscsi_eeh_resume(struct pci_dev *pdev)
5612{
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305613 struct beiscsi_hba *phba;
5614 int ret;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005615
5616 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5617 pci_save_state(pdev);
5618
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305619 ret = beiscsi_enable_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305620 if (ret)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305621 __beiscsi_log(phba, KERN_ERR,
5622 "BM_%d : AER EEH resume failed\n");
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005623}
5624
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005625static int beiscsi_dev_probe(struct pci_dev *pcidev,
5626 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305627{
5628 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305629 struct hwi_controller *phwi_ctrlr;
5630 struct hwi_context_memory *phwi_context;
5631 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305632 unsigned int s_handle;
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305633 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305634
5635 ret = beiscsi_enable_pci(pcidev);
5636 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305637 dev_err(&pcidev->dev,
5638 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305639 return ret;
5640 }
5641
5642 phba = beiscsi_hba_alloc(pcidev);
5643 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305644 dev_err(&pcidev->dev,
5645 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305646 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305647 goto disable_pci;
5648 }
5649
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005650 /* Enable EEH reporting */
5651 ret = pci_enable_pcie_error_reporting(pcidev);
5652 if (ret)
5653 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5654 "BM_%d : PCIe Error Reporting "
5655 "Enabling Failed\n");
5656
5657 pci_save_state(pcidev);
5658
John Soni Jose99bc5d52012-08-20 23:00:18 +05305659 /* Initialize Driver configuration Paramters */
5660 beiscsi_hba_attrs_init(phba);
5661
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005662 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305663
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305664 switch (pcidev->device) {
5665 case BE_DEVICE_ID1:
5666 case OC_DEVICE_ID1:
5667 case OC_DEVICE_ID2:
5668 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305669 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305670 break;
5671 case BE_DEVICE_ID2:
5672 case OC_DEVICE_ID3:
5673 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305674 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305675 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305676 case OC_SKH_ID1:
5677 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305678 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005679 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305680 default:
5681 phba->generation = 0;
5682 }
5683
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305684 ret = be_ctrl_init(phba, pcidev);
5685 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305686 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305687 "BM_%d : be_ctrl_init failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305688 goto hba_free;
5689 }
5690
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305691 ret = beiscsi_init_sliport(phba);
5692 if (ret)
5693 goto hba_free;
5694
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305695 spin_lock_init(&phba->io_sgl_lock);
5696 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005697 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305698 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305699 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305700 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5701 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305702 goto free_port;
5703 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305704 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305705 beiscsi_get_params(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305706 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005707
5708 if (enable_msix)
5709 find_num_cpus(phba);
5710 else
5711 phba->num_cpus = 1;
5712
5713 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5714 "BM_%d : num_cpus = %d\n",
5715 phba->num_cpus);
5716
5717 if (enable_msix) {
5718 beiscsi_msix_enable(phba);
5719 if (!phba->msix_enabled)
5720 phba->num_cpus = 1;
5721 }
5722
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005723 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305724 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305725 ret = beiscsi_init_port(phba);
5726 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305727 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5728 "BM_%d : beiscsi_dev_probe-"
5729 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305730 goto free_port;
5731 }
5732
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005733 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305734 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5735 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305736 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305737 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005738 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005739 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305740 }
5741
5742 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5743
John Soni Jose72fb46a2012-10-20 04:42:49 +05305744 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305745 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005746 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305747 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305748 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5749 "BM_%d : beiscsi_dev_probe-"
5750 "Failed to allocate work queue\n");
Christophe JAILLETdeeea8e2016-08-26 15:09:06 +05305751 ret = -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305752 goto free_twq;
5753 }
5754
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305755 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305756
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305757 phwi_ctrlr = phba->phwi_ctrlr;
5758 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305759
Jens Axboe89f8b332014-03-13 09:38:42 -06005760 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305761 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305762 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305763 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305764
Jens Axboe89f8b332014-03-13 09:38:42 -06005765 i = (phba->msix_enabled) ? i : 0;
5766 /* Work item for MCC handling */
5767 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305768 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005769
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305770 ret = beiscsi_init_irqs(phba);
5771 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305772 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5773 "BM_%d : beiscsi_dev_probe-"
5774 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305775 goto free_blkenbld;
5776 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305777 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005778
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305779 ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
5780 if (ret)
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005781 goto free_blkenbld;
5782
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305783 /* set online bit after port is operational */
5784 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5785 __beiscsi_log(phba, KERN_INFO,
5786 "BM_%d : port online: 0x%lx\n", phba->state);
5787
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305788 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5789 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5790 if (ret > 0) {
5791 beiscsi_start_boot_work(phba, s_handle);
5792 /**
5793 * Set this bit after starting the work to let
5794 * probe handle it first.
5795 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005796 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305797 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5798 }
Mike Christief457a462011-06-24 15:11:53 -05005799
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305800 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305801 schedule_delayed_work(&phba->eqd_update,
5802 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305803
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305804 INIT_WORK(&phba->sess_work, beiscsi_sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305805 INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305806 /**
5807 * Start UE detection here. UE before this will cause stall in probe
5808 * and eventually fail the probe.
5809 */
5810 init_timer(&phba->hw_check);
5811 phba->hw_check.function = beiscsi_hw_health_check;
5812 phba->hw_check.data = (unsigned long)phba;
5813 mod_timer(&phba->hw_check,
5814 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose99bc5d52012-08-20 23:00:18 +05305815 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5816 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305817 return 0;
5818
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305819free_blkenbld:
5820 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005821 for (i = 0; i < phba->num_cpus; i++) {
5822 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005823 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005824 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305825free_twq:
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305826 hwi_cleanup_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305827 beiscsi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305828 beiscsi_free_mem(phba);
5829free_port:
5830 pci_free_consistent(phba->pcidev,
5831 phba->ctrl.mbox_mem_alloced.size,
5832 phba->ctrl.mbox_mem_alloced.va,
5833 phba->ctrl.mbox_mem_alloced.dma);
5834 beiscsi_unmap_pci_function(phba);
5835hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305836 if (phba->msix_enabled)
5837 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305838 pci_dev_put(phba->pcidev);
5839 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305840 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305841disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305842 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305843 pci_disable_device(pcidev);
5844 return ret;
5845}
5846
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305847static void beiscsi_remove(struct pci_dev *pcidev)
5848{
5849 struct beiscsi_hba *phba = NULL;
5850
5851 phba = pci_get_drvdata(pcidev);
5852 if (!phba) {
5853 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5854 return;
5855 }
5856
5857 /* first stop UE detection before unloading */
5858 del_timer_sync(&phba->hw_check);
5859 cancel_delayed_work_sync(&phba->recover_port);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305860 cancel_work_sync(&phba->sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305861
5862 beiscsi_iface_destroy_default(phba);
5863 iscsi_host_remove(phba->shost);
5864 beiscsi_disable_port(phba, 1);
5865
5866 /* after cancelling boot_work */
5867 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5868
5869 /* free all resources */
5870 destroy_workqueue(phba->wq);
5871 beiscsi_cleanup_port(phba);
5872 beiscsi_free_mem(phba);
5873
5874 /* ctrl uninit */
5875 beiscsi_unmap_pci_function(phba);
5876 pci_free_consistent(phba->pcidev,
5877 phba->ctrl.mbox_mem_alloced.size,
5878 phba->ctrl.mbox_mem_alloced.va,
5879 phba->ctrl.mbox_mem_alloced.dma);
5880
5881 pci_dev_put(phba->pcidev);
5882 iscsi_host_free(phba->shost);
5883 pci_disable_pcie_error_reporting(pcidev);
5884 pci_set_drvdata(pcidev, NULL);
5885 pci_release_regions(pcidev);
5886 pci_disable_device(pcidev);
5887}
5888
5889
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005890static struct pci_error_handlers beiscsi_eeh_handlers = {
5891 .error_detected = beiscsi_eeh_err_detected,
5892 .slot_reset = beiscsi_eeh_reset,
5893 .resume = beiscsi_eeh_resume,
5894};
5895
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305896struct iscsi_transport beiscsi_iscsi_transport = {
5897 .owner = THIS_MODULE,
5898 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305899 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305900 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305901 .create_session = beiscsi_session_create,
5902 .destroy_session = beiscsi_session_destroy,
5903 .create_conn = beiscsi_conn_create,
5904 .bind_conn = beiscsi_conn_bind,
5905 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305906 .attr_is_visible = beiscsi_attr_is_visible,
5907 .set_iface_param = beiscsi_iface_set_param,
5908 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305909 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005910 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305911 .get_session_param = iscsi_session_get_param,
5912 .get_host_param = beiscsi_get_host_param,
5913 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005914 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305915 .send_pdu = iscsi_conn_send_pdu,
5916 .xmit_task = beiscsi_task_xmit,
5917 .cleanup_task = beiscsi_cleanup_task,
5918 .alloc_pdu = beiscsi_alloc_pdu,
5919 .parse_pdu_itt = beiscsi_parse_pdu,
5920 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005921 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305922 .ep_connect = beiscsi_ep_connect,
5923 .ep_poll = beiscsi_ep_poll,
5924 .ep_disconnect = beiscsi_ep_disconnect,
5925 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005926 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305927};
5928
5929static struct pci_driver beiscsi_pci_driver = {
5930 .name = DRV_NAME,
5931 .probe = beiscsi_dev_probe,
5932 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005933 .id_table = beiscsi_pci_id_table,
5934 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305935};
5936
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305937static int __init beiscsi_module_init(void)
5938{
5939 int ret;
5940
5941 beiscsi_scsi_transport =
5942 iscsi_register_transport(&beiscsi_iscsi_transport);
5943 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305944 printk(KERN_ERR
5945 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305946 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305947 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305948 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5949 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305950
5951 ret = pci_register_driver(&beiscsi_pci_driver);
5952 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305953 printk(KERN_ERR
5954 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305955 goto unregister_iscsi_transport;
5956 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305957 return 0;
5958
5959unregister_iscsi_transport:
5960 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5961 return ret;
5962}
5963
5964static void __exit beiscsi_module_exit(void)
5965{
5966 pci_unregister_driver(&beiscsi_pci_driver);
5967 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5968}
5969
5970module_init(beiscsi_module_init);
5971module_exit(beiscsi_module_exit);