blob: e7741072995add29dc6cde4e79489b4e5c156d7f [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +05302 * Copyright (C) 2005 - 2015 Emulex
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303 * All rights reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * as published by the Free Software Foundation. The full GNU General
8 * Public License is included in this distribution in the file called COPYING.
9 *
Minh Tran4627de92015-05-14 23:16:17 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@avagotech.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Minh Tran4627de92015-05-14 23:16:17 -070013 * linux-drivers@avagotech.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 unsigned int
901beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
902 struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530903 struct pdu_base *ppdu,
904 unsigned long pdu_len,
905 void *pbuffer, unsigned long buf_len)
906{
907 struct iscsi_conn *conn = beiscsi_conn->conn;
908 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530909 struct iscsi_task *task;
910 struct beiscsi_io_task *io_task;
911 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530912
913 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
914 PDUBASE_OPCODE_MASK) {
915 case ISCSI_OP_NOOP_IN:
916 pbuffer = NULL;
917 buf_len = 0;
918 break;
919 case ISCSI_OP_ASYNC_EVENT:
920 break;
921 case ISCSI_OP_REJECT:
922 WARN_ON(!pbuffer);
923 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +0530924 beiscsi_log(phba, KERN_ERR,
925 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
926 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530927 break;
928 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530929 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530930 task = conn->login_task;
931 io_task = task->dd_data;
932 login_hdr = (struct iscsi_hdr *)ppdu;
933 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530934 break;
935 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +0530936 beiscsi_log(phba, KERN_WARNING,
937 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
938 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
939 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530940 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +0530941 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530942 return 1;
943 }
944
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600945 spin_lock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530946 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
Shlomo Pongratz659743b2014-02-07 00:41:38 -0600947 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530948 return 0;
949}
950
951static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
952{
953 struct sgl_handle *psgl_handle;
954
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530955 spin_lock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530956 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530957 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
958 "BM_%d : In alloc_io_sgl_handle,"
959 " io_sgl_alloc_index=%d\n",
960 phba->io_sgl_alloc_index);
961
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530962 psgl_handle = phba->io_sgl_hndl_base[phba->
963 io_sgl_alloc_index];
964 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
965 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530966 if (phba->io_sgl_alloc_index == (phba->params.
967 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530968 phba->io_sgl_alloc_index = 0;
969 else
970 phba->io_sgl_alloc_index++;
971 } else
972 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530973 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530974 return psgl_handle;
975}
976
977static void
978free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
979{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530980 spin_lock_bh(&phba->io_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530981 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
982 "BM_%d : In free_,io_sgl_free_index=%d\n",
983 phba->io_sgl_free_index);
984
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530985 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
986 /*
987 * this can happen if clean_task is called on a task that
988 * failed in xmit_task or alloc_pdu.
989 */
John Soni Jose99bc5d52012-08-20 23:00:18 +0530990 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
991 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
992 "value there=%p\n", phba->io_sgl_free_index,
993 phba->io_sgl_hndl_base
994 [phba->io_sgl_free_index]);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +0530995 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530996 return;
997 }
998 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
999 phba->io_sgl_hndl_avbl++;
1000 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1001 phba->io_sgl_free_index = 0;
1002 else
1003 phba->io_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301004 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301005}
1006
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301007static inline struct wrb_handle *
1008beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
1009 unsigned int wrbs_per_cxn)
1010{
1011 struct wrb_handle *pwrb_handle;
1012
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301013 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301014 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
1015 pwrb_context->wrb_handles_available--;
1016 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
1017 pwrb_context->alloc_index = 0;
1018 else
1019 pwrb_context->alloc_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301020 spin_unlock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301021
1022 return pwrb_handle;
1023}
1024
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301025/**
1026 * alloc_wrb_handle - To allocate a wrb handle
1027 * @phba: The hba pointer
1028 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +05301029 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301030 *
1031 * This happens under session_lock until submission to chip
1032 */
John Soni Jose340c99e2015-08-20 04:44:30 +05301033struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301034 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301035{
1036 struct hwi_wrb_context *pwrb_context;
1037 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001038 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301039
1040 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001041 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301042 /* return the context address */
1043 *pcontext = pwrb_context;
1044 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1045}
John Soni Jose340c99e2015-08-20 04:44:30 +05301046
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301047static inline void
1048beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1049 struct wrb_handle *pwrb_handle,
1050 unsigned int wrbs_per_cxn)
1051{
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301052 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301053 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1054 pwrb_context->wrb_handles_available++;
1055 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1056 pwrb_context->free_index = 0;
1057 else
1058 pwrb_context->free_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301059 spin_unlock_bh(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301060}
1061
1062/**
1063 * free_wrb_handle - To free the wrb handle back to pool
1064 * @phba: The hba pointer
1065 * @pwrb_context: The context to free from
1066 * @pwrb_handle: The wrb_handle to free
1067 *
1068 * This happens under session_lock until submission to chip
1069 */
1070static void
1071free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1072 struct wrb_handle *pwrb_handle)
1073{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301074 beiscsi_put_wrb_handle(pwrb_context,
1075 pwrb_handle,
1076 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301077 beiscsi_log(phba, KERN_INFO,
1078 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1079 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1080 "wrb_handles_available=%d\n",
1081 pwrb_handle, pwrb_context->free_index,
1082 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301083}
1084
1085static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1086{
1087 struct sgl_handle *psgl_handle;
1088
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301089 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301090 if (phba->eh_sgl_hndl_avbl) {
1091 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1092 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301093 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1094 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1095 phba->eh_sgl_alloc_index,
1096 phba->eh_sgl_alloc_index);
1097
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301098 phba->eh_sgl_hndl_avbl--;
1099 if (phba->eh_sgl_alloc_index ==
1100 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1101 1))
1102 phba->eh_sgl_alloc_index = 0;
1103 else
1104 phba->eh_sgl_alloc_index++;
1105 } else
1106 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301107 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301108 return psgl_handle;
1109}
1110
1111void
1112free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1113{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301114 spin_lock_bh(&phba->mgmt_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301115 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1116 "BM_%d : In free_mgmt_sgl_handle,"
1117 "eh_sgl_free_index=%d\n",
1118 phba->eh_sgl_free_index);
1119
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301120 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1121 /*
1122 * this can happen if clean_task is called on a task that
1123 * failed in xmit_task or alloc_pdu.
1124 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301125 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1126 "BM_%d : Double Free in eh SGL ,"
1127 "eh_sgl_free_index=%d\n",
1128 phba->eh_sgl_free_index);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301129 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301130 return;
1131 }
1132 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1133 phba->eh_sgl_hndl_avbl++;
1134 if (phba->eh_sgl_free_index ==
1135 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1136 phba->eh_sgl_free_index = 0;
1137 else
1138 phba->eh_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301139 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301140}
1141
1142static void
1143be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301144 struct iscsi_task *task,
1145 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301146{
1147 struct beiscsi_io_task *io_task = task->dd_data;
1148 struct be_status_bhs *sts_bhs =
1149 (struct be_status_bhs *)io_task->cmd_bhs;
1150 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301151 unsigned char *sense;
1152 u32 resid = 0, exp_cmdsn, max_cmdsn;
1153 u8 rsp, status, flags;
1154
John Soni Jose73133262012-10-20 04:44:49 +05301155 exp_cmdsn = csol_cqe->exp_cmdsn;
1156 max_cmdsn = (csol_cqe->exp_cmdsn +
1157 csol_cqe->cmd_wnd - 1);
1158 rsp = csol_cqe->i_resp;
1159 status = csol_cqe->i_sts;
1160 flags = csol_cqe->i_flags;
1161 resid = csol_cqe->res_cnt;
1162
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001163 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001164 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001165 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001166 io_task->scsi_cmnd = NULL;
1167 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301168
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001169 return;
1170 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301171 task->sc->result = (DID_OK << 16) | status;
1172 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1173 task->sc->result = DID_ERROR << 16;
1174 goto unmap;
1175 }
1176
1177 /* bidi not initially supported */
1178 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301179 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1180 task->sc->result = DID_ERROR << 16;
1181
1182 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1183 scsi_set_resid(task->sc, resid);
1184 if (!status && (scsi_bufflen(task->sc) - resid <
1185 task->sc->underflow))
1186 task->sc->result = DID_ERROR << 16;
1187 }
1188 }
1189
1190 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001191 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301192 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001193
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301194 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001195 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301196 memcpy(task->sc->sense_buffer, sense,
1197 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1198 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301199
John Soni Jose73133262012-10-20 04:44:49 +05301200 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1201 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301202unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301203 if (io_task->scsi_cmnd) {
1204 scsi_dma_unmap(io_task->scsi_cmnd);
1205 io_task->scsi_cmnd = NULL;
1206 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301207 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1208}
1209
1210static void
1211be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301212 struct iscsi_task *task,
1213 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301214{
1215 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301216 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301217 struct iscsi_conn *conn = beiscsi_conn->conn;
1218
1219 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301220 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301221 hdr->t2wait = 5;
1222 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301223 hdr->flags = csol_cqe->i_flags;
1224 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001225 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1226 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1227 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301228
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301229 hdr->dlength[0] = 0;
1230 hdr->dlength[1] = 0;
1231 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301232 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301233 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301234 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1235}
1236
1237static void
1238be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301239 struct iscsi_task *task,
1240 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301241{
1242 struct iscsi_tm_rsp *hdr;
1243 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301244 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301245
1246 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301247 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301248 hdr->flags = csol_cqe->i_flags;
1249 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001250 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1251 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1252 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301253
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
1258static void
1259hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1260 struct beiscsi_hba *phba, struct sol_cqe *psol)
1261{
1262 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301263 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301264 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301265 struct iscsi_task *task;
1266 struct beiscsi_io_task *io_task;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001267 uint16_t wrb_index, cid, cri_index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301268
1269 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001270 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301271 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1272 wrb_idx, psol);
1273 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1274 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001275 } else {
1276 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1277 wrb_idx, psol);
1278 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1279 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301280 }
1281
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001282 cri_index = BE_GET_CRI_FROM_CID(cid);
1283 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301284 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301285 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301286
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301287 io_task = task->dd_data;
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07001288 memset(io_task->pwrb_handle->pwrb, 0, sizeof(struct iscsi_wrb));
1289 iscsi_put_task(task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301290}
1291
1292static void
1293be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301294 struct iscsi_task *task,
1295 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301296{
1297 struct iscsi_nopin *hdr;
1298 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301299 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301300
1301 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301302 hdr->flags = csol_cqe->i_flags;
1303 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001304 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1305 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301306
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301307 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301308 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301309 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1310}
1311
John Soni Jose73133262012-10-20 04:44:49 +05301312static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1313 struct sol_cqe *psol,
1314 struct common_sol_cqe *csol_cqe)
1315{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001316 if (is_chip_be2_be3r(phba)) {
1317 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1318 i_exp_cmd_sn, psol);
1319 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1320 i_res_cnt, psol);
1321 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1322 i_cmd_wnd, psol);
1323 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1324 wrb_index, psol);
1325 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1326 cid, psol);
1327 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1328 hw_sts, psol);
1329 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1330 i_resp, psol);
1331 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1332 i_sts, psol);
1333 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1334 i_flags, psol);
1335 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301336 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1337 i_exp_cmd_sn, psol);
1338 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1339 i_res_cnt, psol);
1340 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1341 wrb_index, psol);
1342 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1343 cid, psol);
1344 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1345 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001346 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301347 i_cmd_wnd, psol);
1348 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1349 cmd_cmpl, psol))
1350 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1351 i_sts, psol);
1352 else
1353 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1354 i_sts, psol);
1355 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1356 u, psol))
1357 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1358
1359 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1360 o, psol))
1361 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301362 }
1363}
1364
1365
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301366static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1367 struct beiscsi_hba *phba, struct sol_cqe *psol)
1368{
1369 struct hwi_wrb_context *pwrb_context;
1370 struct wrb_handle *pwrb_handle;
1371 struct iscsi_wrb *pwrb = NULL;
1372 struct hwi_controller *phwi_ctrlr;
1373 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301374 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301375 struct iscsi_conn *conn = beiscsi_conn->conn;
1376 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301377 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001378 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301379
1380 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301381
1382 /* Copy the elements to a common structure */
1383 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1384
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001385 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1386 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301387
1388 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1389 csol_cqe.wrb_index];
1390
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301391 task = pwrb_handle->pio_handle;
1392 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301393 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301394
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001395 spin_lock_bh(&session->back_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301396 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301397 case HWH_TYPE_IO:
1398 case HWH_TYPE_IO_RD:
1399 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301400 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301401 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301402 else
John Soni Jose73133262012-10-20 04:44:49 +05301403 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301404 break;
1405
1406 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301407 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301408 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301409 else
John Soni Jose73133262012-10-20 04:44:49 +05301410 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301411 break;
1412
1413 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301414 beiscsi_log(phba, KERN_ERR,
1415 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1416 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1417 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301418 break;
1419
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301420 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301421 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301422 break;
1423
1424 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301425 beiscsi_log(phba, KERN_WARNING,
1426 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1427 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1428 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301429 csol_cqe.wrb_index,
1430 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301431 break;
1432 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301433
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001434 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301435}
1436
1437static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1438 *pasync_ctx, unsigned int is_header,
1439 unsigned int host_write_ptr)
1440{
1441 if (is_header)
1442 return &pasync_ctx->async_entry[host_write_ptr].
1443 header_busy_list;
1444 else
1445 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1446}
1447
1448static struct async_pdu_handle *
1449hwi_get_async_handle(struct beiscsi_hba *phba,
1450 struct beiscsi_conn *beiscsi_conn,
1451 struct hwi_async_pdu_context *pasync_ctx,
1452 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1453{
1454 struct be_bus_address phys_addr;
1455 struct list_head *pbusy_list;
1456 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301457 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301458 unsigned int index, dpl;
1459
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001460 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301461 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1462 dpl, pdpdu_cqe);
1463 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1464 index, pdpdu_cqe);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001465 } else {
1466 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1467 dpl, pdpdu_cqe);
1468 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1469 index, pdpdu_cqe);
John Soni Jose73133262012-10-20 04:44:49 +05301470 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301471
1472 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301473 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1474 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301475 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301476 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1477 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301478
1479 phys_addr.u.a64.address =
1480 *((unsigned long long *)(&phys_addr.u.a64.address));
1481
1482 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1483 & PDUCQE_CODE_MASK) {
1484 case UNSOL_HDR_NOTIFY:
1485 is_header = 1;
1486
John Soni Jose73133262012-10-20 04:44:49 +05301487 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1488 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301489 break;
1490 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301491 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1492 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301493 break;
1494 default:
1495 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301496 beiscsi_log(phba, KERN_WARNING,
1497 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1498 "BM_%d : Unexpected code=%d\n",
1499 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1500 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301501 return NULL;
1502 }
1503
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301504 WARN_ON(list_empty(pbusy_list));
1505 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001506 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301507 break;
1508 }
1509
1510 WARN_ON(!pasync_handle);
1511
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001512 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(
1513 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301514 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301515 pasync_handle->buffer_len = dpl;
1516 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301517
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301518 return pasync_handle;
1519}
1520
1521static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301522hwi_update_async_writables(struct beiscsi_hba *phba,
1523 struct hwi_async_pdu_context *pasync_ctx,
1524 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301525{
1526 struct list_head *pbusy_list;
1527 struct async_pdu_handle *pasync_handle;
1528 unsigned int num_entries, writables = 0;
1529 unsigned int *pep_read_ptr, *pwritables;
1530
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001531 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301532 if (is_header) {
1533 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1534 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301535 } else {
1536 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1537 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301538 }
1539
1540 while ((*pep_read_ptr) != cq_index) {
1541 (*pep_read_ptr)++;
1542 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1543
1544 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1545 *pep_read_ptr);
1546 if (writables == 0)
1547 WARN_ON(list_empty(pbusy_list));
1548
1549 if (!list_empty(pbusy_list)) {
1550 pasync_handle = list_entry(pbusy_list->next,
1551 struct async_pdu_handle,
1552 link);
1553 WARN_ON(!pasync_handle);
1554 pasync_handle->consumed = 1;
1555 }
1556
1557 writables++;
1558 }
1559
1560 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301561 beiscsi_log(phba, KERN_ERR,
1562 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1563 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1564 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301565 WARN_ON(1);
1566 }
1567
1568 *pwritables = *pwritables + writables;
1569 return 0;
1570}
1571
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001572static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001573 struct hwi_async_pdu_context *pasync_ctx,
1574 unsigned int cri)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301575{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301576 struct async_pdu_handle *pasync_handle, *tmp_handle;
1577 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301578
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301579 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301580 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1581 list_del(&pasync_handle->link);
1582
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001583 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301584 list_add_tail(&pasync_handle->link,
1585 &pasync_ctx->async_header.free_list);
1586 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301587 } else {
1588 list_add_tail(&pasync_handle->link,
1589 &pasync_ctx->async_data.free_list);
1590 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301591 }
1592 }
1593
1594 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1595 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1596 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301597}
1598
1599static struct phys_addr *
1600hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1601 unsigned int is_header, unsigned int host_write_ptr)
1602{
1603 struct phys_addr *pasync_sge = NULL;
1604
1605 if (is_header)
1606 pasync_sge = pasync_ctx->async_header.ring_base;
1607 else
1608 pasync_sge = pasync_ctx->async_data.ring_base;
1609
1610 return pasync_sge + host_write_ptr;
1611}
1612
1613static void hwi_post_async_buffers(struct beiscsi_hba *phba,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001614 unsigned int is_header, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301615{
1616 struct hwi_controller *phwi_ctrlr;
1617 struct hwi_async_pdu_context *pasync_ctx;
1618 struct async_pdu_handle *pasync_handle;
1619 struct list_head *pfree_link, *pbusy_list;
1620 struct phys_addr *pasync_sge;
1621 unsigned int ring_id, num_entries;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001622 unsigned int host_write_num, doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301623 unsigned int writables;
1624 unsigned int i = 0;
1625 u32 doorbell = 0;
1626
1627 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001628 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001629 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301630
1631 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301632 writables = min(pasync_ctx->async_header.writables,
1633 pasync_ctx->async_header.free_entries);
1634 pfree_link = pasync_ctx->async_header.free_list.next;
1635 host_write_num = pasync_ctx->async_header.host_write_ptr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001636 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1637 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
1638 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301639 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301640 writables = min(pasync_ctx->async_data.writables,
1641 pasync_ctx->async_data.free_entries);
1642 pfree_link = pasync_ctx->async_data.free_list.next;
1643 host_write_num = pasync_ctx->async_data.host_write_ptr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001644 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1645 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
1646 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301647 }
1648
1649 writables = (writables / 8) * 8;
1650 if (writables) {
1651 for (i = 0; i < writables; i++) {
1652 pbusy_list =
1653 hwi_get_async_busy_list(pasync_ctx, is_header,
1654 host_write_num);
1655 pasync_handle =
1656 list_entry(pfree_link, struct async_pdu_handle,
1657 link);
1658 WARN_ON(!pasync_handle);
1659 pasync_handle->consumed = 0;
1660
1661 pfree_link = pfree_link->next;
1662
1663 pasync_sge = hwi_get_ring_address(pasync_ctx,
1664 is_header, host_write_num);
1665
1666 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1667 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1668
1669 list_move(&pasync_handle->link, pbusy_list);
1670
1671 host_write_num++;
1672 host_write_num = host_write_num % num_entries;
1673 }
1674
1675 if (is_header) {
1676 pasync_ctx->async_header.host_write_ptr =
1677 host_write_num;
1678 pasync_ctx->async_header.free_entries -= writables;
1679 pasync_ctx->async_header.writables -= writables;
1680 pasync_ctx->async_header.busy_entries += writables;
1681 } else {
1682 pasync_ctx->async_data.host_write_ptr = host_write_num;
1683 pasync_ctx->async_data.free_entries -= writables;
1684 pasync_ctx->async_data.writables -= writables;
1685 pasync_ctx->async_data.busy_entries += writables;
1686 }
1687
1688 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1689 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1690 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1691 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1692 << DB_DEF_PDU_CQPROC_SHIFT;
1693
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001694 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301695 }
1696}
1697
1698static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1699 struct beiscsi_conn *beiscsi_conn,
1700 struct i_t_dpdu_cqe *pdpdu_cqe)
1701{
1702 struct hwi_controller *phwi_ctrlr;
1703 struct hwi_async_pdu_context *pasync_ctx;
1704 struct async_pdu_handle *pasync_handle = NULL;
1705 unsigned int cq_index = -1;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001706 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1707 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301708
1709 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001710 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1711 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1712 cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301713
1714 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1715 pdpdu_cqe, &cq_index);
1716 BUG_ON(pasync_handle->is_header != 0);
1717 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301718 hwi_update_async_writables(phba, pasync_ctx,
1719 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301720
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001721 hwi_free_async_msg(phba, pasync_ctx, pasync_handle->cri);
1722 hwi_post_async_buffers(phba, pasync_handle->is_header,
1723 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1724 cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301725}
1726
1727static unsigned int
1728hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1729 struct beiscsi_hba *phba,
1730 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1731{
1732 struct list_head *plist;
1733 struct async_pdu_handle *pasync_handle;
1734 void *phdr = NULL;
1735 unsigned int hdr_len = 0, buf_len = 0;
1736 unsigned int status, index = 0, offset = 0;
1737 void *pfirst_buffer = NULL;
1738 unsigned int num_buf = 0;
1739
1740 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1741
1742 list_for_each_entry(pasync_handle, plist, link) {
1743 if (index == 0) {
1744 phdr = pasync_handle->pbuffer;
1745 hdr_len = pasync_handle->buffer_len;
1746 } else {
1747 buf_len = pasync_handle->buffer_len;
1748 if (!num_buf) {
1749 pfirst_buffer = pasync_handle->pbuffer;
1750 num_buf++;
1751 }
1752 memcpy(pfirst_buffer + offset,
1753 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001754 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301755 }
1756 index++;
1757 }
1758
1759 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301760 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001761 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301762
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001763 hwi_free_async_msg(phba, pasync_ctx, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301764 return 0;
1765}
1766
1767static unsigned int
1768hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1769 struct beiscsi_hba *phba,
1770 struct async_pdu_handle *pasync_handle)
1771{
1772 struct hwi_async_pdu_context *pasync_ctx;
1773 struct hwi_controller *phwi_ctrlr;
1774 unsigned int bytes_needed = 0, status = 0;
1775 unsigned short cri = pasync_handle->cri;
1776 struct pdu_base *ppdu;
1777
1778 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001779 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1780 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1781 BE_GET_CRI_FROM_CID(beiscsi_conn->
1782 beiscsi_conn_cid)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301783
1784 list_del(&pasync_handle->link);
1785 if (pasync_handle->is_header) {
1786 pasync_ctx->async_header.busy_entries--;
1787 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001788 hwi_free_async_msg(phba, pasync_ctx, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301789 BUG();
1790 }
1791
1792 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1793 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1794 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1795 (unsigned short)pasync_handle->buffer_len;
1796 list_add_tail(&pasync_handle->link,
1797 &pasync_ctx->async_entry[cri].wait_queue.list);
1798
1799 ppdu = pasync_handle->pbuffer;
1800 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1801 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1802 0xFFFF0000) | ((be16_to_cpu((ppdu->
1803 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1804 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1805
1806 if (status == 0) {
1807 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1808 bytes_needed;
1809
1810 if (bytes_needed == 0)
1811 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1812 pasync_ctx, cri);
1813 }
1814 } else {
1815 pasync_ctx->async_data.busy_entries--;
1816 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1817 list_add_tail(&pasync_handle->link,
1818 &pasync_ctx->async_entry[cri].wait_queue.
1819 list);
1820 pasync_ctx->async_entry[cri].wait_queue.
1821 bytes_received +=
1822 (unsigned short)pasync_handle->buffer_len;
1823
1824 if (pasync_ctx->async_entry[cri].wait_queue.
1825 bytes_received >=
1826 pasync_ctx->async_entry[cri].wait_queue.
1827 bytes_needed)
1828 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1829 pasync_ctx, cri);
1830 }
1831 }
1832 return status;
1833}
1834
1835static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1836 struct beiscsi_hba *phba,
1837 struct i_t_dpdu_cqe *pdpdu_cqe)
1838{
1839 struct hwi_controller *phwi_ctrlr;
1840 struct hwi_async_pdu_context *pasync_ctx;
1841 struct async_pdu_handle *pasync_handle = NULL;
1842 unsigned int cq_index = -1;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001843 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1844 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301845
1846 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001847 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1848 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1849 cri_index));
1850
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301851 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1852 pdpdu_cqe, &cq_index);
1853
1854 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301855 hwi_update_async_writables(phba, pasync_ctx,
1856 pasync_handle->is_header, cq_index);
1857
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301858 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001859 hwi_post_async_buffers(phba, pasync_handle->is_header,
1860 BEISCSI_GET_ULP_FROM_CRI(
1861 phwi_ctrlr, cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301862}
1863
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301864void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301865{
1866 struct be_queue_info *mcc_cq;
1867 struct be_mcc_compl *mcc_compl;
1868 unsigned int num_processed = 0;
1869
1870 mcc_cq = &phba->ctrl.mcc_obj.cq;
1871 mcc_compl = queue_tail_node(mcc_cq);
1872 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1873 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301874 if (beiscsi_hba_in_error(phba))
1875 return;
1876
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301877 if (num_processed >= 32) {
1878 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301879 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301880 num_processed = 0;
1881 }
1882 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05301883 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301884 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05301885 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301886 }
1887
1888 mcc_compl->flags = 0;
1889 queue_tail_inc(mcc_cq);
1890 mcc_compl = queue_tail_node(mcc_cq);
1891 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1892 num_processed++;
1893 }
1894
1895 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301896 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301897}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301898
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301899static void beiscsi_mcc_work(struct work_struct *work)
1900{
1901 struct be_eq_obj *pbe_eq;
1902 struct beiscsi_hba *phba;
1903
1904 pbe_eq = container_of(work, struct be_eq_obj, mcc_work);
1905 phba = pbe_eq->phba;
1906 beiscsi_process_mcc_cq(phba);
1907 /* rearm EQ for further interrupts */
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301908 if (!beiscsi_hba_in_error(phba))
1909 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05301910}
1911
John Soni Jose6763daa2012-10-20 04:41:45 +05301912/**
1913 * beiscsi_process_cq()- Process the Completion Queue
1914 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301915 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05301916 *
1917 * return
1918 * Number of Completion Entries processed.
1919 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301920unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301921{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301922 struct be_queue_info *cq;
1923 struct sol_cqe *sol;
1924 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301925 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301926 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301927 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001928 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301929 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301930 struct beiscsi_endpoint *beiscsi_ep;
1931 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301932 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301933
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301934 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301935 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301936 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301937
1938 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1939 CQE_VALID_MASK) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05301940 if (beiscsi_hba_in_error(phba))
1941 return 0;
1942
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301943 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1944
John Soni Jose73133262012-10-20 04:44:49 +05301945 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
1946 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301947
John Soni Jose73133262012-10-20 04:44:49 +05301948 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001949 if (is_chip_be2_be3r(phba)) {
1950 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
1951 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301952 if ((code == DRIVERMSG_NOTIFY) ||
1953 (code == UNSOL_HDR_NOTIFY) ||
1954 (code == UNSOL_DATA_NOTIFY))
1955 cid = AMAP_GET_BITS(
1956 struct amap_i_t_dpdu_cqe_v2,
1957 cid, sol);
1958 else
1959 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1960 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001961 }
John Soni Jose73133262012-10-20 04:44:49 +05301962
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001963 cri_index = BE_GET_CRI_FROM_CID(cid);
1964 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04001965
1966 if (ep == NULL) {
1967 /* connection has already been freed
1968 * just move on to next one
1969 */
1970 beiscsi_log(phba, KERN_WARNING,
1971 BEISCSI_LOG_INIT,
1972 "BM_%d : proc cqe of disconn ep: cid %d\n",
1973 cid);
1974 goto proc_next_cqe;
1975 }
1976
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301977 beiscsi_ep = ep->dd_data;
1978 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301979
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301980 /* replenish cq */
1981 if (num_processed == 32) {
1982 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301983 num_processed = 0;
1984 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301985 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301986
John Soni Jose0a513dd2012-08-20 23:00:55 +05301987 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301988 case SOL_CMD_COMPLETE:
1989 hwi_complete_cmd(beiscsi_conn, phba, sol);
1990 break;
1991 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301992 beiscsi_log(phba, KERN_INFO,
1993 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05301994 "BM_%d : Received %s[%d] on CID : %d\n",
1995 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301996
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301997 dmsg = (struct dmsg_cqe *)sol;
1998 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1999 break;
2000 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302001 beiscsi_log(phba, KERN_INFO,
2002 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302003 "BM_%d : Received %s[%d] on CID : %d\n",
2004 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302005
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002006 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302007 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2008 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002009 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302010 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302011 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302012 beiscsi_log(phba, KERN_INFO,
2013 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302014 "BM_%d : Received %s[%d] on CID : %d\n",
2015 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302016
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002017 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2019 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002020 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302021 break;
2022 case CXN_INVALIDATE_INDEX_NOTIFY:
2023 case CMD_INVALIDATED_NOTIFY:
2024 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302025 beiscsi_log(phba, KERN_ERR,
2026 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302027 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2028 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302029 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302030 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302031 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302032 beiscsi_log(phba, KERN_ERR,
2033 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2034 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2035 cqe_desc[code], code, cid);
2036 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302037 case CMD_KILLED_INVALID_STATSN_RCVD:
2038 case CMD_KILLED_INVALID_R2T_RCVD:
2039 case CMD_CXN_KILLED_LUN_INVALID:
2040 case CMD_CXN_KILLED_ICD_INVALID:
2041 case CMD_CXN_KILLED_ITT_INVALID:
2042 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2043 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302044 beiscsi_log(phba, KERN_ERR,
2045 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302046 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2047 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302048 break;
2049 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302050 beiscsi_log(phba, KERN_ERR,
2051 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302052 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2053 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002054 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302055 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2056 (struct i_t_dpdu_cqe *) sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002057 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302058 break;
2059 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2060 case CXN_KILLED_BURST_LEN_MISMATCH:
2061 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302062 case CXN_KILLED_UNKNOWN_HDR:
2063 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2064 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2065 case CXN_KILLED_TIMED_OUT:
2066 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302067 case CXN_KILLED_RST_SENT:
2068 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302069 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2070 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2071 case CXN_KILLED_OVER_RUN_RESIDUAL:
2072 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2073 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302074 beiscsi_log(phba, KERN_ERR,
2075 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302076 "BM_%d : Event %s[%d] received on CID : %d\n",
2077 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302078 if (beiscsi_conn)
2079 iscsi_conn_failure(beiscsi_conn->conn,
2080 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302081 break;
2082 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302083 beiscsi_log(phba, KERN_ERR,
2084 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302085 "BM_%d : Invalid CQE Event Received Code : %d"
2086 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302087 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302088 break;
2089 }
2090
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002091proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302092 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2093 queue_tail_inc(cq);
2094 sol = queue_tail_node(cq);
2095 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302096 if (total == budget)
2097 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302098 }
2099
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302100 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2101 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302102}
2103
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002104static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302105{
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302106 unsigned int ret, io_events;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302107 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302108 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302109 struct be_eq_entry *eqe = NULL;
2110 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302111
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302112 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302113 phba = pbe_eq->phba;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302114 if (beiscsi_hba_in_error(phba)) {
2115 irq_poll_complete(iop);
2116 return 0;
2117 }
2118
2119 io_events = 0;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302120 eq = &pbe_eq->q;
2121 eqe = queue_tail_node(eq);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302122 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2123 EQE_VALID_MASK) {
2124 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2125 queue_tail_inc(eq);
2126 eqe = queue_tail_node(eq);
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302127 io_events++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302128 }
Jitendra Bhivarea30950162016-08-19 15:20:10 +05302129 hwi_ring_eq_db(phba, eq->id, 1, io_events, 0, 1);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302130
2131 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002132 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302133 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002134 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302135 beiscsi_log(phba, KERN_INFO,
2136 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302137 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2138 pbe_eq->q.id, ret);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05302139 if (!beiscsi_hba_in_error(phba))
2140 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302141 }
2142 return ret;
2143}
2144
2145static void
John Soni Jose09a10932012-10-20 04:44:23 +05302146hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2147 unsigned int num_sg, struct beiscsi_io_task *io_task)
2148{
2149 struct iscsi_sge *psgl;
2150 unsigned int sg_len, index;
2151 unsigned int sge_len = 0;
2152 unsigned long long addr;
2153 struct scatterlist *l_sg;
2154 unsigned int offset;
2155
2156 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2157 io_task->bhs_pa.u.a32.address_lo);
2158 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2159 io_task->bhs_pa.u.a32.address_hi);
2160
2161 l_sg = sg;
2162 for (index = 0; (index < num_sg) && (index < 2); index++,
2163 sg = sg_next(sg)) {
2164 if (index == 0) {
2165 sg_len = sg_dma_len(sg);
2166 addr = (u64) sg_dma_address(sg);
2167 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2168 sge0_addr_lo, pwrb,
2169 lower_32_bits(addr));
2170 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2171 sge0_addr_hi, pwrb,
2172 upper_32_bits(addr));
2173 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2174 sge0_len, pwrb,
2175 sg_len);
2176 sge_len = sg_len;
2177 } else {
2178 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2179 pwrb, sge_len);
2180 sg_len = sg_dma_len(sg);
2181 addr = (u64) sg_dma_address(sg);
2182 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2183 sge1_addr_lo, pwrb,
2184 lower_32_bits(addr));
2185 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2186 sge1_addr_hi, pwrb,
2187 upper_32_bits(addr));
2188 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2189 sge1_len, pwrb,
2190 sg_len);
2191 }
2192 }
2193 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2194 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2195
2196 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2197
2198 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2199 io_task->bhs_pa.u.a32.address_hi);
2200 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2201 io_task->bhs_pa.u.a32.address_lo);
2202
2203 if (num_sg == 1) {
2204 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2205 1);
2206 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2207 0);
2208 } else if (num_sg == 2) {
2209 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2210 0);
2211 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2212 1);
2213 } else {
2214 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2215 0);
2216 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2217 0);
2218 }
2219
2220 sg = l_sg;
2221 psgl++;
2222 psgl++;
2223 offset = 0;
2224 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2225 sg_len = sg_dma_len(sg);
2226 addr = (u64) sg_dma_address(sg);
2227 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2228 lower_32_bits(addr));
2229 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2230 upper_32_bits(addr));
2231 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2232 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2233 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2234 offset += sg_len;
2235 }
2236 psgl--;
2237 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2238}
2239
2240static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302241hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2242 unsigned int num_sg, struct beiscsi_io_task *io_task)
2243{
2244 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302245 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302246 unsigned int sge_len = 0;
2247 unsigned long long addr;
2248 struct scatterlist *l_sg;
2249 unsigned int offset;
2250
2251 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2252 io_task->bhs_pa.u.a32.address_lo);
2253 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2254 io_task->bhs_pa.u.a32.address_hi);
2255
2256 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302257 for (index = 0; (index < num_sg) && (index < 2); index++,
2258 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302259 if (index == 0) {
2260 sg_len = sg_dma_len(sg);
2261 addr = (u64) sg_dma_address(sg);
2262 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302263 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302264 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302265 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302266 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2267 sg_len);
2268 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302269 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302270 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2271 pwrb, sge_len);
2272 sg_len = sg_dma_len(sg);
2273 addr = (u64) sg_dma_address(sg);
2274 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302275 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302276 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302277 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302278 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2279 sg_len);
2280 }
2281 }
2282 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2283 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2284
2285 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2286
2287 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2288 io_task->bhs_pa.u.a32.address_hi);
2289 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2290 io_task->bhs_pa.u.a32.address_lo);
2291
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302292 if (num_sg == 1) {
2293 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2294 1);
2295 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2296 0);
2297 } else if (num_sg == 2) {
2298 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2299 0);
2300 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2301 1);
2302 } else {
2303 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2304 0);
2305 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2306 0);
2307 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302308 sg = l_sg;
2309 psgl++;
2310 psgl++;
2311 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302312 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302313 sg_len = sg_dma_len(sg);
2314 addr = (u64) sg_dma_address(sg);
2315 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2316 (addr & 0xFFFFFFFF));
2317 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2318 (addr >> 32));
2319 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2320 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2321 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2322 offset += sg_len;
2323 }
2324 psgl--;
2325 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2326}
2327
John Soni Josed629c472012-10-20 04:42:00 +05302328/**
2329 * hwi_write_buffer()- Populate the WRB with task info
2330 * @pwrb: ptr to the WRB entry
2331 * @task: iscsi task which is to be executed
2332 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002333static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302334{
2335 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302336 struct beiscsi_io_task *io_task = task->dd_data;
2337 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2338 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302339 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302340
2341 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2342 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2343 io_task->bhs_pa.u.a32.address_lo);
2344 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2345 io_task->bhs_pa.u.a32.address_hi);
2346
2347 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302348
2349 /* Check for the data_count */
2350 dsp_value = (task->data_count) ? 1 : 0;
2351
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002352 if (is_chip_be2_be3r(phba))
2353 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302354 pwrb, dsp_value);
2355 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002356 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302357 pwrb, dsp_value);
2358
2359 /* Map addr only if there is data_count */
2360 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302361 io_task->mtask_addr = pci_map_single(phba->pcidev,
2362 task->data,
2363 task->data_count,
2364 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002365 if (pci_dma_mapping_error(phba->pcidev,
2366 io_task->mtask_addr))
2367 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302368 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302369 } else
John Soni Josed629c472012-10-20 04:42:00 +05302370 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302371
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302372 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302373 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302374 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302375 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302376 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2377 task->data_count);
2378
2379 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2380 } else {
2381 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302382 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302383 }
2384
2385 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2386
2387 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2388
2389 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2390 io_task->bhs_pa.u.a32.address_hi);
2391 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2392 io_task->bhs_pa.u.a32.address_lo);
2393 if (task->data) {
2394 psgl++;
2395 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2396 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2397 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2398 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2399 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2400 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2401
2402 psgl++;
2403 if (task->data) {
2404 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302405 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302406 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302407 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302408 }
2409 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2410 }
2411 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002412 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302413}
2414
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002415/**
2416 * beiscsi_find_mem_req()- Find mem needed
2417 * @phba: ptr to HBA struct
2418 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302419static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2420{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002421 uint8_t mem_descr_index, ulp_num;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302422 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302423 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2424 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2425
2426 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2427 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302428
2429 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2430
2431 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2432 BE_ISCSI_PDU_HEADER_SIZE;
2433 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2434 sizeof(struct hwi_context_memory);
2435
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302436
2437 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2438 * (phba->params.wrbs_per_cxn)
2439 * phba->params.cxns_per_ctrl;
2440 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2441 (phba->params.wrbs_per_cxn);
2442 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2443 phba->params.cxns_per_ctrl);
2444
2445 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2446 phba->params.icds_per_ctrl;
2447 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2448 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002449 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2450 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302451
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002452 num_async_pdu_buf_sgl_pages =
2453 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2454 phba, ulp_num) *
2455 sizeof(struct phys_addr));
2456
2457 num_async_pdu_buf_pages =
2458 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2459 phba, ulp_num) *
2460 phba->params.defpdu_hdr_sz);
2461
2462 num_async_pdu_data_pages =
2463 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2464 phba, ulp_num) *
2465 phba->params.defpdu_data_sz);
2466
2467 num_async_pdu_data_sgl_pages =
2468 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2469 phba, ulp_num) *
2470 sizeof(struct phys_addr));
2471
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002472 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2473 (ulp_num * MEM_DESCR_OFFSET));
2474 phba->mem_req[mem_descr_index] =
2475 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2476 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2477
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002478 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2479 (ulp_num * MEM_DESCR_OFFSET));
2480 phba->mem_req[mem_descr_index] =
2481 num_async_pdu_buf_pages *
2482 PAGE_SIZE;
2483
2484 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2485 (ulp_num * MEM_DESCR_OFFSET));
2486 phba->mem_req[mem_descr_index] =
2487 num_async_pdu_data_pages *
2488 PAGE_SIZE;
2489
2490 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2491 (ulp_num * MEM_DESCR_OFFSET));
2492 phba->mem_req[mem_descr_index] =
2493 num_async_pdu_buf_sgl_pages *
2494 PAGE_SIZE;
2495
2496 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2497 (ulp_num * MEM_DESCR_OFFSET));
2498 phba->mem_req[mem_descr_index] =
2499 num_async_pdu_data_sgl_pages *
2500 PAGE_SIZE;
2501
2502 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2503 (ulp_num * MEM_DESCR_OFFSET));
2504 phba->mem_req[mem_descr_index] =
2505 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2506 sizeof(struct async_pdu_handle);
2507
2508 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2509 (ulp_num * MEM_DESCR_OFFSET));
2510 phba->mem_req[mem_descr_index] =
2511 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2512 sizeof(struct async_pdu_handle);
2513
2514 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2515 (ulp_num * MEM_DESCR_OFFSET));
2516 phba->mem_req[mem_descr_index] =
2517 sizeof(struct hwi_async_pdu_context) +
2518 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2519 sizeof(struct hwi_async_entry));
2520 }
2521 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302522}
2523
2524static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2525{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302526 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002527 struct hwi_controller *phwi_ctrlr;
2528 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302529 struct mem_array *mem_arr, *mem_arr_orig;
2530 unsigned int i, j, alloc_size, curr_alloc_size;
2531
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002532 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302533 if (!phba->phwi_ctrlr)
2534 return -ENOMEM;
2535
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002536 /* Allocate memory for wrb_context */
2537 phwi_ctrlr = phba->phwi_ctrlr;
2538 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2539 phba->params.cxns_per_ctrl,
2540 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002541 if (!phwi_ctrlr->wrb_context) {
2542 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002543 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002544 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002545
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302546 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2547 GFP_KERNEL);
2548 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002549 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302550 kfree(phba->phwi_ctrlr);
2551 return -ENOMEM;
2552 }
2553
2554 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2555 GFP_KERNEL);
2556 if (!mem_arr_orig) {
2557 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002558 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302559 kfree(phba->phwi_ctrlr);
2560 return -ENOMEM;
2561 }
2562
2563 mem_descr = phba->init_mem;
2564 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002565 if (!phba->mem_req[i]) {
2566 mem_descr->mem_array = NULL;
2567 mem_descr++;
2568 continue;
2569 }
2570
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302571 j = 0;
2572 mem_arr = mem_arr_orig;
2573 alloc_size = phba->mem_req[i];
2574 memset(mem_arr, 0, sizeof(struct mem_array) *
2575 BEISCSI_MAX_FRAGS_INIT);
2576 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2577 do {
2578 mem_arr->virtual_address = pci_alloc_consistent(
2579 phba->pcidev,
2580 curr_alloc_size,
2581 &bus_add);
2582 if (!mem_arr->virtual_address) {
2583 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2584 goto free_mem;
2585 if (curr_alloc_size -
2586 rounddown_pow_of_two(curr_alloc_size))
2587 curr_alloc_size = rounddown_pow_of_two
2588 (curr_alloc_size);
2589 else
2590 curr_alloc_size = curr_alloc_size / 2;
2591 } else {
2592 mem_arr->bus_address.u.
2593 a64.address = (__u64) bus_add;
2594 mem_arr->size = curr_alloc_size;
2595 alloc_size -= curr_alloc_size;
2596 curr_alloc_size = min(be_max_phys_size *
2597 1024, alloc_size);
2598 j++;
2599 mem_arr++;
2600 }
2601 } while (alloc_size);
2602 mem_descr->num_elements = j;
2603 mem_descr->size_in_bytes = phba->mem_req[i];
2604 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2605 GFP_KERNEL);
2606 if (!mem_descr->mem_array)
2607 goto free_mem;
2608
2609 memcpy(mem_descr->mem_array, mem_arr_orig,
2610 sizeof(struct mem_array) * j);
2611 mem_descr++;
2612 }
2613 kfree(mem_arr_orig);
2614 return 0;
2615free_mem:
2616 mem_descr->num_elements = j;
2617 while ((i) || (j)) {
2618 for (j = mem_descr->num_elements; j > 0; j--) {
2619 pci_free_consistent(phba->pcidev,
2620 mem_descr->mem_array[j - 1].size,
2621 mem_descr->mem_array[j - 1].
2622 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302623 (unsigned long)mem_descr->
2624 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302625 bus_address.u.a64.address);
2626 }
2627 if (i) {
2628 i--;
2629 kfree(mem_descr->mem_array);
2630 mem_descr--;
2631 }
2632 }
2633 kfree(mem_arr_orig);
2634 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002635 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302636 kfree(phba->phwi_ctrlr);
2637 return -ENOMEM;
2638}
2639
2640static int beiscsi_get_memory(struct beiscsi_hba *phba)
2641{
2642 beiscsi_find_mem_req(phba);
2643 return beiscsi_alloc_mem(phba);
2644}
2645
2646static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2647{
2648 struct pdu_data_out *pdata_out;
2649 struct pdu_nop_out *pnop_out;
2650 struct be_mem_descriptor *mem_descr;
2651
2652 mem_descr = phba->init_mem;
2653 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2654 pdata_out =
2655 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2656 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2657
2658 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2659 IIOC_SCSI_DATA);
2660
2661 pnop_out =
2662 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2663 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2664
2665 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2666 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2667 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2668 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2669}
2670
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002671static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302672{
2673 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002674 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002675 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302676 struct hwi_controller *phwi_ctrlr;
2677 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002678 struct iscsi_wrb *pwrb = NULL;
2679 unsigned int num_cxn_wrbh = 0;
2680 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302681
2682 mem_descr_wrbh = phba->init_mem;
2683 mem_descr_wrbh += HWI_MEM_WRBH;
2684
2685 mem_descr_wrb = phba->init_mem;
2686 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302687 phwi_ctrlr = phba->phwi_ctrlr;
2688
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002689 /* Allocate memory for WRBQ */
2690 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2691 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002692 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002693 GFP_KERNEL);
2694 if (!phwi_ctxt->be_wrbq) {
2695 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2696 "BM_%d : WRBQ Mem Alloc Failed\n");
2697 return -ENOMEM;
2698 }
2699
2700 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302701 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302702 pwrb_context->pwrb_handle_base =
2703 kzalloc(sizeof(struct wrb_handle *) *
2704 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002705 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302706 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2707 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002708 goto init_wrb_hndl_failed;
2709 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302710 pwrb_context->pwrb_handle_basestd =
2711 kzalloc(sizeof(struct wrb_handle *) *
2712 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002713 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302714 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2715 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002716 goto init_wrb_hndl_failed;
2717 }
2718 if (!num_cxn_wrbh) {
2719 pwrb_handle =
2720 mem_descr_wrbh->mem_array[idx].virtual_address;
2721 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2722 ((sizeof(struct wrb_handle)) *
2723 phba->params.wrbs_per_cxn));
2724 idx++;
2725 }
2726 pwrb_context->alloc_index = 0;
2727 pwrb_context->wrb_handles_available = 0;
2728 pwrb_context->free_index = 0;
2729
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302730 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302731 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2732 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2733 pwrb_context->pwrb_handle_basestd[j] =
2734 pwrb_handle;
2735 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302736 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302737 pwrb_handle++;
2738 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302739 num_cxn_wrbh--;
2740 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302741 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302742 }
2743 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002744 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302745 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002746 if (!num_cxn_wrb) {
2747 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2748 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2749 ((sizeof(struct iscsi_wrb) *
2750 phba->params.wrbs_per_cxn));
2751 idx++;
2752 }
2753
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302754 if (num_cxn_wrb) {
2755 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2756 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2757 pwrb_handle->pwrb = pwrb;
2758 pwrb++;
2759 }
2760 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302761 }
2762 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002763 return 0;
2764init_wrb_hndl_failed:
2765 for (j = index; j > 0; j--) {
2766 pwrb_context = &phwi_ctrlr->wrb_context[j];
2767 kfree(pwrb_context->pwrb_handle_base);
2768 kfree(pwrb_context->pwrb_handle_basestd);
2769 }
2770 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302771}
2772
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002773static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302774{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002775 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302776 struct hwi_controller *phwi_ctrlr;
2777 struct hba_parameters *p = &phba->params;
2778 struct hwi_async_pdu_context *pasync_ctx;
2779 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002780 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302781 struct be_mem_descriptor *mem_descr;
2782
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002783 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2784 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302785
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002786 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2787 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2788 (ulp_num * MEM_DESCR_OFFSET));
2789
2790 phwi_ctrlr = phba->phwi_ctrlr;
2791 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
2792 (struct hwi_async_pdu_context *)
2793 mem_descr->mem_array[0].virtual_address;
2794
2795 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2796 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2797
2798 pasync_ctx->async_entry =
2799 (struct hwi_async_entry *)
2800 ((long unsigned int)pasync_ctx +
2801 sizeof(struct hwi_async_pdu_context));
2802
2803 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2804 ulp_num);
2805 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
2806
2807 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2808 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2809 (ulp_num * MEM_DESCR_OFFSET);
2810 if (mem_descr->mem_array[0].virtual_address) {
2811 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2812 "BM_%d : hwi_init_async_pdu_ctx"
2813 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2814 ulp_num,
2815 mem_descr->mem_array[0].
2816 virtual_address);
2817 } else
2818 beiscsi_log(phba, KERN_WARNING,
2819 BEISCSI_LOG_INIT,
2820 "BM_%d : No Virtual address for ULP : %d\n",
2821 ulp_num);
2822
2823 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302824 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302825
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002826 pasync_ctx->async_header.pa_base.u.a64.address =
2827 mem_descr->mem_array[0].
2828 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002829
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002830 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2831 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2832 (ulp_num * MEM_DESCR_OFFSET);
2833 if (mem_descr->mem_array[0].virtual_address) {
2834 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2835 "BM_%d : hwi_init_async_pdu_ctx"
2836 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
2837 ulp_num,
2838 mem_descr->mem_array[0].
2839 virtual_address);
2840 } else
2841 beiscsi_log(phba, KERN_WARNING,
2842 BEISCSI_LOG_INIT,
2843 "BM_%d : No Virtual address for ULP : %d\n",
2844 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302845
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002846 pasync_ctx->async_header.ring_base =
2847 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302848
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002849 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2850 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2851 (ulp_num * MEM_DESCR_OFFSET);
2852 if (mem_descr->mem_array[0].virtual_address) {
2853 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2854 "BM_%d : hwi_init_async_pdu_ctx"
2855 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
2856 ulp_num,
2857 mem_descr->mem_array[0].
2858 virtual_address);
2859 } else
2860 beiscsi_log(phba, KERN_WARNING,
2861 BEISCSI_LOG_INIT,
2862 "BM_%d : No Virtual address for ULP : %d\n",
2863 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302864
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002865 pasync_ctx->async_header.handle_base =
2866 mem_descr->mem_array[0].virtual_address;
2867 pasync_ctx->async_header.writables = 0;
2868 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302869
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002870 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2871 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
2872 (ulp_num * MEM_DESCR_OFFSET);
2873 if (mem_descr->mem_array[0].virtual_address) {
2874 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2875 "BM_%d : hwi_init_async_pdu_ctx"
2876 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
2877 ulp_num,
2878 mem_descr->mem_array[0].
2879 virtual_address);
2880 } else
2881 beiscsi_log(phba, KERN_WARNING,
2882 BEISCSI_LOG_INIT,
2883 "BM_%d : No Virtual address for ULP : %d\n",
2884 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302885
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002886 pasync_ctx->async_data.ring_base =
2887 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302888
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002889 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2890 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2891 (ulp_num * MEM_DESCR_OFFSET);
2892 if (!mem_descr->mem_array[0].virtual_address)
2893 beiscsi_log(phba, KERN_WARNING,
2894 BEISCSI_LOG_INIT,
2895 "BM_%d : No Virtual address for ULP : %d\n",
2896 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302897
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002898 pasync_ctx->async_data.handle_base =
2899 mem_descr->mem_array[0].virtual_address;
2900 pasync_ctx->async_data.writables = 0;
2901 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302902
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002903 pasync_header_h =
2904 (struct async_pdu_handle *)
2905 pasync_ctx->async_header.handle_base;
2906 pasync_data_h =
2907 (struct async_pdu_handle *)
2908 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302909
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002910 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2911 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2912 (ulp_num * MEM_DESCR_OFFSET);
2913 if (mem_descr->mem_array[0].virtual_address) {
2914 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2915 "BM_%d : hwi_init_async_pdu_ctx"
2916 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
2917 ulp_num,
2918 mem_descr->mem_array[0].
2919 virtual_address);
2920 } else
2921 beiscsi_log(phba, KERN_WARNING,
2922 BEISCSI_LOG_INIT,
2923 "BM_%d : No Virtual address for ULP : %d\n",
2924 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302925
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002926 idx = 0;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002927 pasync_ctx->async_data.va_base =
2928 mem_descr->mem_array[idx].virtual_address;
2929 pasync_ctx->async_data.pa_base.u.a64.address =
2930 mem_descr->mem_array[idx].
2931 bus_address.u.a64.address;
2932
2933 num_async_data = ((mem_descr->mem_array[idx].size) /
2934 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002935 num_per_mem = 0;
2936
2937 for (index = 0; index < BEISCSI_GET_CID_COUNT
2938 (phba, ulp_num); index++) {
2939 pasync_header_h->cri = -1;
2940 pasync_header_h->index = (char)index;
2941 INIT_LIST_HEAD(&pasync_header_h->link);
2942 pasync_header_h->pbuffer =
2943 (void *)((unsigned long)
2944 (pasync_ctx->
2945 async_header.va_base) +
2946 (p->defpdu_hdr_sz * index));
2947
2948 pasync_header_h->pa.u.a64.address =
2949 pasync_ctx->async_header.pa_base.u.a64.
2950 address + (p->defpdu_hdr_sz * index);
2951
2952 list_add_tail(&pasync_header_h->link,
2953 &pasync_ctx->async_header.
2954 free_list);
2955 pasync_header_h++;
2956 pasync_ctx->async_header.free_entries++;
2957 pasync_ctx->async_header.writables++;
2958
2959 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2960 wait_queue.list);
2961 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2962 header_busy_list);
2963 pasync_data_h->cri = -1;
2964 pasync_data_h->index = (char)index;
2965 INIT_LIST_HEAD(&pasync_data_h->link);
2966
2967 if (!num_async_data) {
2968 num_per_mem = 0;
2969 idx++;
2970 pasync_ctx->async_data.va_base =
2971 mem_descr->mem_array[idx].
2972 virtual_address;
2973 pasync_ctx->async_data.pa_base.u.
2974 a64.address =
2975 mem_descr->mem_array[idx].
2976 bus_address.u.a64.address;
2977 num_async_data =
2978 ((mem_descr->mem_array[idx].
2979 size) /
2980 phba->params.defpdu_data_sz);
2981 }
2982 pasync_data_h->pbuffer =
2983 (void *)((unsigned long)
2984 (pasync_ctx->async_data.va_base) +
2985 (p->defpdu_data_sz * num_per_mem));
2986
2987 pasync_data_h->pa.u.a64.address =
2988 pasync_ctx->async_data.pa_base.u.a64.
2989 address + (p->defpdu_data_sz *
2990 num_per_mem);
2991 num_per_mem++;
2992 num_async_data--;
2993
2994 list_add_tail(&pasync_data_h->link,
2995 &pasync_ctx->async_data.
2996 free_list);
2997 pasync_data_h++;
2998 pasync_ctx->async_data.free_entries++;
2999 pasync_ctx->async_data.writables++;
3000
3001 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3002 data_busy_list);
3003 }
3004
3005 pasync_ctx->async_header.host_write_ptr = 0;
3006 pasync_ctx->async_header.ep_read_ptr = -1;
3007 pasync_ctx->async_data.host_write_ptr = 0;
3008 pasync_ctx->async_data.ep_read_ptr = -1;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05003009 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303010 }
3011
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003012 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303013}
3014
3015static int
3016be_sgl_create_contiguous(void *virtual_address,
3017 u64 physical_address, u32 length,
3018 struct be_dma_mem *sgl)
3019{
3020 WARN_ON(!virtual_address);
3021 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06003022 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303023 WARN_ON(!sgl);
3024
3025 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303026 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303027 sgl->size = length;
3028
3029 return 0;
3030}
3031
3032static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
3033{
3034 memset(sgl, 0, sizeof(*sgl));
3035}
3036
3037static void
3038hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
3039 struct mem_array *pmem, struct be_dma_mem *sgl)
3040{
3041 if (sgl->va)
3042 be_sgl_destroy_contiguous(sgl);
3043
3044 be_sgl_create_contiguous(pmem->virtual_address,
3045 pmem->bus_address.u.a64.address,
3046 pmem->size, sgl);
3047}
3048
3049static void
3050hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
3051 struct mem_array *pmem, struct be_dma_mem *sgl)
3052{
3053 if (sgl->va)
3054 be_sgl_destroy_contiguous(sgl);
3055
3056 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
3057 pmem->bus_address.u.a64.address,
3058 pmem->size, sgl);
3059}
3060
3061static int be_fill_queue(struct be_queue_info *q,
3062 u16 len, u16 entry_size, void *vaddress)
3063{
3064 struct be_dma_mem *mem = &q->dma_mem;
3065
3066 memset(q, 0, sizeof(*q));
3067 q->len = len;
3068 q->entry_size = entry_size;
3069 mem->size = len * entry_size;
3070 mem->va = vaddress;
3071 if (!mem->va)
3072 return -ENOMEM;
3073 memset(mem->va, 0, mem->size);
3074 return 0;
3075}
3076
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303077static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303078 struct hwi_context_memory *phwi_context)
3079{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303080 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303081 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303082 struct be_queue_info *eq;
3083 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303084 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303085 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303086
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303087 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3088 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303089
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303090 if (phba->msix_enabled)
3091 eq_for_mcc = 1;
3092 else
3093 eq_for_mcc = 0;
3094 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3095 eq = &phwi_context->be_eq[i].q;
3096 mem = &eq->dma_mem;
3097 phwi_context->be_eq[i].phba = phba;
3098 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3099 num_eq_pages * PAGE_SIZE,
3100 &paddr);
3101 if (!eq_vaddress)
3102 goto create_eq_error;
3103
3104 mem->va = eq_vaddress;
3105 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3106 sizeof(struct be_eq_entry), eq_vaddress);
3107 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303108 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3109 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303110 goto create_eq_error;
3111 }
3112
3113 mem->dma = paddr;
3114 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3115 phwi_context->cur_eqd);
3116 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303117 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3118 "BM_%d : beiscsi_cmd_eq_create"
3119 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303120 goto create_eq_error;
3121 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303122
3123 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3124 "BM_%d : eqid = %d\n",
3125 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303126 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303127 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303128create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303129 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303130 eq = &phwi_context->be_eq[i].q;
3131 mem = &eq->dma_mem;
3132 if (mem->va)
3133 pci_free_consistent(phba->pcidev, num_eq_pages
3134 * PAGE_SIZE,
3135 mem->va, mem->dma);
3136 }
3137 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303138}
3139
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303140static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303141 struct hwi_context_memory *phwi_context)
3142{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303143 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303144 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303145 struct be_queue_info *cq, *eq;
3146 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303147 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303148 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303149 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303150
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303151 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3152 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303153
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303154 for (i = 0; i < phba->num_cpus; i++) {
3155 cq = &phwi_context->be_cq[i];
3156 eq = &phwi_context->be_eq[i].q;
3157 pbe_eq = &phwi_context->be_eq[i];
3158 pbe_eq->cq = cq;
3159 pbe_eq->phba = phba;
3160 mem = &cq->dma_mem;
3161 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3162 num_cq_pages * PAGE_SIZE,
3163 &paddr);
3164 if (!cq_vaddress)
3165 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303166 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303167 sizeof(struct sol_cqe), cq_vaddress);
3168 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303169 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3170 "BM_%d : be_fill_queue Failed "
3171 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303172 goto create_cq_error;
3173 }
3174
3175 mem->dma = paddr;
3176 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3177 false, 0);
3178 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303179 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3180 "BM_%d : beiscsi_cmd_eq_create"
3181 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303182 goto create_cq_error;
3183 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303184 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3185 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3186 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303187 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303188 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303189
3190create_cq_error:
3191 for (i = 0; i < phba->num_cpus; i++) {
3192 cq = &phwi_context->be_cq[i];
3193 mem = &cq->dma_mem;
3194 if (mem->va)
3195 pci_free_consistent(phba->pcidev, num_cq_pages
3196 * PAGE_SIZE,
3197 mem->va, mem->dma);
3198 }
3199 return ret;
3200
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303201}
3202
3203static int
3204beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3205 struct hwi_context_memory *phwi_context,
3206 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003207 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303208{
3209 unsigned int idx;
3210 int ret;
3211 struct be_queue_info *dq, *cq;
3212 struct be_dma_mem *mem;
3213 struct be_mem_descriptor *mem_descr;
3214 void *dq_vaddress;
3215
3216 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003217 dq = &phwi_context->be_def_hdrq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303218 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303219 mem = &dq->dma_mem;
3220 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003221 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3222 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303223 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3224 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3225 sizeof(struct phys_addr),
3226 sizeof(struct phys_addr), dq_vaddress);
3227 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303228 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003229 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3230 ulp_num);
3231
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303232 return ret;
3233 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303234 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3235 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303236 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3237 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003238 phba->params.defpdu_hdr_sz,
3239 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303240 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303241 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003242 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3243 ulp_num);
3244
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303245 return ret;
3246 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303247
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003248 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3249 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3250 ulp_num,
3251 phwi_context->be_def_hdrq[ulp_num].id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303252 return 0;
3253}
3254
3255static int
3256beiscsi_create_def_data(struct beiscsi_hba *phba,
3257 struct hwi_context_memory *phwi_context,
3258 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003259 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303260{
3261 unsigned int idx;
3262 int ret;
3263 struct be_queue_info *dataq, *cq;
3264 struct be_dma_mem *mem;
3265 struct be_mem_descriptor *mem_descr;
3266 void *dq_vaddress;
3267
3268 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003269 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303270 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303271 mem = &dataq->dma_mem;
3272 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003273 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3274 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303275 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3276 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3277 sizeof(struct phys_addr),
3278 sizeof(struct phys_addr), dq_vaddress);
3279 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303280 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003281 "BM_%d : be_fill_queue Failed for DEF PDU "
3282 "DATA on ULP : %d\n",
3283 ulp_num);
3284
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303285 return ret;
3286 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303287 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3288 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303289 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3290 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003291 phba->params.defpdu_data_sz,
3292 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303293 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303294 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3295 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003296 " Failed for DEF PDU DATA on ULP : %d\n",
3297 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303298 return ret;
3299 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303300
John Soni Jose99bc5d52012-08-20 23:00:18 +05303301 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003302 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3303 ulp_num,
3304 phwi_context->be_def_dataq[ulp_num].id);
3305
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003306 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3307 "BM_%d : DEFAULT PDU DATA RING CREATED"
3308 "on ULP : %d\n", ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303309 return 0;
3310}
3311
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003312
3313static int
3314beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3315{
3316 struct be_mem_descriptor *mem_descr;
3317 struct mem_array *pm_arr;
3318 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003319 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003320
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003321 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3322 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3323 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3324 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3325 (ulp_num * MEM_DESCR_OFFSET);
3326 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003327
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003328 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3329 status = be_cmd_iscsi_post_template_hdr(
3330 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003331
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003332 if (status != 0) {
3333 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3334 "BM_%d : Post Template HDR Failed for"
3335 "ULP_%d\n", ulp_num);
3336 return status;
3337 }
3338
3339 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3340 "BM_%d : Template HDR Pages Posted for"
3341 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003342 }
3343 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003344 return 0;
3345}
3346
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303347static int
3348beiscsi_post_pages(struct beiscsi_hba *phba)
3349{
3350 struct be_mem_descriptor *mem_descr;
3351 struct mem_array *pm_arr;
3352 unsigned int page_offset, i;
3353 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003354 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303355
3356 mem_descr = phba->init_mem;
3357 mem_descr += HWI_MEM_SGE;
3358 pm_arr = mem_descr->mem_array;
3359
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003360 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3361 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3362 break;
3363
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303364 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003365 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303366 for (i = 0; i < mem_descr->num_elements; i++) {
3367 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3368 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3369 page_offset,
3370 (pm_arr->size / PAGE_SIZE));
3371 page_offset += pm_arr->size / PAGE_SIZE;
3372 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303373 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3374 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303375 return status;
3376 }
3377 pm_arr++;
3378 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303379 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3380 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303381 return 0;
3382}
3383
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303384static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3385{
3386 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003387 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303388 pci_free_consistent(phba->pcidev, mem->size,
3389 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003390 mem->va = NULL;
3391 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303392}
3393
3394static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3395 u16 len, u16 entry_size)
3396{
3397 struct be_dma_mem *mem = &q->dma_mem;
3398
3399 memset(q, 0, sizeof(*q));
3400 q->len = len;
3401 q->entry_size = entry_size;
3402 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003403 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303404 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303405 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303406 return 0;
3407}
3408
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303409static int
3410beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3411 struct hwi_context_memory *phwi_context,
3412 struct hwi_controller *phwi_ctrlr)
3413{
3414 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3415 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003416 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303417 struct mem_array *pwrb_arr;
3418 void *wrb_vaddr;
3419 struct be_dma_mem sgl;
3420 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003421 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303422 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003423 uint8_t ulp_count = 0, ulp_base_num = 0;
3424 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303425
3426 idx = 0;
3427 mem_descr = phba->init_mem;
3428 mem_descr += HWI_MEM_WRB;
3429 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3430 GFP_KERNEL);
3431 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303432 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3433 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303434 return -ENOMEM;
3435 }
3436 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3437 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3438 num_wrb_rings = mem_descr->mem_array[idx].size /
3439 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3440
3441 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3442 if (num_wrb_rings) {
3443 pwrb_arr[num].virtual_address = wrb_vaddr;
3444 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3445 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3446 sizeof(struct iscsi_wrb);
3447 wrb_vaddr += pwrb_arr[num].size;
3448 pa_addr_lo += pwrb_arr[num].size;
3449 num_wrb_rings--;
3450 } else {
3451 idx++;
3452 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3453 pa_addr_lo = mem_descr->mem_array[idx].\
3454 bus_address.u.a64.address;
3455 num_wrb_rings = mem_descr->mem_array[idx].size /
3456 (phba->params.wrbs_per_cxn *
3457 sizeof(struct iscsi_wrb));
3458 pwrb_arr[num].virtual_address = wrb_vaddr;
3459 pwrb_arr[num].bus_address.u.a64.address\
3460 = pa_addr_lo;
3461 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3462 sizeof(struct iscsi_wrb);
3463 wrb_vaddr += pwrb_arr[num].size;
3464 pa_addr_lo += pwrb_arr[num].size;
3465 num_wrb_rings--;
3466 }
3467 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003468
3469 /* Get the ULP Count */
3470 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3471 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3472 ulp_count++;
3473 ulp_base_num = ulp_num;
3474 cid_count_ulp[ulp_num] =
3475 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3476 }
3477
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303478 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3479 wrb_mem_index = 0;
3480 offset = 0;
3481 size = 0;
3482
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003483 if (ulp_count > 1) {
3484 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3485
3486 if (!cid_count_ulp[ulp_base_num])
3487 ulp_base_num = (ulp_base_num + 1) %
3488 BEISCSI_ULP_COUNT;
3489
3490 cid_count_ulp[ulp_base_num]--;
3491 }
3492
3493
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303494 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3495 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003496 &phwi_context->be_wrbq[i],
3497 &phwi_ctrlr->wrb_context[i],
3498 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303499 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303500 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3501 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003502 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303503 return status;
3504 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003505 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003506 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303507 }
3508 kfree(pwrb_arr);
3509 return 0;
3510}
3511
3512static void free_wrb_handles(struct beiscsi_hba *phba)
3513{
3514 unsigned int index;
3515 struct hwi_controller *phwi_ctrlr;
3516 struct hwi_wrb_context *pwrb_context;
3517
3518 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003519 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303520 pwrb_context = &phwi_ctrlr->wrb_context[index];
3521 kfree(pwrb_context->pwrb_handle_base);
3522 kfree(pwrb_context->pwrb_handle_basestd);
3523 }
3524}
3525
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303526static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3527{
3528 struct be_queue_info *q;
3529 struct be_ctrl_info *ctrl = &phba->ctrl;
3530
3531 q = &phba->ctrl.mcc_obj.q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303532 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303533 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303534 be_queue_free(phba, q);
3535 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303536
3537 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303538 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303539 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303540 be_queue_free(phba, q);
3541 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303542}
3543
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303544static void hwi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303545{
3546 struct be_queue_info *q;
3547 struct be_ctrl_info *ctrl = &phba->ctrl;
3548 struct hwi_controller *phwi_ctrlr;
3549 struct hwi_context_memory *phwi_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003550 struct hwi_async_pdu_context *pasync_ctx;
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003551 int i, eq_for_mcc, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303552
3553 phwi_ctrlr = phba->phwi_ctrlr;
3554 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003555
3556 be_cmd_iscsi_remove_template_hdr(ctrl);
3557
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303558 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3559 q = &phwi_context->be_wrbq[i];
3560 if (q->created)
3561 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3562 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003563 kfree(phwi_context->be_wrbq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303564 free_wrb_handles(phba);
3565
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003566 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3567 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303568
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003569 q = &phwi_context->be_def_hdrq[ulp_num];
3570 if (q->created)
3571 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3572
3573 q = &phwi_context->be_def_dataq[ulp_num];
3574 if (q->created)
3575 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3576
3577 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3578 }
3579 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303580
3581 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3582
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303583 for (i = 0; i < (phba->num_cpus); i++) {
3584 q = &phwi_context->be_cq[i];
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303585 if (q->created) {
3586 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303587 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303588 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303589 }
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003590
3591 be_mcc_queues_destroy(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303592 if (phba->msix_enabled)
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003593 eq_for_mcc = 1;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303594 else
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003595 eq_for_mcc = 0;
3596 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303597 q = &phwi_context->be_eq[i].q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303598 if (q->created) {
3599 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303600 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303601 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303602 }
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303603 /* last communication, indicate driver is unloading */
3604 beiscsi_cmd_special_wrb(&phba->ctrl, 0);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303605}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303606
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303607static int be_mcc_queues_create(struct beiscsi_hba *phba,
3608 struct hwi_context_memory *phwi_context)
3609{
3610 struct be_queue_info *q, *cq;
3611 struct be_ctrl_info *ctrl = &phba->ctrl;
3612
3613 /* Alloc MCC compl queue */
3614 cq = &phba->ctrl.mcc_obj.cq;
3615 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3616 sizeof(struct be_mcc_compl)))
3617 goto err;
3618 /* Ask BE to create MCC compl queue; */
3619 if (phba->msix_enabled) {
3620 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3621 [phba->num_cpus].q, false, true, 0))
3622 goto mcc_cq_free;
3623 } else {
3624 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3625 false, true, 0))
3626 goto mcc_cq_free;
3627 }
3628
3629 /* Alloc MCC queue */
3630 q = &phba->ctrl.mcc_obj.q;
3631 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3632 goto mcc_cq_destroy;
3633
3634 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303635 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303636 goto mcc_q_free;
3637
3638 return 0;
3639
3640mcc_q_free:
3641 be_queue_free(phba, q);
3642mcc_cq_destroy:
3643 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3644mcc_cq_free:
3645 be_queue_free(phba, cq);
3646err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303647 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303648}
3649
John Soni Jose107dfcb2012-10-20 04:42:13 +05303650/**
3651 * find_num_cpus()- Get the CPU online count
3652 * @phba: ptr to priv structure
3653 *
3654 * CPU count is used for creating EQ.
3655 **/
3656static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303657{
3658 int num_cpus = 0;
3659
3660 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303661
John Soni Jose22abeef2012-10-20 04:43:32 +05303662 switch (phba->generation) {
3663 case BE_GEN2:
3664 case BE_GEN3:
3665 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3666 BEISCSI_MAX_NUM_CPUS : num_cpus;
3667 break;
3668 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003669 /*
3670 * If eqid_count == 1 fall back to
3671 * INTX mechanism
3672 **/
3673 if (phba->fw_config.eqid_count == 1) {
3674 enable_msix = 0;
3675 phba->num_cpus = 1;
3676 return;
3677 }
3678
3679 phba->num_cpus =
3680 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3681 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303682 break;
3683 default:
3684 phba->num_cpus = 1;
3685 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303686}
3687
3688static int hwi_init_port(struct beiscsi_hba *phba)
3689{
3690 struct hwi_controller *phwi_ctrlr;
3691 struct hwi_context_memory *phwi_context;
3692 unsigned int def_pdu_ring_sz;
3693 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003694 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303695
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303696 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303697 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003698 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303699 phwi_context->min_eqd = 0;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003700 phwi_context->cur_eqd = 0;
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303701 /* set port optic state to unknown */
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303702 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303703
3704 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303705 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303706 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3707 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303708 goto error;
3709 }
3710
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303711 status = be_mcc_queues_create(phba, phwi_context);
3712 if (status != 0)
3713 goto error;
3714
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303715 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303716 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303717 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3718 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303719 goto error;
3720 }
3721
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303722 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303723 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303724 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3725 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303726 goto error;
3727 }
3728
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003729 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3730 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303731
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003732 def_pdu_ring_sz =
3733 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3734 sizeof(struct phys_addr);
3735
3736 status = beiscsi_create_def_hdr(phba, phwi_context,
3737 phwi_ctrlr,
3738 def_pdu_ring_sz,
3739 ulp_num);
3740 if (status != 0) {
3741 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3742 "BM_%d : Default Header not created for ULP : %d\n",
3743 ulp_num);
3744 goto error;
3745 }
3746
3747 status = beiscsi_create_def_data(phba, phwi_context,
3748 phwi_ctrlr,
3749 def_pdu_ring_sz,
3750 ulp_num);
3751 if (status != 0) {
3752 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3753 "BM_%d : Default Data not created for ULP : %d\n",
3754 ulp_num);
3755 goto error;
3756 }
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05303757 /**
3758 * Now that the default PDU rings have been created,
3759 * let EP know about it.
3760 * Call beiscsi_cmd_iscsi_cleanup before posting?
3761 */
3762 hwi_post_async_buffers(phba, BEISCSI_DEFQ_HDR,
3763 ulp_num);
3764 hwi_post_async_buffers(phba, BEISCSI_DEFQ_DATA,
3765 ulp_num);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003766 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303767 }
3768
3769 status = beiscsi_post_pages(phba);
3770 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303771 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3772 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303773 goto error;
3774 }
3775
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003776 status = beiscsi_post_template_hdr(phba);
3777 if (status != 0) {
3778 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3779 "BM_%d : Template HDR Posting for CXN Failed\n");
3780 }
3781
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303782 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3783 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303784 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3785 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303786 goto error;
3787 }
3788
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003789 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3790 uint16_t async_arr_idx = 0;
3791
3792 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3793 uint16_t cri = 0;
3794 struct hwi_async_pdu_context *pasync_ctx;
3795
3796 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3797 phwi_ctrlr, ulp_num);
3798 for (cri = 0; cri <
3799 phba->params.cxns_per_ctrl; cri++) {
3800 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3801 (phwi_ctrlr, cri))
3802 pasync_ctx->cid_to_async_cri_map[
3803 phwi_ctrlr->wrb_context[cri].cid] =
3804 async_arr_idx++;
3805 }
3806 }
3807 }
3808
John Soni Jose99bc5d52012-08-20 23:00:18 +05303809 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3810 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303811 return 0;
3812
3813error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303814 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3815 "BM_%d : hwi_init_port failed");
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05303816 hwi_cleanup_port(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003817 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303818}
3819
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303820static int hwi_init_controller(struct beiscsi_hba *phba)
3821{
3822 struct hwi_controller *phwi_ctrlr;
3823
3824 phwi_ctrlr = phba->phwi_ctrlr;
3825 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3826 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3827 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303828 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3829 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3830 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303831 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303832 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3833 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3834 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303835 return -ENOMEM;
3836 }
3837
3838 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003839 if (beiscsi_init_wrb_handle(phba))
3840 return -ENOMEM;
3841
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003842 if (hwi_init_async_pdu_ctx(phba)) {
3843 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3844 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3845 return -ENOMEM;
3846 }
3847
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303848 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303849 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3850 "BM_%d : hwi_init_controller failed\n");
3851
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303852 return -ENOMEM;
3853 }
3854 return 0;
3855}
3856
3857static void beiscsi_free_mem(struct beiscsi_hba *phba)
3858{
3859 struct be_mem_descriptor *mem_descr;
3860 int i, j;
3861
3862 mem_descr = phba->init_mem;
3863 i = 0;
3864 j = 0;
3865 for (i = 0; i < SE_MEM_MAX; i++) {
3866 for (j = mem_descr->num_elements; j > 0; j--) {
3867 pci_free_consistent(phba->pcidev,
3868 mem_descr->mem_array[j - 1].size,
3869 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303870 (unsigned long)mem_descr->mem_array[j - 1].
3871 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303872 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003873
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303874 kfree(mem_descr->mem_array);
3875 mem_descr++;
3876 }
3877 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003878 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303879 kfree(phba->phwi_ctrlr);
3880}
3881
3882static int beiscsi_init_controller(struct beiscsi_hba *phba)
3883{
3884 int ret = -ENOMEM;
3885
3886 ret = beiscsi_get_memory(phba);
3887 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303888 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3889 "BM_%d : beiscsi_dev_probe -"
3890 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303891 return ret;
3892 }
3893
3894 ret = hwi_init_controller(phba);
3895 if (ret)
3896 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303897 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3898 "BM_%d : Return success from beiscsi_init_controller");
3899
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303900 return 0;
3901
3902free_init:
3903 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003904 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303905}
3906
3907static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3908{
3909 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3910 struct sgl_handle *psgl_handle;
3911 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003912 unsigned int arr_index, i, idx;
3913 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303914
3915 phba->io_sgl_hndl_avbl = 0;
3916 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303917
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303918 mem_descr_sglh = phba->init_mem;
3919 mem_descr_sglh += HWI_MEM_SGLH;
3920 if (1 == mem_descr_sglh->num_elements) {
3921 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3922 phba->params.ios_per_ctrl,
3923 GFP_KERNEL);
3924 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303925 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3926 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303927 return -ENOMEM;
3928 }
3929 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3930 (phba->params.icds_per_ctrl -
3931 phba->params.ios_per_ctrl),
3932 GFP_KERNEL);
3933 if (!phba->eh_sgl_hndl_base) {
3934 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303935 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3936 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303937 return -ENOMEM;
3938 }
3939 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303940 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3941 "BM_%d : HWI_MEM_SGLH is more than one element."
3942 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303943 return -ENOMEM;
3944 }
3945
3946 arr_index = 0;
3947 idx = 0;
3948 while (idx < mem_descr_sglh->num_elements) {
3949 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3950
3951 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3952 sizeof(struct sgl_handle)); i++) {
3953 if (arr_index < phba->params.ios_per_ctrl) {
3954 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3955 phba->io_sgl_hndl_avbl++;
3956 arr_index++;
3957 } else {
3958 phba->eh_sgl_hndl_base[arr_index -
3959 phba->params.ios_per_ctrl] =
3960 psgl_handle;
3961 arr_index++;
3962 phba->eh_sgl_hndl_avbl++;
3963 }
3964 psgl_handle++;
3965 }
3966 idx++;
3967 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303968 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3969 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3970 "phba->eh_sgl_hndl_avbl=%d\n",
3971 phba->io_sgl_hndl_avbl,
3972 phba->eh_sgl_hndl_avbl);
3973
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303974 mem_descr_sg = phba->init_mem;
3975 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303976 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3977 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3978 mem_descr_sg->num_elements);
3979
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003980 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3981 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3982 break;
3983
3984 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
3985
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303986 arr_index = 0;
3987 idx = 0;
3988 while (idx < mem_descr_sg->num_elements) {
3989 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3990
3991 for (i = 0;
3992 i < (mem_descr_sg->mem_array[idx].size) /
3993 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3994 i++) {
3995 if (arr_index < phba->params.ios_per_ctrl)
3996 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3997 else
3998 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3999 phba->params.ios_per_ctrl];
4000 psgl_handle->pfrag = pfrag;
4001 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4002 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4003 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004004 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304005 }
4006 idx++;
4007 }
4008 phba->io_sgl_free_index = 0;
4009 phba->io_sgl_alloc_index = 0;
4010 phba->eh_sgl_free_index = 0;
4011 phba->eh_sgl_alloc_index = 0;
4012 return 0;
4013}
4014
4015static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4016{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004017 int ret;
4018 uint16_t i, ulp_num;
4019 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304020
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004021 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4022 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4023 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4024 GFP_KERNEL);
4025
4026 if (!ptr_cid_info) {
4027 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4028 "BM_%d : Failed to allocate memory"
4029 "for ULP_CID_INFO for ULP : %d\n",
4030 ulp_num);
4031 ret = -ENOMEM;
4032 goto free_memory;
4033
4034 }
4035
4036 /* Allocate memory for CID array */
4037 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4038 BEISCSI_GET_CID_COUNT(phba,
4039 ulp_num), GFP_KERNEL);
4040 if (!ptr_cid_info->cid_array) {
4041 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4042 "BM_%d : Failed to allocate memory"
4043 "for CID_ARRAY for ULP : %d\n",
4044 ulp_num);
4045 kfree(ptr_cid_info);
4046 ptr_cid_info = NULL;
4047 ret = -ENOMEM;
4048
4049 goto free_memory;
4050 }
4051 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4052 phba, ulp_num);
4053
4054 /* Save the cid_info_array ptr */
4055 phba->cid_array_info[ulp_num] = ptr_cid_info;
4056 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304057 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304058 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004059 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304060 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304061 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4062 "BM_%d : Failed to allocate memory in "
4063 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004064 ret = -ENOMEM;
4065
4066 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304067 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004068
4069 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4070 phba->params.cxns_per_ctrl, GFP_KERNEL);
4071 if (!phba->conn_table) {
4072 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4073 "BM_%d : Failed to allocate memory in"
4074 "hba_setup_cid_tbls\n");
4075
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004076 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004077 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004078 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004079
4080 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304081 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004082
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004083 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4084 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004085
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004086 ptr_cid_info = phba->cid_array_info[ulp_num];
4087 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4088 phba->phwi_ctrlr->wrb_context[i].cid;
4089
4090 }
4091
4092 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4093 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4094 ptr_cid_info = phba->cid_array_info[ulp_num];
4095
4096 ptr_cid_info->cid_alloc = 0;
4097 ptr_cid_info->cid_free = 0;
4098 }
4099 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304100 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004101
4102free_memory:
4103 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4104 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4105 ptr_cid_info = phba->cid_array_info[ulp_num];
4106
4107 if (ptr_cid_info) {
4108 kfree(ptr_cid_info->cid_array);
4109 kfree(ptr_cid_info);
4110 phba->cid_array_info[ulp_num] = NULL;
4111 }
4112 }
4113 }
4114
4115 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304116}
4117
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304118static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304119{
4120 struct be_ctrl_info *ctrl = &phba->ctrl;
4121 struct hwi_controller *phwi_ctrlr;
4122 struct hwi_context_memory *phwi_context;
4123 struct be_queue_info *eq;
4124 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304125 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304126 u32 enabled;
4127
4128 phwi_ctrlr = phba->phwi_ctrlr;
4129 phwi_context = phwi_ctrlr->phwi_ctxt;
4130
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304131 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4132 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4133 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304134
4135 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4136 if (!enabled) {
4137 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304138 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4139 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304140 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004141 }
4142
4143 if (!phba->msix_enabled) {
4144 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304145 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4146 "BM_%d : eq->id=%d\n", eq->id);
4147
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004148 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4149 } else {
4150 for (i = 0; i <= phba->num_cpus; i++) {
4151 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304152 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4153 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304154 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4155 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304156 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304157}
4158
4159static void hwi_disable_intr(struct beiscsi_hba *phba)
4160{
4161 struct be_ctrl_info *ctrl = &phba->ctrl;
4162
4163 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4164 u32 reg = ioread32(addr);
4165
4166 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4167 if (enabled) {
4168 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4169 iowrite32(reg, addr);
4170 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304171 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4172 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304173}
4174
4175static int beiscsi_init_port(struct beiscsi_hba *phba)
4176{
4177 int ret;
4178
4179 ret = beiscsi_init_controller(phba);
4180 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304181 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4182 "BM_%d : beiscsi_dev_probe - Failed in"
4183 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304184 return ret;
4185 }
4186 ret = beiscsi_init_sgl_handle(phba);
4187 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304188 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4189 "BM_%d : beiscsi_dev_probe - Failed in"
4190 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304191 goto do_cleanup_ctrlr;
4192 }
4193
4194 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304195 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4196 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304197 kfree(phba->io_sgl_hndl_base);
4198 kfree(phba->eh_sgl_hndl_base);
4199 goto do_cleanup_ctrlr;
4200 }
4201
4202 return ret;
4203
4204do_cleanup_ctrlr:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304205 hwi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304206 return ret;
4207}
4208
4209static void hwi_purge_eq(struct beiscsi_hba *phba)
4210{
4211 struct hwi_controller *phwi_ctrlr;
4212 struct hwi_context_memory *phwi_context;
4213 struct be_queue_info *eq;
4214 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304215 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304216 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304217
4218 phwi_ctrlr = phba->phwi_ctrlr;
4219 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304220 if (phba->msix_enabled)
4221 eq_msix = 1;
4222 else
4223 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304224
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304225 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4226 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304227 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304228 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304229 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4230 & EQE_VALID_MASK) {
4231 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4232 queue_tail_inc(eq);
4233 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304234 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304235 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304236
4237 if (num_processed)
4238 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304239 }
4240}
4241
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304242static void beiscsi_cleanup_port(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304243{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004244 struct ulp_cid_info *ptr_cid_info = NULL;
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304245 int ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304246
Jitendra Bhivaref79929d2016-08-19 15:20:17 +05304247 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4248 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported))
4249 beiscsi_cmd_iscsi_cleanup(phba, ulp_num);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304250
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304251 hwi_purge_eq(phba);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05304252 hwi_cleanup_port(phba);
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 }
4269
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304270}
4271
John Soni Josed629c472012-10-20 04:42:00 +05304272/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004273 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4274 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004275 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004276 *
4277 * Free driver mgmt resources binded to CXN.
4278 **/
4279void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004280beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4281 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004282{
4283 struct beiscsi_io_task *io_task;
4284 struct beiscsi_hba *phba = beiscsi_conn->phba;
4285 struct hwi_wrb_context *pwrb_context;
4286 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004287 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4288 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004289
4290 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004291 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4292
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004293 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004294
4295 if (io_task->pwrb_handle) {
4296 memset(io_task->pwrb_handle->pwrb, 0,
4297 sizeof(struct iscsi_wrb));
4298 free_wrb_handle(phba, pwrb_context,
4299 io_task->pwrb_handle);
4300 io_task->pwrb_handle = NULL;
4301 }
4302
4303 if (io_task->psgl_handle) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004304 free_mgmt_sgl_handle(phba,
4305 io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004306 io_task->psgl_handle = NULL;
4307 }
4308
John Soni Joseeb1c4692015-04-25 08:17:45 +05304309 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004310 pci_unmap_single(phba->pcidev,
4311 io_task->mtask_addr,
4312 io_task->mtask_data_count,
4313 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304314 io_task->mtask_addr = 0;
4315 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004316}
4317
4318/**
John Soni Josed629c472012-10-20 04:42:00 +05304319 * beiscsi_cleanup_task()- Free driver resources of the task
4320 * @task: ptr to the iscsi task
4321 *
4322 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004323static void beiscsi_cleanup_task(struct iscsi_task *task)
4324{
4325 struct beiscsi_io_task *io_task = task->dd_data;
4326 struct iscsi_conn *conn = task->conn;
4327 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4328 struct beiscsi_hba *phba = beiscsi_conn->phba;
4329 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4330 struct hwi_wrb_context *pwrb_context;
4331 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004332 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4333 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004334
4335 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004336 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004337
4338 if (io_task->cmd_bhs) {
4339 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4340 io_task->bhs_pa.u.a64.address);
4341 io_task->cmd_bhs = NULL;
4342 }
4343
4344 if (task->sc) {
4345 if (io_task->pwrb_handle) {
4346 free_wrb_handle(phba, pwrb_context,
4347 io_task->pwrb_handle);
4348 io_task->pwrb_handle = NULL;
4349 }
4350
4351 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004352 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004353 io_task->psgl_handle = NULL;
4354 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004355
4356 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304357 if (io_task->num_sg)
4358 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004359 io_task->scsi_cmnd = NULL;
4360 }
Mike Christie1282ab72012-04-18 03:06:00 -05004361 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004362 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004363 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004364 }
4365}
4366
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304367void
4368beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4369 struct beiscsi_offload_params *params)
4370{
4371 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304372 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304373 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004374 struct iscsi_task *task = beiscsi_conn->task;
4375 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304376 u32 doorbell = 0;
4377
4378 /*
4379 * We can always use 0 here because it is reserved by libiscsi for
4380 * login/startup related tasks.
4381 */
Mike Christie1282ab72012-04-18 03:06:00 -05004382 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004383 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004384 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004385 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004386
John Soni Jose340c99e2015-08-20 04:44:30 +05304387 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4388 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304389
John Soni Joseacb96932012-10-20 04:44:35 +05304390 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004391 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304392 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304393 phba->init_mem,
4394 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004395 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304396 beiscsi_offload_cxn_v2(params, pwrb_handle,
4397 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304398
John Soni Joseacb96932012-10-20 04:44:35 +05304399 be_dws_le_to_cpu(pwrb_handle->pwrb,
4400 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304401
4402 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304403 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304404 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304405 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004406 iowrite32(doorbell, phba->db_va +
4407 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304408
4409 /*
4410 * There is no completion for CONTEXT_UPDATE. The completion of next
4411 * WRB posted guarantees FW's processing and DMA'ing of it.
4412 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4413 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4414 */
4415 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4416 phba->params.wrbs_per_cxn);
4417 beiscsi_log(phba, KERN_INFO,
4418 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4419 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4420 pwrb_handle, pwrb_context->free_index,
4421 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304422}
4423
4424static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4425 int *index, int *age)
4426{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304427 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304428 if (age)
4429 *age = conn->session->age;
4430}
4431
4432/**
4433 * beiscsi_alloc_pdu - allocates pdu and related resources
4434 * @task: libiscsi task
4435 * @opcode: opcode of pdu for task
4436 *
4437 * This is called with the session lock held. It will allocate
4438 * the wrb and sgl if needed for the command. And it will prep
4439 * the pdu's itt. beiscsi_parse_pdu will later translate
4440 * the pdu itt to the libiscsi task itt.
4441 */
4442static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4443{
4444 struct beiscsi_io_task *io_task = task->dd_data;
4445 struct iscsi_conn *conn = task->conn;
4446 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4447 struct beiscsi_hba *phba = beiscsi_conn->phba;
4448 struct hwi_wrb_context *pwrb_context;
4449 struct hwi_controller *phwi_ctrlr;
4450 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004451 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304452 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4453 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304454
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304455 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004456 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304457 if (!io_task->cmd_bhs)
4458 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304459 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304460 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304461 io_task->conn = beiscsi_conn;
4462
4463 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4464 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304465 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004466 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304467
4468 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304469 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304470 if (!io_task->psgl_handle) {
4471 beiscsi_log(phba, KERN_ERR,
4472 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4473 "BM_%d : Alloc of IO_SGL_ICD Failed"
4474 "for the CID : %d\n",
4475 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304476 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304477 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304478 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304479 beiscsi_conn->beiscsi_conn_cid,
4480 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304481 if (!io_task->pwrb_handle) {
4482 beiscsi_log(phba, KERN_ERR,
4483 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4484 "BM_%d : Alloc of WRB_HANDLE Failed"
4485 "for the CID : %d\n",
4486 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304487 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304488 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304489 } else {
4490 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304491 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004492 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304493 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304494 io_task->psgl_handle = (struct sgl_handle *)
4495 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304496 if (!io_task->psgl_handle) {
4497 beiscsi_log(phba, KERN_ERR,
4498 BEISCSI_LOG_IO |
4499 BEISCSI_LOG_CONFIG,
4500 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4501 "for the CID : %d\n",
4502 beiscsi_conn->
4503 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304504 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304505 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304506
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304507 beiscsi_conn->login_in_progress = 1;
4508 beiscsi_conn->plogin_sgl_handle =
4509 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304510 io_task->pwrb_handle =
4511 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304512 beiscsi_conn->beiscsi_conn_cid,
4513 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304514 if (!io_task->pwrb_handle) {
4515 beiscsi_log(phba, KERN_ERR,
4516 BEISCSI_LOG_IO |
4517 BEISCSI_LOG_CONFIG,
4518 "BM_%d : Alloc of WRB_HANDLE Failed"
4519 "for the CID : %d\n",
4520 beiscsi_conn->
4521 beiscsi_conn_cid);
4522 goto free_mgmt_hndls;
4523 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304524 beiscsi_conn->plogin_wrb_handle =
4525 io_task->pwrb_handle;
4526
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304527 } else {
4528 io_task->psgl_handle =
4529 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304530 io_task->pwrb_handle =
4531 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304532 }
4533 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304534 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304535 if (!io_task->psgl_handle) {
4536 beiscsi_log(phba, KERN_ERR,
4537 BEISCSI_LOG_IO |
4538 BEISCSI_LOG_CONFIG,
4539 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4540 "for the CID : %d\n",
4541 beiscsi_conn->
4542 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304543 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304544 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304545 io_task->pwrb_handle =
4546 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304547 beiscsi_conn->beiscsi_conn_cid,
4548 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304549 if (!io_task->pwrb_handle) {
4550 beiscsi_log(phba, KERN_ERR,
4551 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4552 "BM_%d : Alloc of WRB_HANDLE Failed"
4553 "for the CID : %d\n",
4554 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304555 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304556 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304557
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304558 }
4559 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304560 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4561 wrb_index << 16) | (unsigned int)
4562 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304563 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304564
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304565 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4566 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304567
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304568free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304569 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304570 goto free_hndls;
4571free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304572 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004573 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304574free_hndls:
4575 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004576 cri_index = BE_GET_CRI_FROM_CID(
4577 beiscsi_conn->beiscsi_conn_cid);
4578 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304579 if (io_task->pwrb_handle)
4580 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304581 io_task->pwrb_handle = NULL;
4582 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4583 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004584 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304585 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304586}
John Soni Jose09a10932012-10-20 04:44:23 +05304587int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4588 unsigned int num_sg, unsigned int xferlen,
4589 unsigned int writedir)
4590{
4591
4592 struct beiscsi_io_task *io_task = task->dd_data;
4593 struct iscsi_conn *conn = task->conn;
4594 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4595 struct beiscsi_hba *phba = beiscsi_conn->phba;
4596 struct iscsi_wrb *pwrb = NULL;
4597 unsigned int doorbell = 0;
4598
4599 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304600
John Soni Jose09a10932012-10-20 04:44:23 +05304601 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4602
4603 if (writedir) {
4604 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4605 INI_WR_CMD);
4606 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4607 } else {
4608 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4609 INI_RD_CMD);
4610 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4611 }
4612
4613 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4614 type, pwrb);
4615
4616 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4617 cpu_to_be16(*(unsigned short *)
4618 &io_task->cmd_bhs->iscsi_hdr.lun));
4619 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4621 io_task->pwrb_handle->wrb_index);
4622 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4623 be32_to_cpu(task->cmdsn));
4624 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4625 io_task->psgl_handle->sgl_index);
4626
4627 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4628 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304629 io_task->pwrb_handle->wrb_index);
4630 if (io_task->pwrb_context->plast_wrb)
4631 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4632 io_task->pwrb_context->plast_wrb,
4633 io_task->pwrb_handle->wrb_index);
4634 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304635
4636 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4637
4638 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4639 doorbell |= (io_task->pwrb_handle->wrb_index &
4640 DB_DEF_PDU_WRB_INDEX_MASK) <<
4641 DB_DEF_PDU_WRB_INDEX_SHIFT;
4642 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004643 iowrite32(doorbell, phba->db_va +
4644 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304645 return 0;
4646}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304647
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304648static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4649 unsigned int num_sg, unsigned int xferlen,
4650 unsigned int writedir)
4651{
4652
4653 struct beiscsi_io_task *io_task = task->dd_data;
4654 struct iscsi_conn *conn = task->conn;
4655 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4656 struct beiscsi_hba *phba = beiscsi_conn->phba;
4657 struct iscsi_wrb *pwrb = NULL;
4658 unsigned int doorbell = 0;
4659
4660 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304661 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4662
4663 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304664 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4665 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304666 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304667 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304668 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4669 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304670 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4671 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304672
John Soni Jose09a10932012-10-20 04:44:23 +05304673 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4674 type, pwrb);
4675
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304676 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004677 cpu_to_be16(*(unsigned short *)
4678 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304679 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4680 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4681 io_task->pwrb_handle->wrb_index);
4682 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4683 be32_to_cpu(task->cmdsn));
4684 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4685 io_task->psgl_handle->sgl_index);
4686
4687 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4688
4689 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304690 io_task->pwrb_handle->wrb_index);
4691 if (io_task->pwrb_context->plast_wrb)
4692 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4693 io_task->pwrb_context->plast_wrb,
4694 io_task->pwrb_handle->wrb_index);
4695 io_task->pwrb_context->plast_wrb = pwrb;
4696
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304697 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4698
4699 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304700 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304701 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4702 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4703
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004704 iowrite32(doorbell, phba->db_va +
4705 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304706 return 0;
4707}
4708
4709static int beiscsi_mtask(struct iscsi_task *task)
4710{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304711 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304712 struct iscsi_conn *conn = task->conn;
4713 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4714 struct beiscsi_hba *phba = beiscsi_conn->phba;
4715 struct iscsi_wrb *pwrb = NULL;
4716 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304717 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304718 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004719 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304720
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304721 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304723 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304724
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004725 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304726 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4727 be32_to_cpu(task->cmdsn));
4728 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4729 io_task->pwrb_handle->wrb_index);
4730 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4731 io_task->psgl_handle->sgl_index);
4732 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4733 task->data_count);
4734 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304735 io_task->pwrb_handle->wrb_index);
4736 if (io_task->pwrb_context->plast_wrb)
4737 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4738 io_task->pwrb_context->plast_wrb,
4739 io_task->pwrb_handle->wrb_index);
4740 io_task->pwrb_context->plast_wrb = pwrb;
4741
John Soni Jose09a10932012-10-20 04:44:23 +05304742 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004743 } else {
4744 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4745 be32_to_cpu(task->cmdsn));
4746 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4747 io_task->pwrb_handle->wrb_index);
4748 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4749 io_task->psgl_handle->sgl_index);
4750 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4751 task->data_count);
4752 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304753 io_task->pwrb_handle->wrb_index);
4754 if (io_task->pwrb_context->plast_wrb)
4755 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4756 io_task->pwrb_context->plast_wrb,
4757 io_task->pwrb_handle->wrb_index);
4758 io_task->pwrb_context->plast_wrb = pwrb;
4759
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004760 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304761 }
4762
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304763
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304764 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4765 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304766 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304767 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004768 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304769 break;
4770 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004771 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304772 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004773 if (is_chip_be2_be3r(phba))
4774 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304775 dmsg, pwrb, 1);
4776 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004777 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304778 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004779 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304780 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004781 if (is_chip_be2_be3r(phba))
4782 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304783 dmsg, pwrb, 0);
4784 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004785 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304786 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004787 }
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_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304791 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_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_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304795 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, 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 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304799 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004800 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304801 break;
4802
4803 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304804 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4805 "BM_%d : opcode =%d Not supported\n",
4806 task->hdr->opcode & ISCSI_OPCODE_MASK);
4807
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304808 return -EINVAL;
4809 }
4810
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004811 if (ret)
4812 return ret;
4813
John Soni Jose09a10932012-10-20 04:44:23 +05304814 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004815 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4816 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4817 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304818
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304819 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304820 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304821 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4822 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004823 iowrite32(doorbell, phba->db_va +
4824 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304825 return 0;
4826}
4827
4828static int beiscsi_task_xmit(struct iscsi_task *task)
4829{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304830 struct beiscsi_io_task *io_task = task->dd_data;
4831 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304832 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304833 struct scatterlist *sg;
4834 int num_sg;
4835 unsigned int writedir = 0, xferlen = 0;
4836
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304837 phba = io_task->conn->phba;
4838 /**
4839 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4840 * operational if FW still gets heartbeat from EP FW. Is management
4841 * path really needed to continue further?
4842 */
4843 if (beiscsi_hba_in_error(phba))
4844 return -EIO;
4845
Jitendra Bhivare18683792016-02-04 15:49:18 +05304846 if (!io_task->conn->login_in_progress)
4847 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304848
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304849 if (!sc)
4850 return beiscsi_mtask(task);
4851
4852 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304853 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304854 num_sg = scsi_dma_map(sc);
4855 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004856 beiscsi_log(phba, KERN_ERR,
4857 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4858 "BM_%d : scsi_dma_map Failed "
4859 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4860 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4861 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304862
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304863 return num_sg;
4864 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304865 /**
4866 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4867 * For management task, cleanup_task checks mtask_addr before unmapping.
4868 */
4869 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304870 xferlen = scsi_bufflen(sc);
4871 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304872 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304873 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304874 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304875 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304876
John Soni Jose09a10932012-10-20 04:44:23 +05304877 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304878}
4879
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004880/**
4881 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4882 * @job: job to handle
4883 */
4884static int beiscsi_bsg_request(struct bsg_job *job)
4885{
4886 struct Scsi_Host *shost;
4887 struct beiscsi_hba *phba;
4888 struct iscsi_bsg_request *bsg_req = job->request;
4889 int rc = -EINVAL;
4890 unsigned int tag;
4891 struct be_dma_mem nonemb_cmd;
4892 struct be_cmd_resp_hdr *resp;
4893 struct iscsi_bsg_reply *bsg_reply = job->reply;
4894 unsigned short status, extd_status;
4895
4896 shost = iscsi_job_to_shost(job);
4897 phba = iscsi_host_priv(shost);
4898
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304899 if (beiscsi_hba_in_error(phba)) {
4900 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4901 "BM_%d : HBA in error 0x%lx\n", phba->state);
4902 return -ENXIO;
4903 }
4904
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004905 switch (bsg_req->msgcode) {
4906 case ISCSI_BSG_HST_VENDOR:
4907 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4908 job->request_payload.payload_len,
4909 &nonemb_cmd.dma);
4910 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304911 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4912 "BM_%d : Failed to allocate memory for "
4913 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304914 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004915 }
4916 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4917 &nonemb_cmd);
4918 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304919 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304920 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304921
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004922 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4923 nonemb_cmd.va, nonemb_cmd.dma);
4924 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304925 }
4926
4927 rc = wait_event_interruptible_timeout(
4928 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304929 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304930 msecs_to_jiffies(
4931 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304932 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4933 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4934 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304935 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004936 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4937 sg_copy_from_buffer(job->reply_payload.sg_list,
4938 job->reply_payload.sg_cnt,
4939 nonemb_cmd.va, (resp->response_length
4940 + sizeof(*resp)));
4941 bsg_reply->reply_payload_rcv_len = resp->response_length;
4942 bsg_reply->result = status;
4943 bsg_job_done(job, bsg_reply->result,
4944 bsg_reply->reply_payload_rcv_len);
4945 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4946 nonemb_cmd.va, nonemb_cmd.dma);
4947 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304948 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304949 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304950 " status = %d extd_status = %d\n",
4951 status, extd_status);
4952
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004953 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304954 } else {
4955 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004956 }
4957 break;
4958
4959 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304960 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4961 "BM_%d : Unsupported bsg command: 0x%x\n",
4962 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004963 break;
4964 }
4965
4966 return rc;
4967}
4968
John Soni Jose99bc5d52012-08-20 23:00:18 +05304969void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4970{
4971 /* Set the logging parameter */
4972 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4973}
4974
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304975/*
4976 * beiscsi_quiesce()- Cleanup Driver resources
4977 * @phba: Instance Priv structure
4978 *
4979 * Free the OS and HW resources held by the driver
4980 **/
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304981static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304982{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304983 struct hwi_controller *phwi_ctrlr;
4984 struct hwi_context_memory *phwi_context;
4985 struct be_eq_obj *pbe_eq;
4986 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304987
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304988 phwi_ctrlr = phba->phwi_ctrlr;
4989 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304990 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304991 if (phba->msix_enabled) {
4992 for (i = 0; i <= phba->num_cpus; i++) {
4993 msix_vec = phba->msix_entries[i].vector;
4994 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004995 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304996 }
4997 } else
Lars-Peter Clausene729b502016-03-04 11:15:06 +01004998 if (phba->pcidev->irq)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304999 free_irq(phba->pcidev->irq, phba);
5000 pci_disable_msix(phba->pcidev);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305001 cancel_delayed_work_sync(&phba->eqd_update);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305002 cancel_work_sync(&phba->boot_work);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305003 del_timer_sync(&phba->hw_check);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005004
Jens Axboe89f8b332014-03-13 09:38:42 -06005005 for (i = 0; i < phba->num_cpus; i++) {
5006 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005007 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005008 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305009
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305010 /* PCI_ERR is set then check if driver is not unloading */
5011 if (test_bit(BEISCSI_HBA_RUNNING, &phba->state) &&
5012 test_bit(BEISCSI_HBA_PCI_ERR, &phba->state)) {
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305013 hwi_cleanup_port(phba);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305014 return;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005015 }
John Soni Jose7a158002012-10-20 04:45:51 +05305016
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305017 destroy_workqueue(phba->wq);
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305018 beiscsi_cleanup_port(phba);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305019 beiscsi_free_mem(phba);
5020
5021 beiscsi_unmap_pci_function(phba);
5022 pci_free_consistent(phba->pcidev,
5023 phba->ctrl.mbox_mem_alloced.size,
5024 phba->ctrl.mbox_mem_alloced.va,
5025 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005026}
5027
5028static void beiscsi_remove(struct pci_dev *pcidev)
5029{
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005030 struct beiscsi_hba *phba = NULL;
5031
5032 phba = pci_get_drvdata(pcidev);
5033 if (!phba) {
5034 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5035 return;
5036 }
5037
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305038 clear_bit(BEISCSI_HBA_RUNNING, &phba->state);
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305039 beiscsi_iface_destroy_default(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305040 iscsi_host_remove(phba->shost);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305041 beiscsi_quiesce(phba);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305042 /* after cancelling boot_work */
5043 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305044 pci_dev_put(phba->pcidev);
5045 iscsi_host_free(phba->shost);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005046 pci_disable_pcie_error_reporting(pcidev);
5047 pci_set_drvdata(pcidev, NULL);
John Soni Josee307f3a2015-04-25 08:17:19 +05305048 pci_release_regions(pcidev);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07005049 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305050}
5051
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305052static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5053{
5054 int i, status;
5055
5056 for (i = 0; i <= phba->num_cpus; i++)
5057 phba->msix_entries[i].entry = i;
5058
Alexander Gordeeve149fc12014-08-18 08:01:48 +02005059 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5060 phba->num_cpus + 1, phba->num_cpus + 1);
5061 if (status > 0)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305062 phba->msix_enabled = true;
5063
5064 return;
5065}
5066
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305067void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
5068{
5069 if (phba->boot_struct.boot_kset)
5070 return;
5071
5072 /* skip if boot work is already in progress */
5073 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
5074 return;
5075
5076 phba->boot_struct.retry = 3;
5077 phba->boot_struct.tag = 0;
5078 phba->boot_struct.s_handle = s_handle;
5079 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
5080 schedule_work(&phba->boot_work);
5081}
5082
5083static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
5084{
5085 struct beiscsi_hba *phba = data;
5086 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
5087 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5088 char *str = buf;
5089 int rc = -EPERM;
5090
5091 switch (type) {
5092 case ISCSI_BOOT_TGT_NAME:
5093 rc = sprintf(buf, "%.*s\n",
5094 (int)strlen(boot_sess->target_name),
5095 (char *)&boot_sess->target_name);
5096 break;
5097 case ISCSI_BOOT_TGT_IP_ADDR:
5098 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5099 rc = sprintf(buf, "%pI4\n",
5100 (char *)&boot_conn->dest_ipaddr.addr);
5101 else
5102 rc = sprintf(str, "%pI6\n",
5103 (char *)&boot_conn->dest_ipaddr.addr);
5104 break;
5105 case ISCSI_BOOT_TGT_PORT:
5106 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5107 break;
5108
5109 case ISCSI_BOOT_TGT_CHAP_NAME:
5110 rc = sprintf(str, "%.*s\n",
5111 boot_conn->negotiated_login_options.auth_data.chap.
5112 target_chap_name_length,
5113 (char *)&boot_conn->negotiated_login_options.
5114 auth_data.chap.target_chap_name);
5115 break;
5116 case ISCSI_BOOT_TGT_CHAP_SECRET:
5117 rc = sprintf(str, "%.*s\n",
5118 boot_conn->negotiated_login_options.auth_data.chap.
5119 target_secret_length,
5120 (char *)&boot_conn->negotiated_login_options.
5121 auth_data.chap.target_secret);
5122 break;
5123 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5124 rc = sprintf(str, "%.*s\n",
5125 boot_conn->negotiated_login_options.auth_data.chap.
5126 intr_chap_name_length,
5127 (char *)&boot_conn->negotiated_login_options.
5128 auth_data.chap.intr_chap_name);
5129 break;
5130 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5131 rc = sprintf(str, "%.*s\n",
5132 boot_conn->negotiated_login_options.auth_data.chap.
5133 intr_secret_length,
5134 (char *)&boot_conn->negotiated_login_options.
5135 auth_data.chap.intr_secret);
5136 break;
5137 case ISCSI_BOOT_TGT_FLAGS:
5138 rc = sprintf(str, "2\n");
5139 break;
5140 case ISCSI_BOOT_TGT_NIC_ASSOC:
5141 rc = sprintf(str, "0\n");
5142 break;
5143 }
5144 return rc;
5145}
5146
5147static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5148{
5149 struct beiscsi_hba *phba = data;
5150 char *str = buf;
5151 int rc = -EPERM;
5152
5153 switch (type) {
5154 case ISCSI_BOOT_INI_INITIATOR_NAME:
5155 rc = sprintf(str, "%s\n",
5156 phba->boot_struct.boot_sess.initiator_iscsiname);
5157 break;
5158 }
5159 return rc;
5160}
5161
5162static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5163{
5164 struct beiscsi_hba *phba = data;
5165 char *str = buf;
5166 int rc = -EPERM;
5167
5168 switch (type) {
5169 case ISCSI_BOOT_ETH_FLAGS:
5170 rc = sprintf(str, "2\n");
5171 break;
5172 case ISCSI_BOOT_ETH_INDEX:
5173 rc = sprintf(str, "0\n");
5174 break;
5175 case ISCSI_BOOT_ETH_MAC:
5176 rc = beiscsi_get_macaddr(str, phba);
5177 break;
5178 }
5179 return rc;
5180}
5181
5182
5183static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5184{
5185 umode_t rc = 0;
5186
5187 switch (type) {
5188 case ISCSI_BOOT_TGT_NAME:
5189 case ISCSI_BOOT_TGT_IP_ADDR:
5190 case ISCSI_BOOT_TGT_PORT:
5191 case ISCSI_BOOT_TGT_CHAP_NAME:
5192 case ISCSI_BOOT_TGT_CHAP_SECRET:
5193 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5194 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5195 case ISCSI_BOOT_TGT_NIC_ASSOC:
5196 case ISCSI_BOOT_TGT_FLAGS:
5197 rc = S_IRUGO;
5198 break;
5199 }
5200 return rc;
5201}
5202
5203static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5204{
5205 umode_t rc = 0;
5206
5207 switch (type) {
5208 case ISCSI_BOOT_INI_INITIATOR_NAME:
5209 rc = S_IRUGO;
5210 break;
5211 }
5212 return rc;
5213}
5214
5215
5216static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5217{
5218 umode_t rc = 0;
5219
5220 switch (type) {
5221 case ISCSI_BOOT_ETH_FLAGS:
5222 case ISCSI_BOOT_ETH_MAC:
5223 case ISCSI_BOOT_ETH_INDEX:
5224 rc = S_IRUGO;
5225 break;
5226 }
5227 return rc;
5228}
5229
5230static void beiscsi_boot_kobj_release(void *data)
5231{
5232 struct beiscsi_hba *phba = data;
5233
5234 scsi_host_put(phba->shost);
5235}
5236
5237static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5238{
5239 struct boot_struct *bs = &phba->boot_struct;
5240 struct iscsi_boot_kobj *boot_kobj;
5241
5242 if (bs->boot_kset) {
5243 __beiscsi_log(phba, KERN_ERR,
5244 "BM_%d: boot_kset already created\n");
5245 return 0;
5246 }
5247
5248 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5249 if (!bs->boot_kset) {
5250 __beiscsi_log(phba, KERN_ERR,
5251 "BM_%d: boot_kset alloc failed\n");
5252 return -ENOMEM;
5253 }
5254
5255 /* get shost ref because the show function will refer phba */
5256 if (!scsi_host_get(phba->shost))
5257 goto free_kset;
5258
5259 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5260 beiscsi_show_boot_tgt_info,
5261 beiscsi_tgt_get_attr_visibility,
5262 beiscsi_boot_kobj_release);
5263 if (!boot_kobj)
5264 goto put_shost;
5265
5266 if (!scsi_host_get(phba->shost))
5267 goto free_kset;
5268
5269 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5270 beiscsi_show_boot_ini_info,
5271 beiscsi_ini_get_attr_visibility,
5272 beiscsi_boot_kobj_release);
5273 if (!boot_kobj)
5274 goto put_shost;
5275
5276 if (!scsi_host_get(phba->shost))
5277 goto free_kset;
5278
5279 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5280 beiscsi_show_boot_eth_info,
5281 beiscsi_eth_get_attr_visibility,
5282 beiscsi_boot_kobj_release);
5283 if (!boot_kobj)
5284 goto put_shost;
5285
5286 return 0;
5287
5288put_shost:
5289 scsi_host_put(phba->shost);
5290free_kset:
5291 iscsi_boot_destroy_kset(bs->boot_kset);
5292 bs->boot_kset = NULL;
5293 return -ENOMEM;
5294}
5295
5296static void beiscsi_boot_work(struct work_struct *work)
5297{
5298 struct beiscsi_hba *phba =
5299 container_of(work, struct beiscsi_hba, boot_work);
5300 struct boot_struct *bs = &phba->boot_struct;
5301 unsigned int tag = 0;
5302
5303 if (beiscsi_hba_in_error(phba))
5304 return;
5305
5306 beiscsi_log(phba, KERN_INFO,
5307 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5308 "BM_%d : %s action %d\n",
5309 __func__, phba->boot_struct.action);
5310
5311 switch (phba->boot_struct.action) {
5312 case BEISCSI_BOOT_REOPEN_SESS:
5313 tag = beiscsi_boot_reopen_sess(phba);
5314 break;
5315 case BEISCSI_BOOT_GET_SHANDLE:
5316 tag = __beiscsi_boot_get_shandle(phba, 1);
5317 break;
5318 case BEISCSI_BOOT_GET_SINFO:
5319 tag = beiscsi_boot_get_sinfo(phba);
5320 break;
5321 case BEISCSI_BOOT_LOGOUT_SESS:
5322 tag = beiscsi_boot_logout_sess(phba);
5323 break;
5324 case BEISCSI_BOOT_CREATE_KSET:
5325 beiscsi_boot_create_kset(phba);
5326 /**
5327 * updated boot_kset is made visible to all before
5328 * ending the boot work.
5329 */
5330 mb();
5331 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5332 return;
5333 }
5334 if (!tag) {
5335 if (bs->retry--)
5336 schedule_work(&phba->boot_work);
5337 else
5338 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5339 }
5340}
5341
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305342static void beiscsi_hw_health_check(unsigned long ptr)
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005343{
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305344 struct beiscsi_hba *phba;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005345
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305346 phba = (struct beiscsi_hba *)ptr;
5347 beiscsi_ue_detect(phba);
5348 if (test_bit(BEISCSI_HBA_IN_UE, &phba->state))
5349 return;
5350
5351 mod_timer(&phba->hw_check,
5352 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5353}
5354
5355static void beiscsi_eqd_update_work(struct work_struct *work)
5356{
5357 struct hwi_context_memory *phwi_context;
5358 struct be_set_eqd set_eqd[MAX_CPUS];
5359 struct hwi_controller *phwi_ctrlr;
5360 struct be_eq_obj *pbe_eq;
5361 struct beiscsi_hba *phba;
5362 unsigned int pps, delta;
5363 struct be_aic_obj *aic;
5364 int eqd, i, num = 0;
5365 unsigned long now;
5366
5367 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305368 if (beiscsi_hba_in_error(phba))
5369 return;
5370
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005371 phwi_ctrlr = phba->phwi_ctrlr;
5372 phwi_context = phwi_ctrlr->phwi_ctxt;
5373
5374 for (i = 0; i <= phba->num_cpus; i++) {
5375 aic = &phba->aic_obj[i];
5376 pbe_eq = &phwi_context->be_eq[i];
5377 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305378 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005379 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305380 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005381 aic->eq_prev = pbe_eq->cq_count;
5382 continue;
5383 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305384 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005385 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5386 eqd = (pps / 1500) << 2;
5387
5388 if (eqd < 8)
5389 eqd = 0;
5390 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5391 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5392
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305393 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005394 aic->eq_prev = pbe_eq->cq_count;
5395
5396 if (eqd != aic->prev_eqd) {
5397 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5398 set_eqd[num].eq_id = pbe_eq->q.id;
5399 aic->prev_eqd = eqd;
5400 num++;
5401 }
5402 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305403 if (num)
5404 /* completion of this is ignored */
5405 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005406
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305407 schedule_delayed_work(&phba->eqd_update,
5408 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305409}
5410
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005411
5412static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5413 pci_channel_state_t state)
5414{
5415 struct beiscsi_hba *phba = NULL;
5416
5417 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305418 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005419
5420 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5421 "BM_%d : EEH error detected\n");
5422
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305423 beiscsi_quiesce(phba);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005424
5425 if (state == pci_channel_io_perm_failure) {
5426 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5427 "BM_%d : EEH : State PERM Failure");
5428 return PCI_ERS_RESULT_DISCONNECT;
5429 }
5430
5431 pci_disable_device(pdev);
5432
5433 /* The error could cause the FW to trigger a flash debug dump.
5434 * Resetting the card while flash dump is in progress
5435 * can cause it not to recover; wait for it to finish.
5436 * Wait only for first function as it is needed only once per
5437 * adapter.
5438 **/
5439 if (pdev->devfn == 0)
5440 ssleep(30);
5441
5442 return PCI_ERS_RESULT_NEED_RESET;
5443}
5444
5445static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5446{
5447 struct beiscsi_hba *phba = NULL;
5448 int status = 0;
5449
5450 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5451
5452 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5453 "BM_%d : EEH Reset\n");
5454
5455 status = pci_enable_device(pdev);
5456 if (status)
5457 return PCI_ERS_RESULT_DISCONNECT;
5458
5459 pci_set_master(pdev);
5460 pci_set_power_state(pdev, PCI_D0);
5461 pci_restore_state(pdev);
5462
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305463 status = beiscsi_check_fw_rdy(phba);
5464 if (status) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005465 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5466 "BM_%d : EEH Reset Completed\n");
5467 } else {
5468 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5469 "BM_%d : EEH Reset Completion Failure\n");
5470 return PCI_ERS_RESULT_DISCONNECT;
5471 }
5472
5473 pci_cleanup_aer_uncorrect_error_status(pdev);
5474 return PCI_ERS_RESULT_RECOVERED;
5475}
5476
5477static void beiscsi_eeh_resume(struct pci_dev *pdev)
5478{
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305479 int ret, i;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005480 struct be_eq_obj *pbe_eq;
5481 struct beiscsi_hba *phba = NULL;
5482 struct hwi_controller *phwi_ctrlr;
5483 struct hwi_context_memory *phwi_context;
5484
5485 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5486 pci_save_state(pdev);
5487
5488 if (enable_msix)
5489 find_num_cpus(phba);
5490 else
5491 phba->num_cpus = 1;
5492
5493 if (enable_msix) {
5494 beiscsi_msix_enable(phba);
5495 if (!phba->msix_enabled)
5496 phba->num_cpus = 1;
5497 }
5498
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305499 ret = beiscsi_init_sliport(phba);
5500 if (ret)
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005501 goto ret_err;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005502
5503 beiscsi_get_params(phba);
5504 phba->shost->max_id = phba->params.cxns_per_ctrl;
5505 phba->shost->can_queue = phba->params.ios_per_ctrl;
5506 ret = hwi_init_controller(phba);
Nicholas Krausec6fff322016-02-27 12:43:25 -05005507 if (ret) {
5508 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5509 "BM_%d : beiscsi_eeh_resume -"
5510 "Failed to initialize beiscsi_hba.\n");
5511 goto ret_err;
5512 }
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005513
5514 for (i = 0; i < MAX_MCC_CMD; i++) {
5515 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5516 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305517 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005518 phba->ctrl.mcc_tag_available++;
5519 }
5520
5521 phwi_ctrlr = phba->phwi_ctrlr;
5522 phwi_context = phwi_ctrlr->phwi_ctxt;
5523
Jens Axboe89f8b332014-03-13 09:38:42 -06005524 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005525 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005526 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005527 be_iopoll);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005528 }
5529
Jens Axboe89f8b332014-03-13 09:38:42 -06005530 i = (phba->msix_enabled) ? i : 0;
5531 /* Work item for MCC handling */
5532 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305533 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005534
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005535 ret = beiscsi_init_irqs(phba);
5536 if (ret < 0) {
5537 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5538 "BM_%d : beiscsi_eeh_resume - "
5539 "Failed to beiscsi_init_irqs\n");
5540 goto ret_err;
5541 }
5542
5543 hwi_enable_intr(phba);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305544 clear_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005545
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305546 /* start hw_check timer and eqd_update work */
5547 schedule_delayed_work(&phba->eqd_update,
5548 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5549 mod_timer(&phba->hw_check,
5550 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005551 return;
5552ret_err:
5553 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5554 "BM_%d : AER EEH Resume Failed\n");
5555}
5556
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005557static int beiscsi_dev_probe(struct pci_dev *pcidev,
5558 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305559{
5560 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305561 struct hwi_controller *phwi_ctrlr;
5562 struct hwi_context_memory *phwi_context;
5563 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305564 unsigned int s_handle;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005565 int ret = 0, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305566
5567 ret = beiscsi_enable_pci(pcidev);
5568 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305569 dev_err(&pcidev->dev,
5570 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305571 return ret;
5572 }
5573
5574 phba = beiscsi_hba_alloc(pcidev);
5575 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305576 dev_err(&pcidev->dev,
5577 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305578 goto disable_pci;
5579 }
5580
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005581 /* Enable EEH reporting */
5582 ret = pci_enable_pcie_error_reporting(pcidev);
5583 if (ret)
5584 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5585 "BM_%d : PCIe Error Reporting "
5586 "Enabling Failed\n");
5587
5588 pci_save_state(pcidev);
5589
John Soni Jose99bc5d52012-08-20 23:00:18 +05305590 /* Initialize Driver configuration Paramters */
5591 beiscsi_hba_attrs_init(phba);
5592
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005593 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305594
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305595 switch (pcidev->device) {
5596 case BE_DEVICE_ID1:
5597 case OC_DEVICE_ID1:
5598 case OC_DEVICE_ID2:
5599 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305600 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305601 break;
5602 case BE_DEVICE_ID2:
5603 case OC_DEVICE_ID3:
5604 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305605 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305606 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305607 case OC_SKH_ID1:
5608 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305609 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005610 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305611 default:
5612 phba->generation = 0;
5613 }
5614
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305615 ret = be_ctrl_init(phba, pcidev);
5616 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305617 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305618 "BM_%d : be_ctrl_init failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305619 goto hba_free;
5620 }
5621
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305622 ret = beiscsi_init_sliport(phba);
5623 if (ret)
5624 goto hba_free;
5625
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305626 set_bit(BEISCSI_HBA_RUNNING, &phba->state);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305627 spin_lock_init(&phba->io_sgl_lock);
5628 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005629 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305630 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305631 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305632 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5633 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305634 goto free_port;
5635 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305636 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305637 beiscsi_get_params(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305638 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005639
5640 if (enable_msix)
5641 find_num_cpus(phba);
5642 else
5643 phba->num_cpus = 1;
5644
5645 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5646 "BM_%d : num_cpus = %d\n",
5647 phba->num_cpus);
5648
5649 if (enable_msix) {
5650 beiscsi_msix_enable(phba);
5651 if (!phba->msix_enabled)
5652 phba->num_cpus = 1;
5653 }
5654
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005655 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305656 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305657 ret = beiscsi_init_port(phba);
5658 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305659 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5660 "BM_%d : beiscsi_dev_probe-"
5661 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305662 goto free_port;
5663 }
5664
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005665 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305666 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5667 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305668 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305669 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005670 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005671 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305672 }
5673
5674 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5675
John Soni Jose72fb46a2012-10-20 04:42:49 +05305676 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305677 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005678 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305679 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305680 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5681 "BM_%d : beiscsi_dev_probe-"
5682 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305683 goto free_twq;
5684 }
5685
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305686 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305687
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305688 phwi_ctrlr = phba->phwi_ctrlr;
5689 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305690
Jens Axboe89f8b332014-03-13 09:38:42 -06005691 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305692 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005693 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005694 be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305695 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305696
Jens Axboe89f8b332014-03-13 09:38:42 -06005697 i = (phba->msix_enabled) ? i : 0;
5698 /* Work item for MCC handling */
5699 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305700 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005701
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305702 ret = beiscsi_init_irqs(phba);
5703 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305704 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5705 "BM_%d : beiscsi_dev_probe-"
5706 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305707 goto free_blkenbld;
5708 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305709 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005710
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005711 if (iscsi_host_add(phba->shost, &phba->pcidev->dev))
5712 goto free_blkenbld;
5713
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305714 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5715 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5716 if (ret > 0) {
5717 beiscsi_start_boot_work(phba, s_handle);
5718 /**
5719 * Set this bit after starting the work to let
5720 * probe handle it first.
5721 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005722 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305723 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5724 }
Mike Christief457a462011-06-24 15:11:53 -05005725
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305726 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305727 schedule_delayed_work(&phba->eqd_update,
5728 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5729 /**
5730 * Start UE detection here. UE before this will cause stall in probe
5731 * and eventually fail the probe.
5732 */
5733 init_timer(&phba->hw_check);
5734 phba->hw_check.function = beiscsi_hw_health_check;
5735 phba->hw_check.data = (unsigned long)phba;
5736 mod_timer(&phba->hw_check,
5737 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305738
John Soni Jose99bc5d52012-08-20 23:00:18 +05305739 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5740 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305741 return 0;
5742
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305743free_blkenbld:
5744 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005745 for (i = 0; i < phba->num_cpus; i++) {
5746 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005747 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005748 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305749free_twq:
Jitendra Bhivare4d2ee1e2016-08-19 15:20:16 +05305750 beiscsi_cleanup_port(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305751 beiscsi_free_mem(phba);
5752free_port:
5753 pci_free_consistent(phba->pcidev,
5754 phba->ctrl.mbox_mem_alloced.size,
5755 phba->ctrl.mbox_mem_alloced.va,
5756 phba->ctrl.mbox_mem_alloced.dma);
5757 beiscsi_unmap_pci_function(phba);
5758hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305759 if (phba->msix_enabled)
5760 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305761 pci_dev_put(phba->pcidev);
5762 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305763 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305764disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305765 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305766 pci_disable_device(pcidev);
5767 return ret;
5768}
5769
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005770static struct pci_error_handlers beiscsi_eeh_handlers = {
5771 .error_detected = beiscsi_eeh_err_detected,
5772 .slot_reset = beiscsi_eeh_reset,
5773 .resume = beiscsi_eeh_resume,
5774};
5775
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305776struct iscsi_transport beiscsi_iscsi_transport = {
5777 .owner = THIS_MODULE,
5778 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305779 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305780 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305781 .create_session = beiscsi_session_create,
5782 .destroy_session = beiscsi_session_destroy,
5783 .create_conn = beiscsi_conn_create,
5784 .bind_conn = beiscsi_conn_bind,
5785 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305786 .attr_is_visible = beiscsi_attr_is_visible,
5787 .set_iface_param = beiscsi_iface_set_param,
5788 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305789 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005790 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305791 .get_session_param = iscsi_session_get_param,
5792 .get_host_param = beiscsi_get_host_param,
5793 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005794 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305795 .send_pdu = iscsi_conn_send_pdu,
5796 .xmit_task = beiscsi_task_xmit,
5797 .cleanup_task = beiscsi_cleanup_task,
5798 .alloc_pdu = beiscsi_alloc_pdu,
5799 .parse_pdu_itt = beiscsi_parse_pdu,
5800 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005801 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305802 .ep_connect = beiscsi_ep_connect,
5803 .ep_poll = beiscsi_ep_poll,
5804 .ep_disconnect = beiscsi_ep_disconnect,
5805 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005806 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305807};
5808
5809static struct pci_driver beiscsi_pci_driver = {
5810 .name = DRV_NAME,
5811 .probe = beiscsi_dev_probe,
5812 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005813 .id_table = beiscsi_pci_id_table,
5814 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305815};
5816
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305817
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305818static int __init beiscsi_module_init(void)
5819{
5820 int ret;
5821
5822 beiscsi_scsi_transport =
5823 iscsi_register_transport(&beiscsi_iscsi_transport);
5824 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305825 printk(KERN_ERR
5826 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305827 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305828 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305829 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5830 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305831
5832 ret = pci_register_driver(&beiscsi_pci_driver);
5833 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305834 printk(KERN_ERR
5835 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305836 goto unregister_iscsi_transport;
5837 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305838 return 0;
5839
5840unregister_iscsi_transport:
5841 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5842 return ret;
5843}
5844
5845static void __exit beiscsi_module_exit(void)
5846{
5847 pci_unregister_driver(&beiscsi_pci_driver);
5848 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5849}
5850
5851module_init(beiscsi_module_init);
5852module_exit(beiscsi_module_exit);