blob: 77ecd6c482073164aab6a4bb2e67b58b5b03e900 [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);
3252 hwi_post_async_buffers(phba, BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303253 return 0;
3254}
3255
3256static int
3257beiscsi_create_def_data(struct beiscsi_hba *phba,
3258 struct hwi_context_memory *phwi_context,
3259 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003260 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303261{
3262 unsigned int idx;
3263 int ret;
3264 struct be_queue_info *dataq, *cq;
3265 struct be_dma_mem *mem;
3266 struct be_mem_descriptor *mem_descr;
3267 void *dq_vaddress;
3268
3269 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003270 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303271 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303272 mem = &dataq->dma_mem;
3273 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003274 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3275 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303276 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3277 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3278 sizeof(struct phys_addr),
3279 sizeof(struct phys_addr), dq_vaddress);
3280 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303281 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003282 "BM_%d : be_fill_queue Failed for DEF PDU "
3283 "DATA on ULP : %d\n",
3284 ulp_num);
3285
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303286 return ret;
3287 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303288 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3289 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303290 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3291 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003292 phba->params.defpdu_data_sz,
3293 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303294 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303295 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3296 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003297 " Failed for DEF PDU DATA on ULP : %d\n",
3298 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303299 return ret;
3300 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303301
John Soni Jose99bc5d52012-08-20 23:00:18 +05303302 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003303 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3304 ulp_num,
3305 phwi_context->be_def_dataq[ulp_num].id);
3306
3307 hwi_post_async_buffers(phba, BEISCSI_DEFQ_DATA, ulp_num);
3308 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3309 "BM_%d : DEFAULT PDU DATA RING CREATED"
3310 "on ULP : %d\n", ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303311
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303312 return 0;
3313}
3314
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003315
3316static int
3317beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3318{
3319 struct be_mem_descriptor *mem_descr;
3320 struct mem_array *pm_arr;
3321 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003322 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003323
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003324 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3325 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3326 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3327 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3328 (ulp_num * MEM_DESCR_OFFSET);
3329 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003330
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003331 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3332 status = be_cmd_iscsi_post_template_hdr(
3333 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003334
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003335 if (status != 0) {
3336 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3337 "BM_%d : Post Template HDR Failed for"
3338 "ULP_%d\n", ulp_num);
3339 return status;
3340 }
3341
3342 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3343 "BM_%d : Template HDR Pages Posted for"
3344 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003345 }
3346 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003347 return 0;
3348}
3349
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303350static int
3351beiscsi_post_pages(struct beiscsi_hba *phba)
3352{
3353 struct be_mem_descriptor *mem_descr;
3354 struct mem_array *pm_arr;
3355 unsigned int page_offset, i;
3356 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003357 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303358
3359 mem_descr = phba->init_mem;
3360 mem_descr += HWI_MEM_SGE;
3361 pm_arr = mem_descr->mem_array;
3362
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003363 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3364 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3365 break;
3366
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303367 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003368 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303369 for (i = 0; i < mem_descr->num_elements; i++) {
3370 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3371 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3372 page_offset,
3373 (pm_arr->size / PAGE_SIZE));
3374 page_offset += pm_arr->size / PAGE_SIZE;
3375 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303376 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3377 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303378 return status;
3379 }
3380 pm_arr++;
3381 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303382 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3383 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303384 return 0;
3385}
3386
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303387static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3388{
3389 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003390 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303391 pci_free_consistent(phba->pcidev, mem->size,
3392 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003393 mem->va = NULL;
3394 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303395}
3396
3397static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3398 u16 len, u16 entry_size)
3399{
3400 struct be_dma_mem *mem = &q->dma_mem;
3401
3402 memset(q, 0, sizeof(*q));
3403 q->len = len;
3404 q->entry_size = entry_size;
3405 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003406 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303407 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303408 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303409 return 0;
3410}
3411
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303412static int
3413beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3414 struct hwi_context_memory *phwi_context,
3415 struct hwi_controller *phwi_ctrlr)
3416{
3417 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3418 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003419 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303420 struct mem_array *pwrb_arr;
3421 void *wrb_vaddr;
3422 struct be_dma_mem sgl;
3423 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003424 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303425 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003426 uint8_t ulp_count = 0, ulp_base_num = 0;
3427 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303428
3429 idx = 0;
3430 mem_descr = phba->init_mem;
3431 mem_descr += HWI_MEM_WRB;
3432 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3433 GFP_KERNEL);
3434 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303435 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3436 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303437 return -ENOMEM;
3438 }
3439 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3440 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3441 num_wrb_rings = mem_descr->mem_array[idx].size /
3442 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3443
3444 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3445 if (num_wrb_rings) {
3446 pwrb_arr[num].virtual_address = wrb_vaddr;
3447 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3448 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3449 sizeof(struct iscsi_wrb);
3450 wrb_vaddr += pwrb_arr[num].size;
3451 pa_addr_lo += pwrb_arr[num].size;
3452 num_wrb_rings--;
3453 } else {
3454 idx++;
3455 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3456 pa_addr_lo = mem_descr->mem_array[idx].\
3457 bus_address.u.a64.address;
3458 num_wrb_rings = mem_descr->mem_array[idx].size /
3459 (phba->params.wrbs_per_cxn *
3460 sizeof(struct iscsi_wrb));
3461 pwrb_arr[num].virtual_address = wrb_vaddr;
3462 pwrb_arr[num].bus_address.u.a64.address\
3463 = pa_addr_lo;
3464 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3465 sizeof(struct iscsi_wrb);
3466 wrb_vaddr += pwrb_arr[num].size;
3467 pa_addr_lo += pwrb_arr[num].size;
3468 num_wrb_rings--;
3469 }
3470 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003471
3472 /* Get the ULP Count */
3473 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3474 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3475 ulp_count++;
3476 ulp_base_num = ulp_num;
3477 cid_count_ulp[ulp_num] =
3478 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3479 }
3480
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303481 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3482 wrb_mem_index = 0;
3483 offset = 0;
3484 size = 0;
3485
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003486 if (ulp_count > 1) {
3487 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3488
3489 if (!cid_count_ulp[ulp_base_num])
3490 ulp_base_num = (ulp_base_num + 1) %
3491 BEISCSI_ULP_COUNT;
3492
3493 cid_count_ulp[ulp_base_num]--;
3494 }
3495
3496
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303497 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3498 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003499 &phwi_context->be_wrbq[i],
3500 &phwi_ctrlr->wrb_context[i],
3501 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303502 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303503 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3504 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003505 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303506 return status;
3507 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003508 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003509 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303510 }
3511 kfree(pwrb_arr);
3512 return 0;
3513}
3514
3515static void free_wrb_handles(struct beiscsi_hba *phba)
3516{
3517 unsigned int index;
3518 struct hwi_controller *phwi_ctrlr;
3519 struct hwi_wrb_context *pwrb_context;
3520
3521 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003522 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303523 pwrb_context = &phwi_ctrlr->wrb_context[index];
3524 kfree(pwrb_context->pwrb_handle_base);
3525 kfree(pwrb_context->pwrb_handle_basestd);
3526 }
3527}
3528
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303529static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3530{
3531 struct be_queue_info *q;
3532 struct be_ctrl_info *ctrl = &phba->ctrl;
3533
3534 q = &phba->ctrl.mcc_obj.q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303535 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303536 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303537 be_queue_free(phba, q);
3538 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303539
3540 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303541 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303542 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303543 be_queue_free(phba, q);
3544 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303545}
3546
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303547static void hwi_cleanup(struct beiscsi_hba *phba)
3548{
3549 struct be_queue_info *q;
3550 struct be_ctrl_info *ctrl = &phba->ctrl;
3551 struct hwi_controller *phwi_ctrlr;
3552 struct hwi_context_memory *phwi_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003553 struct hwi_async_pdu_context *pasync_ctx;
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003554 int i, eq_for_mcc, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303555
3556 phwi_ctrlr = phba->phwi_ctrlr;
3557 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003558
3559 be_cmd_iscsi_remove_template_hdr(ctrl);
3560
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303561 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3562 q = &phwi_context->be_wrbq[i];
3563 if (q->created)
3564 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3565 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003566 kfree(phwi_context->be_wrbq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303567 free_wrb_handles(phba);
3568
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003569 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3570 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303571
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003572 q = &phwi_context->be_def_hdrq[ulp_num];
3573 if (q->created)
3574 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3575
3576 q = &phwi_context->be_def_dataq[ulp_num];
3577 if (q->created)
3578 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3579
3580 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3581 }
3582 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303583
3584 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3585
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303586 for (i = 0; i < (phba->num_cpus); i++) {
3587 q = &phwi_context->be_cq[i];
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303588 if (q->created) {
3589 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303590 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303591 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303592 }
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003593
3594 be_mcc_queues_destroy(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303595 if (phba->msix_enabled)
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003596 eq_for_mcc = 1;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303597 else
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003598 eq_for_mcc = 0;
3599 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303600 q = &phwi_context->be_eq[i].q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303601 if (q->created) {
3602 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303603 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303604 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303605 }
Jayamohan Kallickal0283fbb2013-04-05 20:38:21 -07003606 be_cmd_fw_uninit(ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303607}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303608
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303609static int be_mcc_queues_create(struct beiscsi_hba *phba,
3610 struct hwi_context_memory *phwi_context)
3611{
3612 struct be_queue_info *q, *cq;
3613 struct be_ctrl_info *ctrl = &phba->ctrl;
3614
3615 /* Alloc MCC compl queue */
3616 cq = &phba->ctrl.mcc_obj.cq;
3617 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3618 sizeof(struct be_mcc_compl)))
3619 goto err;
3620 /* Ask BE to create MCC compl queue; */
3621 if (phba->msix_enabled) {
3622 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3623 [phba->num_cpus].q, false, true, 0))
3624 goto mcc_cq_free;
3625 } else {
3626 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3627 false, true, 0))
3628 goto mcc_cq_free;
3629 }
3630
3631 /* Alloc MCC queue */
3632 q = &phba->ctrl.mcc_obj.q;
3633 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3634 goto mcc_cq_destroy;
3635
3636 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303637 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303638 goto mcc_q_free;
3639
3640 return 0;
3641
3642mcc_q_free:
3643 be_queue_free(phba, q);
3644mcc_cq_destroy:
3645 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3646mcc_cq_free:
3647 be_queue_free(phba, cq);
3648err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303649 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303650}
3651
John Soni Jose107dfcb2012-10-20 04:42:13 +05303652/**
3653 * find_num_cpus()- Get the CPU online count
3654 * @phba: ptr to priv structure
3655 *
3656 * CPU count is used for creating EQ.
3657 **/
3658static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303659{
3660 int num_cpus = 0;
3661
3662 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303663
John Soni Jose22abeef2012-10-20 04:43:32 +05303664 switch (phba->generation) {
3665 case BE_GEN2:
3666 case BE_GEN3:
3667 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3668 BEISCSI_MAX_NUM_CPUS : num_cpus;
3669 break;
3670 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003671 /*
3672 * If eqid_count == 1 fall back to
3673 * INTX mechanism
3674 **/
3675 if (phba->fw_config.eqid_count == 1) {
3676 enable_msix = 0;
3677 phba->num_cpus = 1;
3678 return;
3679 }
3680
3681 phba->num_cpus =
3682 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3683 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303684 break;
3685 default:
3686 phba->num_cpus = 1;
3687 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303688}
3689
3690static int hwi_init_port(struct beiscsi_hba *phba)
3691{
3692 struct hwi_controller *phwi_ctrlr;
3693 struct hwi_context_memory *phwi_context;
3694 unsigned int def_pdu_ring_sz;
3695 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003696 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303697
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303698 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303699 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003700 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303701 phwi_context->min_eqd = 0;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003702 phwi_context->cur_eqd = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303703 be_cmd_fw_initialize(&phba->ctrl);
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303704 /* set optic state to unknown */
3705 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303706
3707 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303708 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303709 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3710 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303711 goto error;
3712 }
3713
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303714 status = be_mcc_queues_create(phba, phwi_context);
3715 if (status != 0)
3716 goto error;
3717
Jitendra Bhivare480195c2016-08-19 15:20:15 +05303718 status = beiscsi_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303719 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303720 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3721 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303722 goto error;
3723 }
3724
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303725 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303726 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303727 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3728 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303729 goto error;
3730 }
3731
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003732 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3733 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303734
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003735 def_pdu_ring_sz =
3736 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3737 sizeof(struct phys_addr);
3738
3739 status = beiscsi_create_def_hdr(phba, phwi_context,
3740 phwi_ctrlr,
3741 def_pdu_ring_sz,
3742 ulp_num);
3743 if (status != 0) {
3744 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3745 "BM_%d : Default Header not created for ULP : %d\n",
3746 ulp_num);
3747 goto error;
3748 }
3749
3750 status = beiscsi_create_def_data(phba, phwi_context,
3751 phwi_ctrlr,
3752 def_pdu_ring_sz,
3753 ulp_num);
3754 if (status != 0) {
3755 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3756 "BM_%d : Default Data not created for ULP : %d\n",
3757 ulp_num);
3758 goto error;
3759 }
3760 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303761 }
3762
3763 status = beiscsi_post_pages(phba);
3764 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303765 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3766 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303767 goto error;
3768 }
3769
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003770 status = beiscsi_post_template_hdr(phba);
3771 if (status != 0) {
3772 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3773 "BM_%d : Template HDR Posting for CXN Failed\n");
3774 }
3775
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303776 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3777 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303778 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3779 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303780 goto error;
3781 }
3782
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003783 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3784 uint16_t async_arr_idx = 0;
3785
3786 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3787 uint16_t cri = 0;
3788 struct hwi_async_pdu_context *pasync_ctx;
3789
3790 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3791 phwi_ctrlr, ulp_num);
3792 for (cri = 0; cri <
3793 phba->params.cxns_per_ctrl; cri++) {
3794 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3795 (phwi_ctrlr, cri))
3796 pasync_ctx->cid_to_async_cri_map[
3797 phwi_ctrlr->wrb_context[cri].cid] =
3798 async_arr_idx++;
3799 }
3800 }
3801 }
3802
John Soni Jose99bc5d52012-08-20 23:00:18 +05303803 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3804 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303805 return 0;
3806
3807error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303808 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3809 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303810 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003811 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303812}
3813
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303814static int hwi_init_controller(struct beiscsi_hba *phba)
3815{
3816 struct hwi_controller *phwi_ctrlr;
3817
3818 phwi_ctrlr = phba->phwi_ctrlr;
3819 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3820 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3821 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303822 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3823 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3824 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303825 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303826 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3827 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3828 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303829 return -ENOMEM;
3830 }
3831
3832 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003833 if (beiscsi_init_wrb_handle(phba))
3834 return -ENOMEM;
3835
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003836 if (hwi_init_async_pdu_ctx(phba)) {
3837 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3838 "BM_%d : hwi_init_async_pdu_ctx failed\n");
3839 return -ENOMEM;
3840 }
3841
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303842 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303843 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3844 "BM_%d : hwi_init_controller failed\n");
3845
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303846 return -ENOMEM;
3847 }
3848 return 0;
3849}
3850
3851static void beiscsi_free_mem(struct beiscsi_hba *phba)
3852{
3853 struct be_mem_descriptor *mem_descr;
3854 int i, j;
3855
3856 mem_descr = phba->init_mem;
3857 i = 0;
3858 j = 0;
3859 for (i = 0; i < SE_MEM_MAX; i++) {
3860 for (j = mem_descr->num_elements; j > 0; j--) {
3861 pci_free_consistent(phba->pcidev,
3862 mem_descr->mem_array[j - 1].size,
3863 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303864 (unsigned long)mem_descr->mem_array[j - 1].
3865 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303866 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003867
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303868 kfree(mem_descr->mem_array);
3869 mem_descr++;
3870 }
3871 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003872 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303873 kfree(phba->phwi_ctrlr);
3874}
3875
3876static int beiscsi_init_controller(struct beiscsi_hba *phba)
3877{
3878 int ret = -ENOMEM;
3879
3880 ret = beiscsi_get_memory(phba);
3881 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303882 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3883 "BM_%d : beiscsi_dev_probe -"
3884 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303885 return ret;
3886 }
3887
3888 ret = hwi_init_controller(phba);
3889 if (ret)
3890 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303891 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3892 "BM_%d : Return success from beiscsi_init_controller");
3893
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303894 return 0;
3895
3896free_init:
3897 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003898 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899}
3900
3901static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3902{
3903 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3904 struct sgl_handle *psgl_handle;
3905 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003906 unsigned int arr_index, i, idx;
3907 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303908
3909 phba->io_sgl_hndl_avbl = 0;
3910 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303911
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303912 mem_descr_sglh = phba->init_mem;
3913 mem_descr_sglh += HWI_MEM_SGLH;
3914 if (1 == mem_descr_sglh->num_elements) {
3915 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3916 phba->params.ios_per_ctrl,
3917 GFP_KERNEL);
3918 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303919 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3920 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303921 return -ENOMEM;
3922 }
3923 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3924 (phba->params.icds_per_ctrl -
3925 phba->params.ios_per_ctrl),
3926 GFP_KERNEL);
3927 if (!phba->eh_sgl_hndl_base) {
3928 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303929 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3930 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303931 return -ENOMEM;
3932 }
3933 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303934 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3935 "BM_%d : HWI_MEM_SGLH is more than one element."
3936 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303937 return -ENOMEM;
3938 }
3939
3940 arr_index = 0;
3941 idx = 0;
3942 while (idx < mem_descr_sglh->num_elements) {
3943 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3944
3945 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3946 sizeof(struct sgl_handle)); i++) {
3947 if (arr_index < phba->params.ios_per_ctrl) {
3948 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3949 phba->io_sgl_hndl_avbl++;
3950 arr_index++;
3951 } else {
3952 phba->eh_sgl_hndl_base[arr_index -
3953 phba->params.ios_per_ctrl] =
3954 psgl_handle;
3955 arr_index++;
3956 phba->eh_sgl_hndl_avbl++;
3957 }
3958 psgl_handle++;
3959 }
3960 idx++;
3961 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303962 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3963 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3964 "phba->eh_sgl_hndl_avbl=%d\n",
3965 phba->io_sgl_hndl_avbl,
3966 phba->eh_sgl_hndl_avbl);
3967
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303968 mem_descr_sg = phba->init_mem;
3969 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303970 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3971 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3972 mem_descr_sg->num_elements);
3973
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003974 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3975 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3976 break;
3977
3978 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
3979
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303980 arr_index = 0;
3981 idx = 0;
3982 while (idx < mem_descr_sg->num_elements) {
3983 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3984
3985 for (i = 0;
3986 i < (mem_descr_sg->mem_array[idx].size) /
3987 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3988 i++) {
3989 if (arr_index < phba->params.ios_per_ctrl)
3990 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3991 else
3992 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3993 phba->params.ios_per_ctrl];
3994 psgl_handle->pfrag = pfrag;
3995 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3996 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3997 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003998 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303999 }
4000 idx++;
4001 }
4002 phba->io_sgl_free_index = 0;
4003 phba->io_sgl_alloc_index = 0;
4004 phba->eh_sgl_free_index = 0;
4005 phba->eh_sgl_alloc_index = 0;
4006 return 0;
4007}
4008
4009static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4010{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004011 int ret;
4012 uint16_t i, ulp_num;
4013 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304014
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004015 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4016 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4017 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4018 GFP_KERNEL);
4019
4020 if (!ptr_cid_info) {
4021 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4022 "BM_%d : Failed to allocate memory"
4023 "for ULP_CID_INFO for ULP : %d\n",
4024 ulp_num);
4025 ret = -ENOMEM;
4026 goto free_memory;
4027
4028 }
4029
4030 /* Allocate memory for CID array */
4031 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4032 BEISCSI_GET_CID_COUNT(phba,
4033 ulp_num), GFP_KERNEL);
4034 if (!ptr_cid_info->cid_array) {
4035 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4036 "BM_%d : Failed to allocate memory"
4037 "for CID_ARRAY for ULP : %d\n",
4038 ulp_num);
4039 kfree(ptr_cid_info);
4040 ptr_cid_info = NULL;
4041 ret = -ENOMEM;
4042
4043 goto free_memory;
4044 }
4045 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4046 phba, ulp_num);
4047
4048 /* Save the cid_info_array ptr */
4049 phba->cid_array_info[ulp_num] = ptr_cid_info;
4050 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304051 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304052 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004053 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304054 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304055 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4056 "BM_%d : Failed to allocate memory in "
4057 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004058 ret = -ENOMEM;
4059
4060 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304061 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004062
4063 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4064 phba->params.cxns_per_ctrl, GFP_KERNEL);
4065 if (!phba->conn_table) {
4066 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4067 "BM_%d : Failed to allocate memory in"
4068 "hba_setup_cid_tbls\n");
4069
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004070 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004071 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004072 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004073
4074 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304075 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004076
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004077 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4078 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004079
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004080 ptr_cid_info = phba->cid_array_info[ulp_num];
4081 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4082 phba->phwi_ctrlr->wrb_context[i].cid;
4083
4084 }
4085
4086 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4087 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4088 ptr_cid_info = phba->cid_array_info[ulp_num];
4089
4090 ptr_cid_info->cid_alloc = 0;
4091 ptr_cid_info->cid_free = 0;
4092 }
4093 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304094 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004095
4096free_memory:
4097 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4098 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4099 ptr_cid_info = phba->cid_array_info[ulp_num];
4100
4101 if (ptr_cid_info) {
4102 kfree(ptr_cid_info->cid_array);
4103 kfree(ptr_cid_info);
4104 phba->cid_array_info[ulp_num] = NULL;
4105 }
4106 }
4107 }
4108
4109 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304110}
4111
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304112static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304113{
4114 struct be_ctrl_info *ctrl = &phba->ctrl;
4115 struct hwi_controller *phwi_ctrlr;
4116 struct hwi_context_memory *phwi_context;
4117 struct be_queue_info *eq;
4118 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304119 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304120 u32 enabled;
4121
4122 phwi_ctrlr = phba->phwi_ctrlr;
4123 phwi_context = phwi_ctrlr->phwi_ctxt;
4124
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304125 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4126 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4127 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304128
4129 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4130 if (!enabled) {
4131 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304132 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4133 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304134 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004135 }
4136
4137 if (!phba->msix_enabled) {
4138 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304139 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4140 "BM_%d : eq->id=%d\n", eq->id);
4141
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004142 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4143 } else {
4144 for (i = 0; i <= phba->num_cpus; i++) {
4145 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304146 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4147 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304148 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4149 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304150 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304151}
4152
4153static void hwi_disable_intr(struct beiscsi_hba *phba)
4154{
4155 struct be_ctrl_info *ctrl = &phba->ctrl;
4156
4157 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4158 u32 reg = ioread32(addr);
4159
4160 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4161 if (enabled) {
4162 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4163 iowrite32(reg, addr);
4164 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304165 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4166 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304167}
4168
4169static int beiscsi_init_port(struct beiscsi_hba *phba)
4170{
4171 int ret;
4172
4173 ret = beiscsi_init_controller(phba);
4174 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304175 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4176 "BM_%d : beiscsi_dev_probe - Failed in"
4177 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304178 return ret;
4179 }
4180 ret = beiscsi_init_sgl_handle(phba);
4181 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304182 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4183 "BM_%d : beiscsi_dev_probe - Failed in"
4184 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304185 goto do_cleanup_ctrlr;
4186 }
4187
4188 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304189 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4190 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304191 kfree(phba->io_sgl_hndl_base);
4192 kfree(phba->eh_sgl_hndl_base);
4193 goto do_cleanup_ctrlr;
4194 }
4195
4196 return ret;
4197
4198do_cleanup_ctrlr:
4199 hwi_cleanup(phba);
4200 return ret;
4201}
4202
4203static void hwi_purge_eq(struct beiscsi_hba *phba)
4204{
4205 struct hwi_controller *phwi_ctrlr;
4206 struct hwi_context_memory *phwi_context;
4207 struct be_queue_info *eq;
4208 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304209 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304210 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304211
4212 phwi_ctrlr = phba->phwi_ctrlr;
4213 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304214 if (phba->msix_enabled)
4215 eq_msix = 1;
4216 else
4217 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304218
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304219 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4220 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304221 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304222 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304223 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4224 & EQE_VALID_MASK) {
4225 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4226 queue_tail_inc(eq);
4227 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304228 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304229 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304230
4231 if (num_processed)
4232 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304233 }
4234}
4235
4236static void beiscsi_clean_port(struct beiscsi_hba *phba)
4237{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004238 int mgmt_status, ulp_num;
4239 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304240
Jayamohan Kallickalbd41c2b2013-09-28 15:35:51 -07004241 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4242 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4243 mgmt_status = mgmt_epfw_cleanup(phba, ulp_num);
4244 if (mgmt_status)
4245 beiscsi_log(phba, KERN_WARNING,
4246 BEISCSI_LOG_INIT,
4247 "BM_%d : mgmt_epfw_cleanup FAILED"
4248 " for ULP_%d\n", ulp_num);
4249 }
4250 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304251
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304252 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304253 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304254 kfree(phba->io_sgl_hndl_base);
4255 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304256 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004257 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004258
4259 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4260 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4261 ptr_cid_info = phba->cid_array_info[ulp_num];
4262
4263 if (ptr_cid_info) {
4264 kfree(ptr_cid_info->cid_array);
4265 kfree(ptr_cid_info);
4266 phba->cid_array_info[ulp_num] = NULL;
4267 }
4268 }
4269 }
4270
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304271}
4272
John Soni Josed629c472012-10-20 04:42:00 +05304273/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004274 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4275 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004276 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004277 *
4278 * Free driver mgmt resources binded to CXN.
4279 **/
4280void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004281beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4282 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004283{
4284 struct beiscsi_io_task *io_task;
4285 struct beiscsi_hba *phba = beiscsi_conn->phba;
4286 struct hwi_wrb_context *pwrb_context;
4287 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004288 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4289 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004290
4291 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004292 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4293
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004294 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004295
4296 if (io_task->pwrb_handle) {
4297 memset(io_task->pwrb_handle->pwrb, 0,
4298 sizeof(struct iscsi_wrb));
4299 free_wrb_handle(phba, pwrb_context,
4300 io_task->pwrb_handle);
4301 io_task->pwrb_handle = NULL;
4302 }
4303
4304 if (io_task->psgl_handle) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004305 free_mgmt_sgl_handle(phba,
4306 io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004307 io_task->psgl_handle = NULL;
4308 }
4309
John Soni Joseeb1c4692015-04-25 08:17:45 +05304310 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004311 pci_unmap_single(phba->pcidev,
4312 io_task->mtask_addr,
4313 io_task->mtask_data_count,
4314 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304315 io_task->mtask_addr = 0;
4316 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004317}
4318
4319/**
John Soni Josed629c472012-10-20 04:42:00 +05304320 * beiscsi_cleanup_task()- Free driver resources of the task
4321 * @task: ptr to the iscsi task
4322 *
4323 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004324static void beiscsi_cleanup_task(struct iscsi_task *task)
4325{
4326 struct beiscsi_io_task *io_task = task->dd_data;
4327 struct iscsi_conn *conn = task->conn;
4328 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4329 struct beiscsi_hba *phba = beiscsi_conn->phba;
4330 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4331 struct hwi_wrb_context *pwrb_context;
4332 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004333 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4334 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004335
4336 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004337 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004338
4339 if (io_task->cmd_bhs) {
4340 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4341 io_task->bhs_pa.u.a64.address);
4342 io_task->cmd_bhs = NULL;
4343 }
4344
4345 if (task->sc) {
4346 if (io_task->pwrb_handle) {
4347 free_wrb_handle(phba, pwrb_context,
4348 io_task->pwrb_handle);
4349 io_task->pwrb_handle = NULL;
4350 }
4351
4352 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004353 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004354 io_task->psgl_handle = NULL;
4355 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004356
4357 if (io_task->scsi_cmnd) {
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304358 if (io_task->num_sg)
4359 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004360 io_task->scsi_cmnd = NULL;
4361 }
Mike Christie1282ab72012-04-18 03:06:00 -05004362 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004363 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004364 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004365 }
4366}
4367
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304368void
4369beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4370 struct beiscsi_offload_params *params)
4371{
4372 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304373 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304374 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004375 struct iscsi_task *task = beiscsi_conn->task;
4376 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304377 u32 doorbell = 0;
4378
4379 /*
4380 * We can always use 0 here because it is reserved by libiscsi for
4381 * login/startup related tasks.
4382 */
Mike Christie1282ab72012-04-18 03:06:00 -05004383 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004384 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004385 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004386 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004387
John Soni Jose340c99e2015-08-20 04:44:30 +05304388 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4389 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304390
John Soni Joseacb96932012-10-20 04:44:35 +05304391 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004392 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304393 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304394 phba->init_mem,
4395 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004396 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304397 beiscsi_offload_cxn_v2(params, pwrb_handle,
4398 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304399
John Soni Joseacb96932012-10-20 04:44:35 +05304400 be_dws_le_to_cpu(pwrb_handle->pwrb,
4401 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304402
4403 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304404 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304405 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304406 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004407 iowrite32(doorbell, phba->db_va +
4408 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304409
4410 /*
4411 * There is no completion for CONTEXT_UPDATE. The completion of next
4412 * WRB posted guarantees FW's processing and DMA'ing of it.
4413 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4414 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4415 */
4416 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4417 phba->params.wrbs_per_cxn);
4418 beiscsi_log(phba, KERN_INFO,
4419 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4420 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4421 pwrb_handle, pwrb_context->free_index,
4422 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304423}
4424
4425static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4426 int *index, int *age)
4427{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304428 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304429 if (age)
4430 *age = conn->session->age;
4431}
4432
4433/**
4434 * beiscsi_alloc_pdu - allocates pdu and related resources
4435 * @task: libiscsi task
4436 * @opcode: opcode of pdu for task
4437 *
4438 * This is called with the session lock held. It will allocate
4439 * the wrb and sgl if needed for the command. And it will prep
4440 * the pdu's itt. beiscsi_parse_pdu will later translate
4441 * the pdu itt to the libiscsi task itt.
4442 */
4443static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4444{
4445 struct beiscsi_io_task *io_task = task->dd_data;
4446 struct iscsi_conn *conn = task->conn;
4447 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4448 struct beiscsi_hba *phba = beiscsi_conn->phba;
4449 struct hwi_wrb_context *pwrb_context;
4450 struct hwi_controller *phwi_ctrlr;
4451 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004452 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304453 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4454 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304455
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304456 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004457 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304458 if (!io_task->cmd_bhs)
4459 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304460 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304461 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304462 io_task->conn = beiscsi_conn;
4463
4464 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4465 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304466 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004467 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304468
4469 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304470 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304471 if (!io_task->psgl_handle) {
4472 beiscsi_log(phba, KERN_ERR,
4473 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4474 "BM_%d : Alloc of IO_SGL_ICD Failed"
4475 "for the CID : %d\n",
4476 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304477 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304478 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304479 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304480 beiscsi_conn->beiscsi_conn_cid,
4481 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304482 if (!io_task->pwrb_handle) {
4483 beiscsi_log(phba, KERN_ERR,
4484 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4485 "BM_%d : Alloc of WRB_HANDLE Failed"
4486 "for the CID : %d\n",
4487 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304488 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304489 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304490 } else {
4491 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304492 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004493 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304494 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304495 io_task->psgl_handle = (struct sgl_handle *)
4496 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304497 if (!io_task->psgl_handle) {
4498 beiscsi_log(phba, KERN_ERR,
4499 BEISCSI_LOG_IO |
4500 BEISCSI_LOG_CONFIG,
4501 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4502 "for the CID : %d\n",
4503 beiscsi_conn->
4504 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304505 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304506 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304507
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304508 beiscsi_conn->login_in_progress = 1;
4509 beiscsi_conn->plogin_sgl_handle =
4510 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304511 io_task->pwrb_handle =
4512 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304513 beiscsi_conn->beiscsi_conn_cid,
4514 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304515 if (!io_task->pwrb_handle) {
4516 beiscsi_log(phba, KERN_ERR,
4517 BEISCSI_LOG_IO |
4518 BEISCSI_LOG_CONFIG,
4519 "BM_%d : Alloc of WRB_HANDLE Failed"
4520 "for the CID : %d\n",
4521 beiscsi_conn->
4522 beiscsi_conn_cid);
4523 goto free_mgmt_hndls;
4524 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304525 beiscsi_conn->plogin_wrb_handle =
4526 io_task->pwrb_handle;
4527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304528 } else {
4529 io_task->psgl_handle =
4530 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304531 io_task->pwrb_handle =
4532 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304533 }
4534 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304535 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304536 if (!io_task->psgl_handle) {
4537 beiscsi_log(phba, KERN_ERR,
4538 BEISCSI_LOG_IO |
4539 BEISCSI_LOG_CONFIG,
4540 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4541 "for the CID : %d\n",
4542 beiscsi_conn->
4543 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304544 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304545 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304546 io_task->pwrb_handle =
4547 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304548 beiscsi_conn->beiscsi_conn_cid,
4549 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304550 if (!io_task->pwrb_handle) {
4551 beiscsi_log(phba, KERN_ERR,
4552 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4553 "BM_%d : Alloc of WRB_HANDLE Failed"
4554 "for the CID : %d\n",
4555 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304556 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304557 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304558
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304559 }
4560 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304561 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4562 wrb_index << 16) | (unsigned int)
4563 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304564 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304565
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304566 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4567 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304568
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304569free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304570 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304571 goto free_hndls;
4572free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304573 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004574 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304575free_hndls:
4576 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004577 cri_index = BE_GET_CRI_FROM_CID(
4578 beiscsi_conn->beiscsi_conn_cid);
4579 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304580 if (io_task->pwrb_handle)
4581 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304582 io_task->pwrb_handle = NULL;
4583 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4584 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004585 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304586 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304587}
John Soni Jose09a10932012-10-20 04:44:23 +05304588int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4589 unsigned int num_sg, unsigned int xferlen,
4590 unsigned int writedir)
4591{
4592
4593 struct beiscsi_io_task *io_task = task->dd_data;
4594 struct iscsi_conn *conn = task->conn;
4595 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4596 struct beiscsi_hba *phba = beiscsi_conn->phba;
4597 struct iscsi_wrb *pwrb = NULL;
4598 unsigned int doorbell = 0;
4599
4600 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304601
John Soni Jose09a10932012-10-20 04:44:23 +05304602 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4603
4604 if (writedir) {
4605 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4606 INI_WR_CMD);
4607 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4608 } else {
4609 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4610 INI_RD_CMD);
4611 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4612 }
4613
4614 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4615 type, pwrb);
4616
4617 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4618 cpu_to_be16(*(unsigned short *)
4619 &io_task->cmd_bhs->iscsi_hdr.lun));
4620 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4621 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4622 io_task->pwrb_handle->wrb_index);
4623 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4624 be32_to_cpu(task->cmdsn));
4625 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4626 io_task->psgl_handle->sgl_index);
4627
4628 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4629 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304630 io_task->pwrb_handle->wrb_index);
4631 if (io_task->pwrb_context->plast_wrb)
4632 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4633 io_task->pwrb_context->plast_wrb,
4634 io_task->pwrb_handle->wrb_index);
4635 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304636
4637 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4638
4639 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4640 doorbell |= (io_task->pwrb_handle->wrb_index &
4641 DB_DEF_PDU_WRB_INDEX_MASK) <<
4642 DB_DEF_PDU_WRB_INDEX_SHIFT;
4643 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004644 iowrite32(doorbell, phba->db_va +
4645 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304646 return 0;
4647}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304648
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304649static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4650 unsigned int num_sg, unsigned int xferlen,
4651 unsigned int writedir)
4652{
4653
4654 struct beiscsi_io_task *io_task = task->dd_data;
4655 struct iscsi_conn *conn = task->conn;
4656 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4657 struct beiscsi_hba *phba = beiscsi_conn->phba;
4658 struct iscsi_wrb *pwrb = NULL;
4659 unsigned int doorbell = 0;
4660
4661 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304662 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4663
4664 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304665 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4666 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304667 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304668 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304669 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4670 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304671 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4672 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304673
John Soni Jose09a10932012-10-20 04:44:23 +05304674 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4675 type, pwrb);
4676
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304677 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004678 cpu_to_be16(*(unsigned short *)
4679 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304680 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4681 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4682 io_task->pwrb_handle->wrb_index);
4683 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4684 be32_to_cpu(task->cmdsn));
4685 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4686 io_task->psgl_handle->sgl_index);
4687
4688 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4689
4690 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304691 io_task->pwrb_handle->wrb_index);
4692 if (io_task->pwrb_context->plast_wrb)
4693 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4694 io_task->pwrb_context->plast_wrb,
4695 io_task->pwrb_handle->wrb_index);
4696 io_task->pwrb_context->plast_wrb = pwrb;
4697
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304698 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4699
4700 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304701 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304702 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4703 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4704
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004705 iowrite32(doorbell, phba->db_va +
4706 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304707 return 0;
4708}
4709
4710static int beiscsi_mtask(struct iscsi_task *task)
4711{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304712 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304713 struct iscsi_conn *conn = task->conn;
4714 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4715 struct beiscsi_hba *phba = beiscsi_conn->phba;
4716 struct iscsi_wrb *pwrb = NULL;
4717 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304718 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304719 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004720 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304721
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304722 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304723 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304724 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304725
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004726 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304727 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4728 be32_to_cpu(task->cmdsn));
4729 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4730 io_task->pwrb_handle->wrb_index);
4731 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4732 io_task->psgl_handle->sgl_index);
4733 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4734 task->data_count);
4735 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304736 io_task->pwrb_handle->wrb_index);
4737 if (io_task->pwrb_context->plast_wrb)
4738 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
4739 io_task->pwrb_context->plast_wrb,
4740 io_task->pwrb_handle->wrb_index);
4741 io_task->pwrb_context->plast_wrb = pwrb;
4742
John Soni Jose09a10932012-10-20 04:44:23 +05304743 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004744 } else {
4745 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4746 be32_to_cpu(task->cmdsn));
4747 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4748 io_task->pwrb_handle->wrb_index);
4749 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4750 io_task->psgl_handle->sgl_index);
4751 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4752 task->data_count);
4753 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304754 io_task->pwrb_handle->wrb_index);
4755 if (io_task->pwrb_context->plast_wrb)
4756 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4757 io_task->pwrb_context->plast_wrb,
4758 io_task->pwrb_handle->wrb_index);
4759 io_task->pwrb_context->plast_wrb = pwrb;
4760
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004761 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304762 }
4763
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304764
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304765 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4766 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304767 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304768 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004769 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304770 break;
4771 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004772 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304773 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004774 if (is_chip_be2_be3r(phba))
4775 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304776 dmsg, pwrb, 1);
4777 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004778 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304779 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004780 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304781 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004782 if (is_chip_be2_be3r(phba))
4783 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304784 dmsg, pwrb, 0);
4785 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004786 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304787 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004788 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004789 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304790 break;
4791 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304792 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004793 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304794 break;
4795 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304796 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004797 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304798 break;
4799 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304800 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004801 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304802 break;
4803
4804 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304805 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4806 "BM_%d : opcode =%d Not supported\n",
4807 task->hdr->opcode & ISCSI_OPCODE_MASK);
4808
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304809 return -EINVAL;
4810 }
4811
Alexey Khoroshilove0493622016-01-16 00:45:34 +03004812 if (ret)
4813 return ret;
4814
John Soni Jose09a10932012-10-20 04:44:23 +05304815 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004816 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4817 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4818 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304819
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304820 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304821 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304822 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4823 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004824 iowrite32(doorbell, phba->db_va +
4825 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304826 return 0;
4827}
4828
4829static int beiscsi_task_xmit(struct iscsi_task *task)
4830{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304831 struct beiscsi_io_task *io_task = task->dd_data;
4832 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05304833 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304834 struct scatterlist *sg;
4835 int num_sg;
4836 unsigned int writedir = 0, xferlen = 0;
4837
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304838 phba = io_task->conn->phba;
4839 /**
4840 * HBA in error includes BEISCSI_HBA_FW_TIMEOUT. IO path might be
4841 * operational if FW still gets heartbeat from EP FW. Is management
4842 * path really needed to continue further?
4843 */
4844 if (beiscsi_hba_in_error(phba))
4845 return -EIO;
4846
Jitendra Bhivare18683792016-02-04 15:49:18 +05304847 if (!io_task->conn->login_in_progress)
4848 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05304849
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304850 if (!sc)
4851 return beiscsi_mtask(task);
4852
4853 io_task->scsi_cmnd = sc;
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304854 io_task->num_sg = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304855 num_sg = scsi_dma_map(sc);
4856 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07004857 beiscsi_log(phba, KERN_ERR,
4858 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
4859 "BM_%d : scsi_dma_map Failed "
4860 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
4861 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
4862 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05304863
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304864 return num_sg;
4865 }
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304866 /**
4867 * For scsi cmd task, check num_sg before unmapping in cleanup_task.
4868 * For management task, cleanup_task checks mtask_addr before unmapping.
4869 */
4870 io_task->num_sg = num_sg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304871 xferlen = scsi_bufflen(sc);
4872 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304873 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304874 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304875 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304876 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304877
John Soni Jose09a10932012-10-20 04:44:23 +05304878 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304879}
4880
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004881/**
4882 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4883 * @job: job to handle
4884 */
4885static int beiscsi_bsg_request(struct bsg_job *job)
4886{
4887 struct Scsi_Host *shost;
4888 struct beiscsi_hba *phba;
4889 struct iscsi_bsg_request *bsg_req = job->request;
4890 int rc = -EINVAL;
4891 unsigned int tag;
4892 struct be_dma_mem nonemb_cmd;
4893 struct be_cmd_resp_hdr *resp;
4894 struct iscsi_bsg_reply *bsg_reply = job->reply;
4895 unsigned short status, extd_status;
4896
4897 shost = iscsi_job_to_shost(job);
4898 phba = iscsi_host_priv(shost);
4899
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304900 if (beiscsi_hba_in_error(phba)) {
4901 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
4902 "BM_%d : HBA in error 0x%lx\n", phba->state);
4903 return -ENXIO;
4904 }
4905
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004906 switch (bsg_req->msgcode) {
4907 case ISCSI_BSG_HST_VENDOR:
4908 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4909 job->request_payload.payload_len,
4910 &nonemb_cmd.dma);
4911 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304912 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4913 "BM_%d : Failed to allocate memory for "
4914 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304915 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004916 }
4917 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4918 &nonemb_cmd);
4919 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304920 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304921 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304922
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004923 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4924 nonemb_cmd.va, nonemb_cmd.dma);
4925 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304926 }
4927
4928 rc = wait_event_interruptible_timeout(
4929 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304930 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05304931 msecs_to_jiffies(
4932 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05304933 extd_status = (phba->ctrl.mcc_tag_status[tag] &
4934 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
4935 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05304936 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004937 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4938 sg_copy_from_buffer(job->reply_payload.sg_list,
4939 job->reply_payload.sg_cnt,
4940 nonemb_cmd.va, (resp->response_length
4941 + sizeof(*resp)));
4942 bsg_reply->reply_payload_rcv_len = resp->response_length;
4943 bsg_reply->result = status;
4944 bsg_job_done(job, bsg_reply->result,
4945 bsg_reply->reply_payload_rcv_len);
4946 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4947 nonemb_cmd.va, nonemb_cmd.dma);
4948 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304949 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304950 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304951 " status = %d extd_status = %d\n",
4952 status, extd_status);
4953
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004954 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304955 } else {
4956 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004957 }
4958 break;
4959
4960 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304961 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4962 "BM_%d : Unsupported bsg command: 0x%x\n",
4963 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004964 break;
4965 }
4966
4967 return rc;
4968}
4969
John Soni Jose99bc5d52012-08-20 23:00:18 +05304970void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4971{
4972 /* Set the logging parameter */
4973 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4974}
4975
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304976/*
4977 * beiscsi_quiesce()- Cleanup Driver resources
4978 * @phba: Instance Priv structure
4979 *
4980 * Free the OS and HW resources held by the driver
4981 **/
Jitendra Bhivare9122e992016-08-19 15:20:11 +05304982static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304983{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304984 struct hwi_controller *phwi_ctrlr;
4985 struct hwi_context_memory *phwi_context;
4986 struct be_eq_obj *pbe_eq;
4987 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304988
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304989 phwi_ctrlr = phba->phwi_ctrlr;
4990 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304991 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304992 if (phba->msix_enabled) {
4993 for (i = 0; i <= phba->num_cpus; i++) {
4994 msix_vec = phba->msix_entries[i].vector;
4995 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004996 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304997 }
4998 } else
Lars-Peter Clausene729b502016-03-04 11:15:06 +01004999 if (phba->pcidev->irq)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305000 free_irq(phba->pcidev->irq, phba);
5001 pci_disable_msix(phba->pcidev);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305002 cancel_delayed_work_sync(&phba->eqd_update);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305003 cancel_work_sync(&phba->boot_work);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305004 del_timer_sync(&phba->hw_check);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005005
Jens Axboe89f8b332014-03-13 09:38:42 -06005006 for (i = 0; i < phba->num_cpus; i++) {
5007 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005008 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005009 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305010
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305011 /* PCI_ERR is set then check if driver is not unloading */
5012 if (test_bit(BEISCSI_HBA_RUNNING, &phba->state) &&
5013 test_bit(BEISCSI_HBA_PCI_ERR, &phba->state)) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005014 hwi_cleanup(phba);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305015 return;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005016 }
John Soni Jose7a158002012-10-20 04:45:51 +05305017
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305018 destroy_workqueue(phba->wq);
5019 beiscsi_clean_port(phba);
5020 beiscsi_free_mem(phba);
5021
5022 beiscsi_unmap_pci_function(phba);
5023 pci_free_consistent(phba->pcidev,
5024 phba->ctrl.mbox_mem_alloced.size,
5025 phba->ctrl.mbox_mem_alloced.va,
5026 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005027}
5028
5029static void beiscsi_remove(struct pci_dev *pcidev)
5030{
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005031 struct beiscsi_hba *phba = NULL;
5032
5033 phba = pci_get_drvdata(pcidev);
5034 if (!phba) {
5035 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5036 return;
5037 }
5038
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305039 clear_bit(BEISCSI_HBA_RUNNING, &phba->state);
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305040 beiscsi_iface_destroy_default(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305041 iscsi_host_remove(phba->shost);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305042 beiscsi_quiesce(phba);
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305043 /* after cancelling boot_work */
5044 iscsi_boot_destroy_kset(phba->boot_struct.boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305045 pci_dev_put(phba->pcidev);
5046 iscsi_host_free(phba->shost);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005047 pci_disable_pcie_error_reporting(pcidev);
5048 pci_set_drvdata(pcidev, NULL);
John Soni Josee307f3a2015-04-25 08:17:19 +05305049 pci_release_regions(pcidev);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07005050 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305051}
5052
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305053static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5054{
5055 int i, status;
5056
5057 for (i = 0; i <= phba->num_cpus; i++)
5058 phba->msix_entries[i].entry = i;
5059
Alexander Gordeeve149fc12014-08-18 08:01:48 +02005060 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5061 phba->num_cpus + 1, phba->num_cpus + 1);
5062 if (status > 0)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305063 phba->msix_enabled = true;
5064
5065 return;
5066}
5067
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305068void beiscsi_start_boot_work(struct beiscsi_hba *phba, unsigned int s_handle)
5069{
5070 if (phba->boot_struct.boot_kset)
5071 return;
5072
5073 /* skip if boot work is already in progress */
5074 if (test_and_set_bit(BEISCSI_HBA_BOOT_WORK, &phba->state))
5075 return;
5076
5077 phba->boot_struct.retry = 3;
5078 phba->boot_struct.tag = 0;
5079 phba->boot_struct.s_handle = s_handle;
5080 phba->boot_struct.action = BEISCSI_BOOT_GET_SHANDLE;
5081 schedule_work(&phba->boot_work);
5082}
5083
5084static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
5085{
5086 struct beiscsi_hba *phba = data;
5087 struct mgmt_session_info *boot_sess = &phba->boot_struct.boot_sess;
5088 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
5089 char *str = buf;
5090 int rc = -EPERM;
5091
5092 switch (type) {
5093 case ISCSI_BOOT_TGT_NAME:
5094 rc = sprintf(buf, "%.*s\n",
5095 (int)strlen(boot_sess->target_name),
5096 (char *)&boot_sess->target_name);
5097 break;
5098 case ISCSI_BOOT_TGT_IP_ADDR:
5099 if (boot_conn->dest_ipaddr.ip_type == BEISCSI_IP_TYPE_V4)
5100 rc = sprintf(buf, "%pI4\n",
5101 (char *)&boot_conn->dest_ipaddr.addr);
5102 else
5103 rc = sprintf(str, "%pI6\n",
5104 (char *)&boot_conn->dest_ipaddr.addr);
5105 break;
5106 case ISCSI_BOOT_TGT_PORT:
5107 rc = sprintf(str, "%d\n", boot_conn->dest_port);
5108 break;
5109
5110 case ISCSI_BOOT_TGT_CHAP_NAME:
5111 rc = sprintf(str, "%.*s\n",
5112 boot_conn->negotiated_login_options.auth_data.chap.
5113 target_chap_name_length,
5114 (char *)&boot_conn->negotiated_login_options.
5115 auth_data.chap.target_chap_name);
5116 break;
5117 case ISCSI_BOOT_TGT_CHAP_SECRET:
5118 rc = sprintf(str, "%.*s\n",
5119 boot_conn->negotiated_login_options.auth_data.chap.
5120 target_secret_length,
5121 (char *)&boot_conn->negotiated_login_options.
5122 auth_data.chap.target_secret);
5123 break;
5124 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5125 rc = sprintf(str, "%.*s\n",
5126 boot_conn->negotiated_login_options.auth_data.chap.
5127 intr_chap_name_length,
5128 (char *)&boot_conn->negotiated_login_options.
5129 auth_data.chap.intr_chap_name);
5130 break;
5131 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5132 rc = sprintf(str, "%.*s\n",
5133 boot_conn->negotiated_login_options.auth_data.chap.
5134 intr_secret_length,
5135 (char *)&boot_conn->negotiated_login_options.
5136 auth_data.chap.intr_secret);
5137 break;
5138 case ISCSI_BOOT_TGT_FLAGS:
5139 rc = sprintf(str, "2\n");
5140 break;
5141 case ISCSI_BOOT_TGT_NIC_ASSOC:
5142 rc = sprintf(str, "0\n");
5143 break;
5144 }
5145 return rc;
5146}
5147
5148static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
5149{
5150 struct beiscsi_hba *phba = data;
5151 char *str = buf;
5152 int rc = -EPERM;
5153
5154 switch (type) {
5155 case ISCSI_BOOT_INI_INITIATOR_NAME:
5156 rc = sprintf(str, "%s\n",
5157 phba->boot_struct.boot_sess.initiator_iscsiname);
5158 break;
5159 }
5160 return rc;
5161}
5162
5163static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
5164{
5165 struct beiscsi_hba *phba = data;
5166 char *str = buf;
5167 int rc = -EPERM;
5168
5169 switch (type) {
5170 case ISCSI_BOOT_ETH_FLAGS:
5171 rc = sprintf(str, "2\n");
5172 break;
5173 case ISCSI_BOOT_ETH_INDEX:
5174 rc = sprintf(str, "0\n");
5175 break;
5176 case ISCSI_BOOT_ETH_MAC:
5177 rc = beiscsi_get_macaddr(str, phba);
5178 break;
5179 }
5180 return rc;
5181}
5182
5183
5184static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
5185{
5186 umode_t rc = 0;
5187
5188 switch (type) {
5189 case ISCSI_BOOT_TGT_NAME:
5190 case ISCSI_BOOT_TGT_IP_ADDR:
5191 case ISCSI_BOOT_TGT_PORT:
5192 case ISCSI_BOOT_TGT_CHAP_NAME:
5193 case ISCSI_BOOT_TGT_CHAP_SECRET:
5194 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
5195 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
5196 case ISCSI_BOOT_TGT_NIC_ASSOC:
5197 case ISCSI_BOOT_TGT_FLAGS:
5198 rc = S_IRUGO;
5199 break;
5200 }
5201 return rc;
5202}
5203
5204static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
5205{
5206 umode_t rc = 0;
5207
5208 switch (type) {
5209 case ISCSI_BOOT_INI_INITIATOR_NAME:
5210 rc = S_IRUGO;
5211 break;
5212 }
5213 return rc;
5214}
5215
5216
5217static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
5218{
5219 umode_t rc = 0;
5220
5221 switch (type) {
5222 case ISCSI_BOOT_ETH_FLAGS:
5223 case ISCSI_BOOT_ETH_MAC:
5224 case ISCSI_BOOT_ETH_INDEX:
5225 rc = S_IRUGO;
5226 break;
5227 }
5228 return rc;
5229}
5230
5231static void beiscsi_boot_kobj_release(void *data)
5232{
5233 struct beiscsi_hba *phba = data;
5234
5235 scsi_host_put(phba->shost);
5236}
5237
5238static int beiscsi_boot_create_kset(struct beiscsi_hba *phba)
5239{
5240 struct boot_struct *bs = &phba->boot_struct;
5241 struct iscsi_boot_kobj *boot_kobj;
5242
5243 if (bs->boot_kset) {
5244 __beiscsi_log(phba, KERN_ERR,
5245 "BM_%d: boot_kset already created\n");
5246 return 0;
5247 }
5248
5249 bs->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
5250 if (!bs->boot_kset) {
5251 __beiscsi_log(phba, KERN_ERR,
5252 "BM_%d: boot_kset alloc failed\n");
5253 return -ENOMEM;
5254 }
5255
5256 /* get shost ref because the show function will refer phba */
5257 if (!scsi_host_get(phba->shost))
5258 goto free_kset;
5259
5260 boot_kobj = iscsi_boot_create_target(bs->boot_kset, 0, phba,
5261 beiscsi_show_boot_tgt_info,
5262 beiscsi_tgt_get_attr_visibility,
5263 beiscsi_boot_kobj_release);
5264 if (!boot_kobj)
5265 goto put_shost;
5266
5267 if (!scsi_host_get(phba->shost))
5268 goto free_kset;
5269
5270 boot_kobj = iscsi_boot_create_initiator(bs->boot_kset, 0, phba,
5271 beiscsi_show_boot_ini_info,
5272 beiscsi_ini_get_attr_visibility,
5273 beiscsi_boot_kobj_release);
5274 if (!boot_kobj)
5275 goto put_shost;
5276
5277 if (!scsi_host_get(phba->shost))
5278 goto free_kset;
5279
5280 boot_kobj = iscsi_boot_create_ethernet(bs->boot_kset, 0, phba,
5281 beiscsi_show_boot_eth_info,
5282 beiscsi_eth_get_attr_visibility,
5283 beiscsi_boot_kobj_release);
5284 if (!boot_kobj)
5285 goto put_shost;
5286
5287 return 0;
5288
5289put_shost:
5290 scsi_host_put(phba->shost);
5291free_kset:
5292 iscsi_boot_destroy_kset(bs->boot_kset);
5293 bs->boot_kset = NULL;
5294 return -ENOMEM;
5295}
5296
5297static void beiscsi_boot_work(struct work_struct *work)
5298{
5299 struct beiscsi_hba *phba =
5300 container_of(work, struct beiscsi_hba, boot_work);
5301 struct boot_struct *bs = &phba->boot_struct;
5302 unsigned int tag = 0;
5303
5304 if (beiscsi_hba_in_error(phba))
5305 return;
5306
5307 beiscsi_log(phba, KERN_INFO,
5308 BEISCSI_LOG_CONFIG | BEISCSI_LOG_MBOX,
5309 "BM_%d : %s action %d\n",
5310 __func__, phba->boot_struct.action);
5311
5312 switch (phba->boot_struct.action) {
5313 case BEISCSI_BOOT_REOPEN_SESS:
5314 tag = beiscsi_boot_reopen_sess(phba);
5315 break;
5316 case BEISCSI_BOOT_GET_SHANDLE:
5317 tag = __beiscsi_boot_get_shandle(phba, 1);
5318 break;
5319 case BEISCSI_BOOT_GET_SINFO:
5320 tag = beiscsi_boot_get_sinfo(phba);
5321 break;
5322 case BEISCSI_BOOT_LOGOUT_SESS:
5323 tag = beiscsi_boot_logout_sess(phba);
5324 break;
5325 case BEISCSI_BOOT_CREATE_KSET:
5326 beiscsi_boot_create_kset(phba);
5327 /**
5328 * updated boot_kset is made visible to all before
5329 * ending the boot work.
5330 */
5331 mb();
5332 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5333 return;
5334 }
5335 if (!tag) {
5336 if (bs->retry--)
5337 schedule_work(&phba->boot_work);
5338 else
5339 clear_bit(BEISCSI_HBA_BOOT_WORK, &phba->state);
5340 }
5341}
5342
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305343static void beiscsi_hw_health_check(unsigned long ptr)
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005344{
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305345 struct beiscsi_hba *phba;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005346
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305347 phba = (struct beiscsi_hba *)ptr;
5348 beiscsi_ue_detect(phba);
5349 if (test_bit(BEISCSI_HBA_IN_UE, &phba->state))
5350 return;
5351
5352 mod_timer(&phba->hw_check,
5353 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
5354}
5355
5356static void beiscsi_eqd_update_work(struct work_struct *work)
5357{
5358 struct hwi_context_memory *phwi_context;
5359 struct be_set_eqd set_eqd[MAX_CPUS];
5360 struct hwi_controller *phwi_ctrlr;
5361 struct be_eq_obj *pbe_eq;
5362 struct beiscsi_hba *phba;
5363 unsigned int pps, delta;
5364 struct be_aic_obj *aic;
5365 int eqd, i, num = 0;
5366 unsigned long now;
5367
5368 phba = container_of(work, struct beiscsi_hba, eqd_update.work);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305369 if (beiscsi_hba_in_error(phba))
5370 return;
5371
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005372 phwi_ctrlr = phba->phwi_ctrlr;
5373 phwi_context = phwi_ctrlr->phwi_ctxt;
5374
5375 for (i = 0; i <= phba->num_cpus; i++) {
5376 aic = &phba->aic_obj[i];
5377 pbe_eq = &phwi_context->be_eq[i];
5378 now = jiffies;
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305379 if (!aic->jiffies || time_before(now, aic->jiffies) ||
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005380 pbe_eq->cq_count < aic->eq_prev) {
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305381 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005382 aic->eq_prev = pbe_eq->cq_count;
5383 continue;
5384 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305385 delta = jiffies_to_msecs(now - aic->jiffies);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005386 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5387 eqd = (pps / 1500) << 2;
5388
5389 if (eqd < 8)
5390 eqd = 0;
5391 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5392 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5393
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305394 aic->jiffies = now;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005395 aic->eq_prev = pbe_eq->cq_count;
5396
5397 if (eqd != aic->prev_eqd) {
5398 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5399 set_eqd[num].eq_id = pbe_eq->q.id;
5400 aic->prev_eqd = eqd;
5401 num++;
5402 }
5403 }
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305404 if (num)
5405 /* completion of this is ignored */
5406 beiscsi_modify_eq_delay(phba, set_eqd, num);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005407
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305408 schedule_delayed_work(&phba->eqd_update,
5409 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305410}
5411
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005412
5413static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5414 pci_channel_state_t state)
5415{
5416 struct beiscsi_hba *phba = NULL;
5417
5418 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305419 set_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005420
5421 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5422 "BM_%d : EEH error detected\n");
5423
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305424 beiscsi_quiesce(phba);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005425
5426 if (state == pci_channel_io_perm_failure) {
5427 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5428 "BM_%d : EEH : State PERM Failure");
5429 return PCI_ERS_RESULT_DISCONNECT;
5430 }
5431
5432 pci_disable_device(pdev);
5433
5434 /* The error could cause the FW to trigger a flash debug dump.
5435 * Resetting the card while flash dump is in progress
5436 * can cause it not to recover; wait for it to finish.
5437 * Wait only for first function as it is needed only once per
5438 * adapter.
5439 **/
5440 if (pdev->devfn == 0)
5441 ssleep(30);
5442
5443 return PCI_ERS_RESULT_NEED_RESET;
5444}
5445
5446static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5447{
5448 struct beiscsi_hba *phba = NULL;
5449 int status = 0;
5450
5451 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5452
5453 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5454 "BM_%d : EEH Reset\n");
5455
5456 status = pci_enable_device(pdev);
5457 if (status)
5458 return PCI_ERS_RESULT_DISCONNECT;
5459
5460 pci_set_master(pdev);
5461 pci_set_power_state(pdev, PCI_D0);
5462 pci_restore_state(pdev);
5463
5464 /* Wait for the CHIP Reset to complete */
5465 status = be_chk_reset_complete(phba);
5466 if (!status) {
5467 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5468 "BM_%d : EEH Reset Completed\n");
5469 } else {
5470 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5471 "BM_%d : EEH Reset Completion Failure\n");
5472 return PCI_ERS_RESULT_DISCONNECT;
5473 }
5474
5475 pci_cleanup_aer_uncorrect_error_status(pdev);
5476 return PCI_ERS_RESULT_RECOVERED;
5477}
5478
5479static void beiscsi_eeh_resume(struct pci_dev *pdev)
5480{
5481 int ret = 0, i;
5482 struct be_eq_obj *pbe_eq;
5483 struct beiscsi_hba *phba = NULL;
5484 struct hwi_controller *phwi_ctrlr;
5485 struct hwi_context_memory *phwi_context;
5486
5487 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5488 pci_save_state(pdev);
5489
5490 if (enable_msix)
5491 find_num_cpus(phba);
5492 else
5493 phba->num_cpus = 1;
5494
5495 if (enable_msix) {
5496 beiscsi_msix_enable(phba);
5497 if (!phba->msix_enabled)
5498 phba->num_cpus = 1;
5499 }
5500
5501 ret = beiscsi_cmd_reset_function(phba);
5502 if (ret) {
5503 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5504 "BM_%d : Reset Failed\n");
5505 goto ret_err;
5506 }
5507
5508 ret = be_chk_reset_complete(phba);
5509 if (ret) {
5510 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5511 "BM_%d : Failed to get out of reset.\n");
5512 goto ret_err;
5513 }
5514
5515 beiscsi_get_params(phba);
5516 phba->shost->max_id = phba->params.cxns_per_ctrl;
5517 phba->shost->can_queue = phba->params.ios_per_ctrl;
5518 ret = hwi_init_controller(phba);
Nicholas Krausec6fff322016-02-27 12:43:25 -05005519 if (ret) {
5520 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5521 "BM_%d : beiscsi_eeh_resume -"
5522 "Failed to initialize beiscsi_hba.\n");
5523 goto ret_err;
5524 }
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005525
5526 for (i = 0; i < MAX_MCC_CMD; i++) {
5527 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5528 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305529 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005530 phba->ctrl.mcc_tag_available++;
5531 }
5532
5533 phwi_ctrlr = phba->phwi_ctrlr;
5534 phwi_context = phwi_ctrlr->phwi_ctxt;
5535
Jens Axboe89f8b332014-03-13 09:38:42 -06005536 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005537 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005538 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005539 be_iopoll);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005540 }
5541
Jens Axboe89f8b332014-03-13 09:38:42 -06005542 i = (phba->msix_enabled) ? i : 0;
5543 /* Work item for MCC handling */
5544 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305545 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005546
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005547 ret = beiscsi_init_irqs(phba);
5548 if (ret < 0) {
5549 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5550 "BM_%d : beiscsi_eeh_resume - "
5551 "Failed to beiscsi_init_irqs\n");
5552 goto ret_err;
5553 }
5554
5555 hwi_enable_intr(phba);
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305556 clear_bit(BEISCSI_HBA_PCI_ERR, &phba->state);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005557
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305558 /* start hw_check timer and eqd_update work */
5559 schedule_delayed_work(&phba->eqd_update,
5560 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5561 mod_timer(&phba->hw_check,
5562 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005563 return;
5564ret_err:
5565 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5566 "BM_%d : AER EEH Resume Failed\n");
5567}
5568
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005569static int beiscsi_dev_probe(struct pci_dev *pcidev,
5570 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305571{
5572 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305573 struct hwi_controller *phwi_ctrlr;
5574 struct hwi_context_memory *phwi_context;
5575 struct be_eq_obj *pbe_eq;
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305576 unsigned int s_handle;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005577 int ret = 0, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305578
5579 ret = beiscsi_enable_pci(pcidev);
5580 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305581 dev_err(&pcidev->dev,
5582 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305583 return ret;
5584 }
5585
5586 phba = beiscsi_hba_alloc(pcidev);
5587 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305588 dev_err(&pcidev->dev,
5589 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305590 goto disable_pci;
5591 }
5592
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005593 /* Enable EEH reporting */
5594 ret = pci_enable_pcie_error_reporting(pcidev);
5595 if (ret)
5596 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5597 "BM_%d : PCIe Error Reporting "
5598 "Enabling Failed\n");
5599
5600 pci_save_state(pcidev);
5601
John Soni Jose99bc5d52012-08-20 23:00:18 +05305602 /* Initialize Driver configuration Paramters */
5603 beiscsi_hba_attrs_init(phba);
5604
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005605 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305606
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305607 switch (pcidev->device) {
5608 case BE_DEVICE_ID1:
5609 case OC_DEVICE_ID1:
5610 case OC_DEVICE_ID2:
5611 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305612 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305613 break;
5614 case BE_DEVICE_ID2:
5615 case OC_DEVICE_ID3:
5616 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305617 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305618 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305619 case OC_SKH_ID1:
5620 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305621 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005622 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305623 default:
5624 phba->generation = 0;
5625 }
5626
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305627 ret = be_ctrl_init(phba, pcidev);
5628 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305629 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5630 "BM_%d : beiscsi_dev_probe-"
5631 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305632 goto hba_free;
5633 }
5634
Jitendra Bhivare9122e992016-08-19 15:20:11 +05305635 set_bit(BEISCSI_HBA_RUNNING, &phba->state);
Jitendra Bhivarecdaa4de2016-01-20 14:10:51 +05305636 /*
5637 * FUNCTION_RESET should clean up any stale info in FW for this fn
5638 */
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305639 ret = beiscsi_cmd_reset_function(phba);
5640 if (ret) {
5641 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal92665a62013-09-28 15:35:43 -07005642 "BM_%d : Reset Failed\n");
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305643 goto hba_free;
5644 }
5645 ret = be_chk_reset_complete(phba);
5646 if (ret) {
5647 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal92665a62013-09-28 15:35:43 -07005648 "BM_%d : Failed to get out of reset.\n");
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305649 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05305650 }
5651
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305652 spin_lock_init(&phba->io_sgl_lock);
5653 spin_lock_init(&phba->mgmt_sgl_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005654 spin_lock_init(&phba->async_pdu_lock);
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305655 ret = beiscsi_get_fw_config(&phba->ctrl, phba);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305656 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305657 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5658 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305659 goto free_port;
5660 }
Jitendra Bhivare480195c2016-08-19 15:20:15 +05305661 beiscsi_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305662 beiscsi_get_params(phba);
Jitendra Bhivare66940952016-08-19 15:20:14 +05305663 beiscsi_set_uer_feature(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005664
5665 if (enable_msix)
5666 find_num_cpus(phba);
5667 else
5668 phba->num_cpus = 1;
5669
5670 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5671 "BM_%d : num_cpus = %d\n",
5672 phba->num_cpus);
5673
5674 if (enable_msix) {
5675 beiscsi_msix_enable(phba);
5676 if (!phba->msix_enabled)
5677 phba->num_cpus = 1;
5678 }
5679
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005680 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305681 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305682 ret = beiscsi_init_port(phba);
5683 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305684 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5685 "BM_%d : beiscsi_dev_probe-"
5686 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305687 goto free_port;
5688 }
5689
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005690 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305691 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5692 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305693 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305694 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005695 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005696 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305697 }
5698
5699 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5700
John Soni Jose72fb46a2012-10-20 04:42:49 +05305701 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305702 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005703 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305704 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305705 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5706 "BM_%d : beiscsi_dev_probe-"
5707 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305708 goto free_twq;
5709 }
5710
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305711 INIT_DELAYED_WORK(&phba->eqd_update, beiscsi_eqd_update_work);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305712
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305713 phwi_ctrlr = phba->phwi_ctrlr;
5714 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305715
Jens Axboe89f8b332014-03-13 09:38:42 -06005716 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305717 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005718 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005719 be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305720 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305721
Jens Axboe89f8b332014-03-13 09:38:42 -06005722 i = (phba->msix_enabled) ? i : 0;
5723 /* Work item for MCC handling */
5724 pbe_eq = &phwi_context->be_eq[i];
Jitendra Bhivarea30950162016-08-19 15:20:10 +05305725 INIT_WORK(&pbe_eq->mcc_work, beiscsi_mcc_work);
Jens Axboe89f8b332014-03-13 09:38:42 -06005726
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305727 ret = beiscsi_init_irqs(phba);
5728 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305729 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5730 "BM_%d : beiscsi_dev_probe-"
5731 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305732 goto free_blkenbld;
5733 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305734 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005735
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005736 if (iscsi_host_add(phba->shost, &phba->pcidev->dev))
5737 goto free_blkenbld;
5738
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305739 INIT_WORK(&phba->boot_work, beiscsi_boot_work);
5740 ret = beiscsi_boot_get_shandle(phba, &s_handle);
5741 if (ret > 0) {
5742 beiscsi_start_boot_work(phba, s_handle);
5743 /**
5744 * Set this bit after starting the work to let
5745 * probe handle it first.
5746 * ASYNC event can too schedule this work.
Mike Christief457a462011-06-24 15:11:53 -05005747 */
Jitendra Bhivare50a4b822016-08-19 15:20:12 +05305748 set_bit(BEISCSI_HBA_BOOT_FOUND, &phba->state);
5749 }
Mike Christief457a462011-06-24 15:11:53 -05005750
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305751 beiscsi_iface_create_default(phba);
Jitendra Bhivare10bcd472016-08-19 15:20:13 +05305752 schedule_delayed_work(&phba->eqd_update,
5753 msecs_to_jiffies(BEISCSI_EQD_UPDATE_INTERVAL));
5754 /**
5755 * Start UE detection here. UE before this will cause stall in probe
5756 * and eventually fail the probe.
5757 */
5758 init_timer(&phba->hw_check);
5759 phba->hw_check.function = beiscsi_hw_health_check;
5760 phba->hw_check.data = (unsigned long)phba;
5761 mod_timer(&phba->hw_check,
5762 jiffies + msecs_to_jiffies(BEISCSI_UE_DETECT_INTERVAL));
John Soni Jose7a158002012-10-20 04:45:51 +05305763
John Soni Jose99bc5d52012-08-20 23:00:18 +05305764 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5765 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305766 return 0;
5767
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305768free_blkenbld:
5769 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005770 for (i = 0; i < phba->num_cpus; i++) {
5771 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005772 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005773 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305774free_twq:
5775 beiscsi_clean_port(phba);
5776 beiscsi_free_mem(phba);
5777free_port:
5778 pci_free_consistent(phba->pcidev,
5779 phba->ctrl.mbox_mem_alloced.size,
5780 phba->ctrl.mbox_mem_alloced.va,
5781 phba->ctrl.mbox_mem_alloced.dma);
5782 beiscsi_unmap_pci_function(phba);
5783hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305784 if (phba->msix_enabled)
5785 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305786 pci_dev_put(phba->pcidev);
5787 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305788 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305789disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305790 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305791 pci_disable_device(pcidev);
5792 return ret;
5793}
5794
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005795static struct pci_error_handlers beiscsi_eeh_handlers = {
5796 .error_detected = beiscsi_eeh_err_detected,
5797 .slot_reset = beiscsi_eeh_reset,
5798 .resume = beiscsi_eeh_resume,
5799};
5800
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305801struct iscsi_transport beiscsi_iscsi_transport = {
5802 .owner = THIS_MODULE,
5803 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305804 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305805 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305806 .create_session = beiscsi_session_create,
5807 .destroy_session = beiscsi_session_destroy,
5808 .create_conn = beiscsi_conn_create,
5809 .bind_conn = beiscsi_conn_bind,
5810 .destroy_conn = iscsi_conn_teardown,
Jitendra Bhivare96b48b92016-08-19 15:20:06 +05305811 .attr_is_visible = beiscsi_attr_is_visible,
5812 .set_iface_param = beiscsi_iface_set_param,
5813 .get_iface_param = beiscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305814 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005815 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305816 .get_session_param = iscsi_session_get_param,
5817 .get_host_param = beiscsi_get_host_param,
5818 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005819 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305820 .send_pdu = iscsi_conn_send_pdu,
5821 .xmit_task = beiscsi_task_xmit,
5822 .cleanup_task = beiscsi_cleanup_task,
5823 .alloc_pdu = beiscsi_alloc_pdu,
5824 .parse_pdu_itt = beiscsi_parse_pdu,
5825 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005826 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305827 .ep_connect = beiscsi_ep_connect,
5828 .ep_poll = beiscsi_ep_poll,
5829 .ep_disconnect = beiscsi_ep_disconnect,
5830 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005831 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305832};
5833
5834static struct pci_driver beiscsi_pci_driver = {
5835 .name = DRV_NAME,
5836 .probe = beiscsi_dev_probe,
5837 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005838 .id_table = beiscsi_pci_id_table,
5839 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305840};
5841
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305842
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305843static int __init beiscsi_module_init(void)
5844{
5845 int ret;
5846
5847 beiscsi_scsi_transport =
5848 iscsi_register_transport(&beiscsi_iscsi_transport);
5849 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305850 printk(KERN_ERR
5851 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305852 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305853 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305854 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5855 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305856
5857 ret = pci_register_driver(&beiscsi_pci_driver);
5858 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305859 printk(KERN_ERR
5860 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305861 goto unregister_iscsi_transport;
5862 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305863 return 0;
5864
5865unregister_iscsi_transport:
5866 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5867 return ret;
5868}
5869
5870static void __exit beiscsi_module_exit(void)
5871{
5872 pci_unregister_driver(&beiscsi_pci_driver);
5873 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5874}
5875
5876module_init(beiscsi_module_init);
5877module_exit(beiscsi_module_exit);