blob: 222edfceb50742d4104958186ead1e48b162b5e7 [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 Bhivaree1f9d312016-08-19 15:20:22 +0530970 memset(pwrb_handle->pwrb, 0, sizeof(*pwrb_handle->pwrb));
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530971
972 return pwrb_handle;
973}
974
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530975/**
976 * alloc_wrb_handle - To allocate a wrb handle
977 * @phba: The hba pointer
978 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +0530979 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530980 *
981 * This happens under session_lock until submission to chip
982 */
John Soni Jose340c99e2015-08-20 04:44:30 +0530983struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530984 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530985{
986 struct hwi_wrb_context *pwrb_context;
987 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700988 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530989
990 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -0700991 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530992 /* return the context address */
993 *pcontext = pwrb_context;
994 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
995}
John Soni Jose340c99e2015-08-20 04:44:30 +0530996
Jitendra Bhivarecb564c62016-01-20 14:11:00 +0530997static inline void
998beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
999 struct wrb_handle *pwrb_handle,
1000 unsigned int wrbs_per_cxn)
1001{
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301002 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301003 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1004 pwrb_context->wrb_handles_available++;
1005 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1006 pwrb_context->free_index = 0;
1007 else
1008 pwrb_context->free_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301009 spin_unlock_bh(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301010}
1011
1012/**
1013 * free_wrb_handle - To free the wrb handle back to pool
1014 * @phba: The hba pointer
1015 * @pwrb_context: The context to free from
1016 * @pwrb_handle: The wrb_handle to free
1017 *
1018 * This happens under session_lock until submission to chip
1019 */
1020static void
1021free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1022 struct wrb_handle *pwrb_handle)
1023{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301024 beiscsi_put_wrb_handle(pwrb_context,
1025 pwrb_handle,
1026 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301027 beiscsi_log(phba, KERN_INFO,
1028 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1029 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1030 "wrb_handles_available=%d\n",
1031 pwrb_handle, pwrb_context->free_index,
1032 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301033}
1034
1035static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1036{
1037 struct sgl_handle *psgl_handle;
1038
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301039 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301040 if (phba->eh_sgl_hndl_avbl) {
1041 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1042 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301043 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1044 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1045 phba->eh_sgl_alloc_index,
1046 phba->eh_sgl_alloc_index);
1047
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301048 phba->eh_sgl_hndl_avbl--;
1049 if (phba->eh_sgl_alloc_index ==
1050 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1051 1))
1052 phba->eh_sgl_alloc_index = 0;
1053 else
1054 phba->eh_sgl_alloc_index++;
1055 } else
1056 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301057 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301058 return psgl_handle;
1059}
1060
1061void
1062free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1063{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301064 spin_lock_bh(&phba->mgmt_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301065 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1066 "BM_%d : In free_mgmt_sgl_handle,"
1067 "eh_sgl_free_index=%d\n",
1068 phba->eh_sgl_free_index);
1069
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301070 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1071 /*
1072 * this can happen if clean_task is called on a task that
1073 * failed in xmit_task or alloc_pdu.
1074 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301075 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1076 "BM_%d : Double Free in eh SGL ,"
1077 "eh_sgl_free_index=%d\n",
1078 phba->eh_sgl_free_index);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301079 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301080 return;
1081 }
1082 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1083 phba->eh_sgl_hndl_avbl++;
1084 if (phba->eh_sgl_free_index ==
1085 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1086 phba->eh_sgl_free_index = 0;
1087 else
1088 phba->eh_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301089 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301090}
1091
1092static void
1093be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301094 struct iscsi_task *task,
1095 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301096{
1097 struct beiscsi_io_task *io_task = task->dd_data;
1098 struct be_status_bhs *sts_bhs =
1099 (struct be_status_bhs *)io_task->cmd_bhs;
1100 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301101 unsigned char *sense;
1102 u32 resid = 0, exp_cmdsn, max_cmdsn;
1103 u8 rsp, status, flags;
1104
John Soni Jose73133262012-10-20 04:44:49 +05301105 exp_cmdsn = csol_cqe->exp_cmdsn;
1106 max_cmdsn = (csol_cqe->exp_cmdsn +
1107 csol_cqe->cmd_wnd - 1);
1108 rsp = csol_cqe->i_resp;
1109 status = csol_cqe->i_sts;
1110 flags = csol_cqe->i_flags;
1111 resid = csol_cqe->res_cnt;
1112
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001113 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001114 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001115 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001116 io_task->scsi_cmnd = NULL;
1117 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301118
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001119 return;
1120 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301121 task->sc->result = (DID_OK << 16) | status;
1122 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1123 task->sc->result = DID_ERROR << 16;
1124 goto unmap;
1125 }
1126
1127 /* bidi not initially supported */
1128 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1130 task->sc->result = DID_ERROR << 16;
1131
1132 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1133 scsi_set_resid(task->sc, resid);
1134 if (!status && (scsi_bufflen(task->sc) - resid <
1135 task->sc->underflow))
1136 task->sc->result = DID_ERROR << 16;
1137 }
1138 }
1139
1140 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001141 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301142 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001143
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301144 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001145 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301146 memcpy(task->sc->sense_buffer, sense,
1147 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1148 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301149
John Soni Jose73133262012-10-20 04:44:49 +05301150 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1151 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301152unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301153 if (io_task->scsi_cmnd) {
1154 scsi_dma_unmap(io_task->scsi_cmnd);
1155 io_task->scsi_cmnd = NULL;
1156 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301157 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1158}
1159
1160static void
1161be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301162 struct iscsi_task *task,
1163 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301164{
1165 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301166 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301167 struct iscsi_conn *conn = beiscsi_conn->conn;
1168
1169 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301170 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301171 hdr->t2wait = 5;
1172 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301173 hdr->flags = csol_cqe->i_flags;
1174 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001175 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1176 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1177 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301178
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301179 hdr->dlength[0] = 0;
1180 hdr->dlength[1] = 0;
1181 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301182 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301183 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301184 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1185}
1186
1187static void
1188be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301189 struct iscsi_task *task,
1190 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301191{
1192 struct iscsi_tm_rsp *hdr;
1193 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301194 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301195
1196 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301197 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301198 hdr->flags = csol_cqe->i_flags;
1199 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001200 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1201 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1202 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301203
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301204 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301205 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1206}
1207
1208static void
1209hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1210 struct beiscsi_hba *phba, struct sol_cqe *psol)
1211{
1212 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001213 uint16_t wrb_index, cid, cri_index;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05301214 struct hwi_controller *phwi_ctrlr;
1215 struct wrb_handle *pwrb_handle;
1216 struct iscsi_task *task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301217
1218 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001219 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301220 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1221 wrb_idx, psol);
1222 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1223 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001224 } else {
1225 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1226 wrb_idx, psol);
1227 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1228 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301229 }
1230
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001231 cri_index = BE_GET_CRI_FROM_CID(cid);
1232 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301233 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301234 task = pwrb_handle->pio_handle;
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07001235 iscsi_put_task(task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301236}
1237
1238static void
1239be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301240 struct iscsi_task *task,
1241 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301242{
1243 struct iscsi_nopin *hdr;
1244 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301245 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301246
1247 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301248 hdr->flags = csol_cqe->i_flags;
1249 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001250 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1251 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301252
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301253 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301254 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301255 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1256}
1257
John Soni Jose73133262012-10-20 04:44:49 +05301258static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1259 struct sol_cqe *psol,
1260 struct common_sol_cqe *csol_cqe)
1261{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001262 if (is_chip_be2_be3r(phba)) {
1263 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1264 i_exp_cmd_sn, psol);
1265 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1266 i_res_cnt, psol);
1267 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1268 i_cmd_wnd, psol);
1269 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1270 wrb_index, psol);
1271 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1272 cid, psol);
1273 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1274 hw_sts, psol);
1275 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1276 i_resp, psol);
1277 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1278 i_sts, psol);
1279 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1280 i_flags, psol);
1281 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301282 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1283 i_exp_cmd_sn, psol);
1284 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1285 i_res_cnt, psol);
1286 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1287 wrb_index, psol);
1288 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1289 cid, psol);
1290 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1291 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001292 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301293 i_cmd_wnd, psol);
1294 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1295 cmd_cmpl, psol))
1296 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1297 i_sts, psol);
1298 else
1299 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1300 i_sts, psol);
1301 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1302 u, psol))
1303 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1304
1305 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1306 o, psol))
1307 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301308 }
1309}
1310
1311
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301312static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1313 struct beiscsi_hba *phba, struct sol_cqe *psol)
1314{
1315 struct hwi_wrb_context *pwrb_context;
1316 struct wrb_handle *pwrb_handle;
1317 struct iscsi_wrb *pwrb = NULL;
1318 struct hwi_controller *phwi_ctrlr;
1319 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301320 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301321 struct iscsi_conn *conn = beiscsi_conn->conn;
1322 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301323 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001324 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301325
1326 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301327
1328 /* Copy the elements to a common structure */
1329 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1330
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001331 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1332 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301333
1334 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1335 csol_cqe.wrb_index];
1336
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301337 task = pwrb_handle->pio_handle;
1338 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301339 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301340
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001341 spin_lock_bh(&session->back_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301342 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301343 case HWH_TYPE_IO:
1344 case HWH_TYPE_IO_RD:
1345 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301346 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301347 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301348 else
John Soni Jose73133262012-10-20 04:44:49 +05301349 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301350 break;
1351
1352 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301353 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301354 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301355 else
John Soni Jose73133262012-10-20 04:44:49 +05301356 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301357 break;
1358
1359 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301360 beiscsi_log(phba, KERN_ERR,
1361 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1362 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1363 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301364 break;
1365
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301366 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301367 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301368 break;
1369
1370 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301371 beiscsi_log(phba, KERN_WARNING,
1372 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1373 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1374 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301375 csol_cqe.wrb_index,
1376 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301377 break;
1378 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301379
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001380 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301381}
1382
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301383/**
1384 * ASYNC PDUs include
1385 * a. Unsolicited NOP-In (target initiated NOP-In)
1386 * b. ASYNC Messages
1387 * c. Reject PDU
1388 * d. Login response
1389 * These headers arrive unprocessed by the EP firmware.
1390 * iSCSI layer processes them.
1391 */
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301392static unsigned int
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301393beiscsi_complete_pdu(struct beiscsi_conn *beiscsi_conn,
1394 struct pdu_base *phdr, void *pdata, unsigned int dlen)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301395{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301396 struct beiscsi_hba *phba = beiscsi_conn->phba;
1397 struct iscsi_conn *conn = beiscsi_conn->conn;
1398 struct beiscsi_io_task *io_task;
1399 struct iscsi_hdr *login_hdr;
1400 struct iscsi_task *task;
1401 u8 code;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301402
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301403 code = AMAP_GET_BITS(struct amap_pdu_base, opcode, phdr);
1404 switch (code) {
1405 case ISCSI_OP_NOOP_IN:
1406 pdata = NULL;
1407 dlen = 0;
1408 break;
1409 case ISCSI_OP_ASYNC_EVENT:
1410 break;
1411 case ISCSI_OP_REJECT:
1412 WARN_ON(!pdata);
1413 WARN_ON(!(dlen == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301414 beiscsi_log(phba, KERN_ERR,
1415 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301416 "BM_%d : In ISCSI_OP_REJECT\n");
1417 break;
1418 case ISCSI_OP_LOGIN_RSP:
1419 case ISCSI_OP_TEXT_RSP:
1420 task = conn->login_task;
1421 io_task = task->dd_data;
1422 login_hdr = (struct iscsi_hdr *)phdr;
1423 login_hdr->itt = io_task->libiscsi_itt;
1424 break;
1425 default:
1426 beiscsi_log(phba, KERN_WARNING,
1427 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1428 "BM_%d : unrecognized async PDU opcode 0x%x\n",
1429 code);
1430 return 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301431 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301432 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)phdr, pdata, dlen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301433 return 0;
1434}
1435
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301436static inline void
1437beiscsi_hdl_put_handle(struct hd_async_context *pasync_ctx,
1438 struct hd_async_handle *pasync_handle)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301439{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301440 if (pasync_handle->is_header) {
1441 list_add_tail(&pasync_handle->link,
1442 &pasync_ctx->async_header.free_list);
1443 pasync_ctx->async_header.free_entries++;
1444 } else {
1445 list_add_tail(&pasync_handle->link,
1446 &pasync_ctx->async_data.free_list);
1447 pasync_ctx->async_data.free_entries++;
1448 }
1449}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301450
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301451static struct hd_async_handle *
1452beiscsi_hdl_get_handle(struct beiscsi_conn *beiscsi_conn,
1453 struct hd_async_context *pasync_ctx,
1454 struct i_t_dpdu_cqe *pdpdu_cqe)
1455{
1456 struct beiscsi_hba *phba = beiscsi_conn->phba;
1457 struct hd_async_handle *pasync_handle;
1458 struct be_bus_address phys_addr;
1459 u8 final, error = 0;
1460 u16 cid, code, ci;
1461 u32 dpl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301462
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301463 cid = beiscsi_conn->beiscsi_conn_cid;
1464 /**
1465 * This function is invoked to get the right async_handle structure
1466 * from a given DEF PDU CQ entry.
1467 *
1468 * - index in CQ entry gives the vertical index
1469 * - address in CQ entry is the offset where the DMA last ended
1470 * - final - no more notifications for this PDU
1471 */
1472 if (is_chip_be2_be3r(phba)) {
1473 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1474 dpl, pdpdu_cqe);
1475 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1476 index, pdpdu_cqe);
1477 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1478 final, pdpdu_cqe);
1479 } else {
1480 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1481 dpl, pdpdu_cqe);
1482 ci = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1483 index, pdpdu_cqe);
1484 final = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1485 final, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301486 }
1487
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301488 /**
1489 * DB addr Hi/Lo is same for BE and SKH.
1490 * Subtract the dataplacementlength to get to the base.
1491 */
1492 phys_addr.u.a32.address_lo = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1493 db_addr_lo, pdpdu_cqe);
1494 phys_addr.u.a32.address_lo -= dpl;
1495 phys_addr.u.a32.address_hi = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1496 db_addr_hi, pdpdu_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301497
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301498 code = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe, code, pdpdu_cqe);
1499 switch (code) {
1500 case UNSOL_HDR_NOTIFY:
1501 pasync_handle = pasync_ctx->async_entry[ci].header;
1502 break;
1503 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1504 error = 1;
1505 case UNSOL_DATA_NOTIFY:
1506 pasync_handle = pasync_ctx->async_entry[ci].data;
1507 break;
1508 /* called only for above codes */
1509 default:
1510 pasync_handle = NULL;
1511 break;
1512 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301513
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301514 if (!pasync_handle) {
1515 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1516 "BM_%d : cid %d async PDU handle not found - code %d ci %d addr %llx\n",
1517 cid, code, ci, phys_addr.u.a64.address);
1518 return pasync_handle;
1519 }
1520
1521 if (pasync_handle->pa.u.a64.address != phys_addr.u.a64.address ||
1522 pasync_handle->index != ci) {
1523 /* driver bug - if ci does not match async handle index */
1524 error = 1;
1525 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1526 "BM_%d : cid %u async PDU handle mismatch - addr in %cQE %llx at %u:addr in CQE %llx ci %u\n",
1527 cid, pasync_handle->is_header ? 'H' : 'D',
1528 pasync_handle->pa.u.a64.address,
1529 pasync_handle->index,
1530 phys_addr.u.a64.address, ci);
1531 /* FW has stale address - attempt continuing by dropping */
1532 }
1533
1534 /**
1535 * Each CID is associated with unique CRI.
1536 * ASYNC_CRI_FROM_CID mapping and CRI_FROM_CID are totaly different.
1537 **/
1538 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(cid);
1539 pasync_handle->is_final = final;
1540 pasync_handle->buffer_len = dpl;
1541 /* empty the slot */
1542 if (pasync_handle->is_header)
1543 pasync_ctx->async_entry[ci].header = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301544 else
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301545 pasync_ctx->async_entry[ci].data = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301546
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301547 /**
1548 * DEF PDU header and data buffers with errors should be simply
1549 * dropped as there are no consumers for it.
1550 */
1551 if (error) {
1552 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1553 pasync_handle = NULL;
1554 }
1555 return pasync_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301556}
1557
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301558static void
1559beiscsi_hdl_purge_handles(struct beiscsi_hba *phba,
1560 struct hd_async_context *pasync_ctx,
1561 u16 cri)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301562{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301563 struct hd_async_handle *pasync_handle, *tmp_handle;
1564 struct list_head *plist;
1565
1566 plist = &pasync_ctx->async_entry[cri].wq.list;
1567 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1568 list_del(&pasync_handle->link);
1569 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1570 }
1571
1572 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wq.list);
1573 pasync_ctx->async_entry[cri].wq.hdr_len = 0;
1574 pasync_ctx->async_entry[cri].wq.bytes_received = 0;
1575 pasync_ctx->async_entry[cri].wq.bytes_needed = 0;
1576}
1577
1578static unsigned int
1579beiscsi_hdl_fwd_pdu(struct beiscsi_conn *beiscsi_conn,
1580 struct hd_async_context *pasync_ctx,
1581 u16 cri)
1582{
1583 struct iscsi_session *session = beiscsi_conn->conn->session;
1584 struct hd_async_handle *pasync_handle, *plast_handle;
1585 struct beiscsi_hba *phba = beiscsi_conn->phba;
1586 void *phdr = NULL, *pdata = NULL;
1587 u32 dlen = 0, status = 0;
1588 struct list_head *plist;
1589
1590 plist = &pasync_ctx->async_entry[cri].wq.list;
1591 plast_handle = NULL;
1592 list_for_each_entry(pasync_handle, plist, link) {
1593 plast_handle = pasync_handle;
1594 /* get the header, the first entry */
1595 if (!phdr) {
1596 phdr = pasync_handle->pbuffer;
1597 continue;
1598 }
1599 /* use first buffer to collect all the data */
1600 if (!pdata) {
1601 pdata = pasync_handle->pbuffer;
1602 dlen = pasync_handle->buffer_len;
1603 continue;
1604 }
1605 memcpy(pdata + dlen, pasync_handle->pbuffer,
1606 pasync_handle->buffer_len);
1607 dlen += pasync_handle->buffer_len;
1608 }
1609
1610 if (!plast_handle->is_final) {
1611 /* last handle should have final PDU notification from FW */
1612 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1613 "BM_%d : cid %u %p fwd async PDU with last handle missing - HL%u:DN%u:DR%u\n",
1614 beiscsi_conn->beiscsi_conn_cid, plast_handle,
1615 pasync_ctx->async_entry[cri].wq.hdr_len,
1616 pasync_ctx->async_entry[cri].wq.bytes_needed,
1617 pasync_ctx->async_entry[cri].wq.bytes_received);
1618 }
1619 spin_lock_bh(&session->back_lock);
1620 status = beiscsi_complete_pdu(beiscsi_conn, phdr, pdata, dlen);
1621 spin_unlock_bh(&session->back_lock);
1622 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1623 return status;
1624}
1625
1626static unsigned int
1627beiscsi_hdl_gather_pdu(struct beiscsi_conn *beiscsi_conn,
1628 struct hd_async_context *pasync_ctx,
1629 struct hd_async_handle *pasync_handle)
1630{
1631 unsigned int bytes_needed = 0, status = 0;
1632 u16 cri = pasync_handle->cri;
1633 struct cri_wait_queue *wq;
1634 struct beiscsi_hba *phba;
1635 struct pdu_base *ppdu;
1636 char *err = "";
1637
1638 phba = beiscsi_conn->phba;
1639 wq = &pasync_ctx->async_entry[cri].wq;
1640 if (pasync_handle->is_header) {
1641 /* check if PDU hdr is rcv'd when old hdr not completed */
1642 if (wq->hdr_len) {
1643 err = "incomplete";
1644 goto drop_pdu;
1645 }
1646 ppdu = pasync_handle->pbuffer;
1647 bytes_needed = AMAP_GET_BITS(struct amap_pdu_base,
1648 data_len_hi, ppdu);
1649 bytes_needed <<= 16;
1650 bytes_needed |= be16_to_cpu(AMAP_GET_BITS(struct amap_pdu_base,
1651 data_len_lo, ppdu));
1652 wq->hdr_len = pasync_handle->buffer_len;
1653 wq->bytes_received = 0;
1654 wq->bytes_needed = bytes_needed;
1655 list_add_tail(&pasync_handle->link, &wq->list);
1656 if (!bytes_needed)
1657 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1658 pasync_ctx, cri);
1659 } else {
1660 /* check if data received has header and is needed */
1661 if (!wq->hdr_len || !wq->bytes_needed) {
1662 err = "header less";
1663 goto drop_pdu;
1664 }
1665 wq->bytes_received += pasync_handle->buffer_len;
1666 /* Something got overwritten? Better catch it here. */
1667 if (wq->bytes_received > wq->bytes_needed) {
1668 err = "overflow";
1669 goto drop_pdu;
1670 }
1671 list_add_tail(&pasync_handle->link, &wq->list);
1672 if (wq->bytes_received == wq->bytes_needed)
1673 status = beiscsi_hdl_fwd_pdu(beiscsi_conn,
1674 pasync_ctx, cri);
1675 }
1676 return status;
1677
1678drop_pdu:
1679 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1680 "BM_%d : cid %u async PDU %s - def-%c:HL%u:DN%u:DR%u\n",
1681 beiscsi_conn->beiscsi_conn_cid, err,
1682 pasync_handle->is_header ? 'H' : 'D',
1683 wq->hdr_len, wq->bytes_needed,
1684 pasync_handle->buffer_len);
1685 /* discard this handle */
1686 beiscsi_hdl_put_handle(pasync_ctx, pasync_handle);
1687 /* free all the other handles in cri_wait_queue */
1688 beiscsi_hdl_purge_handles(phba, pasync_ctx, cri);
1689 /* try continuing */
1690 return status;
1691}
1692
1693static void
1694beiscsi_hdq_post_handles(struct beiscsi_hba *phba,
1695 u8 header, u8 ulp_num)
1696{
1697 struct hd_async_handle *pasync_handle, *tmp, **slot;
1698 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301699 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301700 struct list_head *hfree_list;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301701 struct phys_addr *pasync_sge;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301702 u32 ring_id, doorbell = 0;
1703 u16 index, num_entries;
1704 u32 doorbell_offset;
1705 u16 prod = 0, cons;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301706
1707 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001708 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001709 num_entries = pasync_ctx->num_entries;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301710 if (header) {
1711 cons = pasync_ctx->async_header.free_entries;
1712 hfree_list = &pasync_ctx->async_header.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001713 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1714 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301715 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301716 } else {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301717 cons = pasync_ctx->async_data.free_entries;
1718 hfree_list = &pasync_ctx->async_data.free_list;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001719 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1720 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301721 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301722 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301723 /* number of entries posted must be in multiples of 8 */
1724 if (cons % 8)
1725 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301726
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301727 list_for_each_entry_safe(pasync_handle, tmp, hfree_list, link) {
1728 list_del_init(&pasync_handle->link);
1729 pasync_handle->is_final = 0;
1730 pasync_handle->buffer_len = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301731
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301732 /* handles can be consumed out of order, use index in handle */
1733 index = pasync_handle->index;
1734 WARN_ON(pasync_handle->is_header != header);
1735 if (header)
1736 slot = &pasync_ctx->async_entry[index].header;
1737 else
1738 slot = &pasync_ctx->async_entry[index].data;
1739 /**
1740 * The slot just tracks handle's hold and release, so
1741 * overwriting at the same index won't do any harm but
1742 * needs to be caught.
1743 */
1744 if (*slot != NULL) {
1745 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_ISCSI,
1746 "BM_%d : async PDU %s slot at %u not empty\n",
1747 header ? "header" : "data", index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301748 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301749 /**
1750 * We use same freed index as in completion to post so this
1751 * operation is not required for refills. Its required only
1752 * for ring creation.
1753 */
1754 if (header)
1755 pasync_sge = pasync_ctx->async_header.ring_base;
1756 else
1757 pasync_sge = pasync_ctx->async_data.ring_base;
1758 pasync_sge += index;
1759 /* if its a refill then address is same; hi is lo */
1760 WARN_ON(pasync_sge->hi &&
1761 pasync_sge->hi != pasync_handle->pa.u.a32.address_lo);
1762 WARN_ON(pasync_sge->lo &&
1763 pasync_sge->lo != pasync_handle->pa.u.a32.address_hi);
1764 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1765 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301766
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301767 *slot = pasync_handle;
1768 if (++prod == cons)
1769 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301770 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301771 if (header)
1772 pasync_ctx->async_header.free_entries -= prod;
1773 else
1774 pasync_ctx->async_data.free_entries -= prod;
1775
1776 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1777 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1778 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1779 doorbell |= (prod & DB_DEF_PDU_CQPROC_MASK) << DB_DEF_PDU_CQPROC_SHIFT;
1780 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301781}
1782
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301783static void
1784beiscsi_hdq_process_compl(struct beiscsi_conn *beiscsi_conn,
1785 struct i_t_dpdu_cqe *pdpdu_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301786{
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301787 struct beiscsi_hba *phba = beiscsi_conn->phba;
1788 struct hd_async_handle *pasync_handle = NULL;
1789 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301790 struct hwi_controller *phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301791 u16 cid_cri;
1792 u8 ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301793
1794 phwi_ctrlr = phba->phwi_ctrlr;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301795 cid_cri = BE_GET_CRI_FROM_CID(beiscsi_conn->beiscsi_conn_cid);
1796 ulp_num = BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr, cid_cri);
1797 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
1798 pasync_handle = beiscsi_hdl_get_handle(beiscsi_conn, pasync_ctx,
1799 pdpdu_cqe);
1800 if (!pasync_handle)
1801 return;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301802
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301803 beiscsi_hdl_gather_pdu(beiscsi_conn, pasync_ctx, pasync_handle);
1804 beiscsi_hdq_post_handles(phba, pasync_handle->is_header, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301805}
1806
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301807void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301808{
1809 struct be_queue_info *mcc_cq;
1810 struct be_mcc_compl *mcc_compl;
1811 unsigned int num_processed = 0;
1812
1813 mcc_cq = &phba->ctrl.mcc_obj.cq;
1814 mcc_compl = queue_tail_node(mcc_cq);
1815 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1816 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301817 if (beiscsi_hba_in_error(phba))
1818 return;
1819
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301820 if (num_processed >= 32) {
1821 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301822 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301823 num_processed = 0;
1824 }
1825 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05301826 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301827 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301828 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301829 }
1830
1831 mcc_compl->flags = 0;
1832 queue_tail_inc(mcc_cq);
1833 mcc_compl = queue_tail_node(mcc_cq);
1834 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1835 num_processed++;
1836 }
1837
1838 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301839 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301840}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301841
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301842static void beiscsi_mcc_work(struct work_struct *work)
1843{
1844 struct be_eq_obj *pbe_eq;
1845 struct beiscsi_hba *phba;
1846
1847 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1848 phba = pbe_eq->phba;
1849 beiscsi_process_mcc_cq(phba);
1850 /* rearm EQ for further interrupts */
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301851 if (!beiscsi_hba_in_error(phba))
1852 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301853}
1854
John Soni Jose6763daa2012-10-20 04:41:45 +05301855/**
1856 * beiscsi_process_cq()- Process the Completion Queue
1857 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301858 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05301859 *
1860 * return
1861 * Number of Completion Entries processed.
1862 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301863unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301864{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301865 struct be_queue_info *cq;
1866 struct sol_cqe *sol;
1867 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301868 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301869 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301870 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001871 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301872 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301873 struct beiscsi_endpoint *beiscsi_ep;
1874 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301875 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301876
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301877 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301878 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301879 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301880
1881 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1882 CQE_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301883 if (beiscsi_hba_in_error(phba))
1884 return 0;
1885
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301886 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1887
John Soni Jose73133262012-10-20 04:44:49 +05301888 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
1889 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301890
John Soni Jose73133262012-10-20 04:44:49 +05301891 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001892 if (is_chip_be2_be3r(phba)) {
1893 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1894 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301895 if ((code == DRIVERMSG_NOTIFY) ||
1896 (code == UNSOL_HDR_NOTIFY) ||
1897 (code == UNSOL_DATA_NOTIFY))
1898 cid = AMAP_GET_BITS(
1899 struct amap_i_t_dpdu_cqe_v2,
1900 cid, sol);
1901 else
1902 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1903 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001904 }
John Soni Jose73133262012-10-20 04:44:49 +05301905
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001906 cri_index = BE_GET_CRI_FROM_CID(cid);
1907 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04001908
1909 if (ep == NULL) {
1910 /* connection has already been freed
1911 * just move on to next one
1912 */
1913 beiscsi_log(phba, KERN_WARNING,
1914 BEISCSI_LOG_INIT,
1915 "BM_%d : proc cqe of disconn ep: cid %d\n",
1916 cid);
1917 goto proc_next_cqe;
1918 }
1919
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301920 beiscsi_ep = ep->dd_data;
1921 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301922
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301923 /* replenish cq */
1924 if (num_processed == 32) {
1925 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301926 num_processed = 0;
1927 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301928 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301929
John Soni Jose0a513dd2012-08-20 23:00:55 +05301930 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301931 case SOL_CMD_COMPLETE:
1932 hwi_complete_cmd(beiscsi_conn, phba, sol);
1933 break;
1934 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301935 beiscsi_log(phba, KERN_INFO,
1936 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301937 "BM_%d : Received %s[%d] on CID : %d\n",
1938 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301939
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301940 dmsg = (struct dmsg_cqe *)sol;
1941 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1942 break;
1943 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301944 beiscsi_log(phba, KERN_INFO,
1945 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301946 "BM_%d : Received %s[%d] on CID : %d\n",
1947 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301948
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001949 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301950 beiscsi_hdq_process_compl(beiscsi_conn,
1951 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001952 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301953 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301954 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301955 beiscsi_log(phba, KERN_INFO,
1956 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301957 "BM_%d : Received %s[%d] on CID : %d\n",
1958 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301959
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001960 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301961 beiscsi_hdq_process_compl(beiscsi_conn,
1962 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001963 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301964 break;
1965 case CXN_INVALIDATE_INDEX_NOTIFY:
1966 case CMD_INVALIDATED_NOTIFY:
1967 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301968 beiscsi_log(phba, KERN_ERR,
1969 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301970 "BM_%d : Ignoring %s[%d] on CID : %d\n",
1971 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301972 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301973 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301974 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301975 beiscsi_log(phba, KERN_ERR,
1976 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1977 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1978 cqe_desc[code], code, cid);
1979 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301980 case CMD_KILLED_INVALID_STATSN_RCVD:
1981 case CMD_KILLED_INVALID_R2T_RCVD:
1982 case CMD_CXN_KILLED_LUN_INVALID:
1983 case CMD_CXN_KILLED_ICD_INVALID:
1984 case CMD_CXN_KILLED_ITT_INVALID:
1985 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1986 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301987 beiscsi_log(phba, KERN_ERR,
1988 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05301989 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
1990 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301991 break;
1992 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301993 beiscsi_log(phba, KERN_ERR,
1994 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301995 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
1996 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07001997 spin_lock_bh(&phba->async_pdu_lock);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05301998 /* driver consumes the entry and drops the contents */
1999 beiscsi_hdq_process_compl(beiscsi_conn,
2000 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002001 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302002 break;
2003 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2004 case CXN_KILLED_BURST_LEN_MISMATCH:
2005 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302006 case CXN_KILLED_UNKNOWN_HDR:
2007 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2008 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2009 case CXN_KILLED_TIMED_OUT:
2010 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302011 case CXN_KILLED_RST_SENT:
2012 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302013 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2014 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2015 case CXN_KILLED_OVER_RUN_RESIDUAL:
2016 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2017 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302018 beiscsi_log(phba, KERN_ERR,
2019 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302020 "BM_%d : Event %s[%d] received on CID : %d\n",
2021 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302022 if (beiscsi_conn)
2023 iscsi_conn_failure(beiscsi_conn->conn,
2024 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302025 break;
2026 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302027 beiscsi_log(phba, KERN_ERR,
2028 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302029 "BM_%d : Invalid CQE Event Received Code : %d"
2030 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302031 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302032 break;
2033 }
2034
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002035proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302036 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2037 queue_tail_inc(cq);
2038 sol = queue_tail_node(cq);
2039 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302040 if (total == budget)
2041 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302042 }
2043
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302044 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2045 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302046}
2047
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002048static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302049{
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302050 unsigned int ret, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302051 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302052 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302053 struct be_eq_entry *eqe = NULL;
2054 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302055
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302056 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302057 phba = pbe_eq->phba;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302058 if (beiscsi_hba_in_error(phba)) {
2059 irq_poll_complete(iop);
2060 return 0;
2061 }
2062
2063 io_events = 0;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302064 eq = &pbe_eq->q;
2065 eqe = queue_tail_node(eq);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302066 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2067 EQE_VALID_MASK) {
2068 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2069 queue_tail_inc(eq);
2070 eqe = queue_tail_node(eq);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302071 io_events++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302072 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302073 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302074
2075 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002076 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302077 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002078 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302079 beiscsi_log(phba, KERN_INFO,
2080 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302081 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2082 pbe_eq->q.id, ret);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302083 if (!beiscsi_hba_in_error(phba))
2084 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302085 }
2086 return ret;
2087}
2088
2089static void
John Soni Jose09a10932012-10-20 04:44:23 +05302090hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2091 unsigned int num_sg, struct beiscsi_io_task *io_task)
2092{
2093 struct iscsi_sge *psgl;
2094 unsigned int sg_len, index;
2095 unsigned int sge_len = 0;
2096 unsigned long long addr;
2097 struct scatterlist *l_sg;
2098 unsigned int offset;
2099
2100 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2101 io_task->bhs_pa.u.a32.address_lo);
2102 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2103 io_task->bhs_pa.u.a32.address_hi);
2104
2105 l_sg = sg;
2106 for (index = 0; (index < num_sg) && (index < 2); index++,
2107 sg = sg_next(sg)) {
2108 if (index == 0) {
2109 sg_len = sg_dma_len(sg);
2110 addr = (u64) sg_dma_address(sg);
2111 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2112 sge0_addr_lo, pwrb,
2113 lower_32_bits(addr));
2114 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2115 sge0_addr_hi, pwrb,
2116 upper_32_bits(addr));
2117 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2118 sge0_len, pwrb,
2119 sg_len);
2120 sge_len = sg_len;
2121 } else {
2122 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2123 pwrb, sge_len);
2124 sg_len = sg_dma_len(sg);
2125 addr = (u64) sg_dma_address(sg);
2126 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2127 sge1_addr_lo, pwrb,
2128 lower_32_bits(addr));
2129 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2130 sge1_addr_hi, pwrb,
2131 upper_32_bits(addr));
2132 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2133 sge1_len, pwrb,
2134 sg_len);
2135 }
2136 }
2137 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2138 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2139
2140 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2141
2142 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2143 io_task->bhs_pa.u.a32.address_hi);
2144 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2145 io_task->bhs_pa.u.a32.address_lo);
2146
2147 if (num_sg == 1) {
2148 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2149 1);
2150 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2151 0);
2152 } else if (num_sg == 2) {
2153 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2154 0);
2155 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2156 1);
2157 } else {
2158 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2159 0);
2160 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2161 0);
2162 }
2163
2164 sg = l_sg;
2165 psgl++;
2166 psgl++;
2167 offset = 0;
2168 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2169 sg_len = sg_dma_len(sg);
2170 addr = (u64) sg_dma_address(sg);
2171 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2172 lower_32_bits(addr));
2173 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2174 upper_32_bits(addr));
2175 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2176 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2177 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2178 offset += sg_len;
2179 }
2180 psgl--;
2181 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2182}
2183
2184static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302185hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2186 unsigned int num_sg, struct beiscsi_io_task *io_task)
2187{
2188 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302189 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302190 unsigned int sge_len = 0;
2191 unsigned long long addr;
2192 struct scatterlist *l_sg;
2193 unsigned int offset;
2194
2195 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2196 io_task->bhs_pa.u.a32.address_lo);
2197 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2198 io_task->bhs_pa.u.a32.address_hi);
2199
2200 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302201 for (index = 0; (index < num_sg) && (index < 2); index++,
2202 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302203 if (index == 0) {
2204 sg_len = sg_dma_len(sg);
2205 addr = (u64) sg_dma_address(sg);
2206 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302207 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302208 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302209 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302210 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2211 sg_len);
2212 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302213 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302214 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2215 pwrb, sge_len);
2216 sg_len = sg_dma_len(sg);
2217 addr = (u64) sg_dma_address(sg);
2218 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302219 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302220 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302221 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302222 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2223 sg_len);
2224 }
2225 }
2226 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2227 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2228
2229 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2230
2231 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2232 io_task->bhs_pa.u.a32.address_hi);
2233 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2234 io_task->bhs_pa.u.a32.address_lo);
2235
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302236 if (num_sg == 1) {
2237 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2238 1);
2239 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2240 0);
2241 } else if (num_sg == 2) {
2242 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2243 0);
2244 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2245 1);
2246 } else {
2247 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2248 0);
2249 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2250 0);
2251 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302252 sg = l_sg;
2253 psgl++;
2254 psgl++;
2255 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302256 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302257 sg_len = sg_dma_len(sg);
2258 addr = (u64) sg_dma_address(sg);
2259 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2260 (addr & 0xFFFFFFFF));
2261 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2262 (addr >> 32));
2263 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2264 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2265 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2266 offset += sg_len;
2267 }
2268 psgl--;
2269 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2270}
2271
John Soni Josed629c472012-10-20 04:42:00 +05302272/**
2273 * hwi_write_buffer()- Populate the WRB with task info
2274 * @pwrb: ptr to the WRB entry
2275 * @task: iscsi task which is to be executed
2276 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002277static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302278{
2279 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302280 struct beiscsi_io_task *io_task = task->dd_data;
2281 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2282 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302283 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302284
2285 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2286 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2287 io_task->bhs_pa.u.a32.address_lo);
2288 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2289 io_task->bhs_pa.u.a32.address_hi);
2290
2291 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302292
2293 /* Check for the data_count */
2294 dsp_value = (task->data_count) ? 1 : 0;
2295
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002296 if (is_chip_be2_be3r(phba))
2297 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302298 pwrb, dsp_value);
2299 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002300 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302301 pwrb, dsp_value);
2302
2303 /* Map addr only if there is data_count */
2304 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302305 io_task->mtask_addr = pci_map_single(phba->pcidev,
2306 task->data,
2307 task->data_count,
2308 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002309 if (pci_dma_mapping_error(phba->pcidev,
2310 io_task->mtask_addr))
2311 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302312 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302313 } else
John Soni Josed629c472012-10-20 04:42:00 +05302314 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302315
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302316 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302317 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302318 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302319 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302320 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2321 task->data_count);
2322
2323 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2324 } else {
2325 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302326 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302327 }
2328
2329 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2330
2331 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2332
2333 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2334 io_task->bhs_pa.u.a32.address_hi);
2335 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2336 io_task->bhs_pa.u.a32.address_lo);
2337 if (task->data) {
2338 psgl++;
2339 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2340 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2341 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2342 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2343 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2344 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2345
2346 psgl++;
2347 if (task->data) {
2348 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302349 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302350 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302351 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302352 }
2353 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2354 }
2355 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002356 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302357}
2358
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002359/**
2360 * beiscsi_find_mem_req()- Find mem needed
2361 * @phba: ptr to HBA struct
2362 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302363static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2364{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002365 uint8_t mem_descr_index, ulp_num;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302366 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302367 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2368 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2369
2370 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2371 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302372
2373 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2374
2375 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2376 BE_ISCSI_PDU_HEADER_SIZE;
2377 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2378 sizeof(struct hwi_context_memory);
2379
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302380
2381 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2382 * (phba->params.wrbs_per_cxn)
2383 * phba->params.cxns_per_ctrl;
2384 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2385 (phba->params.wrbs_per_cxn);
2386 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2387 phba->params.cxns_per_ctrl);
2388
2389 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2390 phba->params.icds_per_ctrl;
2391 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2392 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002393 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2394 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302395
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002396 num_async_pdu_buf_sgl_pages =
2397 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2398 phba, ulp_num) *
2399 sizeof(struct phys_addr));
2400
2401 num_async_pdu_buf_pages =
2402 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2403 phba, ulp_num) *
2404 phba->params.defpdu_hdr_sz);
2405
2406 num_async_pdu_data_pages =
2407 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2408 phba, ulp_num) *
2409 phba->params.defpdu_data_sz);
2410
2411 num_async_pdu_data_sgl_pages =
2412 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2413 phba, ulp_num) *
2414 sizeof(struct phys_addr));
2415
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002416 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2417 (ulp_num * MEM_DESCR_OFFSET));
2418 phba->mem_req[mem_descr_index] =
2419 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2420 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2421
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002422 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2423 (ulp_num * MEM_DESCR_OFFSET));
2424 phba->mem_req[mem_descr_index] =
2425 num_async_pdu_buf_pages *
2426 PAGE_SIZE;
2427
2428 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2429 (ulp_num * MEM_DESCR_OFFSET));
2430 phba->mem_req[mem_descr_index] =
2431 num_async_pdu_data_pages *
2432 PAGE_SIZE;
2433
2434 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2435 (ulp_num * MEM_DESCR_OFFSET));
2436 phba->mem_req[mem_descr_index] =
2437 num_async_pdu_buf_sgl_pages *
2438 PAGE_SIZE;
2439
2440 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2441 (ulp_num * MEM_DESCR_OFFSET));
2442 phba->mem_req[mem_descr_index] =
2443 num_async_pdu_data_sgl_pages *
2444 PAGE_SIZE;
2445
2446 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2447 (ulp_num * MEM_DESCR_OFFSET));
2448 phba->mem_req[mem_descr_index] =
2449 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302450 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002451
2452 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2453 (ulp_num * MEM_DESCR_OFFSET));
2454 phba->mem_req[mem_descr_index] =
2455 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302456 sizeof(struct hd_async_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002457
2458 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2459 (ulp_num * MEM_DESCR_OFFSET));
2460 phba->mem_req[mem_descr_index] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302461 sizeof(struct hd_async_context) +
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002462 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302463 sizeof(struct hd_async_entry));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002464 }
2465 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302466}
2467
2468static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2469{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302470 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002471 struct hwi_controller *phwi_ctrlr;
2472 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302473 struct mem_array *mem_arr, *mem_arr_orig;
2474 unsigned int i, j, alloc_size, curr_alloc_size;
2475
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002476 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302477 if (!phba->phwi_ctrlr)
2478 return -ENOMEM;
2479
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002480 /* Allocate memory for wrb_context */
2481 phwi_ctrlr = phba->phwi_ctrlr;
2482 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2483 phba->params.cxns_per_ctrl,
2484 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002485 if (!phwi_ctrlr->wrb_context) {
2486 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002487 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002488 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002489
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302490 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2491 GFP_KERNEL);
2492 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002493 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302494 kfree(phba->phwi_ctrlr);
2495 return -ENOMEM;
2496 }
2497
2498 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2499 GFP_KERNEL);
2500 if (!mem_arr_orig) {
2501 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002502 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302503 kfree(phba->phwi_ctrlr);
2504 return -ENOMEM;
2505 }
2506
2507 mem_descr = phba->init_mem;
2508 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002509 if (!phba->mem_req[i]) {
2510 mem_descr->mem_array = NULL;
2511 mem_descr++;
2512 continue;
2513 }
2514
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302515 j = 0;
2516 mem_arr = mem_arr_orig;
2517 alloc_size = phba->mem_req[i];
2518 memset(mem_arr, 0, sizeof(struct mem_array) *
2519 BEISCSI_MAX_FRAGS_INIT);
2520 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2521 do {
2522 mem_arr->virtual_address = pci_alloc_consistent(
2523 phba->pcidev,
2524 curr_alloc_size,
2525 &bus_add);
2526 if (!mem_arr->virtual_address) {
2527 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2528 goto free_mem;
2529 if (curr_alloc_size -
2530 rounddown_pow_of_two(curr_alloc_size))
2531 curr_alloc_size = rounddown_pow_of_two
2532 (curr_alloc_size);
2533 else
2534 curr_alloc_size = curr_alloc_size / 2;
2535 } else {
2536 mem_arr->bus_address.u.
2537 a64.address = (__u64) bus_add;
2538 mem_arr->size = curr_alloc_size;
2539 alloc_size -= curr_alloc_size;
2540 curr_alloc_size = min(be_max_phys_size *
2541 1024, alloc_size);
2542 j++;
2543 mem_arr++;
2544 }
2545 } while (alloc_size);
2546 mem_descr->num_elements = j;
2547 mem_descr->size_in_bytes = phba->mem_req[i];
2548 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2549 GFP_KERNEL);
2550 if (!mem_descr->mem_array)
2551 goto free_mem;
2552
2553 memcpy(mem_descr->mem_array, mem_arr_orig,
2554 sizeof(struct mem_array) * j);
2555 mem_descr++;
2556 }
2557 kfree(mem_arr_orig);
2558 return 0;
2559free_mem:
2560 mem_descr->num_elements = j;
2561 while ((i) || (j)) {
2562 for (j = mem_descr->num_elements; j > 0; j--) {
2563 pci_free_consistent(phba->pcidev,
2564 mem_descr->mem_array[j - 1].size,
2565 mem_descr->mem_array[j - 1].
2566 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302567 (unsigned long)mem_descr->
2568 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302569 bus_address.u.a64.address);
2570 }
2571 if (i) {
2572 i--;
2573 kfree(mem_descr->mem_array);
2574 mem_descr--;
2575 }
2576 }
2577 kfree(mem_arr_orig);
2578 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002579 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302580 kfree(phba->phwi_ctrlr);
2581 return -ENOMEM;
2582}
2583
2584static int beiscsi_get_memory(struct beiscsi_hba *phba)
2585{
2586 beiscsi_find_mem_req(phba);
2587 return beiscsi_alloc_mem(phba);
2588}
2589
2590static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2591{
2592 struct pdu_data_out *pdata_out;
2593 struct pdu_nop_out *pnop_out;
2594 struct be_mem_descriptor *mem_descr;
2595
2596 mem_descr = phba->init_mem;
2597 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2598 pdata_out =
2599 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2600 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2601
2602 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2603 IIOC_SCSI_DATA);
2604
2605 pnop_out =
2606 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2607 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2608
2609 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2610 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2611 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2612 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2613}
2614
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002615static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302616{
2617 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002618 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002619 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302620 struct hwi_controller *phwi_ctrlr;
2621 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002622 struct iscsi_wrb *pwrb = NULL;
2623 unsigned int num_cxn_wrbh = 0;
2624 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302625
2626 mem_descr_wrbh = phba->init_mem;
2627 mem_descr_wrbh += HWI_MEM_WRBH;
2628
2629 mem_descr_wrb = phba->init_mem;
2630 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302631 phwi_ctrlr = phba->phwi_ctrlr;
2632
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002633 /* Allocate memory for WRBQ */
2634 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2635 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002636 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002637 GFP_KERNEL);
2638 if (!phwi_ctxt->be_wrbq) {
2639 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2640 "BM_%d : WRBQ Mem Alloc Failed\n");
2641 return -ENOMEM;
2642 }
2643
2644 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302645 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302646 pwrb_context->pwrb_handle_base =
2647 kzalloc(sizeof(struct wrb_handle *) *
2648 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002649 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302650 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2651 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002652 goto init_wrb_hndl_failed;
2653 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302654 pwrb_context->pwrb_handle_basestd =
2655 kzalloc(sizeof(struct wrb_handle *) *
2656 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002657 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302658 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2659 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002660 goto init_wrb_hndl_failed;
2661 }
2662 if (!num_cxn_wrbh) {
2663 pwrb_handle =
2664 mem_descr_wrbh->mem_array[idx].virtual_address;
2665 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2666 ((sizeof(struct wrb_handle)) *
2667 phba->params.wrbs_per_cxn));
2668 idx++;
2669 }
2670 pwrb_context->alloc_index = 0;
2671 pwrb_context->wrb_handles_available = 0;
2672 pwrb_context->free_index = 0;
2673
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302674 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302675 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2676 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2677 pwrb_context->pwrb_handle_basestd[j] =
2678 pwrb_handle;
2679 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302680 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302681 pwrb_handle++;
2682 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302683 num_cxn_wrbh--;
2684 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302685 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302686 }
2687 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002688 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302689 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002690 if (!num_cxn_wrb) {
2691 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2692 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2693 ((sizeof(struct iscsi_wrb) *
2694 phba->params.wrbs_per_cxn));
2695 idx++;
2696 }
2697
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302698 if (num_cxn_wrb) {
2699 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2700 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2701 pwrb_handle->pwrb = pwrb;
2702 pwrb++;
2703 }
2704 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302705 }
2706 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002707 return 0;
2708init_wrb_hndl_failed:
2709 for (j = index; j > 0; j--) {
2710 pwrb_context = &phwi_ctrlr->wrb_context[j];
2711 kfree(pwrb_context->pwrb_handle_base);
2712 kfree(pwrb_context->pwrb_handle_basestd);
2713 }
2714 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302715}
2716
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002717static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302718{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002719 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302720 struct hwi_controller *phwi_ctrlr;
2721 struct hba_parameters *p = &phba->params;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302722 struct hd_async_context *pasync_ctx;
2723 struct hd_async_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002724 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302725 struct be_mem_descriptor *mem_descr;
2726
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002727 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2728 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302729 /* get async_ctx for each ULP */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002730 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2731 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2732 (ulp_num * MEM_DESCR_OFFSET));
2733
2734 phwi_ctrlr = phba->phwi_ctrlr;
2735 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302736 (struct hd_async_context *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002737 mem_descr->mem_array[0].virtual_address;
2738
2739 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2740 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2741
2742 pasync_ctx->async_entry =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302743 (struct hd_async_entry *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002744 ((long unsigned int)pasync_ctx +
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302745 sizeof(struct hd_async_context));
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002746
2747 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2748 ulp_num);
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302749 /* setup header buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002750 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2751 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2752 (ulp_num * MEM_DESCR_OFFSET);
2753 if (mem_descr->mem_array[0].virtual_address) {
2754 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2755 "BM_%d : hwi_init_async_pdu_ctx"
2756 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2757 ulp_num,
2758 mem_descr->mem_array[0].
2759 virtual_address);
2760 } else
2761 beiscsi_log(phba, KERN_WARNING,
2762 BEISCSI_LOG_INIT,
2763 "BM_%d : No Virtual address for ULP : %d\n",
2764 ulp_num);
2765
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302766 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002767 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302768 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302769
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002770 pasync_ctx->async_header.pa_base.u.a64.address =
2771 mem_descr->mem_array[0].
2772 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002773
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302774 /* setup header buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002775 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2776 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2777 (ulp_num * MEM_DESCR_OFFSET);
2778 if (mem_descr->mem_array[0].virtual_address) {
2779 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2780 "BM_%d : hwi_init_async_pdu_ctx"
2781 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2782 ulp_num,
2783 mem_descr->mem_array[0].
2784 virtual_address);
2785 } else
2786 beiscsi_log(phba, KERN_WARNING,
2787 BEISCSI_LOG_INIT,
2788 "BM_%d : No Virtual address for ULP : %d\n",
2789 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302790
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002791 pasync_ctx->async_header.ring_base =
2792 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302793
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302794 /* setup header buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002795 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2796 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2797 (ulp_num * MEM_DESCR_OFFSET);
2798 if (mem_descr->mem_array[0].virtual_address) {
2799 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2800 "BM_%d : hwi_init_async_pdu_ctx"
2801 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2802 ulp_num,
2803 mem_descr->mem_array[0].
2804 virtual_address);
2805 } else
2806 beiscsi_log(phba, KERN_WARNING,
2807 BEISCSI_LOG_INIT,
2808 "BM_%d : No Virtual address for ULP : %d\n",
2809 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302810
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002811 pasync_ctx->async_header.handle_base =
2812 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002813 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302814
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302815 /* setup data buffer sgls */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002816 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2817 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2818 (ulp_num * MEM_DESCR_OFFSET);
2819 if (mem_descr->mem_array[0].virtual_address) {
2820 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2821 "BM_%d : hwi_init_async_pdu_ctx"
2822 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2823 ulp_num,
2824 mem_descr->mem_array[0].
2825 virtual_address);
2826 } else
2827 beiscsi_log(phba, KERN_WARNING,
2828 BEISCSI_LOG_INIT,
2829 "BM_%d : No Virtual address for ULP : %d\n",
2830 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302831
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002832 pasync_ctx->async_data.ring_base =
2833 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302834
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302835 /* setup data buffer handles */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002836 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2837 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2838 (ulp_num * MEM_DESCR_OFFSET);
2839 if (!mem_descr->mem_array[0].virtual_address)
2840 beiscsi_log(phba, KERN_WARNING,
2841 BEISCSI_LOG_INIT,
2842 "BM_%d : No Virtual address for ULP : %d\n",
2843 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302844
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002845 pasync_ctx->async_data.handle_base =
2846 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002847 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302848
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002849 pasync_header_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302850 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002851 pasync_ctx->async_header.handle_base;
2852 pasync_data_h =
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302853 (struct hd_async_handle *)
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002854 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302855
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302856 /* setup data buffers */
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002857 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2858 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2859 (ulp_num * MEM_DESCR_OFFSET);
2860 if (mem_descr->mem_array[0].virtual_address) {
2861 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2862 "BM_%d : hwi_init_async_pdu_ctx"
2863 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2864 ulp_num,
2865 mem_descr->mem_array[0].
2866 virtual_address);
2867 } else
2868 beiscsi_log(phba, KERN_WARNING,
2869 BEISCSI_LOG_INIT,
2870 "BM_%d : No Virtual address for ULP : %d\n",
2871 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302872
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002873 idx = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302874 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002875 pasync_ctx->async_data.va_base =
2876 mem_descr->mem_array[idx].virtual_address;
2877 pasync_ctx->async_data.pa_base.u.a64.address =
2878 mem_descr->mem_array[idx].
2879 bus_address.u.a64.address;
2880
2881 num_async_data = ((mem_descr->mem_array[idx].size) /
2882 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002883 num_per_mem = 0;
2884
2885 for (index = 0; index < BEISCSI_GET_CID_COUNT
2886 (phba, ulp_num); index++) {
2887 pasync_header_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302888 pasync_header_h->is_header = 1;
2889 pasync_header_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002890 INIT_LIST_HEAD(&pasync_header_h->link);
2891 pasync_header_h->pbuffer =
2892 (void *)((unsigned long)
2893 (pasync_ctx->
2894 async_header.va_base) +
2895 (p->defpdu_hdr_sz * index));
2896
2897 pasync_header_h->pa.u.a64.address =
2898 pasync_ctx->async_header.pa_base.u.a64.
2899 address + (p->defpdu_hdr_sz * index);
2900
2901 list_add_tail(&pasync_header_h->link,
2902 &pasync_ctx->async_header.
2903 free_list);
2904 pasync_header_h++;
2905 pasync_ctx->async_header.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302906 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2907 wq.list);
2908 pasync_ctx->async_entry[index].header = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002909
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002910 pasync_data_h->cri = -1;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302911 pasync_data_h->is_header = 0;
2912 pasync_data_h->index = index;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002913 INIT_LIST_HEAD(&pasync_data_h->link);
2914
2915 if (!num_async_data) {
2916 num_per_mem = 0;
2917 idx++;
2918 pasync_ctx->async_data.va_base =
2919 mem_descr->mem_array[idx].
2920 virtual_address;
2921 pasync_ctx->async_data.pa_base.u.
2922 a64.address =
2923 mem_descr->mem_array[idx].
2924 bus_address.u.a64.address;
2925 num_async_data =
2926 ((mem_descr->mem_array[idx].
2927 size) /
2928 phba->params.defpdu_data_sz);
2929 }
2930 pasync_data_h->pbuffer =
2931 (void *)((unsigned long)
2932 (pasync_ctx->async_data.va_base) +
2933 (p->defpdu_data_sz * num_per_mem));
2934
2935 pasync_data_h->pa.u.a64.address =
2936 pasync_ctx->async_data.pa_base.u.a64.
2937 address + (p->defpdu_data_sz *
2938 num_per_mem);
2939 num_per_mem++;
2940 num_async_data--;
2941
2942 list_add_tail(&pasync_data_h->link,
2943 &pasync_ctx->async_data.
2944 free_list);
2945 pasync_data_h++;
2946 pasync_ctx->async_data.free_entries++;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05302947 pasync_ctx->async_entry[index].data = NULL;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002948 }
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002949 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302950 }
2951
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002952 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302953}
2954
2955static int
2956be_sgl_create_contiguous(void *virtual_address,
2957 u64 physical_address, u32 length,
2958 struct be_dma_mem *sgl)
2959{
2960 WARN_ON(!virtual_address);
2961 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06002962 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302963 WARN_ON(!sgl);
2964
2965 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302966 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302967 sgl->size = length;
2968
2969 return 0;
2970}
2971
2972static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2973{
2974 memset(sgl, 0, sizeof(*sgl));
2975}
2976
2977static void
2978hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2979 struct mem_array *pmem, struct be_dma_mem *sgl)
2980{
2981 if (sgl->va)
2982 be_sgl_destroy_contiguous(sgl);
2983
2984 be_sgl_create_contiguous(pmem->virtual_address,
2985 pmem->bus_address.u.a64.address,
2986 pmem->size, sgl);
2987}
2988
2989static void
2990hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2991 struct mem_array *pmem, struct be_dma_mem *sgl)
2992{
2993 if (sgl->va)
2994 be_sgl_destroy_contiguous(sgl);
2995
2996 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2997 pmem->bus_address.u.a64.address,
2998 pmem->size, sgl);
2999}
3000
3001static int be_fill_queue(struct be_queue_info *q,
3002 u16 len, u16 entry_size, void *vaddress)
3003{
3004 struct be_dma_mem *mem = &q->dma_mem;
3005
3006 memset(q, 0, sizeof(*q));
3007 q->len = len;
3008 q->entry_size = entry_size;
3009 mem->size = len * entry_size;
3010 mem->va = vaddress;
3011 if (!mem->va)
3012 return -ENOMEM;
3013 memset(mem->va, 0, mem->size);
3014 return 0;
3015}
3016
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303017static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303018 struct hwi_context_memory *phwi_context)
3019{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303020 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303021 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303022 struct be_queue_info *eq;
3023 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303024 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303025 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303026
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303027 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3028 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303029
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303030 if (phba->msix_enabled)
3031 eq_for_mcc = 1;
3032 else
3033 eq_for_mcc = 0;
3034 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3035 eq = &phwi_context->be_eq[i].q;
3036 mem = &eq->dma_mem;
3037 phwi_context->be_eq[i].phba = phba;
3038 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3039 num_eq_pages * PAGE_SIZE,
3040 &paddr);
3041 if (!eq_vaddress)
3042 goto create_eq_error;
3043
3044 mem->va = eq_vaddress;
3045 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3046 sizeof(struct be_eq_entry), eq_vaddress);
3047 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303048 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3049 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303050 goto create_eq_error;
3051 }
3052
3053 mem->dma = paddr;
3054 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3055 phwi_context->cur_eqd);
3056 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303057 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3058 "BM_%d : beiscsi_cmd_eq_create"
3059 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303060 goto create_eq_error;
3061 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303062
3063 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3064 "BM_%d : eqid = %d\n",
3065 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303066 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303067 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303068create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303069 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303070 eq = &phwi_context->be_eq[i].q;
3071 mem = &eq->dma_mem;
3072 if (mem->va)
3073 pci_free_consistent(phba->pcidev, num_eq_pages
3074 * PAGE_SIZE,
3075 mem->va, mem->dma);
3076 }
3077 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303078}
3079
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303080static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303081 struct hwi_context_memory *phwi_context)
3082{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303083 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303084 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303085 struct be_queue_info *cq, *eq;
3086 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303087 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303088 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303089 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303090
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303091 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3092 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303093
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303094 for (i = 0; i < phba->num_cpus; i++) {
3095 cq = &phwi_context->be_cq[i];
3096 eq = &phwi_context->be_eq[i].q;
3097 pbe_eq = &phwi_context->be_eq[i];
3098 pbe_eq->cq = cq;
3099 pbe_eq->phba = phba;
3100 mem = &cq->dma_mem;
3101 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3102 num_cq_pages * PAGE_SIZE,
3103 &paddr);
3104 if (!cq_vaddress)
3105 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303106 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303107 sizeof(struct sol_cqe), cq_vaddress);
3108 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303109 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3110 "BM_%d : be_fill_queue Failed "
3111 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303112 goto create_cq_error;
3113 }
3114
3115 mem->dma = paddr;
3116 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3117 false, 0);
3118 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303119 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3120 "BM_%d : beiscsi_cmd_eq_create"
3121 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303122 goto create_cq_error;
3123 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303124 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3125 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3126 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303127 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303128 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303129
3130create_cq_error:
3131 for (i = 0; i < phba->num_cpus; i++) {
3132 cq = &phwi_context->be_cq[i];
3133 mem = &cq->dma_mem;
3134 if (mem->va)
3135 pci_free_consistent(phba->pcidev, num_cq_pages
3136 * PAGE_SIZE,
3137 mem->va, mem->dma);
3138 }
3139 return ret;
3140
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303141}
3142
3143static int
3144beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3145 struct hwi_context_memory *phwi_context,
3146 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003147 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303148{
3149 unsigned int idx;
3150 int ret;
3151 struct be_queue_info *dq, *cq;
3152 struct be_dma_mem *mem;
3153 struct be_mem_descriptor *mem_descr;
3154 void *dq_vaddress;
3155
3156 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003157 dq = &phwi_context->be_def_hdrq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303158 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303159 mem = &dq->dma_mem;
3160 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003161 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3162 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303163 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3164 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3165 sizeof(struct phys_addr),
3166 sizeof(struct phys_addr), dq_vaddress);
3167 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303168 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003169 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3170 ulp_num);
3171
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303172 return ret;
3173 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303174 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3175 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303176 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3177 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003178 phba->params.defpdu_hdr_sz,
3179 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303180 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303181 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003182 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3183 ulp_num);
3184
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303185 return ret;
3186 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303187
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003188 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3189 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3190 ulp_num,
3191 phwi_context->be_def_hdrq[ulp_num].id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303192 return 0;
3193}
3194
3195static int
3196beiscsi_create_def_data(struct beiscsi_hba *phba,
3197 struct hwi_context_memory *phwi_context,
3198 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003199 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303200{
3201 unsigned int idx;
3202 int ret;
3203 struct be_queue_info *dataq, *cq;
3204 struct be_dma_mem *mem;
3205 struct be_mem_descriptor *mem_descr;
3206 void *dq_vaddress;
3207
3208 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003209 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303210 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303211 mem = &dataq->dma_mem;
3212 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003213 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3214 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303215 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3216 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3217 sizeof(struct phys_addr),
3218 sizeof(struct phys_addr), dq_vaddress);
3219 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303220 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003221 "BM_%d : be_fill_queue Failed for DEF PDU "
3222 "DATA on ULP : %d\n",
3223 ulp_num);
3224
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303225 return ret;
3226 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303227 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3228 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303229 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3230 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003231 phba->params.defpdu_data_sz,
3232 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303233 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303234 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3235 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003236 " Failed for DEF PDU DATA on ULP : %d\n",
3237 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303238 return ret;
3239 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303240
John Soni Jose99bc5d52012-08-20 23:00:18 +05303241 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003242 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3243 ulp_num,
3244 phwi_context->be_def_dataq[ulp_num].id);
3245
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003246 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3247 "BM_%d : DEFAULT PDU DATA RING CREATED"
3248 "on ULP : %d\n", ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303249 return 0;
3250}
3251
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003252
3253static int
3254beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3255{
3256 struct be_mem_descriptor *mem_descr;
3257 struct mem_array *pm_arr;
3258 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003259 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003260
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003261 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3262 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3263 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3264 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3265 (ulp_num * MEM_DESCR_OFFSET);
3266 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003267
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003268 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3269 status = be_cmd_iscsi_post_template_hdr(
3270 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003271
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003272 if (status != 0) {
3273 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3274 "BM_%d : Post Template HDR Failed for"
3275 "ULP_%d\n", ulp_num);
3276 return status;
3277 }
3278
3279 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3280 "BM_%d : Template HDR Pages Posted for"
3281 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003282 }
3283 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003284 return 0;
3285}
3286
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303287static int
3288beiscsi_post_pages(struct beiscsi_hba *phba)
3289{
3290 struct be_mem_descriptor *mem_descr;
3291 struct mem_array *pm_arr;
3292 unsigned int page_offset, i;
3293 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003294 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303295
3296 mem_descr = phba->init_mem;
3297 mem_descr += HWI_MEM_SGE;
3298 pm_arr = mem_descr->mem_array;
3299
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003300 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3301 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3302 break;
3303
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303304 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003305 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303306 for (i = 0; i < mem_descr->num_elements; i++) {
3307 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3308 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3309 page_offset,
3310 (pm_arr->size / PAGE_SIZE));
3311 page_offset += pm_arr->size / PAGE_SIZE;
3312 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303313 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3314 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303315 return status;
3316 }
3317 pm_arr++;
3318 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303319 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3320 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303321 return 0;
3322}
3323
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303324static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3325{
3326 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003327 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303328 pci_free_consistent(phba->pcidev, mem->size,
3329 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003330 mem->va = NULL;
3331 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303332}
3333
3334static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3335 u16 len, u16 entry_size)
3336{
3337 struct be_dma_mem *mem = &q->dma_mem;
3338
3339 memset(q, 0, sizeof(*q));
3340 q->len = len;
3341 q->entry_size = entry_size;
3342 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003343 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303344 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303345 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303346 return 0;
3347}
3348
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303349static int
3350beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3351 struct hwi_context_memory *phwi_context,
3352 struct hwi_controller *phwi_ctrlr)
3353{
3354 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3355 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003356 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303357 struct mem_array *pwrb_arr;
3358 void *wrb_vaddr;
3359 struct be_dma_mem sgl;
3360 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003361 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303362 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003363 uint8_t ulp_count = 0, ulp_base_num = 0;
3364 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303365
3366 idx = 0;
3367 mem_descr = phba->init_mem;
3368 mem_descr += HWI_MEM_WRB;
3369 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3370 GFP_KERNEL);
3371 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303372 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3373 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303374 return -ENOMEM;
3375 }
3376 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3377 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3378 num_wrb_rings = mem_descr->mem_array[idx].size /
3379 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3380
3381 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3382 if (num_wrb_rings) {
3383 pwrb_arr[num].virtual_address = wrb_vaddr;
3384 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3385 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3386 sizeof(struct iscsi_wrb);
3387 wrb_vaddr += pwrb_arr[num].size;
3388 pa_addr_lo += pwrb_arr[num].size;
3389 num_wrb_rings--;
3390 } else {
3391 idx++;
3392 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3393 pa_addr_lo = mem_descr->mem_array[idx].\
3394 bus_address.u.a64.address;
3395 num_wrb_rings = mem_descr->mem_array[idx].size /
3396 (phba->params.wrbs_per_cxn *
3397 sizeof(struct iscsi_wrb));
3398 pwrb_arr[num].virtual_address = wrb_vaddr;
3399 pwrb_arr[num].bus_address.u.a64.address\
3400 = pa_addr_lo;
3401 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3402 sizeof(struct iscsi_wrb);
3403 wrb_vaddr += pwrb_arr[num].size;
3404 pa_addr_lo += pwrb_arr[num].size;
3405 num_wrb_rings--;
3406 }
3407 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003408
3409 /* Get the ULP Count */
3410 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3411 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3412 ulp_count++;
3413 ulp_base_num = ulp_num;
3414 cid_count_ulp[ulp_num] =
3415 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3416 }
3417
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303418 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3419 wrb_mem_index = 0;
3420 offset = 0;
3421 size = 0;
3422
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003423 if (ulp_count > 1) {
3424 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3425
3426 if (!cid_count_ulp[ulp_base_num])
3427 ulp_base_num = (ulp_base_num + 1) %
3428 BEISCSI_ULP_COUNT;
3429
3430 cid_count_ulp[ulp_base_num]--;
3431 }
3432
3433
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303434 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3435 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003436 &phwi_context->be_wrbq[i],
3437 &phwi_ctrlr->wrb_context[i],
3438 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303439 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303440 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3441 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003442 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303443 return status;
3444 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003445 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003446 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303447 }
3448 kfree(pwrb_arr);
3449 return 0;
3450}
3451
3452static void free_wrb_handles(struct beiscsi_hba *phba)
3453{
3454 unsigned int index;
3455 struct hwi_controller *phwi_ctrlr;
3456 struct hwi_wrb_context *pwrb_context;
3457
3458 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003459 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303460 pwrb_context = &phwi_ctrlr->wrb_context[index];
3461 kfree(pwrb_context->pwrb_handle_base);
3462 kfree(pwrb_context->pwrb_handle_basestd);
3463 }
3464}
3465
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303466static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3467{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303468 struct be_ctrl_info *ctrl = &phba->ctrl;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303469 struct be_dma_mem *ptag_mem;
3470 struct be_queue_info *q;
3471 int i, tag;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303472
3473 q = &phba->ctrl.mcc_obj.q;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303474 for (i = 0; i < MAX_MCC_CMD; i++) {
3475 tag = i + 1;
3476 if (!test_bit(MCC_TAG_STATE_RUNNING,
3477 &ctrl->ptag_state[tag].tag_state))
3478 continue;
3479
3480 if (test_bit(MCC_TAG_STATE_TIMEOUT,
3481 &ctrl->ptag_state[tag].tag_state)) {
3482 ptag_mem = &ctrl->ptag_state[tag].tag_mem_state;
3483 if (ptag_mem->size) {
3484 pci_free_consistent(ctrl->pdev,
3485 ptag_mem->size,
3486 ptag_mem->va,
3487 ptag_mem->dma);
3488 ptag_mem->size = 0;
3489 }
3490 continue;
3491 }
3492 /**
3493 * If MCC is still active and waiting then wake up the process.
3494 * We are here only because port is going offline. The process
3495 * sees that (BEISCSI_HBA_ONLINE is cleared) and EIO error is
3496 * returned for the operation and allocated memory cleaned up.
3497 */
3498 if (waitqueue_active(&ctrl->mcc_wait[tag])) {
3499 ctrl->mcc_tag_status[tag] = MCC_STATUS_FAILED;
3500 ctrl->mcc_tag_status[tag] |= CQE_VALID_MASK;
3501 wake_up_interruptible(&ctrl->mcc_wait[tag]);
3502 /*
3503 * Control tag info gets reinitialized in enable
3504 * so wait for the process to clear running state.
3505 */
3506 while (test_bit(MCC_TAG_STATE_RUNNING,
3507 &ctrl->ptag_state[tag].tag_state))
3508 schedule_timeout_uninterruptible(HZ);
3509 }
3510 /**
3511 * For MCC with tag_states MCC_TAG_STATE_ASYNC and
3512 * MCC_TAG_STATE_IGNORE nothing needs to done.
3513 */
3514 }
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303515 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303516 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303517 be_queue_free(phba, q);
3518 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303519
3520 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303521 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303522 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303523 be_queue_free(phba, q);
3524 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303525}
3526
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303527static int be_mcc_queues_create(struct beiscsi_hba *phba,
3528 struct hwi_context_memory *phwi_context)
3529{
3530 struct be_queue_info *q, *cq;
3531 struct be_ctrl_info *ctrl = &phba->ctrl;
3532
3533 /* Alloc MCC compl queue */
3534 cq = &phba->ctrl.mcc_obj.cq;
3535 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3536 sizeof(struct be_mcc_compl)))
3537 goto err;
3538 /* Ask BE to create MCC compl queue; */
3539 if (phba->msix_enabled) {
3540 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3541 [phba->num_cpus].q, false, true, 0))
3542 goto mcc_cq_free;
3543 } else {
3544 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3545 false, true, 0))
3546 goto mcc_cq_free;
3547 }
3548
3549 /* Alloc MCC queue */
3550 q = &phba->ctrl.mcc_obj.q;
3551 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3552 goto mcc_cq_destroy;
3553
3554 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303555 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303556 goto mcc_q_free;
3557
3558 return 0;
3559
3560mcc_q_free:
3561 be_queue_free(phba, q);
3562mcc_cq_destroy:
3563 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3564mcc_cq_free:
3565 be_queue_free(phba, cq);
3566err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303567 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303568}
3569
John Soni Jose107dfcb2012-10-20 04:42:13 +05303570/**
3571 * find_num_cpus()- Get the CPU online count
3572 * @phba: ptr to priv structure
3573 *
3574 * CPU count is used for creating EQ.
3575 **/
3576static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303577{
3578 int num_cpus = 0;
3579
3580 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303581
John Soni Jose22abeef2012-10-20 04:43:32 +05303582 switch (phba->generation) {
3583 case BE_GEN2:
3584 case BE_GEN3:
3585 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3586 BEISCSI_MAX_NUM_CPUS : num_cpus;
3587 break;
3588 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003589 /*
3590 * If eqid_count == 1 fall back to
3591 * INTX mechanism
3592 **/
3593 if (phba->fw_config.eqid_count == 1) {
3594 enable_msix = 0;
3595 phba->num_cpus = 1;
3596 return;
3597 }
3598
3599 phba->num_cpus =
3600 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3601 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303602 break;
3603 default:
3604 phba->num_cpus = 1;
3605 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303606}
3607
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303608static void hwi_purge_eq(struct beiscsi_hba *phba)
3609{
3610 struct hwi_controller *phwi_ctrlr;
3611 struct hwi_context_memory *phwi_context;
3612 struct be_queue_info *eq;
3613 struct be_eq_entry *eqe = NULL;
3614 int i, eq_msix;
3615 unsigned int num_processed;
3616
3617 if (beiscsi_hba_in_error(phba))
3618 return;
3619
3620 phwi_ctrlr = phba->phwi_ctrlr;
3621 phwi_context = phwi_ctrlr->phwi_ctxt;
3622 if (phba->msix_enabled)
3623 eq_msix = 1;
3624 else
3625 eq_msix = 0;
3626
3627 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3628 eq = &phwi_context->be_eq[i].q;
3629 eqe = queue_tail_node(eq);
3630 num_processed = 0;
3631 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3632 & EQE_VALID_MASK) {
3633 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3634 queue_tail_inc(eq);
3635 eqe = queue_tail_node(eq);
3636 num_processed++;
3637 }
3638
3639 if (num_processed)
3640 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
3641 }
3642}
3643
3644static void hwi_cleanup_port(struct beiscsi_hba *phba)
3645{
3646 struct be_queue_info *q;
3647 struct be_ctrl_info *ctrl = &phba->ctrl;
3648 struct hwi_controller *phwi_ctrlr;
3649 struct hwi_context_memory *phwi_context;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303650 struct hd_async_context *pasync_ctx;
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303651 int i, eq_for_mcc, ulp_num;
3652
3653 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3654 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3655 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
3656
3657 /**
3658 * Purge all EQ entries that may have been left out. This is to
3659 * workaround a problem we've seen occasionally where driver gets an
3660 * interrupt with EQ entry bit set after stopping the controller.
3661 */
3662 hwi_purge_eq(phba);
3663
3664 phwi_ctrlr = phba->phwi_ctrlr;
3665 phwi_context = phwi_ctrlr->phwi_ctxt;
3666
3667 be_cmd_iscsi_remove_template_hdr(ctrl);
3668
3669 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3670 q = &phwi_context->be_wrbq[i];
3671 if (q->created)
3672 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3673 }
3674 kfree(phwi_context->be_wrbq);
3675 free_wrb_handles(phba);
3676
3677 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3678 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3679
3680 q = &phwi_context->be_def_hdrq[ulp_num];
3681 if (q->created)
3682 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3683
3684 q = &phwi_context->be_def_dataq[ulp_num];
3685 if (q->created)
3686 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3687
3688 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3689 }
3690 }
3691
3692 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3693
3694 for (i = 0; i < (phba->num_cpus); i++) {
3695 q = &phwi_context->be_cq[i];
3696 if (q->created) {
3697 be_queue_free(phba, q);
3698 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3699 }
3700 }
3701
3702 be_mcc_queues_destroy(phba);
3703 if (phba->msix_enabled)
3704 eq_for_mcc = 1;
3705 else
3706 eq_for_mcc = 0;
3707 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3708 q = &phwi_context->be_eq[i].q;
3709 if (q->created) {
3710 be_queue_free(phba, q);
3711 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3712 }
3713 }
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303714 /* this ensures complete FW cleanup */
3715 beiscsi_cmd_function_reset(phba);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05303716 /* last communication, indicate driver is unloading */
3717 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
3718}
Jitendra Bhivare4ee1ec42016-08-19 15:20:20 +05303719
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303720static int hwi_init_port(struct beiscsi_hba *phba)
3721{
3722 struct hwi_controller *phwi_ctrlr;
3723 struct hwi_context_memory *phwi_context;
3724 unsigned int def_pdu_ring_sz;
3725 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003726 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303727
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303728 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303729 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003730 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303731 phwi_context->min_eqd = 0;
Jitendra Bhivare1b7a7dd2016-08-19 15:20:23 +05303732 phwi_context->cur_eqd = 32;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303733 /* set port optic state to unknown */
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303734 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303735
3736 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303737 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303738 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3739 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303740 goto error;
3741 }
3742
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303743 status = be_mcc_queues_create(phba, phwi_context);
3744 if (status != 0)
3745 goto error;
3746
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303747 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303748 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303749 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3750 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303751 goto error;
3752 }
3753
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303754 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303755 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303756 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3757 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303758 goto error;
3759 }
3760
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003761 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3762 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003763 def_pdu_ring_sz =
3764 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3765 sizeof(struct phys_addr);
3766
3767 status = beiscsi_create_def_hdr(phba, phwi_context,
3768 phwi_ctrlr,
3769 def_pdu_ring_sz,
3770 ulp_num);
3771 if (status != 0) {
3772 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3773 "BM_%d : Default Header not created for ULP : %d\n",
3774 ulp_num);
3775 goto error;
3776 }
3777
3778 status = beiscsi_create_def_data(phba, phwi_context,
3779 phwi_ctrlr,
3780 def_pdu_ring_sz,
3781 ulp_num);
3782 if (status != 0) {
3783 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3784 "BM_%d : Default Data not created for ULP : %d\n",
3785 ulp_num);
3786 goto error;
3787 }
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303788 /**
3789 * Now that the default PDU rings have been created,
3790 * let EP know about it.
3791 * Call beiscsi_cmd_iscsi_cleanup before posting?
3792 */
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303793 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3794 ulp_num);
3795 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3796 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003797 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303798 }
3799
3800 status = beiscsi_post_pages(phba);
3801 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303802 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3803 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303804 goto error;
3805 }
3806
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003807 status = beiscsi_post_template_hdr(phba);
3808 if (status != 0) {
3809 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3810 "BM_%d : Template HDR Posting for CXN Failed\n");
3811 }
3812
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303813 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3814 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303815 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3816 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303817 goto error;
3818 }
3819
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003820 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3821 uint16_t async_arr_idx = 0;
3822
3823 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3824 uint16_t cri = 0;
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303825 struct hd_async_context *pasync_ctx;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003826
3827 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3828 phwi_ctrlr, ulp_num);
3829 for (cri = 0; cri <
3830 phba->params.cxns_per_ctrl; cri++) {
3831 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3832 (phwi_ctrlr, cri))
3833 pasync_ctx->cid_to_async_cri_map[
3834 phwi_ctrlr->wrb_context[cri].cid] =
3835 async_arr_idx++;
3836 }
Jitendra Bhivare938f3722016-08-19 15:20:21 +05303837 /**
3838 * Now that the default PDU rings have been created,
3839 * let EP know about it.
3840 */
3841 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_HDR,
3842 ulp_num);
3843 beiscsi_hdq_post_handles(phba, BEISCSI_DEFQ_DATA,
3844 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003845 }
3846 }
3847
John Soni Jose99bc5d52012-08-20 23:00:18 +05303848 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3849 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303850 return 0;
3851
3852error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303853 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3854 "BM_%d : hwi_init_port failed");
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303855 hwi_cleanup_port(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003856 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303857}
3858
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303859static int hwi_init_controller(struct beiscsi_hba *phba)
3860{
3861 struct hwi_controller *phwi_ctrlr;
3862
3863 phwi_ctrlr = phba->phwi_ctrlr;
3864 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3865 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3866 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303867 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3868 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3869 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303870 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303871 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3872 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3873 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303874 return -ENOMEM;
3875 }
3876
3877 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003878 if (beiscsi_init_wrb_handle(phba))
3879 return -ENOMEM;
3880
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003881 if (hwi_init_async_pdu_ctx(phba)) {
3882 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3883 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3884 return -ENOMEM;
3885 }
3886
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303887 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303888 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3889 "BM_%d : hwi_init_controller failed\n");
3890
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303891 return -ENOMEM;
3892 }
3893 return 0;
3894}
3895
3896static void beiscsi_free_mem(struct beiscsi_hba *phba)
3897{
3898 struct be_mem_descriptor *mem_descr;
3899 int i, j;
3900
3901 mem_descr = phba->init_mem;
3902 i = 0;
3903 j = 0;
3904 for (i = 0; i < SE_MEM_MAX; i++) {
3905 for (j = mem_descr->num_elements; j > 0; j--) {
3906 pci_free_consistent(phba->pcidev,
3907 mem_descr->mem_array[j - 1].size,
3908 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303909 (unsigned long)mem_descr->mem_array[j - 1].
3910 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303911 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003912
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303913 kfree(mem_descr->mem_array);
3914 mem_descr++;
3915 }
3916 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003917 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303918 kfree(phba->phwi_ctrlr);
3919}
3920
3921static int beiscsi_init_controller(struct beiscsi_hba *phba)
3922{
3923 int ret = -ENOMEM;
3924
3925 ret = beiscsi_get_memory(phba);
3926 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303927 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3928 "BM_%d : beiscsi_dev_probe -"
3929 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303930 return ret;
3931 }
3932
3933 ret = hwi_init_controller(phba);
3934 if (ret)
3935 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303936 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3937 "BM_%d : Return success from beiscsi_init_controller");
3938
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303939 return 0;
3940
3941free_init:
3942 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003943 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303944}
3945
3946static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3947{
3948 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3949 struct sgl_handle *psgl_handle;
3950 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003951 unsigned int arr_index, i, idx;
3952 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303953
3954 phba->io_sgl_hndl_avbl = 0;
3955 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303956
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303957 mem_descr_sglh = phba->init_mem;
3958 mem_descr_sglh += HWI_MEM_SGLH;
3959 if (1 == mem_descr_sglh->num_elements) {
3960 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3961 phba->params.ios_per_ctrl,
3962 GFP_KERNEL);
3963 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303964 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3965 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303966 return -ENOMEM;
3967 }
3968 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3969 (phba->params.icds_per_ctrl -
3970 phba->params.ios_per_ctrl),
3971 GFP_KERNEL);
3972 if (!phba->eh_sgl_hndl_base) {
3973 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303974 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3975 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303976 return -ENOMEM;
3977 }
3978 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303979 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3980 "BM_%d : HWI_MEM_SGLH is more than one element."
3981 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303982 return -ENOMEM;
3983 }
3984
3985 arr_index = 0;
3986 idx = 0;
3987 while (idx < mem_descr_sglh->num_elements) {
3988 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3989
3990 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3991 sizeof(struct sgl_handle)); i++) {
3992 if (arr_index < phba->params.ios_per_ctrl) {
3993 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3994 phba->io_sgl_hndl_avbl++;
3995 arr_index++;
3996 } else {
3997 phba->eh_sgl_hndl_base[arr_index -
3998 phba->params.ios_per_ctrl] =
3999 psgl_handle;
4000 arr_index++;
4001 phba->eh_sgl_hndl_avbl++;
4002 }
4003 psgl_handle++;
4004 }
4005 idx++;
4006 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304007 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4008 "BM_%d : phba->io_sgl_hndl_avbl=%d"
4009 "phba->eh_sgl_hndl_avbl=%d\n",
4010 phba->io_sgl_hndl_avbl,
4011 phba->eh_sgl_hndl_avbl);
4012
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304013 mem_descr_sg = phba->init_mem;
4014 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304015 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4016 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
4017 mem_descr_sg->num_elements);
4018
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004019 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4020 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
4021 break;
4022
4023 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
4024
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304025 arr_index = 0;
4026 idx = 0;
4027 while (idx < mem_descr_sg->num_elements) {
4028 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
4029
4030 for (i = 0;
4031 i < (mem_descr_sg->mem_array[idx].size) /
4032 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
4033 i++) {
4034 if (arr_index < phba->params.ios_per_ctrl)
4035 psgl_handle = phba->io_sgl_hndl_base[arr_index];
4036 else
4037 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
4038 phba->params.ios_per_ctrl];
4039 psgl_handle->pfrag = pfrag;
4040 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4041 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4042 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004043 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304044 }
4045 idx++;
4046 }
4047 phba->io_sgl_free_index = 0;
4048 phba->io_sgl_alloc_index = 0;
4049 phba->eh_sgl_free_index = 0;
4050 phba->eh_sgl_alloc_index = 0;
4051 return 0;
4052}
4053
4054static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4055{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004056 int ret;
4057 uint16_t i, ulp_num;
4058 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304059
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004060 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4061 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4062 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4063 GFP_KERNEL);
4064
4065 if (!ptr_cid_info) {
4066 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4067 "BM_%d : Failed to allocate memory"
4068 "for ULP_CID_INFO for ULP : %d\n",
4069 ulp_num);
4070 ret = -ENOMEM;
4071 goto free_memory;
4072
4073 }
4074
4075 /* Allocate memory for CID array */
4076 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4077 BEISCSI_GET_CID_COUNT(phba,
4078 ulp_num), GFP_KERNEL);
4079 if (!ptr_cid_info->cid_array) {
4080 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4081 "BM_%d : Failed to allocate memory"
4082 "for CID_ARRAY for ULP : %d\n",
4083 ulp_num);
4084 kfree(ptr_cid_info);
4085 ptr_cid_info = NULL;
4086 ret = -ENOMEM;
4087
4088 goto free_memory;
4089 }
4090 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4091 phba, ulp_num);
4092
4093 /* Save the cid_info_array ptr */
4094 phba->cid_array_info[ulp_num] = ptr_cid_info;
4095 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304096 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304097 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004098 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304099 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304100 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4101 "BM_%d : Failed to allocate memory in "
4102 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004103 ret = -ENOMEM;
4104
4105 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304106 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004107
4108 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4109 phba->params.cxns_per_ctrl, GFP_KERNEL);
4110 if (!phba->conn_table) {
4111 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4112 "BM_%d : Failed to allocate memory in"
4113 "hba_setup_cid_tbls\n");
4114
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004115 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004116 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004117 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004118
4119 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304120 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004121
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004122 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4123 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004124
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004125 ptr_cid_info = phba->cid_array_info[ulp_num];
4126 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4127 phba->phwi_ctrlr->wrb_context[i].cid;
4128
4129 }
4130
4131 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4132 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4133 ptr_cid_info = phba->cid_array_info[ulp_num];
4134
4135 ptr_cid_info->cid_alloc = 0;
4136 ptr_cid_info->cid_free = 0;
4137 }
4138 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304139 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004140
4141free_memory:
4142 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4143 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4144 ptr_cid_info = phba->cid_array_info[ulp_num];
4145
4146 if (ptr_cid_info) {
4147 kfree(ptr_cid_info->cid_array);
4148 kfree(ptr_cid_info);
4149 phba->cid_array_info[ulp_num] = NULL;
4150 }
4151 }
4152 }
4153
4154 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304155}
4156
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304157static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304158{
4159 struct be_ctrl_info *ctrl = &phba->ctrl;
4160 struct hwi_controller *phwi_ctrlr;
4161 struct hwi_context_memory *phwi_context;
4162 struct be_queue_info *eq;
4163 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304164 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304165 u32 enabled;
4166
4167 phwi_ctrlr = phba->phwi_ctrlr;
4168 phwi_context = phwi_ctrlr->phwi_ctxt;
4169
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304170 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4171 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4172 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304173
4174 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4175 if (!enabled) {
4176 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304177 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4178 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304179 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004180 }
4181
4182 if (!phba->msix_enabled) {
4183 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304184 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4185 "BM_%d : eq->id=%d\n", eq->id);
4186
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004187 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4188 } else {
4189 for (i = 0; i <= phba->num_cpus; i++) {
4190 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304191 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4192 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304193 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4194 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304195 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304196}
4197
4198static void hwi_disable_intr(struct beiscsi_hba *phba)
4199{
4200 struct be_ctrl_info *ctrl = &phba->ctrl;
4201
4202 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4203 u32 reg = ioread32(addr);
4204
4205 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4206 if (enabled) {
4207 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4208 iowrite32(reg, addr);
4209 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304210 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4211 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304212}
4213
4214static int beiscsi_init_port(struct beiscsi_hba *phba)
4215{
4216 int ret;
4217
4218 ret = beiscsi_init_controller(phba);
4219 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304220 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4221 "BM_%d : beiscsi_dev_probe - Failed in"
4222 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304223 return ret;
4224 }
4225 ret = beiscsi_init_sgl_handle(phba);
4226 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304227 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4228 "BM_%d : beiscsi_dev_probe - Failed in"
4229 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304230 goto do_cleanup_ctrlr;
4231 }
4232
4233 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304234 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4235 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304236 kfree(phba->io_sgl_hndl_base);
4237 kfree(phba->eh_sgl_hndl_base);
4238 goto do_cleanup_ctrlr;
4239 }
4240
4241 return ret;
4242
4243do_cleanup_ctrlr:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304244 hwi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304245 return ret;
4246}
4247
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304248static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304249{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004250 struct ulp_cid_info *ptr_cid_info = NULL;
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304251 int ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304252
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304253 kfree(phba->io_sgl_hndl_base);
4254 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304255 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004256 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004257
4258 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4259 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4260 ptr_cid_info = phba->cid_array_info[ulp_num];
4261
4262 if (ptr_cid_info) {
4263 kfree(ptr_cid_info->cid_array);
4264 kfree(ptr_cid_info);
4265 phba->cid_array_info[ulp_num] = NULL;
4266 }
4267 }
4268 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304269}
4270
John Soni Josed629c472012-10-20 04:42:00 +05304271/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004272 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4273 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004274 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004275 *
4276 * Free driver mgmt resources binded to CXN.
4277 **/
4278void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004279beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4280 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004281{
4282 struct beiscsi_io_task *io_task;
4283 struct beiscsi_hba *phba = beiscsi_conn->phba;
4284 struct hwi_wrb_context *pwrb_context;
4285 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004286 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4287 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004288
4289 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004290 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4291
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004292 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004293
4294 if (io_task->pwrb_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304295 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004296 io_task->pwrb_handle = NULL;
4297 }
4298
4299 if (io_task->psgl_handle) {
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304300 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004301 io_task->psgl_handle = NULL;
4302 }
4303
John Soni Joseeb1c4692015-04-25 08:17:45 +05304304 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004305 pci_unmap_single(phba->pcidev,
4306 io_task->mtask_addr,
4307 io_task->mtask_data_count,
4308 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304309 io_task->mtask_addr = 0;
4310 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004311}
4312
4313/**
John Soni Josed629c472012-10-20 04:42:00 +05304314 * beiscsi_cleanup_task()- Free driver resources of the task
4315 * @task: ptr to the iscsi task
4316 *
4317 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004318static void beiscsi_cleanup_task(struct iscsi_task *task)
4319{
4320 struct beiscsi_io_task *io_task = task->dd_data;
4321 struct iscsi_conn *conn = task->conn;
4322 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4323 struct beiscsi_hba *phba = beiscsi_conn->phba;
4324 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4325 struct hwi_wrb_context *pwrb_context;
4326 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004327 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4328 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004329
4330 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004331 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004332
4333 if (io_task->cmd_bhs) {
4334 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4335 io_task->bhs_pa.u.a64.address);
4336 io_task->cmd_bhs = NULL;
Jitendra Bhivaree1f9d312016-08-19 15:20:22 +05304337 task->hdr = NULL;
Mike Christie1282ab72012-04-18 03:06:00 -05004338 }
4339
4340 if (task->sc) {
4341 if (io_task->pwrb_handle) {
4342 free_wrb_handle(phba, pwrb_context,
4343 io_task->pwrb_handle);
4344 io_task->pwrb_handle = NULL;
4345 }
4346
4347 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004348 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004349 io_task->psgl_handle = NULL;
4350 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004351
4352 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304353 if (io_task->num_sg)
4354 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004355 io_task->scsi_cmnd = NULL;
4356 }
Mike Christie1282ab72012-04-18 03:06:00 -05004357 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004358 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004359 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004360 }
4361}
4362
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304363void
4364beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4365 struct beiscsi_offload_params *params)
4366{
4367 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304368 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304369 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004370 struct iscsi_task *task = beiscsi_conn->task;
4371 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304372 u32 doorbell = 0;
4373
4374 /*
4375 * We can always use 0 here because it is reserved by libiscsi for
4376 * login/startup related tasks.
4377 */
Mike Christie1282ab72012-04-18 03:06:00 -05004378 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004379 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004380 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004381 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004382
John Soni Jose340c99e2015-08-20 04:44:30 +05304383 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4384 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304385
John Soni Joseacb96932012-10-20 04:44:35 +05304386 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004387 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304388 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304389 phba->init_mem,
4390 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004391 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304392 beiscsi_offload_cxn_v2(params, pwrb_handle,
4393 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304394
John Soni Joseacb96932012-10-20 04:44:35 +05304395 be_dws_le_to_cpu(pwrb_handle->pwrb,
4396 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304397
4398 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304399 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304400 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304401 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004402 iowrite32(doorbell, phba->db_va +
4403 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304404
4405 /*
4406 * There is no completion for CONTEXT_UPDATE. The completion of next
4407 * WRB posted guarantees FW's processing and DMA'ing of it.
4408 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4409 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4410 */
4411 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4412 phba->params.wrbs_per_cxn);
4413 beiscsi_log(phba, KERN_INFO,
4414 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4415 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4416 pwrb_handle, pwrb_context->free_index,
4417 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304418}
4419
4420static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4421 int *index, int *age)
4422{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304423 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304424 if (age)
4425 *age = conn->session->age;
4426}
4427
4428/**
4429 * beiscsi_alloc_pdu - allocates pdu and related resources
4430 * @task: libiscsi task
4431 * @opcode: opcode of pdu for task
4432 *
4433 * This is called with the session lock held. It will allocate
4434 * the wrb and sgl if needed for the command. And it will prep
4435 * the pdu's itt. beiscsi_parse_pdu will later translate
4436 * the pdu itt to the libiscsi task itt.
4437 */
4438static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4439{
4440 struct beiscsi_io_task *io_task = task->dd_data;
4441 struct iscsi_conn *conn = task->conn;
4442 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4443 struct beiscsi_hba *phba = beiscsi_conn->phba;
4444 struct hwi_wrb_context *pwrb_context;
4445 struct hwi_controller *phwi_ctrlr;
4446 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004447 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304448 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4449 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304450
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304451 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004452 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304453 if (!io_task->cmd_bhs)
4454 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304455 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304456 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304457 io_task->conn = beiscsi_conn;
4458
4459 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4460 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304461 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004462 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304463
4464 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304465 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304466 if (!io_task->psgl_handle) {
4467 beiscsi_log(phba, KERN_ERR,
4468 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4469 "BM_%d : Alloc of IO_SGL_ICD Failed"
4470 "for the CID : %d\n",
4471 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304472 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304473 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304474 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304475 beiscsi_conn->beiscsi_conn_cid,
4476 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304477 if (!io_task->pwrb_handle) {
4478 beiscsi_log(phba, KERN_ERR,
4479 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4480 "BM_%d : Alloc of WRB_HANDLE Failed"
4481 "for the CID : %d\n",
4482 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304483 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304484 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304485 } else {
4486 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304487 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004488 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304489 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304490 io_task->psgl_handle = (struct sgl_handle *)
4491 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304492 if (!io_task->psgl_handle) {
4493 beiscsi_log(phba, KERN_ERR,
4494 BEISCSI_LOG_IO |
4495 BEISCSI_LOG_CONFIG,
4496 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4497 "for the CID : %d\n",
4498 beiscsi_conn->
4499 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304500 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304501 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304502
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304503 beiscsi_conn->login_in_progress = 1;
4504 beiscsi_conn->plogin_sgl_handle =
4505 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304506 io_task->pwrb_handle =
4507 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304508 beiscsi_conn->beiscsi_conn_cid,
4509 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304510 if (!io_task->pwrb_handle) {
4511 beiscsi_log(phba, KERN_ERR,
4512 BEISCSI_LOG_IO |
4513 BEISCSI_LOG_CONFIG,
4514 "BM_%d : Alloc of WRB_HANDLE Failed"
4515 "for the CID : %d\n",
4516 beiscsi_conn->
4517 beiscsi_conn_cid);
4518 goto free_mgmt_hndls;
4519 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304520 beiscsi_conn->plogin_wrb_handle =
4521 io_task->pwrb_handle;
4522
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304523 } else {
4524 io_task->psgl_handle =
4525 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304526 io_task->pwrb_handle =
4527 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304528 }
4529 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304530 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304531 if (!io_task->psgl_handle) {
4532 beiscsi_log(phba, KERN_ERR,
4533 BEISCSI_LOG_IO |
4534 BEISCSI_LOG_CONFIG,
4535 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4536 "for the CID : %d\n",
4537 beiscsi_conn->
4538 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304539 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304540 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304541 io_task->pwrb_handle =
4542 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304543 beiscsi_conn->beiscsi_conn_cid,
4544 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304545 if (!io_task->pwrb_handle) {
4546 beiscsi_log(phba, KERN_ERR,
4547 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4548 "BM_%d : Alloc of WRB_HANDLE Failed"
4549 "for the CID : %d\n",
4550 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304551 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304552 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304553
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304554 }
4555 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304556 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4557 wrb_index << 16) | (unsigned int)
4558 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304559 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304560
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304561 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4562 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304563
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304564free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304565 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304566 goto free_hndls;
4567free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304568 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004569 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304570free_hndls:
4571 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004572 cri_index = BE_GET_CRI_FROM_CID(
4573 beiscsi_conn->beiscsi_conn_cid);
4574 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304575 if (io_task->pwrb_handle)
4576 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304577 io_task->pwrb_handle = NULL;
4578 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4579 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004580 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304581 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304582}
John Soni Jose09a10932012-10-20 04:44:23 +05304583int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4584 unsigned int num_sg, unsigned int xferlen,
4585 unsigned int writedir)
4586{
4587
4588 struct beiscsi_io_task *io_task = task->dd_data;
4589 struct iscsi_conn *conn = task->conn;
4590 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4591 struct beiscsi_hba *phba = beiscsi_conn->phba;
4592 struct iscsi_wrb *pwrb = NULL;
4593 unsigned int doorbell = 0;
4594
4595 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304596
John Soni Jose09a10932012-10-20 04:44:23 +05304597 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4598
4599 if (writedir) {
4600 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4601 INI_WR_CMD);
4602 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4603 } else {
4604 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4605 INI_RD_CMD);
4606 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4607 }
4608
4609 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4610 type, pwrb);
4611
4612 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4613 cpu_to_be16(*(unsigned short *)
4614 &io_task->cmd_bhs->iscsi_hdr.lun));
4615 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4616 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4617 io_task->pwrb_handle->wrb_index);
4618 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4619 be32_to_cpu(task->cmdsn));
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4621 io_task->psgl_handle->sgl_index);
4622
4623 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4624 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304625 io_task->pwrb_handle->wrb_index);
4626 if (io_task->pwrb_context->plast_wrb)
4627 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4628 io_task->pwrb_context->plast_wrb,
4629 io_task->pwrb_handle->wrb_index);
4630 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304631
4632 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4633
4634 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4635 doorbell |= (io_task->pwrb_handle->wrb_index &
4636 DB_DEF_PDU_WRB_INDEX_MASK) <<
4637 DB_DEF_PDU_WRB_INDEX_SHIFT;
4638 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004639 iowrite32(doorbell, phba->db_va +
4640 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304641 return 0;
4642}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304643
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304644static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4645 unsigned int num_sg, unsigned int xferlen,
4646 unsigned int writedir)
4647{
4648
4649 struct beiscsi_io_task *io_task = task->dd_data;
4650 struct iscsi_conn *conn = task->conn;
4651 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4652 struct beiscsi_hba *phba = beiscsi_conn->phba;
4653 struct iscsi_wrb *pwrb = NULL;
4654 unsigned int doorbell = 0;
4655
4656 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304657 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4658
4659 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304660 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4661 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304662 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304663 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304664 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4665 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304666 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4667 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304668
John Soni Jose09a10932012-10-20 04:44:23 +05304669 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4670 type, pwrb);
4671
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304672 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004673 cpu_to_be16(*(unsigned short *)
4674 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304675 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4676 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4677 io_task->pwrb_handle->wrb_index);
4678 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4679 be32_to_cpu(task->cmdsn));
4680 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4681 io_task->psgl_handle->sgl_index);
4682
4683 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4684
4685 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304686 io_task->pwrb_handle->wrb_index);
4687 if (io_task->pwrb_context->plast_wrb)
4688 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4689 io_task->pwrb_context->plast_wrb,
4690 io_task->pwrb_handle->wrb_index);
4691 io_task->pwrb_context->plast_wrb = pwrb;
4692
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304693 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4694
4695 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304696 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304697 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4698 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4699
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004700 iowrite32(doorbell, phba->db_va +
4701 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304702 return 0;
4703}
4704
4705static int beiscsi_mtask(struct iscsi_task *task)
4706{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304707 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304708 struct iscsi_conn *conn = task->conn;
4709 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4710 struct beiscsi_hba *phba = beiscsi_conn->phba;
4711 struct iscsi_wrb *pwrb = NULL;
4712 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304713 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304714 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004715 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304716
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304717 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304718 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304719 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304720
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004721 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304722 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4723 be32_to_cpu(task->cmdsn));
4724 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4725 io_task->pwrb_handle->wrb_index);
4726 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4727 io_task->psgl_handle->sgl_index);
4728 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4729 task->data_count);
4730 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304731 io_task->pwrb_handle->wrb_index);
4732 if (io_task->pwrb_context->plast_wrb)
4733 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4734 io_task->pwrb_context->plast_wrb,
4735 io_task->pwrb_handle->wrb_index);
4736 io_task->pwrb_context->plast_wrb = pwrb;
4737
John Soni Jose09a10932012-10-20 04:44:23 +05304738 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004739 } else {
4740 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4741 be32_to_cpu(task->cmdsn));
4742 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4743 io_task->pwrb_handle->wrb_index);
4744 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4745 io_task->psgl_handle->sgl_index);
4746 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4747 task->data_count);
4748 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304749 io_task->pwrb_handle->wrb_index);
4750 if (io_task->pwrb_context->plast_wrb)
4751 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4752 io_task->pwrb_context->plast_wrb,
4753 io_task->pwrb_handle->wrb_index);
4754 io_task->pwrb_context->plast_wrb = pwrb;
4755
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004756 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304757 }
4758
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304759
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304760 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4761 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304762 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304763 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004764 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304765 break;
4766 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004767 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304768 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004769 if (is_chip_be2_be3r(phba))
4770 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304771 dmsg, pwrb, 1);
4772 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004773 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304774 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004775 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304776 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004777 if (is_chip_be2_be3r(phba))
4778 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304779 dmsg, pwrb, 0);
4780 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004781 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304782 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004783 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004784 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304785 break;
4786 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304787 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004788 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304789 break;
4790 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304791 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004792 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304793 break;
4794 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304795 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004796 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304797 break;
4798
4799 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304800 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4801 "BM_%d : opcode =%d Not supported\n",
4802 task->hdr->opcode & ISCSI_OPCODE_MASK);
4803
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304804 return -EINVAL;
4805 }
4806
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004807 if (ret)
4808 return ret;
4809
John Soni Jose09a10932012-10-20 04:44:23 +05304810 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004811 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4812 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4813 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304814
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304815 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304816 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304817 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4818 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004819 iowrite32(doorbell, phba->db_va +
4820 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304821 return 0;
4822}
4823
4824static int beiscsi_task_xmit(struct iscsi_task *task)
4825{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304826 struct beiscsi_io_task *io_task = task->dd_data;
4827 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304828 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304829 struct scatterlist *sg;
4830 int num_sg;
4831 unsigned int writedir = 0, xferlen = 0;
4832
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304833 phba = io_task->conn->phba;
4834 /**
4835 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4836 * operational if FW still gets heartbeat from EP FW. Is management
4837 * path really needed to continue further?
4838 */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304839 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304840 return -EIO;
4841
Jitendra Bhivare18683792016-02-04 15:49:18 +05304842 if (!io_task->conn->login_in_progress)
4843 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304844
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304845 if (!sc)
4846 return beiscsi_mtask(task);
4847
4848 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304849 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304850 num_sg = scsi_dma_map(sc);
4851 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004852 beiscsi_log(phba, KERN_ERR,
4853 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4854 "BM_%d : scsi_dma_map Failed "
4855 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4856 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4857 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304858
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304859 return num_sg;
4860 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304861 /**
4862 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4863 * For management task, cleanup_task checks mtask_addr before unmapping.
4864 */
4865 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304866 xferlen = scsi_bufflen(sc);
4867 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304868 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304869 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304870 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304871 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304872
John Soni Jose09a10932012-10-20 04:44:23 +05304873 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304874}
4875
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004876/**
4877 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4878 * @job: job to handle
4879 */
4880static int beiscsi_bsg_request(struct bsg_job *job)
4881{
4882 struct Scsi_Host *shost;
4883 struct beiscsi_hba *phba;
4884 struct iscsi_bsg_request *bsg_req = job->request;
4885 int rc = -EINVAL;
4886 unsigned int tag;
4887 struct be_dma_mem nonemb_cmd;
4888 struct be_cmd_resp_hdr *resp;
4889 struct iscsi_bsg_reply *bsg_reply = job->reply;
4890 unsigned short status, extd_status;
4891
4892 shost = iscsi_job_to_shost(job);
4893 phba = iscsi_host_priv(shost);
4894
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304895 if (!beiscsi_hba_is_online(phba)) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304896 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4897 "BM_%d : HBA in error 0x%lx\n", phba->state);
4898 return -ENXIO;
4899 }
4900
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004901 switch (bsg_req->msgcode) {
4902 case ISCSI_BSG_HST_VENDOR:
4903 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4904 job->request_payload.payload_len,
4905 &nonemb_cmd.dma);
4906 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304907 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4908 "BM_%d : Failed to allocate memory for "
4909 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304910 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004911 }
4912 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4913 &nonemb_cmd);
4914 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304915 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304916 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304917
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004918 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4919 nonemb_cmd.va, nonemb_cmd.dma);
4920 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304921 }
4922
4923 rc = wait_event_interruptible_timeout(
4924 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304925 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304926 msecs_to_jiffies(
4927 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05304928
4929 if (!test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
4930 clear_bit(MCC_TAG_STATE_RUNNING,
4931 &phba->ctrl.ptag_state[tag].tag_state);
4932 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4933 nonemb_cmd.va, nonemb_cmd.dma);
4934 return -EIO;
4935 }
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304936 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4937 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4938 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304939 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004940 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4941 sg_copy_from_buffer(job->reply_payload.sg_list,
4942 job->reply_payload.sg_cnt,
4943 nonemb_cmd.va, (resp->response_length
4944 + sizeof(*resp)));
4945 bsg_reply->reply_payload_rcv_len = resp->response_length;
4946 bsg_reply->result = status;
4947 bsg_job_done(job, bsg_reply->result,
4948 bsg_reply->reply_payload_rcv_len);
4949 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4950 nonemb_cmd.va, nonemb_cmd.dma);
4951 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304952 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304953 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304954 " status = %d extd_status = %d\n",
4955 status, extd_status);
4956
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004957 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304958 } else {
4959 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004960 }
4961 break;
4962
4963 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304964 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4965 "BM_%d : Unsupported bsg command: 0x%x\n",
4966 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004967 break;
4968 }
4969
4970 return rc;
4971}
4972
John Soni Jose99bc5d52012-08-20 23:00:18 +05304973void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4974{
4975 /* Set the logging parameter */
4976 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4977}
4978
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05304979void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
4980{
4981 if (phba->boot_struct.boot_kset)
4982 return;
4983
4984 /* skip if boot work is already in progress */
4985 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
4986 return;
4987
4988 phba->boot_struct.retry = 3;
4989 phba->boot_struct.tag = 0;
4990 phba->boot_struct.s_handle = s_handle;
4991 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
4992 schedule_work(&phba->boot_work);
4993}
4994
4995static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
4996{
4997 struct beiscsi_hba *phba = data;
4998 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
4999 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5000 char *str = buf;
5001 int rc = -EPERM;
5002
5003 switch (type) {
5004 case ISCSI_BOOT_TGT_NAME:
5005 rc = sprintf(buf, "%.*s\n",
5006 (int)strlen(boot_sess->target_name),
5007 (char *)&boot_sess->target_name);
5008 break;
5009 case ISCSI_BOOT_TGT_IP_ADDR:
5010 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5011 rc = sprintf(buf, "%pI4\n",
5012 (char *)&boot_conn->dest_ipaddr.addr);
5013 else
5014 rc = sprintf(str, "%pI6\n",
5015 (char *)&boot_conn->dest_ipaddr.addr);
5016 break;
5017 case ISCSI_BOOT_TGT_PORT:
5018 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5019 break;
5020
5021 case ISCSI_BOOT_TGT_CHAP_NAME:
5022 rc = sprintf(str, "%.*s\n",
5023 boot_conn->negotiated_login_options.auth_data.chap.
5024 target_chap_name_length,
5025 (char *)&boot_conn->negotiated_login_options.
5026 auth_data.chap.target_chap_name);
5027 break;
5028 case ISCSI_BOOT_TGT_CHAP_SECRET:
5029 rc = sprintf(str, "%.*s\n",
5030 boot_conn->negotiated_login_options.auth_data.chap.
5031 target_secret_length,
5032 (char *)&boot_conn->negotiated_login_options.
5033 auth_data.chap.target_secret);
5034 break;
5035 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5036 rc = sprintf(str, "%.*s\n",
5037 boot_conn->negotiated_login_options.auth_data.chap.
5038 intr_chap_name_length,
5039 (char *)&boot_conn->negotiated_login_options.
5040 auth_data.chap.intr_chap_name);
5041 break;
5042 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5043 rc = sprintf(str, "%.*s\n",
5044 boot_conn->negotiated_login_options.auth_data.chap.
5045 intr_secret_length,
5046 (char *)&boot_conn->negotiated_login_options.
5047 auth_data.chap.intr_secret);
5048 break;
5049 case ISCSI_BOOT_TGT_FLAGS:
5050 rc = sprintf(str, "2\n");
5051 break;
5052 case ISCSI_BOOT_TGT_NIC_ASSOC:
5053 rc = sprintf(str, "0\n");
5054 break;
5055 }
5056 return rc;
5057}
5058
5059static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5060{
5061 struct beiscsi_hba *phba = data;
5062 char *str = buf;
5063 int rc = -EPERM;
5064
5065 switch (type) {
5066 case ISCSI_BOOT_INI_INITIATOR_NAME:
5067 rc = sprintf(str, "%s\n",
5068 phba->boot_struct.boot_sess.initiator_iscsiname);
5069 break;
5070 }
5071 return rc;
5072}
5073
5074static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5075{
5076 struct beiscsi_hba *phba = data;
5077 char *str = buf;
5078 int rc = -EPERM;
5079
5080 switch (type) {
5081 case ISCSI_BOOT_ETH_FLAGS:
5082 rc = sprintf(str, "2\n");
5083 break;
5084 case ISCSI_BOOT_ETH_INDEX:
5085 rc = sprintf(str, "0\n");
5086 break;
5087 case ISCSI_BOOT_ETH_MAC:
5088 rc = beiscsi_get_macaddr(str, phba);
5089 break;
5090 }
5091 return rc;
5092}
5093
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305094static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5095{
5096 umode_t rc = 0;
5097
5098 switch (type) {
5099 case ISCSI_BOOT_TGT_NAME:
5100 case ISCSI_BOOT_TGT_IP_ADDR:
5101 case ISCSI_BOOT_TGT_PORT:
5102 case ISCSI_BOOT_TGT_CHAP_NAME:
5103 case ISCSI_BOOT_TGT_CHAP_SECRET:
5104 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5105 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5106 case ISCSI_BOOT_TGT_NIC_ASSOC:
5107 case ISCSI_BOOT_TGT_FLAGS:
5108 rc = S_IRUGO;
5109 break;
5110 }
5111 return rc;
5112}
5113
5114static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5115{
5116 umode_t rc = 0;
5117
5118 switch (type) {
5119 case ISCSI_BOOT_INI_INITIATOR_NAME:
5120 rc = S_IRUGO;
5121 break;
5122 }
5123 return rc;
5124}
5125
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305126static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5127{
5128 umode_t rc = 0;
5129
5130 switch (type) {
5131 case ISCSI_BOOT_ETH_FLAGS:
5132 case ISCSI_BOOT_ETH_MAC:
5133 case ISCSI_BOOT_ETH_INDEX:
5134 rc = S_IRUGO;
5135 break;
5136 }
5137 return rc;
5138}
5139
5140static void beiscsi_boot_kobj_release(void *data)
5141{
5142 struct beiscsi_hba *phba = data;
5143
5144 scsi_host_put(phba->shost);
5145}
5146
5147static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5148{
5149 struct boot_struct *bs = &phba->boot_struct;
5150 struct iscsi_boot_kobj *boot_kobj;
5151
5152 if (bs->boot_kset) {
5153 __beiscsi_log(phba, KERN_ERR,
5154 "BM_%d: boot_kset already created\n");
5155 return 0;
5156 }
5157
5158 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5159 if (!bs->boot_kset) {
5160 __beiscsi_log(phba, KERN_ERR,
5161 "BM_%d: boot_kset alloc failed\n");
5162 return -ENOMEM;
5163 }
5164
5165 /* get shost ref because the show function will refer phba */
5166 if (!scsi_host_get(phba->shost))
5167 goto free_kset;
5168
5169 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5170 beiscsi_show_boot_tgt_info,
5171 beiscsi_tgt_get_attr_visibility,
5172 beiscsi_boot_kobj_release);
5173 if (!boot_kobj)
5174 goto put_shost;
5175
5176 if (!scsi_host_get(phba->shost))
5177 goto free_kset;
5178
5179 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5180 beiscsi_show_boot_ini_info,
5181 beiscsi_ini_get_attr_visibility,
5182 beiscsi_boot_kobj_release);
5183 if (!boot_kobj)
5184 goto put_shost;
5185
5186 if (!scsi_host_get(phba->shost))
5187 goto free_kset;
5188
5189 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5190 beiscsi_show_boot_eth_info,
5191 beiscsi_eth_get_attr_visibility,
5192 beiscsi_boot_kobj_release);
5193 if (!boot_kobj)
5194 goto put_shost;
5195
5196 return 0;
5197
5198put_shost:
5199 scsi_host_put(phba->shost);
5200free_kset:
5201 iscsi_boot_destroy_kset(bs->boot_kset);
5202 bs->boot_kset = NULL;
5203 return -ENOMEM;
5204}
5205
5206static void beiscsi_boot_work(struct work_struct *work)
5207{
5208 struct beiscsi_hba *phba =
5209 container_of(work, struct beiscsi_hba, boot_work);
5210 struct boot_struct *bs = &phba->boot_struct;
5211 unsigned int tag = 0;
5212
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305213 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305214 return;
5215
5216 beiscsi_log(phba, KERN_INFO,
5217 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5218 "BM_%d : %s action %d\n",
5219 __func__, phba->boot_struct.action);
5220
5221 switch (phba->boot_struct.action) {
5222 case BEISCSI_BOOT_REOPEN_SESS:
5223 tag = beiscsi_boot_reopen_sess(phba);
5224 break;
5225 case BEISCSI_BOOT_GET_SHANDLE:
5226 tag = __beiscsi_boot_get_shandle(phba, 1);
5227 break;
5228 case BEISCSI_BOOT_GET_SINFO:
5229 tag = beiscsi_boot_get_sinfo(phba);
5230 break;
5231 case BEISCSI_BOOT_LOGOUT_SESS:
5232 tag = beiscsi_boot_logout_sess(phba);
5233 break;
5234 case BEISCSI_BOOT_CREATE_KSET:
5235 beiscsi_boot_create_kset(phba);
5236 /**
5237 * updated boot_kset is made visible to all before
5238 * ending the boot work.
5239 */
5240 mb();
5241 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5242 return;
5243 }
5244 if (!tag) {
5245 if (bs->retry--)
5246 schedule_work(&phba->boot_work);
5247 else
5248 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5249 }
5250}
5251
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305252static void beiscsi_eqd_update_work(struct work_struct *work)
5253{
5254 struct hwi_context_memory *phwi_context;
5255 struct be_set_eqd set_eqd[MAX_CPUS];
5256 struct hwi_controller *phwi_ctrlr;
5257 struct be_eq_obj *pbe_eq;
5258 struct beiscsi_hba *phba;
5259 unsigned int pps, delta;
5260 struct be_aic_obj *aic;
5261 int eqd, i, num = 0;
5262 unsigned long now;
5263
5264 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305265 if (!beiscsi_hba_is_online(phba))
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305266 return;
5267
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005268 phwi_ctrlr = phba->phwi_ctrlr;
5269 phwi_context = phwi_ctrlr->phwi_ctxt;
5270
5271 for (i = 0; i <= phba->num_cpus; i++) {
5272 aic = &phba->aic_obj[i];
5273 pbe_eq = &phwi_context->be_eq[i];
5274 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305275 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005276 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305277 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005278 aic->eq_prev = pbe_eq->cq_count;
5279 continue;
5280 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305281 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005282 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5283 eqd = (pps / 1500) << 2;
5284
5285 if (eqd < 8)
5286 eqd = 0;
5287 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5288 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5289
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305290 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005291 aic->eq_prev = pbe_eq->cq_count;
5292
5293 if (eqd != aic->prev_eqd) {
5294 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5295 set_eqd[num].eq_id = pbe_eq->q.id;
5296 aic->prev_eqd = eqd;
5297 num++;
5298 }
5299 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305300 if (num)
5301 /* completion of this is ignored */
5302 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005303
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305304 schedule_delayed_work(&phba->eqd_update,
5305 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305306}
5307
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305308static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5309{
5310 int i, status;
5311
5312 for (i = 0; i <= phba->num_cpus; i++)
5313 phba->msix_entries[i].entry = i;
5314
5315 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5316 phba->num_cpus + 1, phba->num_cpus + 1);
5317 if (status > 0)
5318 phba->msix_enabled = true;
5319}
5320
5321static void beiscsi_hw_tpe_check(unsigned long ptr)
5322{
5323 struct beiscsi_hba *phba;
5324 u32 wait;
5325
5326 phba = (struct beiscsi_hba *)ptr;
5327 /* if not TPE, do nothing */
5328 if (!beiscsi_detect_tpe(phba))
5329 return;
5330
5331 /* wait default 4000ms before recovering */
5332 wait = 4000;
5333 if (phba->ue2rp > BEISCSI_UE_DETECT_INTERVAL)
5334 wait = phba->ue2rp - BEISCSI_UE_DETECT_INTERVAL;
5335 queue_delayed_work(phba->wq, &phba->recover_port,
5336 msecs_to_jiffies(wait));
5337}
5338
5339static void beiscsi_hw_health_check(unsigned long ptr)
5340{
5341 struct beiscsi_hba *phba;
5342
5343 phba = (struct beiscsi_hba *)ptr;
5344 beiscsi_detect_ue(phba);
5345 if (beiscsi_detect_ue(phba)) {
5346 __beiscsi_log(phba, KERN_ERR,
5347 "BM_%d : port in error: %lx\n", phba->state);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305348 /* sessions are no longer valid, so first fail the sessions */
5349 queue_work(phba->wq, &phba->sess_work);
5350
5351 /* detect UER supported */
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305352 if (!test_bit(BEISCSI_HBA_UER_SUPP, &phba->state))
5353 return;
5354 /* modify this timer to check TPE */
5355 phba->hw_check.function = beiscsi_hw_tpe_check;
5356 }
5357
5358 mod_timer(&phba->hw_check,
5359 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5360}
5361
5362/*
5363 * beiscsi_enable_port()- Enables the disabled port.
5364 * Only port resources freed in disable function are reallocated.
5365 * This is called in HBA error handling path.
5366 *
5367 * @phba: Instance of driver private structure
5368 *
5369 **/
5370static int beiscsi_enable_port(struct beiscsi_hba *phba)
5371{
5372 struct hwi_context_memory *phwi_context;
5373 struct hwi_controller *phwi_ctrlr;
5374 struct be_eq_obj *pbe_eq;
5375 int ret, i;
5376
5377 if (test_bit(BEISCSI_HBA_ONLINE, &phba->state)) {
5378 __beiscsi_log(phba, KERN_ERR,
5379 "BM_%d : %s : port is online %lx\n",
5380 __func__, phba->state);
5381 return 0;
5382 }
5383
5384 ret = beiscsi_init_sliport(phba);
5385 if (ret)
5386 return ret;
5387
5388 if (enable_msix)
5389 find_num_cpus(phba);
5390 else
5391 phba->num_cpus = 1;
5392 if (enable_msix) {
5393 beiscsi_msix_enable(phba);
5394 if (!phba->msix_enabled)
5395 phba->num_cpus = 1;
5396 }
5397
5398 beiscsi_get_params(phba);
5399 /* Re-enable UER. If different TPE occurs then it is recoverable. */
5400 beiscsi_set_uer_feature(phba);
5401
5402 phba->shost->max_id = phba->params.cxns_per_ctrl;
5403 phba->shost->can_queue = phba->params.ios_per_ctrl;
5404 ret = hwi_init_controller(phba);
5405 if (ret) {
5406 __beiscsi_log(phba, KERN_ERR,
5407 "BM_%d : init controller failed %d\n", ret);
5408 goto disable_msix;
5409 }
5410
5411 for (i = 0; i < MAX_MCC_CMD; i++) {
5412 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5413 phba->ctrl.mcc_tag[i] = i + 1;
5414 phba->ctrl.mcc_tag_status[i + 1] = 0;
5415 phba->ctrl.mcc_tag_available++;
5416 }
5417
5418 phwi_ctrlr = phba->phwi_ctrlr;
5419 phwi_context = phwi_ctrlr->phwi_ctxt;
5420 for (i = 0; i < phba->num_cpus; i++) {
5421 pbe_eq = &phwi_context->be_eq[i];
5422 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
5423 }
5424
5425 i = (phba->msix_enabled) ? i : 0;
5426 /* Work item for MCC handling */
5427 pbe_eq = &phwi_context->be_eq[i];
5428 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
5429
5430 ret = beiscsi_init_irqs(phba);
5431 if (ret < 0) {
5432 __beiscsi_log(phba, KERN_ERR,
5433 "BM_%d : setup IRQs failed %d\n", ret);
5434 goto cleanup_port;
5435 }
5436 hwi_enable_intr(phba);
5437 /* port operational: clear all error bits */
5438 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5439 __beiscsi_log(phba, KERN_INFO,
5440 "BM_%d : port online: 0x%lx\n", phba->state);
5441
5442 /* start hw_check timer and eqd_update work */
5443 schedule_delayed_work(&phba->eqd_update,
5444 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5445
5446 /**
5447 * Timer function gets modified for TPE detection.
5448 * Always reinit to do health check first.
5449 */
5450 phba->hw_check.function = beiscsi_hw_health_check;
5451 mod_timer(&phba->hw_check,
5452 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5453 return 0;
5454
5455cleanup_port:
5456 for (i = 0; i < phba->num_cpus; i++) {
5457 pbe_eq = &phwi_context->be_eq[i];
5458 irq_poll_disable(&pbe_eq->iopoll);
5459 }
5460 hwi_cleanup_port(phba);
5461
5462disable_msix:
5463 if (phba->msix_enabled)
5464 pci_disable_msix(phba->pcidev);
5465
5466 return ret;
5467}
5468
5469/*
5470 * beiscsi_disable_port()- Disable port and cleanup driver resources.
5471 * This is called in HBA error handling and driver removal.
5472 * @phba: Instance Priv structure
5473 * @unload: indicate driver is unloading
5474 *
5475 * Free the OS and HW resources held by the driver
5476 **/
5477static void beiscsi_disable_port(struct beiscsi_hba *phba, int unload)
5478{
5479 struct hwi_context_memory *phwi_context;
5480 struct hwi_controller *phwi_ctrlr;
5481 struct be_eq_obj *pbe_eq;
5482 unsigned int i, msix_vec;
5483
5484 if (!test_and_clear_bit(BEISCSI_HBA_ONLINE, &phba->state))
5485 return;
5486
5487 phwi_ctrlr = phba->phwi_ctrlr;
5488 phwi_context = phwi_ctrlr->phwi_ctxt;
5489 hwi_disable_intr(phba);
5490 if (phba->msix_enabled) {
5491 for (i = 0; i <= phba->num_cpus; i++) {
5492 msix_vec = phba->msix_entries[i].vector;
5493 free_irq(msix_vec, &phwi_context->be_eq[i]);
5494 kfree(phba->msi_name[i]);
5495 }
5496 } else
5497 if (phba->pcidev->irq)
5498 free_irq(phba->pcidev->irq, phba);
5499 pci_disable_msix(phba->pcidev);
5500
5501 for (i = 0; i < phba->num_cpus; i++) {
5502 pbe_eq = &phwi_context->be_eq[i];
5503 irq_poll_disable(&pbe_eq->iopoll);
5504 }
5505 cancel_delayed_work_sync(&phba->eqd_update);
5506 cancel_work_sync(&phba->boot_work);
5507 /* WQ might be running cancel queued mcc_work if we are not exiting */
5508 if (!unload && beiscsi_hba_in_error(phba)) {
5509 pbe_eq = &phwi_context->be_eq[i];
5510 cancel_work_sync(&pbe_eq->mcc_work);
5511 }
5512 hwi_cleanup_port(phba);
5513}
5514
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305515static void beiscsi_sess_work(struct work_struct *work)
5516{
5517 struct beiscsi_hba *phba;
5518
5519 phba = container_of(work, struct beiscsi_hba, sess_work);
5520 /*
5521 * This work gets scheduled only in case of HBA error.
5522 * Old sessions are gone so need to be re-established.
5523 * iscsi_session_failure needs process context hence this work.
5524 */
5525 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
5526}
5527
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305528static void beiscsi_recover_port(struct work_struct *work)
5529{
5530 struct beiscsi_hba *phba;
5531
5532 phba = container_of(work, struct beiscsi_hba, recover_port.work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305533 beiscsi_disable_port(phba, 0);
5534 beiscsi_enable_port(phba);
5535}
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005536
5537static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5538 pci_channel_state_t state)
5539{
5540 struct beiscsi_hba *phba = NULL;
5541
5542 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305543 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005544
5545 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5546 "BM_%d : EEH error detected\n");
5547
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305548 /* first stop UE detection when PCI error detected */
5549 del_timer_sync(&phba->hw_check);
5550 cancel_delayed_work_sync(&phba->recover_port);
5551
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305552 /* sessions are no longer valid, so first fail the sessions */
5553 iscsi_host_for_each_session(phba->shost, beiscsi_session_fail);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305554 beiscsi_disable_port(phba, 0);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005555
5556 if (state == pci_channel_io_perm_failure) {
5557 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5558 "BM_%d : EEH : State PERM Failure");
5559 return PCI_ERS_RESULT_DISCONNECT;
5560 }
5561
5562 pci_disable_device(pdev);
5563
5564 /* The error could cause the FW to trigger a flash debug dump.
5565 * Resetting the card while flash dump is in progress
5566 * can cause it not to recover; wait for it to finish.
5567 * Wait only for first function as it is needed only once per
5568 * adapter.
5569 **/
5570 if (pdev->devfn == 0)
5571 ssleep(30);
5572
5573 return PCI_ERS_RESULT_NEED_RESET;
5574}
5575
5576static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5577{
5578 struct beiscsi_hba *phba = NULL;
5579 int status = 0;
5580
5581 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5582
5583 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5584 "BM_%d : EEH Reset\n");
5585
5586 status = pci_enable_device(pdev);
5587 if (status)
5588 return PCI_ERS_RESULT_DISCONNECT;
5589
5590 pci_set_master(pdev);
5591 pci_set_power_state(pdev, PCI_D0);
5592 pci_restore_state(pdev);
5593
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305594 status = beiscsi_check_fw_rdy(phba);
5595 if (status) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005596 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5597 "BM_%d : EEH Reset Completed\n");
5598 } else {
5599 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5600 "BM_%d : EEH Reset Completion Failure\n");
5601 return PCI_ERS_RESULT_DISCONNECT;
5602 }
5603
5604 pci_cleanup_aer_uncorrect_error_status(pdev);
5605 return PCI_ERS_RESULT_RECOVERED;
5606}
5607
5608static void beiscsi_eeh_resume(struct pci_dev *pdev)
5609{
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305610 struct beiscsi_hba *phba;
5611 int ret;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005612
5613 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5614 pci_save_state(pdev);
5615
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305616 ret = beiscsi_enable_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305617 if (ret)
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305618 __beiscsi_log(phba, KERN_ERR,
5619 "BM_%d : AER EEH resume failed\n");
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005620}
5621
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005622static int beiscsi_dev_probe(struct pci_dev *pcidev,
5623 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305624{
5625 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305626 struct hwi_controller *phwi_ctrlr;
5627 struct hwi_context_memory *phwi_context;
5628 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305629 unsigned int s_handle;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005630 int ret = 0, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305631
5632 ret = beiscsi_enable_pci(pcidev);
5633 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305634 dev_err(&pcidev->dev,
5635 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305636 return ret;
5637 }
5638
5639 phba = beiscsi_hba_alloc(pcidev);
5640 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305641 dev_err(&pcidev->dev,
5642 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305643 goto disable_pci;
5644 }
5645
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005646 /* Enable EEH reporting */
5647 ret = pci_enable_pcie_error_reporting(pcidev);
5648 if (ret)
5649 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5650 "BM_%d : PCIe Error Reporting "
5651 "Enabling Failed\n");
5652
5653 pci_save_state(pcidev);
5654
John Soni Jose99bc5d52012-08-20 23:00:18 +05305655 /* Initialize Driver configuration Paramters */
5656 beiscsi_hba_attrs_init(phba);
5657
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005658 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305659
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305660 switch (pcidev->device) {
5661 case BE_DEVICE_ID1:
5662 case OC_DEVICE_ID1:
5663 case OC_DEVICE_ID2:
5664 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305665 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305666 break;
5667 case BE_DEVICE_ID2:
5668 case OC_DEVICE_ID3:
5669 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305670 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305671 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305672 case OC_SKH_ID1:
5673 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305674 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005675 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305676 default:
5677 phba->generation = 0;
5678 }
5679
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305680 ret = be_ctrl_init(phba, pcidev);
5681 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305682 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305683 "BM_%d : be_ctrl_init failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305684 goto hba_free;
5685 }
5686
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305687 ret = beiscsi_init_sliport(phba);
5688 if (ret)
5689 goto hba_free;
5690
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305691 spin_lock_init(&phba->io_sgl_lock);
5692 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005693 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305694 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305695 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305696 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5697 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305698 goto free_port;
5699 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305700 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305701 beiscsi_get_params(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305702 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005703
5704 if (enable_msix)
5705 find_num_cpus(phba);
5706 else
5707 phba->num_cpus = 1;
5708
5709 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5710 "BM_%d : num_cpus = %d\n",
5711 phba->num_cpus);
5712
5713 if (enable_msix) {
5714 beiscsi_msix_enable(phba);
5715 if (!phba->msix_enabled)
5716 phba->num_cpus = 1;
5717 }
5718
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005719 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305720 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305721 ret = beiscsi_init_port(phba);
5722 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305723 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5724 "BM_%d : beiscsi_dev_probe-"
5725 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305726 goto free_port;
5727 }
5728
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005729 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305730 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5731 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305732 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305733 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005734 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005735 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305736 }
5737
5738 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5739
John Soni Jose72fb46a2012-10-20 04:42:49 +05305740 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305741 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005742 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305743 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305744 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5745 "BM_%d : beiscsi_dev_probe-"
5746 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305747 goto free_twq;
5748 }
5749
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305750 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305751
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305752 phwi_ctrlr = phba->phwi_ctrlr;
5753 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305754
Jens Axboe89f8b332014-03-13 09:38:42 -06005755 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305756 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305757 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget, be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305758 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305759
Jens Axboe89f8b332014-03-13 09:38:42 -06005760 i = (phba->msix_enabled) ? i : 0;
5761 /* Work item for MCC handling */
5762 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305763 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005764
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305765 ret = beiscsi_init_irqs(phba);
5766 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305767 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5768 "BM_%d : beiscsi_dev_probe-"
5769 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305770 goto free_blkenbld;
5771 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305772 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005773
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305774 ret = iscsi_host_add(phba->shost, &phba->pcidev->dev);
5775 if (ret)
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005776 goto free_blkenbld;
5777
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305778 /* set online bit after port is operational */
5779 set_bit(BEISCSI_HBA_ONLINE, &phba->state);
5780 __beiscsi_log(phba, KERN_INFO,
5781 "BM_%d : port online: 0x%lx\n", phba->state);
5782
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305783 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5784 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5785 if (ret > 0) {
5786 beiscsi_start_boot_work(phba, s_handle);
5787 /**
5788 * Set this bit after starting the work to let
5789 * probe handle it first.
5790 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005791 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305792 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5793 }
Mike Christief457a462011-06-24 15:11:53 -05005794
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305795 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305796 schedule_delayed_work(&phba->eqd_update,
5797 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305798
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305799 INIT_WORK(&phba->sess_work, beiscsi_sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305800 INIT_DELAYED_WORK(&phba->recover_port, beiscsi_recover_port);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305801 /**
5802 * Start UE detection here. UE before this will cause stall in probe
5803 * and eventually fail the probe.
5804 */
5805 init_timer(&phba->hw_check);
5806 phba->hw_check.function = beiscsi_hw_health_check;
5807 phba->hw_check.data = (unsigned long)phba;
5808 mod_timer(&phba->hw_check,
5809 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose99bc5d52012-08-20 23:00:18 +05305810 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5811 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305812 return 0;
5813
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305814free_blkenbld:
5815 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005816 for (i = 0; i < phba->num_cpus; i++) {
5817 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005818 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005819 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305820free_twq:
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305821 hwi_cleanup_port(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305822 beiscsi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305823 beiscsi_free_mem(phba);
5824free_port:
5825 pci_free_consistent(phba->pcidev,
5826 phba->ctrl.mbox_mem_alloced.size,
5827 phba->ctrl.mbox_mem_alloced.va,
5828 phba->ctrl.mbox_mem_alloced.dma);
5829 beiscsi_unmap_pci_function(phba);
5830hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305831 if (phba->msix_enabled)
5832 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305833 pci_dev_put(phba->pcidev);
5834 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305835 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305836disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305837 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305838 pci_disable_device(pcidev);
5839 return ret;
5840}
5841
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305842static void beiscsi_remove(struct pci_dev *pcidev)
5843{
5844 struct beiscsi_hba *phba = NULL;
5845
5846 phba = pci_get_drvdata(pcidev);
5847 if (!phba) {
5848 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5849 return;
5850 }
5851
5852 /* first stop UE detection before unloading */
5853 del_timer_sync(&phba->hw_check);
5854 cancel_delayed_work_sync(&phba->recover_port);
Jitendra Bhivare10e1a442016-08-19 15:20:19 +05305855 cancel_work_sync(&phba->sess_work);
Jitendra Bhivared1d5ca82016-08-19 15:20:18 +05305856
5857 beiscsi_iface_destroy_default(phba);
5858 iscsi_host_remove(phba->shost);
5859 beiscsi_disable_port(phba, 1);
5860
5861 /* after cancelling boot_work */
5862 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
5863
5864 /* free all resources */
5865 destroy_workqueue(phba->wq);
5866 beiscsi_cleanup_port(phba);
5867 beiscsi_free_mem(phba);
5868
5869 /* ctrl uninit */
5870 beiscsi_unmap_pci_function(phba);
5871 pci_free_consistent(phba->pcidev,
5872 phba->ctrl.mbox_mem_alloced.size,
5873 phba->ctrl.mbox_mem_alloced.va,
5874 phba->ctrl.mbox_mem_alloced.dma);
5875
5876 pci_dev_put(phba->pcidev);
5877 iscsi_host_free(phba->shost);
5878 pci_disable_pcie_error_reporting(pcidev);
5879 pci_set_drvdata(pcidev, NULL);
5880 pci_release_regions(pcidev);
5881 pci_disable_device(pcidev);
5882}
5883
5884
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005885static struct pci_error_handlers beiscsi_eeh_handlers = {
5886 .error_detected = beiscsi_eeh_err_detected,
5887 .slot_reset = beiscsi_eeh_reset,
5888 .resume = beiscsi_eeh_resume,
5889};
5890
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305891struct iscsi_transport beiscsi_iscsi_transport = {
5892 .owner = THIS_MODULE,
5893 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305894 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305895 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305896 .create_session = beiscsi_session_create,
5897 .destroy_session = beiscsi_session_destroy,
5898 .create_conn = beiscsi_conn_create,
5899 .bind_conn = beiscsi_conn_bind,
5900 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305901 .attr_is_visible = beiscsi_attr_is_visible,
5902 .set_iface_param = beiscsi_iface_set_param,
5903 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305904 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005905 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305906 .get_session_param = iscsi_session_get_param,
5907 .get_host_param = beiscsi_get_host_param,
5908 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005909 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305910 .send_pdu = iscsi_conn_send_pdu,
5911 .xmit_task = beiscsi_task_xmit,
5912 .cleanup_task = beiscsi_cleanup_task,
5913 .alloc_pdu = beiscsi_alloc_pdu,
5914 .parse_pdu_itt = beiscsi_parse_pdu,
5915 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005916 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305917 .ep_connect = beiscsi_ep_connect,
5918 .ep_poll = beiscsi_ep_poll,
5919 .ep_disconnect = beiscsi_ep_disconnect,
5920 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005921 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305922};
5923
5924static struct pci_driver beiscsi_pci_driver = {
5925 .name = DRV_NAME,
5926 .probe = beiscsi_dev_probe,
5927 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005928 .id_table = beiscsi_pci_id_table,
5929 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305930};
5931
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305932static int __init beiscsi_module_init(void)
5933{
5934 int ret;
5935
5936 beiscsi_scsi_transport =
5937 iscsi_register_transport(&beiscsi_iscsi_transport);
5938 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305939 printk(KERN_ERR
5940 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305941 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305942 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305943 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5944 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305945
5946 ret = pci_register_driver(&beiscsi_pci_driver);
5947 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305948 printk(KERN_ERR
5949 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305950 goto unregister_iscsi_transport;
5951 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305952 return 0;
5953
5954unregister_iscsi_transport:
5955 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5956 return ret;
5957}
5958
5959static void __exit beiscsi_module_exit(void)
5960{
5961 pci_unregister_driver(&beiscsi_pci_driver);
5962 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5963}
5964
5965module_init(beiscsi_module_init);
5966module_exit(beiscsi_module_exit);