blob: 01052d365d3619765fdbc917ad2a73339618c108 [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 Kallickalc7acc5b2010-07-22 04:29:18 +0530377static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
378{
379 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500380 struct mgmt_session_info *boot_sess = &phba->boot_sess;
381 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530382 char *str = buf;
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530383 int rc = -EPERM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530384
385 switch (type) {
386 case ISCSI_BOOT_TGT_NAME:
387 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500388 (int)strlen(boot_sess->target_name),
389 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530390 break;
391 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500392 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530393 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500394 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530395 else
396 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500397 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530398 break;
399 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500400 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530401 break;
402
403 case ISCSI_BOOT_TGT_CHAP_NAME:
404 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500405 boot_conn->negotiated_login_options.auth_data.chap.
406 target_chap_name_length,
407 (char *)&boot_conn->negotiated_login_options.
408 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530409 break;
410 case ISCSI_BOOT_TGT_CHAP_SECRET:
411 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500412 boot_conn->negotiated_login_options.auth_data.chap.
413 target_secret_length,
414 (char *)&boot_conn->negotiated_login_options.
415 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530416 break;
417 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
418 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500419 boot_conn->negotiated_login_options.auth_data.chap.
420 intr_chap_name_length,
421 (char *)&boot_conn->negotiated_login_options.
422 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530423 break;
424 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500425 rc = sprintf(str, "%.*s\n",
426 boot_conn->negotiated_login_options.auth_data.chap.
427 intr_secret_length,
428 (char *)&boot_conn->negotiated_login_options.
429 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530430 break;
431 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500432 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530433 break;
434 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500435 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530436 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530437 }
438 return rc;
439}
440
441static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
442{
443 struct beiscsi_hba *phba = data;
444 char *str = buf;
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530445 int rc = -EPERM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530446
447 switch (type) {
448 case ISCSI_BOOT_INI_INITIATOR_NAME:
449 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
450 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530451 }
452 return rc;
453}
454
455static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
456{
457 struct beiscsi_hba *phba = data;
458 char *str = buf;
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530459 int rc = -EPERM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530460
461 switch (type) {
462 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500463 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464 break;
465 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500466 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530467 break;
468 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500469 rc = beiscsi_get_macaddr(str, phba);
470 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530471 }
472 return rc;
473}
474
475
Al Viro587a1f12011-07-23 23:11:19 -0400476static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530477{
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530478 umode_t rc = 0;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530479
480 switch (type) {
481 case ISCSI_BOOT_TGT_NAME:
482 case ISCSI_BOOT_TGT_IP_ADDR:
483 case ISCSI_BOOT_TGT_PORT:
484 case ISCSI_BOOT_TGT_CHAP_NAME:
485 case ISCSI_BOOT_TGT_CHAP_SECRET:
486 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
487 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
488 case ISCSI_BOOT_TGT_NIC_ASSOC:
489 case ISCSI_BOOT_TGT_FLAGS:
490 rc = S_IRUGO;
491 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530492 }
493 return rc;
494}
495
Al Viro587a1f12011-07-23 23:11:19 -0400496static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530497{
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530498 umode_t rc = 0;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530499
500 switch (type) {
501 case ISCSI_BOOT_INI_INITIATOR_NAME:
502 rc = S_IRUGO;
503 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530504 }
505 return rc;
506}
507
508
Al Viro587a1f12011-07-23 23:11:19 -0400509static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530510{
Jitendra Bhivarec5bf8882016-08-19 15:20:05 +0530511 umode_t rc = 0;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530512
513 switch (type) {
514 case ISCSI_BOOT_ETH_FLAGS:
515 case ISCSI_BOOT_ETH_MAC:
516 case ISCSI_BOOT_ETH_INDEX:
517 rc = S_IRUGO;
518 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530519 }
520 return rc;
521}
522
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530523/*------------------- PCI Driver operations and data ----------------- */
Benoit Taine9baa3c32014-08-08 15:56:03 +0200524static const struct pci_device_id beiscsi_pci_id_table[] = {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530525 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530526 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530527 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
528 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
529 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530530 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530531 { 0 }
532};
533MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
534
John Soni Jose99bc5d52012-08-20 23:00:18 +0530535
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530536static struct scsi_host_template beiscsi_sht = {
537 .module = THIS_MODULE,
Ketan Mukadamc4f39bd2015-07-04 04:12:33 +0530538 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530539 .proc_name = DRV_NAME,
540 .queuecommand = iscsi_queuecommand,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100541 .change_queue_depth = scsi_change_queue_depth,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530542 .slave_configure = beiscsi_slave_configure,
543 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530544 .eh_abort_handler = beiscsi_eh_abort,
545 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530546 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530547 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530548 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
549 .can_queue = BE2_IO_DEPTH,
550 .this_id = -1,
551 .max_sectors = BEISCSI_MAX_SECTORS,
552 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
553 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500554 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100555 .track_queue_depth = 1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530556};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530557
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530558static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530559
560static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
561{
562 struct beiscsi_hba *phba;
563 struct Scsi_Host *shost;
564
565 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
566 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530567 dev_err(&pcidev->dev,
568 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530569 return NULL;
570 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530571 shost->max_id = BE2_MAX_SESSIONS;
572 shost->max_channel = 0;
573 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
574 shost->max_lun = BEISCSI_NUM_MAX_LUN;
575 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530576 phba = iscsi_host_priv(shost);
577 memset(phba, 0, sizeof(*phba));
578 phba->shost = shost;
579 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530580 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500581 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530582
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530583 return phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530584}
585
586static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
587{
588 if (phba->csr_va) {
589 iounmap(phba->csr_va);
590 phba->csr_va = NULL;
591 }
592 if (phba->db_va) {
593 iounmap(phba->db_va);
594 phba->db_va = NULL;
595 }
596 if (phba->pci_va) {
597 iounmap(phba->pci_va);
598 phba->pci_va = NULL;
599 }
600}
601
602static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
603 struct pci_dev *pcidev)
604{
605 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530606 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530607
608 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
609 pci_resource_len(pcidev, 2));
610 if (addr == NULL)
611 return -ENOMEM;
612 phba->ctrl.csr = addr;
613 phba->csr_va = addr;
614 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
615
616 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
617 if (addr == NULL)
618 goto pci_map_err;
619 phba->ctrl.db = addr;
620 phba->db_va = addr;
621 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
622
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530623 if (phba->generation == BE_GEN2)
624 pcicfg_reg = 1;
625 else
626 pcicfg_reg = 0;
627
628 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
629 pci_resource_len(pcidev, pcicfg_reg));
630
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530631 if (addr == NULL)
632 goto pci_map_err;
633 phba->ctrl.pcicfg = addr;
634 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530635 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530636 return 0;
637
638pci_map_err:
639 beiscsi_unmap_pci_function(phba);
640 return -ENOMEM;
641}
642
643static int beiscsi_enable_pci(struct pci_dev *pcidev)
644{
645 int ret;
646
647 ret = pci_enable_device(pcidev);
648 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530649 dev_err(&pcidev->dev,
650 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530651 return ret;
652 }
653
John Soni Josee307f3a2015-04-25 08:17:19 +0530654 ret = pci_request_regions(pcidev, DRV_NAME);
655 if (ret) {
656 dev_err(&pcidev->dev,
657 "beiscsi_enable_pci - request region failed\n");
658 goto pci_dev_disable;
659 }
660
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530661 pci_set_master(pcidev);
Jayamohan Kallickal6c57625b2014-01-29 02:16:45 -0500662 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(64));
663 if (ret) {
664 ret = pci_set_dma_mask(pcidev, DMA_BIT_MASK(32));
665 if (ret) {
666 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530667 goto pci_region_release;
Jayamohan Kallickal6c57625b2014-01-29 02:16:45 -0500668 } else {
669 ret = pci_set_consistent_dma_mask(pcidev,
670 DMA_BIT_MASK(32));
671 }
672 } else {
673 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530674 if (ret) {
675 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
John Soni Josee307f3a2015-04-25 08:17:19 +0530676 goto pci_region_release;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530677 }
678 }
679 return 0;
John Soni Josee307f3a2015-04-25 08:17:19 +0530680
681pci_region_release:
682 pci_release_regions(pcidev);
683pci_dev_disable:
684 pci_disable_device(pcidev);
685
686 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530687}
688
689static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
690{
691 struct be_ctrl_info *ctrl = &phba->ctrl;
692 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
693 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
694 int status = 0;
695
696 ctrl->pdev = pdev;
697 status = beiscsi_map_pci_bars(phba, pdev);
698 if (status)
699 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530700 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
701 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
702 mbox_mem_alloc->size,
703 &mbox_mem_alloc->dma);
704 if (!mbox_mem_alloc->va) {
705 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500706 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530707 }
708
709 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
710 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
711 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
712 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
Jitendra Bhivarec03a50f2016-01-20 14:10:46 +0530713 mutex_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530714 spin_lock_init(&phba->ctrl.mcc_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530715
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530716 return status;
717}
718
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700719/**
720 * beiscsi_get_params()- Set the config paramters
721 * @phba: ptr device priv structure
722 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530723static void beiscsi_get_params(struct beiscsi_hba *phba)
724{
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700725 uint32_t total_cid_count = 0;
726 uint32_t total_icd_count = 0;
727 uint8_t ulp_num = 0;
728
729 total_cid_count = BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP0) +
730 BEISCSI_GET_CID_COUNT(phba, BEISCSI_ULP1);
731
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700732 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
733 uint32_t align_mask = 0;
734 uint32_t icd_post_per_page = 0;
735 uint32_t icd_count_unavailable = 0;
736 uint32_t icd_start = 0, icd_count = 0;
737 uint32_t icd_start_align = 0, icd_count_align = 0;
738
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700739 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700740 icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
741 icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
742
743 /* Get ICD count that can be posted on each page */
744 icd_post_per_page = (PAGE_SIZE / (BE2_SGE *
745 sizeof(struct iscsi_sge)));
746 align_mask = (icd_post_per_page - 1);
747
748 /* Check if icd_start is aligned ICD per page posting */
749 if (icd_start % icd_post_per_page) {
750 icd_start_align = ((icd_start +
751 icd_post_per_page) &
752 ~(align_mask));
753 phba->fw_config.
754 iscsi_icd_start[ulp_num] =
755 icd_start_align;
756 }
757
758 icd_count_align = (icd_count & ~align_mask);
759
760 /* ICD discarded in the process of alignment */
761 if (icd_start_align)
762 icd_count_unavailable = ((icd_start_align -
763 icd_start) +
764 (icd_count -
765 icd_count_align));
766
767 /* Updated ICD count available */
768 phba->fw_config.iscsi_icd_count[ulp_num] = (icd_count -
769 icd_count_unavailable);
770
771 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
772 "BM_%d : Aligned ICD values\n"
773 "\t ICD Start : %d\n"
774 "\t ICD Count : %d\n"
775 "\t ICD Discarded : %d\n",
776 phba->fw_config.
777 iscsi_icd_start[ulp_num],
778 phba->fw_config.
779 iscsi_icd_count[ulp_num],
780 icd_count_unavailable);
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700781 break;
782 }
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700783 }
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700784
Jayamohan Kallickalcf987b72013-09-28 15:35:59 -0700785 total_icd_count = phba->fw_config.iscsi_icd_count[ulp_num];
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700786 phba->params.ios_per_ctrl = (total_icd_count -
787 (total_cid_count +
788 BE2_TMFS + BE2_NOPOUT_REQ));
789 phba->params.cxns_per_ctrl = total_cid_count;
790 phba->params.asyncpdus_per_ctrl = total_cid_count;
791 phba->params.icds_per_ctrl = total_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530792 phba->params.num_sge_per_io = BE2_SGE;
793 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
794 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
795 phba->params.eq_timer = 64;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -0700796 phba->params.num_eq_entries = 1024;
797 phba->params.num_cq_entries = 1024;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530798 phba->params.wrbs_per_cxn = 256;
799}
800
801static void hwi_ring_eq_db(struct beiscsi_hba *phba,
802 unsigned int id, unsigned int clr_interrupt,
803 unsigned int num_processed,
804 unsigned char rearm, unsigned char event)
805{
806 u32 val = 0;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500807
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530808 if (rearm)
809 val |= 1 << DB_EQ_REARM_SHIFT;
810 if (clr_interrupt)
811 val |= 1 << DB_EQ_CLR_SHIFT;
812 if (event)
813 val |= 1 << DB_EQ_EVNT_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500814
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530815 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -0500816 /* Setting lower order EQ_ID Bits */
817 val |= (id & DB_EQ_RING_ID_LOW_MASK);
818
819 /* Setting Higher order EQ_ID Bits */
820 val |= (((id >> DB_EQ_HIGH_FEILD_SHIFT) &
821 DB_EQ_RING_ID_HIGH_MASK)
822 << DB_EQ_HIGH_SET_SHIFT);
823
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530824 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
825}
826
827/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530828 * be_isr_mcc - The isr routine of the driver.
829 * @irq: Not used
830 * @dev_id: Pointer to host adapter structure
831 */
832static irqreturn_t be_isr_mcc(int irq, void *dev_id)
833{
834 struct beiscsi_hba *phba;
835 struct be_eq_entry *eqe = NULL;
836 struct be_queue_info *eq;
837 struct be_queue_info *mcc;
838 unsigned int num_eq_processed;
839 struct be_eq_obj *pbe_eq;
840 unsigned long flags;
841
842 pbe_eq = dev_id;
843 eq = &pbe_eq->q;
844 phba = pbe_eq->phba;
845 mcc = &phba->ctrl.mcc_obj.cq;
846 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530847
848 num_eq_processed = 0;
849
850 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
851 & EQE_VALID_MASK) {
852 if (((eqe->dw[offsetof(struct amap_eq_entry,
853 resource_id) / 32] &
854 EQE_RESID_MASK) >> 16) == mcc->id) {
855 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530856 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530857 spin_unlock_irqrestore(&phba->isr_lock, flags);
858 }
859 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
860 queue_tail_inc(eq);
861 eqe = queue_tail_node(eq);
862 num_eq_processed++;
863 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530864 if (pbe_eq->todo_mcc_cq)
865 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 if (num_eq_processed)
867 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
868
869 return IRQ_HANDLED;
870}
871
872/**
873 * be_isr_msix - The isr routine of the driver.
874 * @irq: Not used
875 * @dev_id: Pointer to host adapter structure
876 */
877static irqreturn_t be_isr_msix(int irq, void *dev_id)
878{
879 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530880 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530881 struct be_eq_obj *pbe_eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530882
883 pbe_eq = dev_id;
884 eq = &pbe_eq->q;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530885
886 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530887
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530888 /* disable interrupt till iopoll completes */
889 hwi_ring_eq_db(phba, eq->id, 1, 0, 0, 1);
890 irq_poll_sched(&pbe_eq->iopoll);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530891
892 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530893}
894
895/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530896 * be_isr - The isr routine of the driver.
897 * @irq: Not used
898 * @dev_id: Pointer to host adapter structure
899 */
900static irqreturn_t be_isr(int irq, void *dev_id)
901{
902 struct beiscsi_hba *phba;
903 struct hwi_controller *phwi_ctrlr;
904 struct hwi_context_memory *phwi_context;
905 struct be_eq_entry *eqe = NULL;
906 struct be_queue_info *eq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530907 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530908 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530909 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530910 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530911 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530912 int isr;
913
914 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700915 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530916 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
917 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
918 if (!isr)
919 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530920
921 phwi_ctrlr = phba->phwi_ctrlr;
922 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530923 pbe_eq = &phwi_context->be_eq[0];
924
925 eq = &phwi_context->be_eq[0].q;
926 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530927 index = 0;
928 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530929
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530930 num_ioeq_processed = 0;
931 num_mcceq_processed = 0;
Jens Axboe89f8b332014-03-13 09:38:42 -0600932 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
933 & EQE_VALID_MASK) {
934 if (((eqe->dw[offsetof(struct amap_eq_entry,
935 resource_id) / 32] &
936 EQE_RESID_MASK) >> 16) == mcc->id) {
937 spin_lock_irqsave(&phba->isr_lock, flags);
938 pbe_eq->todo_mcc_cq = true;
939 spin_unlock_irqrestore(&phba->isr_lock, flags);
940 num_mcceq_processed++;
941 } else {
Christoph Hellwigea511902015-12-07 06:41:11 -0800942 irq_poll_sched(&pbe_eq->iopoll);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530943 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530944 }
Jens Axboe89f8b332014-03-13 09:38:42 -0600945 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
946 queue_tail_inc(eq);
947 eqe = queue_tail_node(eq);
948 }
949 if (num_ioeq_processed || num_mcceq_processed) {
950 if (pbe_eq->todo_mcc_cq)
John Soni Jose72fb46a2012-10-20 04:42:49 +0530951 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530952
Jens Axboe89f8b332014-03-13 09:38:42 -0600953 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530954 hwi_ring_eq_db(phba, eq->id, 0,
Jens Axboe89f8b332014-03-13 09:38:42 -0600955 (num_ioeq_processed +
956 num_mcceq_processed) , 1, 1);
957 else
958 hwi_ring_eq_db(phba, eq->id, 0,
959 (num_ioeq_processed +
960 num_mcceq_processed), 0, 1);
961
962 return IRQ_HANDLED;
963 } else
964 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530965}
966
Jitendra Bhivare1094cf62016-01-20 14:10:56 +0530967
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530968static int beiscsi_init_irqs(struct beiscsi_hba *phba)
969{
970 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530971 struct hwi_controller *phwi_ctrlr;
972 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530973 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530974
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530975 phwi_ctrlr = phba->phwi_ctrlr;
976 phwi_context = phwi_ctrlr->phwi_ctxt;
977
978 if (phba->msix_enabled) {
979 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700980 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
981 GFP_KERNEL);
982 if (!phba->msi_name[i]) {
983 ret = -ENOMEM;
984 goto free_msix_irqs;
985 }
986
987 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
988 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530989 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700990 ret = request_irq(msix_vec, be_isr_msix, 0,
991 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530992 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530993 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530994 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
995 "BM_%d : beiscsi_init_irqs-Failed to"
996 "register msix for i = %d\n",
997 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700998 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530999 goto free_msix_irqs;
1000 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301001 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001002 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
1003 if (!phba->msi_name[i]) {
1004 ret = -ENOMEM;
1005 goto free_msix_irqs;
1006 }
1007 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
1008 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301009 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001010 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301011 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301012 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301013 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
1014 "BM_%d : beiscsi_init_irqs-"
1015 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001016 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301017 goto free_msix_irqs;
1018 }
1019
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301020 } else {
1021 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1022 "beiscsi", phba);
1023 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301024 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1025 "BM_%d : beiscsi_init_irqs-"
1026 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301027 return ret;
1028 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301029 }
1030 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301031free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001032 for (j = i - 1; j >= 0; j--) {
1033 kfree(phba->msi_name[j]);
1034 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301035 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001036 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301037 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301038}
1039
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -05001040void hwi_ring_cq_db(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301041 unsigned int id, unsigned int num_processed,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05301042 unsigned char rearm)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301043{
1044 u32 val = 0;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -05001045
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301046 if (rearm)
1047 val |= 1 << DB_CQ_REARM_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -05001048
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301049 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
Jayamohan Kallickale08b3c82014-01-29 02:16:42 -05001050
1051 /* Setting lower order CQ_ID Bits */
1052 val |= (id & DB_CQ_RING_ID_LOW_MASK);
1053
1054 /* Setting Higher order CQ_ID Bits */
1055 val |= (((id >> DB_CQ_HIGH_FEILD_SHIFT) &
1056 DB_CQ_RING_ID_HIGH_MASK)
1057 << DB_CQ_HIGH_SET_SHIFT);
1058
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301059 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1060}
1061
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301062static unsigned int
1063beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1064 struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301065 struct pdu_base *ppdu,
1066 unsigned long pdu_len,
1067 void *pbuffer, unsigned long buf_len)
1068{
1069 struct iscsi_conn *conn = beiscsi_conn->conn;
1070 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301071 struct iscsi_task *task;
1072 struct beiscsi_io_task *io_task;
1073 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301074
1075 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1076 PDUBASE_OPCODE_MASK) {
1077 case ISCSI_OP_NOOP_IN:
1078 pbuffer = NULL;
1079 buf_len = 0;
1080 break;
1081 case ISCSI_OP_ASYNC_EVENT:
1082 break;
1083 case ISCSI_OP_REJECT:
1084 WARN_ON(!pbuffer);
1085 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301086 beiscsi_log(phba, KERN_ERR,
1087 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1088 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301089 break;
1090 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301091 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301092 task = conn->login_task;
1093 io_task = task->dd_data;
1094 login_hdr = (struct iscsi_hdr *)ppdu;
1095 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301096 break;
1097 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301098 beiscsi_log(phba, KERN_WARNING,
1099 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1100 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1101 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301102 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301103 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301104 return 1;
1105 }
1106
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001107 spin_lock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301108 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001109 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301110 return 0;
1111}
1112
1113static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1114{
1115 struct sgl_handle *psgl_handle;
1116
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301117 spin_lock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301118 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301119 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1120 "BM_%d : In alloc_io_sgl_handle,"
1121 " io_sgl_alloc_index=%d\n",
1122 phba->io_sgl_alloc_index);
1123
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301124 psgl_handle = phba->io_sgl_hndl_base[phba->
1125 io_sgl_alloc_index];
1126 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1127 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301128 if (phba->io_sgl_alloc_index == (phba->params.
1129 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301130 phba->io_sgl_alloc_index = 0;
1131 else
1132 phba->io_sgl_alloc_index++;
1133 } else
1134 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301135 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301136 return psgl_handle;
1137}
1138
1139static void
1140free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1141{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301142 spin_lock_bh(&phba->io_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301143 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1144 "BM_%d : In free_,io_sgl_free_index=%d\n",
1145 phba->io_sgl_free_index);
1146
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301147 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1148 /*
1149 * this can happen if clean_task is called on a task that
1150 * failed in xmit_task or alloc_pdu.
1151 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301152 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1153 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1154 "value there=%p\n", phba->io_sgl_free_index,
1155 phba->io_sgl_hndl_base
1156 [phba->io_sgl_free_index]);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301157 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301158 return;
1159 }
1160 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1161 phba->io_sgl_hndl_avbl++;
1162 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1163 phba->io_sgl_free_index = 0;
1164 else
1165 phba->io_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301166 spin_unlock_bh(&phba->io_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301167}
1168
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301169static inline struct wrb_handle *
1170beiscsi_get_wrb_handle(struct hwi_wrb_context *pwrb_context,
1171 unsigned int wrbs_per_cxn)
1172{
1173 struct wrb_handle *pwrb_handle;
1174
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301175 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301176 pwrb_handle = pwrb_context->pwrb_handle_base[pwrb_context->alloc_index];
1177 pwrb_context->wrb_handles_available--;
1178 if (pwrb_context->alloc_index == (wrbs_per_cxn - 1))
1179 pwrb_context->alloc_index = 0;
1180 else
1181 pwrb_context->alloc_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301182 spin_unlock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301183
1184 return pwrb_handle;
1185}
1186
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301187/**
1188 * alloc_wrb_handle - To allocate a wrb handle
1189 * @phba: The hba pointer
1190 * @cid: The cid to use for allocation
John Soni Jose340c99e2015-08-20 04:44:30 +05301191 * @pwrb_context: ptr to ptr to wrb context
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301192 *
1193 * This happens under session_lock until submission to chip
1194 */
John Soni Jose340c99e2015-08-20 04:44:30 +05301195struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid,
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301196 struct hwi_wrb_context **pcontext)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301197{
1198 struct hwi_wrb_context *pwrb_context;
1199 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001200 uint16_t cri_index = BE_GET_CRI_FROM_CID(cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301201
1202 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001203 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301204 /* return the context address */
1205 *pcontext = pwrb_context;
1206 return beiscsi_get_wrb_handle(pwrb_context, phba->params.wrbs_per_cxn);
1207}
John Soni Jose340c99e2015-08-20 04:44:30 +05301208
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301209static inline void
1210beiscsi_put_wrb_handle(struct hwi_wrb_context *pwrb_context,
1211 struct wrb_handle *pwrb_handle,
1212 unsigned int wrbs_per_cxn)
1213{
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301214 spin_lock_bh(&pwrb_context->wrb_lock);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301215 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
1216 pwrb_context->wrb_handles_available++;
1217 if (pwrb_context->free_index == (wrbs_per_cxn - 1))
1218 pwrb_context->free_index = 0;
1219 else
1220 pwrb_context->free_index++;
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05301221 spin_unlock_bh(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301222}
1223
1224/**
1225 * free_wrb_handle - To free the wrb handle back to pool
1226 * @phba: The hba pointer
1227 * @pwrb_context: The context to free from
1228 * @pwrb_handle: The wrb_handle to free
1229 *
1230 * This happens under session_lock until submission to chip
1231 */
1232static void
1233free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1234 struct wrb_handle *pwrb_handle)
1235{
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05301236 beiscsi_put_wrb_handle(pwrb_context,
1237 pwrb_handle,
1238 phba->params.wrbs_per_cxn);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301239 beiscsi_log(phba, KERN_INFO,
1240 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1241 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1242 "wrb_handles_available=%d\n",
1243 pwrb_handle, pwrb_context->free_index,
1244 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301245}
1246
1247static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1248{
1249 struct sgl_handle *psgl_handle;
1250
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301251 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301252 if (phba->eh_sgl_hndl_avbl) {
1253 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1254 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301255 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1256 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1257 phba->eh_sgl_alloc_index,
1258 phba->eh_sgl_alloc_index);
1259
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301260 phba->eh_sgl_hndl_avbl--;
1261 if (phba->eh_sgl_alloc_index ==
1262 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1263 1))
1264 phba->eh_sgl_alloc_index = 0;
1265 else
1266 phba->eh_sgl_alloc_index++;
1267 } else
1268 psgl_handle = NULL;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301269 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301270 return psgl_handle;
1271}
1272
1273void
1274free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1275{
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301276 spin_lock_bh(&phba->mgmt_sgl_lock);
John Soni Jose99bc5d52012-08-20 23:00:18 +05301277 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1278 "BM_%d : In free_mgmt_sgl_handle,"
1279 "eh_sgl_free_index=%d\n",
1280 phba->eh_sgl_free_index);
1281
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301282 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1283 /*
1284 * this can happen if clean_task is called on a task that
1285 * failed in xmit_task or alloc_pdu.
1286 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301287 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1288 "BM_%d : Double Free in eh SGL ,"
1289 "eh_sgl_free_index=%d\n",
1290 phba->eh_sgl_free_index);
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301291 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301292 return;
1293 }
1294 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1295 phba->eh_sgl_hndl_avbl++;
1296 if (phba->eh_sgl_free_index ==
1297 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1298 phba->eh_sgl_free_index = 0;
1299 else
1300 phba->eh_sgl_free_index++;
Jitendra Bhivare10139fe2016-02-04 15:49:19 +05301301 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301302}
1303
1304static void
1305be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301306 struct iscsi_task *task,
1307 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301308{
1309 struct beiscsi_io_task *io_task = task->dd_data;
1310 struct be_status_bhs *sts_bhs =
1311 (struct be_status_bhs *)io_task->cmd_bhs;
1312 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301313 unsigned char *sense;
1314 u32 resid = 0, exp_cmdsn, max_cmdsn;
1315 u8 rsp, status, flags;
1316
John Soni Jose73133262012-10-20 04:44:49 +05301317 exp_cmdsn = csol_cqe->exp_cmdsn;
1318 max_cmdsn = (csol_cqe->exp_cmdsn +
1319 csol_cqe->cmd_wnd - 1);
1320 rsp = csol_cqe->i_resp;
1321 status = csol_cqe->i_sts;
1322 flags = csol_cqe->i_flags;
1323 resid = csol_cqe->res_cnt;
1324
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001325 if (!task->sc) {
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001326 if (io_task->scsi_cmnd) {
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001327 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickalda334972014-01-29 02:16:44 -05001328 io_task->scsi_cmnd = NULL;
1329 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301330
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001331 return;
1332 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 task->sc->result = (DID_OK << 16) | status;
1334 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1335 task->sc->result = DID_ERROR << 16;
1336 goto unmap;
1337 }
1338
1339 /* bidi not initially supported */
1340 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301341 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1342 task->sc->result = DID_ERROR << 16;
1343
1344 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1345 scsi_set_resid(task->sc, resid);
1346 if (!status && (scsi_bufflen(task->sc) - resid <
1347 task->sc->underflow))
1348 task->sc->result = DID_ERROR << 16;
1349 }
1350 }
1351
1352 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001353 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301354 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001355
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301356 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001357 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301358 memcpy(task->sc->sense_buffer, sense,
1359 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1360 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301361
John Soni Jose73133262012-10-20 04:44:49 +05301362 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1363 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301364unmap:
John Soni Joseeb1c4692015-04-25 08:17:45 +05301365 if (io_task->scsi_cmnd) {
1366 scsi_dma_unmap(io_task->scsi_cmnd);
1367 io_task->scsi_cmnd = NULL;
1368 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301369 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1370}
1371
1372static void
1373be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301374 struct iscsi_task *task,
1375 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301376{
1377 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301378 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301379 struct iscsi_conn *conn = beiscsi_conn->conn;
1380
1381 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301382 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301383 hdr->t2wait = 5;
1384 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301385 hdr->flags = csol_cqe->i_flags;
1386 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001387 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1388 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1389 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301390
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301391 hdr->dlength[0] = 0;
1392 hdr->dlength[1] = 0;
1393 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301395 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301396 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1397}
1398
1399static void
1400be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301401 struct iscsi_task *task,
1402 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301403{
1404 struct iscsi_tm_rsp *hdr;
1405 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301406 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301407
1408 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301409 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301410 hdr->flags = csol_cqe->i_flags;
1411 hdr->response = csol_cqe->i_resp;
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001412 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1413 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1414 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301415
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301416 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301417 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1418}
1419
1420static void
1421hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1422 struct beiscsi_hba *phba, struct sol_cqe *psol)
1423{
1424 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301425 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301426 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301427 struct iscsi_task *task;
1428 struct beiscsi_io_task *io_task;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001429 uint16_t wrb_index, cid, cri_index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301430
1431 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001432 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301433 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1434 wrb_idx, psol);
1435 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1436 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001437 } else {
1438 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1439 wrb_idx, psol);
1440 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1441 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301442 }
1443
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001444 cri_index = BE_GET_CRI_FROM_CID(cid);
1445 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301446 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301447 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301448
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301449 io_task = task->dd_data;
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07001450 memset(io_task->pwrb_handle->pwrb, 0, sizeof(struct iscsi_wrb));
1451 iscsi_put_task(task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301452}
1453
1454static void
1455be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301456 struct iscsi_task *task,
1457 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301458{
1459 struct iscsi_nopin *hdr;
1460 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301461 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301462
1463 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301464 hdr->flags = csol_cqe->i_flags;
1465 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001466 hdr->max_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn +
1467 csol_cqe->cmd_wnd - 1);
John Soni Jose73133262012-10-20 04:44:49 +05301468
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301469 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301470 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301471 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1472}
1473
John Soni Jose73133262012-10-20 04:44:49 +05301474static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1475 struct sol_cqe *psol,
1476 struct common_sol_cqe *csol_cqe)
1477{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001478 if (is_chip_be2_be3r(phba)) {
1479 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1480 i_exp_cmd_sn, psol);
1481 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1482 i_res_cnt, psol);
1483 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1484 i_cmd_wnd, psol);
1485 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1486 wrb_index, psol);
1487 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1488 cid, psol);
1489 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1490 hw_sts, psol);
1491 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1492 i_resp, psol);
1493 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1494 i_sts, psol);
1495 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1496 i_flags, psol);
1497 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301498 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1499 i_exp_cmd_sn, psol);
1500 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1501 i_res_cnt, psol);
1502 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1503 wrb_index, psol);
1504 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1505 cid, psol);
1506 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1507 hw_sts, psol);
Jayamohan Kallickal702dc5e2013-04-05 20:38:37 -07001508 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe_v2,
John Soni Jose73133262012-10-20 04:44:49 +05301509 i_cmd_wnd, psol);
1510 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1511 cmd_cmpl, psol))
1512 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1513 i_sts, psol);
1514 else
1515 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1516 i_sts, psol);
1517 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1518 u, psol))
1519 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1520
1521 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1522 o, psol))
1523 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301524 }
1525}
1526
1527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301528static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1529 struct beiscsi_hba *phba, struct sol_cqe *psol)
1530{
1531 struct hwi_wrb_context *pwrb_context;
1532 struct wrb_handle *pwrb_handle;
1533 struct iscsi_wrb *pwrb = NULL;
1534 struct hwi_controller *phwi_ctrlr;
1535 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301536 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301537 struct iscsi_conn *conn = beiscsi_conn->conn;
1538 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301539 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001540 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301541
1542 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301543
1544 /* Copy the elements to a common structure */
1545 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1546
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07001547 cri_index = BE_GET_CRI_FROM_CID(csol_cqe.cid);
1548 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
John Soni Jose73133262012-10-20 04:44:49 +05301549
1550 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1551 csol_cqe.wrb_index];
1552
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301553 task = pwrb_handle->pio_handle;
1554 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301555 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301556
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001557 spin_lock_bh(&session->back_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301558 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301559 case HWH_TYPE_IO:
1560 case HWH_TYPE_IO_RD:
1561 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301562 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301563 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301564 else
John Soni Jose73133262012-10-20 04:44:49 +05301565 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301566 break;
1567
1568 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301569 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301570 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301571 else
John Soni Jose73133262012-10-20 04:44:49 +05301572 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301573 break;
1574
1575 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301576 beiscsi_log(phba, KERN_ERR,
1577 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1578 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1579 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301580 break;
1581
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301582 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301583 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301584 break;
1585
1586 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301587 beiscsi_log(phba, KERN_WARNING,
1588 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1589 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1590 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301591 csol_cqe.wrb_index,
1592 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301593 break;
1594 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301595
Shlomo Pongratz659743b2014-02-07 00:41:38 -06001596 spin_unlock_bh(&session->back_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301597}
1598
1599static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1600 *pasync_ctx, unsigned int is_header,
1601 unsigned int host_write_ptr)
1602{
1603 if (is_header)
1604 return &pasync_ctx->async_entry[host_write_ptr].
1605 header_busy_list;
1606 else
1607 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1608}
1609
1610static struct async_pdu_handle *
1611hwi_get_async_handle(struct beiscsi_hba *phba,
1612 struct beiscsi_conn *beiscsi_conn,
1613 struct hwi_async_pdu_context *pasync_ctx,
1614 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1615{
1616 struct be_bus_address phys_addr;
1617 struct list_head *pbusy_list;
1618 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301619 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301620 unsigned int index, dpl;
1621
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001622 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301623 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1624 dpl, pdpdu_cqe);
1625 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1626 index, pdpdu_cqe);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001627 } else {
1628 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1629 dpl, pdpdu_cqe);
1630 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1631 index, pdpdu_cqe);
John Soni Jose73133262012-10-20 04:44:49 +05301632 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301633
1634 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301635 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1636 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301637 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301638 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1639 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301640
1641 phys_addr.u.a64.address =
1642 *((unsigned long long *)(&phys_addr.u.a64.address));
1643
1644 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1645 & PDUCQE_CODE_MASK) {
1646 case UNSOL_HDR_NOTIFY:
1647 is_header = 1;
1648
John Soni Jose73133262012-10-20 04:44:49 +05301649 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1650 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301651 break;
1652 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301653 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1654 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301655 break;
1656 default:
1657 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301658 beiscsi_log(phba, KERN_WARNING,
1659 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1660 "BM_%d : Unexpected code=%d\n",
1661 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1662 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301663 return NULL;
1664 }
1665
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301666 WARN_ON(list_empty(pbusy_list));
1667 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001668 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301669 break;
1670 }
1671
1672 WARN_ON(!pasync_handle);
1673
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001674 pasync_handle->cri = BE_GET_ASYNC_CRI_FROM_CID(
1675 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301676 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301677 pasync_handle->buffer_len = dpl;
1678 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301679
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301680 return pasync_handle;
1681}
1682
1683static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301684hwi_update_async_writables(struct beiscsi_hba *phba,
1685 struct hwi_async_pdu_context *pasync_ctx,
1686 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301687{
1688 struct list_head *pbusy_list;
1689 struct async_pdu_handle *pasync_handle;
1690 unsigned int num_entries, writables = 0;
1691 unsigned int *pep_read_ptr, *pwritables;
1692
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001693 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301694 if (is_header) {
1695 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1696 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301697 } else {
1698 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1699 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301700 }
1701
1702 while ((*pep_read_ptr) != cq_index) {
1703 (*pep_read_ptr)++;
1704 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1705
1706 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1707 *pep_read_ptr);
1708 if (writables == 0)
1709 WARN_ON(list_empty(pbusy_list));
1710
1711 if (!list_empty(pbusy_list)) {
1712 pasync_handle = list_entry(pbusy_list->next,
1713 struct async_pdu_handle,
1714 link);
1715 WARN_ON(!pasync_handle);
1716 pasync_handle->consumed = 1;
1717 }
1718
1719 writables++;
1720 }
1721
1722 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301723 beiscsi_log(phba, KERN_ERR,
1724 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1725 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1726 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301727 WARN_ON(1);
1728 }
1729
1730 *pwritables = *pwritables + writables;
1731 return 0;
1732}
1733
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001734static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001735 struct hwi_async_pdu_context *pasync_ctx,
1736 unsigned int cri)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301737{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301738 struct async_pdu_handle *pasync_handle, *tmp_handle;
1739 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301741 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301742 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1743 list_del(&pasync_handle->link);
1744
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001745 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301746 list_add_tail(&pasync_handle->link,
1747 &pasync_ctx->async_header.free_list);
1748 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301749 } else {
1750 list_add_tail(&pasync_handle->link,
1751 &pasync_ctx->async_data.free_list);
1752 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301753 }
1754 }
1755
1756 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1757 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1758 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301759}
1760
1761static struct phys_addr *
1762hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1763 unsigned int is_header, unsigned int host_write_ptr)
1764{
1765 struct phys_addr *pasync_sge = NULL;
1766
1767 if (is_header)
1768 pasync_sge = pasync_ctx->async_header.ring_base;
1769 else
1770 pasync_sge = pasync_ctx->async_data.ring_base;
1771
1772 return pasync_sge + host_write_ptr;
1773}
1774
1775static void hwi_post_async_buffers(struct beiscsi_hba *phba,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001776 unsigned int is_header, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301777{
1778 struct hwi_controller *phwi_ctrlr;
1779 struct hwi_async_pdu_context *pasync_ctx;
1780 struct async_pdu_handle *pasync_handle;
1781 struct list_head *pfree_link, *pbusy_list;
1782 struct phys_addr *pasync_sge;
1783 unsigned int ring_id, num_entries;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001784 unsigned int host_write_num, doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301785 unsigned int writables;
1786 unsigned int i = 0;
1787 u32 doorbell = 0;
1788
1789 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001790 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr, ulp_num);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001791 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301792
1793 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301794 writables = min(pasync_ctx->async_header.writables,
1795 pasync_ctx->async_header.free_entries);
1796 pfree_link = pasync_ctx->async_header.free_list.next;
1797 host_write_num = pasync_ctx->async_header.host_write_ptr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001798 ring_id = phwi_ctrlr->default_pdu_hdr[ulp_num].id;
1799 doorbell_offset = phwi_ctrlr->default_pdu_hdr[ulp_num].
1800 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301801 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301802 writables = min(pasync_ctx->async_data.writables,
1803 pasync_ctx->async_data.free_entries);
1804 pfree_link = pasync_ctx->async_data.free_list.next;
1805 host_write_num = pasync_ctx->async_data.host_write_ptr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001806 ring_id = phwi_ctrlr->default_pdu_data[ulp_num].id;
1807 doorbell_offset = phwi_ctrlr->default_pdu_data[ulp_num].
1808 doorbell_offset;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301809 }
1810
1811 writables = (writables / 8) * 8;
1812 if (writables) {
1813 for (i = 0; i < writables; i++) {
1814 pbusy_list =
1815 hwi_get_async_busy_list(pasync_ctx, is_header,
1816 host_write_num);
1817 pasync_handle =
1818 list_entry(pfree_link, struct async_pdu_handle,
1819 link);
1820 WARN_ON(!pasync_handle);
1821 pasync_handle->consumed = 0;
1822
1823 pfree_link = pfree_link->next;
1824
1825 pasync_sge = hwi_get_ring_address(pasync_ctx,
1826 is_header, host_write_num);
1827
1828 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1829 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1830
1831 list_move(&pasync_handle->link, pbusy_list);
1832
1833 host_write_num++;
1834 host_write_num = host_write_num % num_entries;
1835 }
1836
1837 if (is_header) {
1838 pasync_ctx->async_header.host_write_ptr =
1839 host_write_num;
1840 pasync_ctx->async_header.free_entries -= writables;
1841 pasync_ctx->async_header.writables -= writables;
1842 pasync_ctx->async_header.busy_entries += writables;
1843 } else {
1844 pasync_ctx->async_data.host_write_ptr = host_write_num;
1845 pasync_ctx->async_data.free_entries -= writables;
1846 pasync_ctx->async_data.writables -= writables;
1847 pasync_ctx->async_data.busy_entries += writables;
1848 }
1849
1850 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1851 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1852 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1853 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1854 << DB_DEF_PDU_CQPROC_SHIFT;
1855
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001856 iowrite32(doorbell, phba->db_va + doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301857 }
1858}
1859
1860static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1861 struct beiscsi_conn *beiscsi_conn,
1862 struct i_t_dpdu_cqe *pdpdu_cqe)
1863{
1864 struct hwi_controller *phwi_ctrlr;
1865 struct hwi_async_pdu_context *pasync_ctx;
1866 struct async_pdu_handle *pasync_handle = NULL;
1867 unsigned int cq_index = -1;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001868 uint16_t cri_index = BE_GET_CRI_FROM_CID(
1869 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301870
1871 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001872 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1873 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1874 cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301875
1876 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1877 pdpdu_cqe, &cq_index);
1878 BUG_ON(pasync_handle->is_header != 0);
1879 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301880 hwi_update_async_writables(phba, pasync_ctx,
1881 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301882
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001883 hwi_free_async_msg(phba, pasync_ctx, pasync_handle->cri);
1884 hwi_post_async_buffers(phba, pasync_handle->is_header,
1885 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1886 cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301887}
1888
1889static unsigned int
1890hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1891 struct beiscsi_hba *phba,
1892 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1893{
1894 struct list_head *plist;
1895 struct async_pdu_handle *pasync_handle;
1896 void *phdr = NULL;
1897 unsigned int hdr_len = 0, buf_len = 0;
1898 unsigned int status, index = 0, offset = 0;
1899 void *pfirst_buffer = NULL;
1900 unsigned int num_buf = 0;
1901
1902 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1903
1904 list_for_each_entry(pasync_handle, plist, link) {
1905 if (index == 0) {
1906 phdr = pasync_handle->pbuffer;
1907 hdr_len = pasync_handle->buffer_len;
1908 } else {
1909 buf_len = pasync_handle->buffer_len;
1910 if (!num_buf) {
1911 pfirst_buffer = pasync_handle->pbuffer;
1912 num_buf++;
1913 }
1914 memcpy(pfirst_buffer + offset,
1915 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001916 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301917 }
1918 index++;
1919 }
1920
1921 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301922 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001923 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301924
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001925 hwi_free_async_msg(phba, pasync_ctx, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301926 return 0;
1927}
1928
1929static unsigned int
1930hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1931 struct beiscsi_hba *phba,
1932 struct async_pdu_handle *pasync_handle)
1933{
1934 struct hwi_async_pdu_context *pasync_ctx;
1935 struct hwi_controller *phwi_ctrlr;
1936 unsigned int bytes_needed = 0, status = 0;
1937 unsigned short cri = pasync_handle->cri;
1938 struct pdu_base *ppdu;
1939
1940 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001941 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
1942 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
1943 BE_GET_CRI_FROM_CID(beiscsi_conn->
1944 beiscsi_conn_cid)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301945
1946 list_del(&pasync_handle->link);
1947 if (pasync_handle->is_header) {
1948 pasync_ctx->async_header.busy_entries--;
1949 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07001950 hwi_free_async_msg(phba, pasync_ctx, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301951 BUG();
1952 }
1953
1954 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1955 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1956 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1957 (unsigned short)pasync_handle->buffer_len;
1958 list_add_tail(&pasync_handle->link,
1959 &pasync_ctx->async_entry[cri].wait_queue.list);
1960
1961 ppdu = pasync_handle->pbuffer;
1962 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1963 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1964 0xFFFF0000) | ((be16_to_cpu((ppdu->
1965 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1966 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1967
1968 if (status == 0) {
1969 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1970 bytes_needed;
1971
1972 if (bytes_needed == 0)
1973 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1974 pasync_ctx, cri);
1975 }
1976 } else {
1977 pasync_ctx->async_data.busy_entries--;
1978 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1979 list_add_tail(&pasync_handle->link,
1980 &pasync_ctx->async_entry[cri].wait_queue.
1981 list);
1982 pasync_ctx->async_entry[cri].wait_queue.
1983 bytes_received +=
1984 (unsigned short)pasync_handle->buffer_len;
1985
1986 if (pasync_ctx->async_entry[cri].wait_queue.
1987 bytes_received >=
1988 pasync_ctx->async_entry[cri].wait_queue.
1989 bytes_needed)
1990 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1991 pasync_ctx, cri);
1992 }
1993 }
1994 return status;
1995}
1996
1997static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1998 struct beiscsi_hba *phba,
1999 struct i_t_dpdu_cqe *pdpdu_cqe)
2000{
2001 struct hwi_controller *phwi_ctrlr;
2002 struct hwi_async_pdu_context *pasync_ctx;
2003 struct async_pdu_handle *pasync_handle = NULL;
2004 unsigned int cq_index = -1;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002005 uint16_t cri_index = BE_GET_CRI_FROM_CID(
2006 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302007
2008 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002009 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr,
2010 BEISCSI_GET_ULP_FROM_CRI(phwi_ctrlr,
2011 cri_index));
2012
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302013 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
2014 pdpdu_cqe, &cq_index);
2015
2016 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302017 hwi_update_async_writables(phba, pasync_ctx,
2018 pasync_handle->is_header, cq_index);
2019
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302020 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002021 hwi_post_async_buffers(phba, pasync_handle->is_header,
2022 BEISCSI_GET_ULP_FROM_CRI(
2023 phwi_ctrlr, cri_index));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024}
2025
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05302026void beiscsi_process_mcc_cq(struct beiscsi_hba *phba)
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302027{
2028 struct be_queue_info *mcc_cq;
2029 struct be_mcc_compl *mcc_compl;
2030 unsigned int num_processed = 0;
2031
2032 mcc_cq = &phba->ctrl.mcc_obj.cq;
2033 mcc_compl = queue_tail_node(mcc_cq);
2034 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
2035 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302036 if (num_processed >= 32) {
2037 hwi_ring_cq_db(phba, mcc_cq->id,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302038 num_processed, 0);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302039 num_processed = 0;
2040 }
2041 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05302042 beiscsi_process_async_event(phba, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302043 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05302044 beiscsi_process_mcc_compl(&phba->ctrl, mcc_compl);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302045 }
2046
2047 mcc_compl->flags = 0;
2048 queue_tail_inc(mcc_cq);
2049 mcc_compl = queue_tail_node(mcc_cq);
2050 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
2051 num_processed++;
2052 }
2053
2054 if (num_processed > 0)
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302055 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302056}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302057
John Soni Jose6763daa2012-10-20 04:41:45 +05302058/**
2059 * beiscsi_process_cq()- Process the Completion Queue
2060 * @pbe_eq: Event Q on which the Completion has come
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302061 * @budget: Max number of events to processed
John Soni Jose6763daa2012-10-20 04:41:45 +05302062 *
2063 * return
2064 * Number of Completion Entries processed.
2065 **/
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302066unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302067{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302068 struct be_queue_info *cq;
2069 struct sol_cqe *sol;
2070 struct dmsg_cqe *dmsg;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302071 unsigned int total = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302072 unsigned int num_processed = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302073 unsigned short code = 0, cid = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002074 uint16_t cri_index = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302075 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302076 struct beiscsi_endpoint *beiscsi_ep;
2077 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302078 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302079
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302080 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302081 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302082 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302083
2084 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2085 CQE_VALID_MASK) {
2086 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2087
John Soni Jose73133262012-10-20 04:44:49 +05302088 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2089 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302090
John Soni Jose73133262012-10-20 04:44:49 +05302091 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002092 if (is_chip_be2_be3r(phba)) {
2093 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2094 } else {
John Soni Jose73133262012-10-20 04:44:49 +05302095 if ((code == DRIVERMSG_NOTIFY) ||
2096 (code == UNSOL_HDR_NOTIFY) ||
2097 (code == UNSOL_DATA_NOTIFY))
2098 cid = AMAP_GET_BITS(
2099 struct amap_i_t_dpdu_cqe_v2,
2100 cid, sol);
2101 else
2102 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2103 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002104 }
John Soni Jose73133262012-10-20 04:44:49 +05302105
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002106 cri_index = BE_GET_CRI_FROM_CID(cid);
2107 ep = phba->ep_array[cri_index];
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002108
2109 if (ep == NULL) {
2110 /* connection has already been freed
2111 * just move on to next one
2112 */
2113 beiscsi_log(phba, KERN_WARNING,
2114 BEISCSI_LOG_INIT,
2115 "BM_%d : proc cqe of disconn ep: cid %d\n",
2116 cid);
2117 goto proc_next_cqe;
2118 }
2119
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302120 beiscsi_ep = ep->dd_data;
2121 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302122
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302123 /* replenish cq */
2124 if (num_processed == 32) {
2125 hwi_ring_cq_db(phba, cq->id, 32, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302126 num_processed = 0;
2127 }
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302128 total++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302129
John Soni Jose0a513dd2012-08-20 23:00:55 +05302130 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302131 case SOL_CMD_COMPLETE:
2132 hwi_complete_cmd(beiscsi_conn, phba, sol);
2133 break;
2134 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302135 beiscsi_log(phba, KERN_INFO,
2136 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302137 "BM_%d : Received %s[%d] on CID : %d\n",
2138 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302139
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302140 dmsg = (struct dmsg_cqe *)sol;
2141 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2142 break;
2143 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302144 beiscsi_log(phba, KERN_INFO,
2145 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302146 "BM_%d : Received %s[%d] on CID : %d\n",
2147 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302148
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002149 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302150 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2151 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002152 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302153 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302154 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302155 beiscsi_log(phba, KERN_INFO,
2156 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302157 "BM_%d : Received %s[%d] on CID : %d\n",
2158 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302159
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002160 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302161 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2162 (struct i_t_dpdu_cqe *)sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002163 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302164 break;
2165 case CXN_INVALIDATE_INDEX_NOTIFY:
2166 case CMD_INVALIDATED_NOTIFY:
2167 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302168 beiscsi_log(phba, KERN_ERR,
2169 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302170 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2171 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302172 break;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302173 case CXN_KILLED_HDR_DIGEST_ERR:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302174 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302175 beiscsi_log(phba, KERN_ERR,
2176 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2177 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2178 cqe_desc[code], code, cid);
2179 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302180 case CMD_KILLED_INVALID_STATSN_RCVD:
2181 case CMD_KILLED_INVALID_R2T_RCVD:
2182 case CMD_CXN_KILLED_LUN_INVALID:
2183 case CMD_CXN_KILLED_ICD_INVALID:
2184 case CMD_CXN_KILLED_ITT_INVALID:
2185 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2186 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302187 beiscsi_log(phba, KERN_ERR,
2188 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302189 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2190 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302191 break;
2192 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302193 beiscsi_log(phba, KERN_ERR,
2194 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302195 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2196 cqe_desc[code], code, cid);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002197 spin_lock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302198 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2199 (struct i_t_dpdu_cqe *) sol);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07002200 spin_unlock_bh(&phba->async_pdu_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302201 break;
2202 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2203 case CXN_KILLED_BURST_LEN_MISMATCH:
2204 case CXN_KILLED_AHS_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302205 case CXN_KILLED_UNKNOWN_HDR:
2206 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2207 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2208 case CXN_KILLED_TIMED_OUT:
2209 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302210 case CXN_KILLED_RST_SENT:
2211 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302212 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2213 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2214 case CXN_KILLED_OVER_RUN_RESIDUAL:
2215 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2216 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302217 beiscsi_log(phba, KERN_ERR,
2218 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302219 "BM_%d : Event %s[%d] received on CID : %d\n",
2220 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302221 if (beiscsi_conn)
2222 iscsi_conn_failure(beiscsi_conn->conn,
2223 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302224 break;
2225 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302226 beiscsi_log(phba, KERN_ERR,
2227 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302228 "BM_%d : Invalid CQE Event Received Code : %d"
2229 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302230 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302231 break;
2232 }
2233
Jayamohan Kallickalb7ab35b2014-08-08 01:00:01 -04002234proc_next_cqe:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302235 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2236 queue_tail_inc(cq);
2237 sol = queue_tail_node(cq);
2238 num_processed++;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302239 if (total == budget)
2240 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302241 }
2242
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302243 hwi_ring_cq_db(phba, cq->id, num_processed, 1);
2244 return total;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302245}
2246
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302247void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302248{
2249 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302250 struct hwi_controller *phwi_ctrlr;
2251 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302252 struct beiscsi_hba *phba;
2253 struct be_eq_obj *pbe_eq =
2254 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302255
John Soni Jose72fb46a2012-10-20 04:42:49 +05302256 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302257 phwi_ctrlr = phba->phwi_ctrlr;
2258 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302259
John Soni Jose72fb46a2012-10-20 04:42:49 +05302260 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302261 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302262 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302263 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jitendra Bhivare2e4e8f62016-02-04 15:49:11 +05302264 beiscsi_process_mcc_cq(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302265 }
2266
John Soni Jose72fb46a2012-10-20 04:42:49 +05302267 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302268 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302269 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302270 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302271 beiscsi_process_cq(pbe_eq, BE2_MAX_NUM_CQ_PROC);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302272 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302273
2274 /* rearm EQ for further interrupts */
2275 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302276}
2277
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002278static int be_iopoll(struct irq_poll *iop, int budget)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302279{
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302280 unsigned int ret, num_eq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302281 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302282 struct be_eq_obj *pbe_eq;
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302283 struct be_eq_entry *eqe = NULL;
2284 struct be_queue_info *eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302285
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302286 num_eq_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302287 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302288 phba = pbe_eq->phba;
2289 eq = &pbe_eq->q;
2290 eqe = queue_tail_node(eq);
2291
2292 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32] &
2293 EQE_VALID_MASK) {
2294 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
2295 queue_tail_inc(eq);
2296 eqe = queue_tail_node(eq);
2297 num_eq_processed++;
2298 }
2299
2300 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
2301
2302 ret = beiscsi_process_cq(pbe_eq, budget);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04002303 pbe_eq->cq_count += ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302304 if (ret < budget) {
Christoph Hellwig511cbce2015-11-10 14:56:14 +01002305 irq_poll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302306 beiscsi_log(phba, KERN_INFO,
2307 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
Jitendra Bhivare1094cf62016-01-20 14:10:56 +05302308 "BM_%d : rearm pbe_eq->q.id =%d ret %d\n",
2309 pbe_eq->q.id, ret);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302310 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302311 }
2312 return ret;
2313}
2314
2315static void
John Soni Jose09a10932012-10-20 04:44:23 +05302316hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2317 unsigned int num_sg, struct beiscsi_io_task *io_task)
2318{
2319 struct iscsi_sge *psgl;
2320 unsigned int sg_len, index;
2321 unsigned int sge_len = 0;
2322 unsigned long long addr;
2323 struct scatterlist *l_sg;
2324 unsigned int offset;
2325
2326 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2327 io_task->bhs_pa.u.a32.address_lo);
2328 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2329 io_task->bhs_pa.u.a32.address_hi);
2330
2331 l_sg = sg;
2332 for (index = 0; (index < num_sg) && (index < 2); index++,
2333 sg = sg_next(sg)) {
2334 if (index == 0) {
2335 sg_len = sg_dma_len(sg);
2336 addr = (u64) sg_dma_address(sg);
2337 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2338 sge0_addr_lo, pwrb,
2339 lower_32_bits(addr));
2340 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2341 sge0_addr_hi, pwrb,
2342 upper_32_bits(addr));
2343 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2344 sge0_len, pwrb,
2345 sg_len);
2346 sge_len = sg_len;
2347 } else {
2348 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2349 pwrb, sge_len);
2350 sg_len = sg_dma_len(sg);
2351 addr = (u64) sg_dma_address(sg);
2352 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2353 sge1_addr_lo, pwrb,
2354 lower_32_bits(addr));
2355 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2356 sge1_addr_hi, pwrb,
2357 upper_32_bits(addr));
2358 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2359 sge1_len, pwrb,
2360 sg_len);
2361 }
2362 }
2363 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2364 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2365
2366 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2367
2368 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2369 io_task->bhs_pa.u.a32.address_hi);
2370 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2371 io_task->bhs_pa.u.a32.address_lo);
2372
2373 if (num_sg == 1) {
2374 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2375 1);
2376 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2377 0);
2378 } else if (num_sg == 2) {
2379 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2380 0);
2381 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2382 1);
2383 } else {
2384 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2385 0);
2386 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2387 0);
2388 }
2389
2390 sg = l_sg;
2391 psgl++;
2392 psgl++;
2393 offset = 0;
2394 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2395 sg_len = sg_dma_len(sg);
2396 addr = (u64) sg_dma_address(sg);
2397 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2398 lower_32_bits(addr));
2399 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2400 upper_32_bits(addr));
2401 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2402 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2403 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2404 offset += sg_len;
2405 }
2406 psgl--;
2407 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2408}
2409
2410static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302411hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2412 unsigned int num_sg, struct beiscsi_io_task *io_task)
2413{
2414 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302415 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302416 unsigned int sge_len = 0;
2417 unsigned long long addr;
2418 struct scatterlist *l_sg;
2419 unsigned int offset;
2420
2421 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2422 io_task->bhs_pa.u.a32.address_lo);
2423 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2424 io_task->bhs_pa.u.a32.address_hi);
2425
2426 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302427 for (index = 0; (index < num_sg) && (index < 2); index++,
2428 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302429 if (index == 0) {
2430 sg_len = sg_dma_len(sg);
2431 addr = (u64) sg_dma_address(sg);
2432 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302433 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302434 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302435 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302436 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2437 sg_len);
2438 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302439 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302440 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2441 pwrb, sge_len);
2442 sg_len = sg_dma_len(sg);
2443 addr = (u64) sg_dma_address(sg);
2444 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302445 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302446 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302447 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302448 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2449 sg_len);
2450 }
2451 }
2452 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2453 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2454
2455 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2456
2457 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2458 io_task->bhs_pa.u.a32.address_hi);
2459 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2460 io_task->bhs_pa.u.a32.address_lo);
2461
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302462 if (num_sg == 1) {
2463 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2464 1);
2465 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2466 0);
2467 } else if (num_sg == 2) {
2468 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2469 0);
2470 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2471 1);
2472 } else {
2473 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2474 0);
2475 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2476 0);
2477 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302478 sg = l_sg;
2479 psgl++;
2480 psgl++;
2481 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302482 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302483 sg_len = sg_dma_len(sg);
2484 addr = (u64) sg_dma_address(sg);
2485 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2486 (addr & 0xFFFFFFFF));
2487 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2488 (addr >> 32));
2489 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2490 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2491 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2492 offset += sg_len;
2493 }
2494 psgl--;
2495 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2496}
2497
John Soni Josed629c472012-10-20 04:42:00 +05302498/**
2499 * hwi_write_buffer()- Populate the WRB with task info
2500 * @pwrb: ptr to the WRB entry
2501 * @task: iscsi task which is to be executed
2502 **/
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002503static int hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302504{
2505 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302506 struct beiscsi_io_task *io_task = task->dd_data;
2507 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2508 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302509 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302510
2511 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2512 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2513 io_task->bhs_pa.u.a32.address_lo);
2514 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2515 io_task->bhs_pa.u.a32.address_hi);
2516
2517 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302518
2519 /* Check for the data_count */
2520 dsp_value = (task->data_count) ? 1 : 0;
2521
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002522 if (is_chip_be2_be3r(phba))
2523 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302524 pwrb, dsp_value);
2525 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002526 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302527 pwrb, dsp_value);
2528
2529 /* Map addr only if there is data_count */
2530 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302531 io_task->mtask_addr = pci_map_single(phba->pcidev,
2532 task->data,
2533 task->data_count,
2534 PCI_DMA_TODEVICE);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002535 if (pci_dma_mapping_error(phba->pcidev,
2536 io_task->mtask_addr))
2537 return -ENOMEM;
John Soni Josed629c472012-10-20 04:42:00 +05302538 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302539 } else
John Soni Josed629c472012-10-20 04:42:00 +05302540 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302541
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302542 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302543 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302544 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302545 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302546 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2547 task->data_count);
2548
2549 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2550 } else {
2551 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302552 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302553 }
2554
2555 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2556
2557 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2558
2559 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2560 io_task->bhs_pa.u.a32.address_hi);
2561 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2562 io_task->bhs_pa.u.a32.address_lo);
2563 if (task->data) {
2564 psgl++;
2565 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2566 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2567 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2568 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2569 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2570 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2571
2572 psgl++;
2573 if (task->data) {
2574 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302575 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302576 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302577 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302578 }
2579 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2580 }
2581 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03002582 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302583}
2584
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002585/**
2586 * beiscsi_find_mem_req()- Find mem needed
2587 * @phba: ptr to HBA struct
2588 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302589static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2590{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002591 uint8_t mem_descr_index, ulp_num;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302592 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302593 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2594 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2595
2596 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2597 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302598
2599 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2600
2601 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2602 BE_ISCSI_PDU_HEADER_SIZE;
2603 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2604 sizeof(struct hwi_context_memory);
2605
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302606
2607 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2608 * (phba->params.wrbs_per_cxn)
2609 * phba->params.cxns_per_ctrl;
2610 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2611 (phba->params.wrbs_per_cxn);
2612 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2613 phba->params.cxns_per_ctrl);
2614
2615 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2616 phba->params.icds_per_ctrl;
2617 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2618 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002619 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2620 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302621
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002622 num_async_pdu_buf_sgl_pages =
2623 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2624 phba, ulp_num) *
2625 sizeof(struct phys_addr));
2626
2627 num_async_pdu_buf_pages =
2628 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2629 phba, ulp_num) *
2630 phba->params.defpdu_hdr_sz);
2631
2632 num_async_pdu_data_pages =
2633 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2634 phba, ulp_num) *
2635 phba->params.defpdu_data_sz);
2636
2637 num_async_pdu_data_sgl_pages =
2638 PAGES_REQUIRED(BEISCSI_GET_CID_COUNT(
2639 phba, ulp_num) *
2640 sizeof(struct phys_addr));
2641
Jayamohan Kallickala129d922013-09-28 15:35:46 -07002642 mem_descr_index = (HWI_MEM_TEMPLATE_HDR_ULP0 +
2643 (ulp_num * MEM_DESCR_OFFSET));
2644 phba->mem_req[mem_descr_index] =
2645 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2646 BEISCSI_TEMPLATE_HDR_PER_CXN_SIZE;
2647
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002648 mem_descr_index = (HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2649 (ulp_num * MEM_DESCR_OFFSET));
2650 phba->mem_req[mem_descr_index] =
2651 num_async_pdu_buf_pages *
2652 PAGE_SIZE;
2653
2654 mem_descr_index = (HWI_MEM_ASYNC_DATA_BUF_ULP0 +
2655 (ulp_num * MEM_DESCR_OFFSET));
2656 phba->mem_req[mem_descr_index] =
2657 num_async_pdu_data_pages *
2658 PAGE_SIZE;
2659
2660 mem_descr_index = (HWI_MEM_ASYNC_HEADER_RING_ULP0 +
2661 (ulp_num * MEM_DESCR_OFFSET));
2662 phba->mem_req[mem_descr_index] =
2663 num_async_pdu_buf_sgl_pages *
2664 PAGE_SIZE;
2665
2666 mem_descr_index = (HWI_MEM_ASYNC_DATA_RING_ULP0 +
2667 (ulp_num * MEM_DESCR_OFFSET));
2668 phba->mem_req[mem_descr_index] =
2669 num_async_pdu_data_sgl_pages *
2670 PAGE_SIZE;
2671
2672 mem_descr_index = (HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
2673 (ulp_num * MEM_DESCR_OFFSET));
2674 phba->mem_req[mem_descr_index] =
2675 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2676 sizeof(struct async_pdu_handle);
2677
2678 mem_descr_index = (HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
2679 (ulp_num * MEM_DESCR_OFFSET));
2680 phba->mem_req[mem_descr_index] =
2681 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2682 sizeof(struct async_pdu_handle);
2683
2684 mem_descr_index = (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2685 (ulp_num * MEM_DESCR_OFFSET));
2686 phba->mem_req[mem_descr_index] =
2687 sizeof(struct hwi_async_pdu_context) +
2688 (BEISCSI_GET_CID_COUNT(phba, ulp_num) *
2689 sizeof(struct hwi_async_entry));
2690 }
2691 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302692}
2693
2694static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2695{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302696 dma_addr_t bus_add;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002697 struct hwi_controller *phwi_ctrlr;
2698 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302699 struct mem_array *mem_arr, *mem_arr_orig;
2700 unsigned int i, j, alloc_size, curr_alloc_size;
2701
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002702 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302703 if (!phba->phwi_ctrlr)
2704 return -ENOMEM;
2705
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002706 /* Allocate memory for wrb_context */
2707 phwi_ctrlr = phba->phwi_ctrlr;
2708 phwi_ctrlr->wrb_context = kzalloc(sizeof(struct hwi_wrb_context) *
2709 phba->params.cxns_per_ctrl,
2710 GFP_KERNEL);
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002711 if (!phwi_ctrlr->wrb_context) {
2712 kfree(phba->phwi_ctrlr);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002713 return -ENOMEM;
Maurizio Lombardi0c887402015-10-01 10:56:25 +02002714 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002715
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2717 GFP_KERNEL);
2718 if (!phba->init_mem) {
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002719 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302720 kfree(phba->phwi_ctrlr);
2721 return -ENOMEM;
2722 }
2723
2724 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2725 GFP_KERNEL);
2726 if (!mem_arr_orig) {
2727 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002728 kfree(phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302729 kfree(phba->phwi_ctrlr);
2730 return -ENOMEM;
2731 }
2732
2733 mem_descr = phba->init_mem;
2734 for (i = 0; i < SE_MEM_MAX; i++) {
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002735 if (!phba->mem_req[i]) {
2736 mem_descr->mem_array = NULL;
2737 mem_descr++;
2738 continue;
2739 }
2740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302741 j = 0;
2742 mem_arr = mem_arr_orig;
2743 alloc_size = phba->mem_req[i];
2744 memset(mem_arr, 0, sizeof(struct mem_array) *
2745 BEISCSI_MAX_FRAGS_INIT);
2746 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2747 do {
2748 mem_arr->virtual_address = pci_alloc_consistent(
2749 phba->pcidev,
2750 curr_alloc_size,
2751 &bus_add);
2752 if (!mem_arr->virtual_address) {
2753 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2754 goto free_mem;
2755 if (curr_alloc_size -
2756 rounddown_pow_of_two(curr_alloc_size))
2757 curr_alloc_size = rounddown_pow_of_two
2758 (curr_alloc_size);
2759 else
2760 curr_alloc_size = curr_alloc_size / 2;
2761 } else {
2762 mem_arr->bus_address.u.
2763 a64.address = (__u64) bus_add;
2764 mem_arr->size = curr_alloc_size;
2765 alloc_size -= curr_alloc_size;
2766 curr_alloc_size = min(be_max_phys_size *
2767 1024, alloc_size);
2768 j++;
2769 mem_arr++;
2770 }
2771 } while (alloc_size);
2772 mem_descr->num_elements = j;
2773 mem_descr->size_in_bytes = phba->mem_req[i];
2774 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2775 GFP_KERNEL);
2776 if (!mem_descr->mem_array)
2777 goto free_mem;
2778
2779 memcpy(mem_descr->mem_array, mem_arr_orig,
2780 sizeof(struct mem_array) * j);
2781 mem_descr++;
2782 }
2783 kfree(mem_arr_orig);
2784 return 0;
2785free_mem:
2786 mem_descr->num_elements = j;
2787 while ((i) || (j)) {
2788 for (j = mem_descr->num_elements; j > 0; j--) {
2789 pci_free_consistent(phba->pcidev,
2790 mem_descr->mem_array[j - 1].size,
2791 mem_descr->mem_array[j - 1].
2792 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302793 (unsigned long)mem_descr->
2794 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302795 bus_address.u.a64.address);
2796 }
2797 if (i) {
2798 i--;
2799 kfree(mem_descr->mem_array);
2800 mem_descr--;
2801 }
2802 }
2803 kfree(mem_arr_orig);
2804 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002805 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302806 kfree(phba->phwi_ctrlr);
2807 return -ENOMEM;
2808}
2809
2810static int beiscsi_get_memory(struct beiscsi_hba *phba)
2811{
2812 beiscsi_find_mem_req(phba);
2813 return beiscsi_alloc_mem(phba);
2814}
2815
2816static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2817{
2818 struct pdu_data_out *pdata_out;
2819 struct pdu_nop_out *pnop_out;
2820 struct be_mem_descriptor *mem_descr;
2821
2822 mem_descr = phba->init_mem;
2823 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2824 pdata_out =
2825 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2826 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2827
2828 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2829 IIOC_SCSI_DATA);
2830
2831 pnop_out =
2832 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2833 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2834
2835 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2836 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2837 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2838 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2839}
2840
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002841static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302842{
2843 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002844 struct hwi_context_memory *phwi_ctxt;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002845 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302846 struct hwi_controller *phwi_ctrlr;
2847 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002848 struct iscsi_wrb *pwrb = NULL;
2849 unsigned int num_cxn_wrbh = 0;
2850 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302851
2852 mem_descr_wrbh = phba->init_mem;
2853 mem_descr_wrbh += HWI_MEM_WRBH;
2854
2855 mem_descr_wrb = phba->init_mem;
2856 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302857 phwi_ctrlr = phba->phwi_ctrlr;
2858
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002859 /* Allocate memory for WRBQ */
2860 phwi_ctxt = phwi_ctrlr->phwi_ctxt;
2861 phwi_ctxt->be_wrbq = kzalloc(sizeof(struct be_queue_info) *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07002862 phba->params.cxns_per_ctrl,
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002863 GFP_KERNEL);
2864 if (!phwi_ctxt->be_wrbq) {
2865 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2866 "BM_%d : WRBQ Mem Alloc Failed\n");
2867 return -ENOMEM;
2868 }
2869
2870 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302871 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302872 pwrb_context->pwrb_handle_base =
2873 kzalloc(sizeof(struct wrb_handle *) *
2874 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002875 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302876 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2877 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002878 goto init_wrb_hndl_failed;
2879 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302880 pwrb_context->pwrb_handle_basestd =
2881 kzalloc(sizeof(struct wrb_handle *) *
2882 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002883 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302884 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2885 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002886 goto init_wrb_hndl_failed;
2887 }
2888 if (!num_cxn_wrbh) {
2889 pwrb_handle =
2890 mem_descr_wrbh->mem_array[idx].virtual_address;
2891 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2892 ((sizeof(struct wrb_handle)) *
2893 phba->params.wrbs_per_cxn));
2894 idx++;
2895 }
2896 pwrb_context->alloc_index = 0;
2897 pwrb_context->wrb_handles_available = 0;
2898 pwrb_context->free_index = 0;
2899
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302900 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302901 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2902 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2903 pwrb_context->pwrb_handle_basestd[j] =
2904 pwrb_handle;
2905 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302906 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302907 pwrb_handle++;
2908 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302909 num_cxn_wrbh--;
2910 }
Jitendra Bhivaref64d92e2016-02-04 15:49:20 +05302911 spin_lock_init(&pwrb_context->wrb_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302912 }
2913 idx = 0;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002914 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302915 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002916 if (!num_cxn_wrb) {
2917 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2918 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2919 ((sizeof(struct iscsi_wrb) *
2920 phba->params.wrbs_per_cxn));
2921 idx++;
2922 }
2923
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302924 if (num_cxn_wrb) {
2925 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2926 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2927 pwrb_handle->pwrb = pwrb;
2928 pwrb++;
2929 }
2930 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302931 }
2932 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002933 return 0;
2934init_wrb_hndl_failed:
2935 for (j = index; j > 0; j--) {
2936 pwrb_context = &phwi_ctrlr->wrb_context[j];
2937 kfree(pwrb_context->pwrb_handle_base);
2938 kfree(pwrb_context->pwrb_handle_basestd);
2939 }
2940 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302941}
2942
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002943static int hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302944{
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002945 uint8_t ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302946 struct hwi_controller *phwi_ctrlr;
2947 struct hba_parameters *p = &phba->params;
2948 struct hwi_async_pdu_context *pasync_ctx;
2949 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002950 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302951 struct be_mem_descriptor *mem_descr;
2952
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002953 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
2954 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302955
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002956 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2957 mem_descr += (HWI_MEM_ASYNC_PDU_CONTEXT_ULP0 +
2958 (ulp_num * MEM_DESCR_OFFSET));
2959
2960 phwi_ctrlr = phba->phwi_ctrlr;
2961 phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num] =
2962 (struct hwi_async_pdu_context *)
2963 mem_descr->mem_array[0].virtual_address;
2964
2965 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
2966 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2967
2968 pasync_ctx->async_entry =
2969 (struct hwi_async_entry *)
2970 ((long unsigned int)pasync_ctx +
2971 sizeof(struct hwi_async_pdu_context));
2972
2973 pasync_ctx->num_entries = BEISCSI_GET_CID_COUNT(phba,
2974 ulp_num);
2975 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
2976
2977 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2978 mem_descr += HWI_MEM_ASYNC_HEADER_BUF_ULP0 +
2979 (ulp_num * MEM_DESCR_OFFSET);
2980 if (mem_descr->mem_array[0].virtual_address) {
2981 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2982 "BM_%d : hwi_init_async_pdu_ctx"
2983 " HWI_MEM_ASYNC_HEADER_BUF_ULP%d va=%p\n",
2984 ulp_num,
2985 mem_descr->mem_array[0].
2986 virtual_address);
2987 } else
2988 beiscsi_log(phba, KERN_WARNING,
2989 BEISCSI_LOG_INIT,
2990 "BM_%d : No Virtual address for ULP : %d\n",
2991 ulp_num);
2992
2993 pasync_ctx->async_header.va_base =
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302994 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302995
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07002996 pasync_ctx->async_header.pa_base.u.a64.address =
2997 mem_descr->mem_array[0].
2998 bus_address.u.a64.address;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07002999
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003000 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3001 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3002 (ulp_num * MEM_DESCR_OFFSET);
3003 if (mem_descr->mem_array[0].virtual_address) {
3004 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3005 "BM_%d : hwi_init_async_pdu_ctx"
3006 " HWI_MEM_ASYNC_HEADER_RING_ULP%d va=%p\n",
3007 ulp_num,
3008 mem_descr->mem_array[0].
3009 virtual_address);
3010 } else
3011 beiscsi_log(phba, KERN_WARNING,
3012 BEISCSI_LOG_INIT,
3013 "BM_%d : No Virtual address for ULP : %d\n",
3014 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303015
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003016 pasync_ctx->async_header.ring_base =
3017 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303018
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003019 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3020 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE_ULP0 +
3021 (ulp_num * MEM_DESCR_OFFSET);
3022 if (mem_descr->mem_array[0].virtual_address) {
3023 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3024 "BM_%d : hwi_init_async_pdu_ctx"
3025 " HWI_MEM_ASYNC_HEADER_HANDLE_ULP%d va=%p\n",
3026 ulp_num,
3027 mem_descr->mem_array[0].
3028 virtual_address);
3029 } else
3030 beiscsi_log(phba, KERN_WARNING,
3031 BEISCSI_LOG_INIT,
3032 "BM_%d : No Virtual address for ULP : %d\n",
3033 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303034
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003035 pasync_ctx->async_header.handle_base =
3036 mem_descr->mem_array[0].virtual_address;
3037 pasync_ctx->async_header.writables = 0;
3038 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303039
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003040 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3041 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3042 (ulp_num * MEM_DESCR_OFFSET);
3043 if (mem_descr->mem_array[0].virtual_address) {
3044 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3045 "BM_%d : hwi_init_async_pdu_ctx"
3046 " HWI_MEM_ASYNC_DATA_RING_ULP%d va=%p\n",
3047 ulp_num,
3048 mem_descr->mem_array[0].
3049 virtual_address);
3050 } else
3051 beiscsi_log(phba, KERN_WARNING,
3052 BEISCSI_LOG_INIT,
3053 "BM_%d : No Virtual address for ULP : %d\n",
3054 ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303055
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003056 pasync_ctx->async_data.ring_base =
3057 mem_descr->mem_array[0].virtual_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303058
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003059 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3060 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE_ULP0 +
3061 (ulp_num * MEM_DESCR_OFFSET);
3062 if (!mem_descr->mem_array[0].virtual_address)
3063 beiscsi_log(phba, KERN_WARNING,
3064 BEISCSI_LOG_INIT,
3065 "BM_%d : No Virtual address for ULP : %d\n",
3066 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303067
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003068 pasync_ctx->async_data.handle_base =
3069 mem_descr->mem_array[0].virtual_address;
3070 pasync_ctx->async_data.writables = 0;
3071 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303072
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003073 pasync_header_h =
3074 (struct async_pdu_handle *)
3075 pasync_ctx->async_header.handle_base;
3076 pasync_data_h =
3077 (struct async_pdu_handle *)
3078 pasync_ctx->async_data.handle_base;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303079
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003080 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3081 mem_descr += HWI_MEM_ASYNC_DATA_BUF_ULP0 +
3082 (ulp_num * MEM_DESCR_OFFSET);
3083 if (mem_descr->mem_array[0].virtual_address) {
3084 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3085 "BM_%d : hwi_init_async_pdu_ctx"
3086 " HWI_MEM_ASYNC_DATA_BUF_ULP%d va=%p\n",
3087 ulp_num,
3088 mem_descr->mem_array[0].
3089 virtual_address);
3090 } else
3091 beiscsi_log(phba, KERN_WARNING,
3092 BEISCSI_LOG_INIT,
3093 "BM_%d : No Virtual address for ULP : %d\n",
3094 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303095
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003096 idx = 0;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05003097 pasync_ctx->async_data.va_base =
3098 mem_descr->mem_array[idx].virtual_address;
3099 pasync_ctx->async_data.pa_base.u.a64.address =
3100 mem_descr->mem_array[idx].
3101 bus_address.u.a64.address;
3102
3103 num_async_data = ((mem_descr->mem_array[idx].size) /
3104 phba->params.defpdu_data_sz);
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003105 num_per_mem = 0;
3106
3107 for (index = 0; index < BEISCSI_GET_CID_COUNT
3108 (phba, ulp_num); index++) {
3109 pasync_header_h->cri = -1;
3110 pasync_header_h->index = (char)index;
3111 INIT_LIST_HEAD(&pasync_header_h->link);
3112 pasync_header_h->pbuffer =
3113 (void *)((unsigned long)
3114 (pasync_ctx->
3115 async_header.va_base) +
3116 (p->defpdu_hdr_sz * index));
3117
3118 pasync_header_h->pa.u.a64.address =
3119 pasync_ctx->async_header.pa_base.u.a64.
3120 address + (p->defpdu_hdr_sz * index);
3121
3122 list_add_tail(&pasync_header_h->link,
3123 &pasync_ctx->async_header.
3124 free_list);
3125 pasync_header_h++;
3126 pasync_ctx->async_header.free_entries++;
3127 pasync_ctx->async_header.writables++;
3128
3129 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3130 wait_queue.list);
3131 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3132 header_busy_list);
3133 pasync_data_h->cri = -1;
3134 pasync_data_h->index = (char)index;
3135 INIT_LIST_HEAD(&pasync_data_h->link);
3136
3137 if (!num_async_data) {
3138 num_per_mem = 0;
3139 idx++;
3140 pasync_ctx->async_data.va_base =
3141 mem_descr->mem_array[idx].
3142 virtual_address;
3143 pasync_ctx->async_data.pa_base.u.
3144 a64.address =
3145 mem_descr->mem_array[idx].
3146 bus_address.u.a64.address;
3147 num_async_data =
3148 ((mem_descr->mem_array[idx].
3149 size) /
3150 phba->params.defpdu_data_sz);
3151 }
3152 pasync_data_h->pbuffer =
3153 (void *)((unsigned long)
3154 (pasync_ctx->async_data.va_base) +
3155 (p->defpdu_data_sz * num_per_mem));
3156
3157 pasync_data_h->pa.u.a64.address =
3158 pasync_ctx->async_data.pa_base.u.a64.
3159 address + (p->defpdu_data_sz *
3160 num_per_mem);
3161 num_per_mem++;
3162 num_async_data--;
3163
3164 list_add_tail(&pasync_data_h->link,
3165 &pasync_ctx->async_data.
3166 free_list);
3167 pasync_data_h++;
3168 pasync_ctx->async_data.free_entries++;
3169 pasync_ctx->async_data.writables++;
3170
3171 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
3172 data_busy_list);
3173 }
3174
3175 pasync_ctx->async_header.host_write_ptr = 0;
3176 pasync_ctx->async_header.ep_read_ptr = -1;
3177 pasync_ctx->async_data.host_write_ptr = 0;
3178 pasync_ctx->async_data.ep_read_ptr = -1;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05003179 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303180 }
3181
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003182 return 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303183}
3184
3185static int
3186be_sgl_create_contiguous(void *virtual_address,
3187 u64 physical_address, u32 length,
3188 struct be_dma_mem *sgl)
3189{
3190 WARN_ON(!virtual_address);
3191 WARN_ON(!physical_address);
Tim Gardnerdd29dae2015-10-30 12:22:58 -06003192 WARN_ON(!length);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303193 WARN_ON(!sgl);
3194
3195 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303196 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303197 sgl->size = length;
3198
3199 return 0;
3200}
3201
3202static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
3203{
3204 memset(sgl, 0, sizeof(*sgl));
3205}
3206
3207static void
3208hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
3209 struct mem_array *pmem, struct be_dma_mem *sgl)
3210{
3211 if (sgl->va)
3212 be_sgl_destroy_contiguous(sgl);
3213
3214 be_sgl_create_contiguous(pmem->virtual_address,
3215 pmem->bus_address.u.a64.address,
3216 pmem->size, sgl);
3217}
3218
3219static void
3220hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
3221 struct mem_array *pmem, struct be_dma_mem *sgl)
3222{
3223 if (sgl->va)
3224 be_sgl_destroy_contiguous(sgl);
3225
3226 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
3227 pmem->bus_address.u.a64.address,
3228 pmem->size, sgl);
3229}
3230
3231static int be_fill_queue(struct be_queue_info *q,
3232 u16 len, u16 entry_size, void *vaddress)
3233{
3234 struct be_dma_mem *mem = &q->dma_mem;
3235
3236 memset(q, 0, sizeof(*q));
3237 q->len = len;
3238 q->entry_size = entry_size;
3239 mem->size = len * entry_size;
3240 mem->va = vaddress;
3241 if (!mem->va)
3242 return -ENOMEM;
3243 memset(mem->va, 0, mem->size);
3244 return 0;
3245}
3246
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303247static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303248 struct hwi_context_memory *phwi_context)
3249{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303250 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303251 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303252 struct be_queue_info *eq;
3253 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303254 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303255 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303256
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303257 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3258 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303259
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303260 if (phba->msix_enabled)
3261 eq_for_mcc = 1;
3262 else
3263 eq_for_mcc = 0;
3264 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3265 eq = &phwi_context->be_eq[i].q;
3266 mem = &eq->dma_mem;
3267 phwi_context->be_eq[i].phba = phba;
3268 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3269 num_eq_pages * PAGE_SIZE,
3270 &paddr);
3271 if (!eq_vaddress)
3272 goto create_eq_error;
3273
3274 mem->va = eq_vaddress;
3275 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3276 sizeof(struct be_eq_entry), eq_vaddress);
3277 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303278 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3279 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303280 goto create_eq_error;
3281 }
3282
3283 mem->dma = paddr;
3284 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3285 phwi_context->cur_eqd);
3286 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303287 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3288 "BM_%d : beiscsi_cmd_eq_create"
3289 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303290 goto create_eq_error;
3291 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303292
3293 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3294 "BM_%d : eqid = %d\n",
3295 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303296 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303297 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303298create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303299 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303300 eq = &phwi_context->be_eq[i].q;
3301 mem = &eq->dma_mem;
3302 if (mem->va)
3303 pci_free_consistent(phba->pcidev, num_eq_pages
3304 * PAGE_SIZE,
3305 mem->va, mem->dma);
3306 }
3307 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303308}
3309
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303310static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303311 struct hwi_context_memory *phwi_context)
3312{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303313 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303314 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303315 struct be_queue_info *cq, *eq;
3316 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303317 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303318 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303319 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303320
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303321 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3322 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303323
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303324 for (i = 0; i < phba->num_cpus; i++) {
3325 cq = &phwi_context->be_cq[i];
3326 eq = &phwi_context->be_eq[i].q;
3327 pbe_eq = &phwi_context->be_eq[i];
3328 pbe_eq->cq = cq;
3329 pbe_eq->phba = phba;
3330 mem = &cq->dma_mem;
3331 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3332 num_cq_pages * PAGE_SIZE,
3333 &paddr);
3334 if (!cq_vaddress)
3335 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303336 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303337 sizeof(struct sol_cqe), cq_vaddress);
3338 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303339 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3340 "BM_%d : be_fill_queue Failed "
3341 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303342 goto create_cq_error;
3343 }
3344
3345 mem->dma = paddr;
3346 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3347 false, 0);
3348 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303349 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3350 "BM_%d : beiscsi_cmd_eq_create"
3351 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303352 goto create_cq_error;
3353 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303354 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3355 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3356 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303357 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303358 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303359
3360create_cq_error:
3361 for (i = 0; i < phba->num_cpus; i++) {
3362 cq = &phwi_context->be_cq[i];
3363 mem = &cq->dma_mem;
3364 if (mem->va)
3365 pci_free_consistent(phba->pcidev, num_cq_pages
3366 * PAGE_SIZE,
3367 mem->va, mem->dma);
3368 }
3369 return ret;
3370
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303371}
3372
3373static int
3374beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3375 struct hwi_context_memory *phwi_context,
3376 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003377 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303378{
3379 unsigned int idx;
3380 int ret;
3381 struct be_queue_info *dq, *cq;
3382 struct be_dma_mem *mem;
3383 struct be_mem_descriptor *mem_descr;
3384 void *dq_vaddress;
3385
3386 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003387 dq = &phwi_context->be_def_hdrq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303388 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303389 mem = &dq->dma_mem;
3390 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003391 mem_descr += HWI_MEM_ASYNC_HEADER_RING_ULP0 +
3392 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303393 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3394 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3395 sizeof(struct phys_addr),
3396 sizeof(struct phys_addr), dq_vaddress);
3397 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303398 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003399 "BM_%d : be_fill_queue Failed for DEF PDU HDR on ULP : %d\n",
3400 ulp_num);
3401
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303402 return ret;
3403 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303404 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3405 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303406 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3407 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003408 phba->params.defpdu_hdr_sz,
3409 BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303410 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303411 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003412 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR on ULP : %d\n",
3413 ulp_num);
3414
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303415 return ret;
3416 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303417
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003418 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3419 "BM_%d : iscsi hdr def pdu id for ULP : %d is %d\n",
3420 ulp_num,
3421 phwi_context->be_def_hdrq[ulp_num].id);
3422 hwi_post_async_buffers(phba, BEISCSI_DEFQ_HDR, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303423 return 0;
3424}
3425
3426static int
3427beiscsi_create_def_data(struct beiscsi_hba *phba,
3428 struct hwi_context_memory *phwi_context,
3429 struct hwi_controller *phwi_ctrlr,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003430 unsigned int def_pdu_ring_sz, uint8_t ulp_num)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303431{
3432 unsigned int idx;
3433 int ret;
3434 struct be_queue_info *dataq, *cq;
3435 struct be_dma_mem *mem;
3436 struct be_mem_descriptor *mem_descr;
3437 void *dq_vaddress;
3438
3439 idx = 0;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003440 dataq = &phwi_context->be_def_dataq[ulp_num];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303441 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303442 mem = &dataq->dma_mem;
3443 mem_descr = phba->init_mem;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003444 mem_descr += HWI_MEM_ASYNC_DATA_RING_ULP0 +
3445 (ulp_num * MEM_DESCR_OFFSET);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303446 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3447 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3448 sizeof(struct phys_addr),
3449 sizeof(struct phys_addr), dq_vaddress);
3450 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303451 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003452 "BM_%d : be_fill_queue Failed for DEF PDU "
3453 "DATA on ULP : %d\n",
3454 ulp_num);
3455
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303456 return ret;
3457 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303458 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3459 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303460 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3461 def_pdu_ring_sz,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003462 phba->params.defpdu_data_sz,
3463 BEISCSI_DEFQ_DATA, ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303464 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303465 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3466 "BM_%d be_cmd_create_default_pdu_queue"
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003467 " Failed for DEF PDU DATA on ULP : %d\n",
3468 ulp_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303469 return ret;
3470 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303471
John Soni Jose99bc5d52012-08-20 23:00:18 +05303472 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003473 "BM_%d : iscsi def data id on ULP : %d is %d\n",
3474 ulp_num,
3475 phwi_context->be_def_dataq[ulp_num].id);
3476
3477 hwi_post_async_buffers(phba, BEISCSI_DEFQ_DATA, ulp_num);
3478 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3479 "BM_%d : DEFAULT PDU DATA RING CREATED"
3480 "on ULP : %d\n", ulp_num);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303481
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303482 return 0;
3483}
3484
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003485
3486static int
3487beiscsi_post_template_hdr(struct beiscsi_hba *phba)
3488{
3489 struct be_mem_descriptor *mem_descr;
3490 struct mem_array *pm_arr;
3491 struct be_dma_mem sgl;
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003492 int status, ulp_num;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003493
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003494 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3495 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3496 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
3497 mem_descr += HWI_MEM_TEMPLATE_HDR_ULP0 +
3498 (ulp_num * MEM_DESCR_OFFSET);
3499 pm_arr = mem_descr->mem_array;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003500
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003501 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3502 status = be_cmd_iscsi_post_template_hdr(
3503 &phba->ctrl, &sgl);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003504
Jayamohan Kallickala129d922013-09-28 15:35:46 -07003505 if (status != 0) {
3506 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3507 "BM_%d : Post Template HDR Failed for"
3508 "ULP_%d\n", ulp_num);
3509 return status;
3510 }
3511
3512 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3513 "BM_%d : Template HDR Pages Posted for"
3514 "ULP_%d\n", ulp_num);
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003515 }
3516 }
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003517 return 0;
3518}
3519
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303520static int
3521beiscsi_post_pages(struct beiscsi_hba *phba)
3522{
3523 struct be_mem_descriptor *mem_descr;
3524 struct mem_array *pm_arr;
3525 unsigned int page_offset, i;
3526 struct be_dma_mem sgl;
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003527 int status, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303528
3529 mem_descr = phba->init_mem;
3530 mem_descr += HWI_MEM_SGE;
3531 pm_arr = mem_descr->mem_array;
3532
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07003533 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3534 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
3535 break;
3536
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303537 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07003538 phba->fw_config.iscsi_icd_start[ulp_num]) / PAGE_SIZE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303539 for (i = 0; i < mem_descr->num_elements; i++) {
3540 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3541 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3542 page_offset,
3543 (pm_arr->size / PAGE_SIZE));
3544 page_offset += pm_arr->size / PAGE_SIZE;
3545 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303546 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3547 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303548 return status;
3549 }
3550 pm_arr++;
3551 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303552 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3553 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303554 return 0;
3555}
3556
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303557static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3558{
3559 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003560 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303561 pci_free_consistent(phba->pcidev, mem->size,
3562 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003563 mem->va = NULL;
3564 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303565}
3566
3567static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3568 u16 len, u16 entry_size)
3569{
3570 struct be_dma_mem *mem = &q->dma_mem;
3571
3572 memset(q, 0, sizeof(*q));
3573 q->len = len;
3574 q->entry_size = entry_size;
3575 mem->size = len * entry_size;
Joe Perches7c845eb2014-08-08 14:24:46 -07003576 mem->va = pci_zalloc_consistent(phba->pcidev, mem->size, &mem->dma);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303577 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303578 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303579 return 0;
3580}
3581
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303582static int
3583beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3584 struct hwi_context_memory *phwi_context,
3585 struct hwi_controller *phwi_ctrlr)
3586{
3587 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3588 u64 pa_addr_lo;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003589 unsigned int idx, num, i, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590 struct mem_array *pwrb_arr;
3591 void *wrb_vaddr;
3592 struct be_dma_mem sgl;
3593 struct be_mem_descriptor *mem_descr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003594 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303595 int status;
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003596 uint8_t ulp_count = 0, ulp_base_num = 0;
3597 uint16_t cid_count_ulp[BEISCSI_ULP_COUNT] = { 0 };
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303598
3599 idx = 0;
3600 mem_descr = phba->init_mem;
3601 mem_descr += HWI_MEM_WRB;
3602 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3603 GFP_KERNEL);
3604 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303605 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3606 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303607 return -ENOMEM;
3608 }
3609 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3610 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3611 num_wrb_rings = mem_descr->mem_array[idx].size /
3612 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3613
3614 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3615 if (num_wrb_rings) {
3616 pwrb_arr[num].virtual_address = wrb_vaddr;
3617 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3618 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3619 sizeof(struct iscsi_wrb);
3620 wrb_vaddr += pwrb_arr[num].size;
3621 pa_addr_lo += pwrb_arr[num].size;
3622 num_wrb_rings--;
3623 } else {
3624 idx++;
3625 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3626 pa_addr_lo = mem_descr->mem_array[idx].\
3627 bus_address.u.a64.address;
3628 num_wrb_rings = mem_descr->mem_array[idx].size /
3629 (phba->params.wrbs_per_cxn *
3630 sizeof(struct iscsi_wrb));
3631 pwrb_arr[num].virtual_address = wrb_vaddr;
3632 pwrb_arr[num].bus_address.u.a64.address\
3633 = pa_addr_lo;
3634 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3635 sizeof(struct iscsi_wrb);
3636 wrb_vaddr += pwrb_arr[num].size;
3637 pa_addr_lo += pwrb_arr[num].size;
3638 num_wrb_rings--;
3639 }
3640 }
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003641
3642 /* Get the ULP Count */
3643 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
3644 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3645 ulp_count++;
3646 ulp_base_num = ulp_num;
3647 cid_count_ulp[ulp_num] =
3648 BEISCSI_GET_CID_COUNT(phba, ulp_num);
3649 }
3650
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303651 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3652 wrb_mem_index = 0;
3653 offset = 0;
3654 size = 0;
3655
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003656 if (ulp_count > 1) {
3657 ulp_base_num = (ulp_base_num + 1) % BEISCSI_ULP_COUNT;
3658
3659 if (!cid_count_ulp[ulp_base_num])
3660 ulp_base_num = (ulp_base_num + 1) %
3661 BEISCSI_ULP_COUNT;
3662
3663 cid_count_ulp[ulp_base_num]--;
3664 }
3665
3666
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303667 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3668 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
Jayamohan Kallickal4eea99d2013-09-28 15:35:48 -07003669 &phwi_context->be_wrbq[i],
3670 &phwi_ctrlr->wrb_context[i],
3671 ulp_base_num);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303672 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303673 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3674 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003675 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303676 return status;
3677 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003678 pwrb_context = &phwi_ctrlr->wrb_context[i];
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003679 BE_SET_CID_TO_CRI(i, pwrb_context->cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303680 }
3681 kfree(pwrb_arr);
3682 return 0;
3683}
3684
3685static void free_wrb_handles(struct beiscsi_hba *phba)
3686{
3687 unsigned int index;
3688 struct hwi_controller *phwi_ctrlr;
3689 struct hwi_wrb_context *pwrb_context;
3690
3691 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003692 for (index = 0; index < phba->params.cxns_per_ctrl; index++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303693 pwrb_context = &phwi_ctrlr->wrb_context[index];
3694 kfree(pwrb_context->pwrb_handle_base);
3695 kfree(pwrb_context->pwrb_handle_basestd);
3696 }
3697}
3698
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303699static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3700{
3701 struct be_queue_info *q;
3702 struct be_ctrl_info *ctrl = &phba->ctrl;
3703
3704 q = &phba->ctrl.mcc_obj.q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303705 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303706 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303707 be_queue_free(phba, q);
3708 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303709
3710 q = &phba->ctrl.mcc_obj.cq;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303711 if (q->created) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303712 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303713 be_queue_free(phba, q);
3714 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303715}
3716
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303717static void hwi_cleanup(struct beiscsi_hba *phba)
3718{
3719 struct be_queue_info *q;
3720 struct be_ctrl_info *ctrl = &phba->ctrl;
3721 struct hwi_controller *phwi_ctrlr;
3722 struct hwi_context_memory *phwi_context;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003723 struct hwi_async_pdu_context *pasync_ctx;
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003724 int i, eq_for_mcc, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303725
3726 phwi_ctrlr = phba->phwi_ctrlr;
3727 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003728
3729 be_cmd_iscsi_remove_template_hdr(ctrl);
3730
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303731 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3732 q = &phwi_context->be_wrbq[i];
3733 if (q->created)
3734 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3735 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07003736 kfree(phwi_context->be_wrbq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303737 free_wrb_handles(phba);
3738
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003739 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3740 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303741
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003742 q = &phwi_context->be_def_hdrq[ulp_num];
3743 if (q->created)
3744 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3745
3746 q = &phwi_context->be_def_dataq[ulp_num];
3747 if (q->created)
3748 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3749
3750 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx[ulp_num];
3751 }
3752 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303753
3754 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3755
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303756 for (i = 0; i < (phba->num_cpus); i++) {
3757 q = &phwi_context->be_cq[i];
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303758 if (q->created) {
3759 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303760 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303761 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303762 }
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003763
3764 be_mcc_queues_destroy(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303765 if (phba->msix_enabled)
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003766 eq_for_mcc = 1;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303767 else
Jayamohan Kallickal23188162014-05-05 21:41:29 -04003768 eq_for_mcc = 0;
3769 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303770 q = &phwi_context->be_eq[i].q;
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303771 if (q->created) {
3772 be_queue_free(phba, q);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303773 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
John Soni Jose4e2bdf72015-04-25 08:17:31 +05303774 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303775 }
Jayamohan Kallickal0283fbb2013-04-05 20:38:21 -07003776 be_cmd_fw_uninit(ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303777}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303778
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303779static int be_mcc_queues_create(struct beiscsi_hba *phba,
3780 struct hwi_context_memory *phwi_context)
3781{
3782 struct be_queue_info *q, *cq;
3783 struct be_ctrl_info *ctrl = &phba->ctrl;
3784
3785 /* Alloc MCC compl queue */
3786 cq = &phba->ctrl.mcc_obj.cq;
3787 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3788 sizeof(struct be_mcc_compl)))
3789 goto err;
3790 /* Ask BE to create MCC compl queue; */
3791 if (phba->msix_enabled) {
3792 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3793 [phba->num_cpus].q, false, true, 0))
3794 goto mcc_cq_free;
3795 } else {
3796 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3797 false, true, 0))
3798 goto mcc_cq_free;
3799 }
3800
3801 /* Alloc MCC queue */
3802 q = &phba->ctrl.mcc_obj.q;
3803 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3804 goto mcc_cq_destroy;
3805
3806 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303807 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303808 goto mcc_q_free;
3809
3810 return 0;
3811
3812mcc_q_free:
3813 be_queue_free(phba, q);
3814mcc_cq_destroy:
3815 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3816mcc_cq_free:
3817 be_queue_free(phba, cq);
3818err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303819 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303820}
3821
John Soni Jose107dfcb2012-10-20 04:42:13 +05303822/**
3823 * find_num_cpus()- Get the CPU online count
3824 * @phba: ptr to priv structure
3825 *
3826 * CPU count is used for creating EQ.
3827 **/
3828static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303829{
3830 int num_cpus = 0;
3831
3832 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303833
John Soni Jose22abeef2012-10-20 04:43:32 +05303834 switch (phba->generation) {
3835 case BE_GEN2:
3836 case BE_GEN3:
3837 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3838 BEISCSI_MAX_NUM_CPUS : num_cpus;
3839 break;
3840 case BE_GEN4:
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07003841 /*
3842 * If eqid_count == 1 fall back to
3843 * INTX mechanism
3844 **/
3845 if (phba->fw_config.eqid_count == 1) {
3846 enable_msix = 0;
3847 phba->num_cpus = 1;
3848 return;
3849 }
3850
3851 phba->num_cpus =
3852 (num_cpus > (phba->fw_config.eqid_count - 1)) ?
3853 (phba->fw_config.eqid_count - 1) : num_cpus;
John Soni Jose22abeef2012-10-20 04:43:32 +05303854 break;
3855 default:
3856 phba->num_cpus = 1;
3857 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303858}
3859
3860static int hwi_init_port(struct beiscsi_hba *phba)
3861{
3862 struct hwi_controller *phwi_ctrlr;
3863 struct hwi_context_memory *phwi_context;
3864 unsigned int def_pdu_ring_sz;
3865 struct be_ctrl_info *ctrl = &phba->ctrl;
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003866 int status, ulp_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303867
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303868 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303869 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003870 phwi_context->max_eqd = 128;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303871 phwi_context->min_eqd = 0;
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04003872 phwi_context->cur_eqd = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303873 be_cmd_fw_initialize(&phba->ctrl);
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05303874 /* set optic state to unknown */
3875 phba->optic_state = 0xff;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303876
3877 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303878 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303879 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3880 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303881 goto error;
3882 }
3883
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303884 status = be_mcc_queues_create(phba, phwi_context);
3885 if (status != 0)
3886 goto error;
3887
3888 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303889 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303890 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3891 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303892 goto error;
3893 }
3894
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303895 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303896 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303897 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3898 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899 goto error;
3900 }
3901
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003902 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3903 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303904
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003905 def_pdu_ring_sz =
3906 BEISCSI_GET_CID_COUNT(phba, ulp_num) *
3907 sizeof(struct phys_addr);
3908
3909 status = beiscsi_create_def_hdr(phba, phwi_context,
3910 phwi_ctrlr,
3911 def_pdu_ring_sz,
3912 ulp_num);
3913 if (status != 0) {
3914 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3915 "BM_%d : Default Header not created for ULP : %d\n",
3916 ulp_num);
3917 goto error;
3918 }
3919
3920 status = beiscsi_create_def_data(phba, phwi_context,
3921 phwi_ctrlr,
3922 def_pdu_ring_sz,
3923 ulp_num);
3924 if (status != 0) {
3925 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3926 "BM_%d : Default Data not created for ULP : %d\n",
3927 ulp_num);
3928 goto error;
3929 }
3930 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303931 }
3932
3933 status = beiscsi_post_pages(phba);
3934 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303935 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3936 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303937 goto error;
3938 }
3939
Jayamohan Kallickal15a90fe2013-09-28 15:35:38 -07003940 status = beiscsi_post_template_hdr(phba);
3941 if (status != 0) {
3942 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3943 "BM_%d : Template HDR Posting for CXN Failed\n");
3944 }
3945
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303946 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3947 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303948 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3949 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303950 goto error;
3951 }
3952
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07003953 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
3954 uint16_t async_arr_idx = 0;
3955
3956 if (test_bit(ulp_num, &phba->fw_config.ulp_supported)) {
3957 uint16_t cri = 0;
3958 struct hwi_async_pdu_context *pasync_ctx;
3959
3960 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(
3961 phwi_ctrlr, ulp_num);
3962 for (cri = 0; cri <
3963 phba->params.cxns_per_ctrl; cri++) {
3964 if (ulp_num == BEISCSI_GET_ULP_FROM_CRI
3965 (phwi_ctrlr, cri))
3966 pasync_ctx->cid_to_async_cri_map[
3967 phwi_ctrlr->wrb_context[cri].cid] =
3968 async_arr_idx++;
3969 }
3970 }
3971 }
3972
John Soni Jose99bc5d52012-08-20 23:00:18 +05303973 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3974 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303975 return 0;
3976
3977error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303978 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3979 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303980 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003981 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303982}
3983
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303984static int hwi_init_controller(struct beiscsi_hba *phba)
3985{
3986 struct hwi_controller *phwi_ctrlr;
3987
3988 phwi_ctrlr = phba->phwi_ctrlr;
3989 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3990 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3991 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303992 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3993 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3994 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303995 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303996 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3997 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3998 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303999 return -ENOMEM;
4000 }
4001
4002 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004003 if (beiscsi_init_wrb_handle(phba))
4004 return -ENOMEM;
4005
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004006 if (hwi_init_async_pdu_ctx(phba)) {
4007 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4008 "BM_%d : hwi_init_async_pdu_ctx failed\n");
4009 return -ENOMEM;
4010 }
4011
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304012 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304013 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4014 "BM_%d : hwi_init_controller failed\n");
4015
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304016 return -ENOMEM;
4017 }
4018 return 0;
4019}
4020
4021static void beiscsi_free_mem(struct beiscsi_hba *phba)
4022{
4023 struct be_mem_descriptor *mem_descr;
4024 int i, j;
4025
4026 mem_descr = phba->init_mem;
4027 i = 0;
4028 j = 0;
4029 for (i = 0; i < SE_MEM_MAX; i++) {
4030 for (j = mem_descr->num_elements; j > 0; j--) {
4031 pci_free_consistent(phba->pcidev,
4032 mem_descr->mem_array[j - 1].size,
4033 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05304034 (unsigned long)mem_descr->mem_array[j - 1].
4035 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304036 }
Jayamohan Kallickal8a86e832013-09-28 15:35:45 -07004037
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304038 kfree(mem_descr->mem_array);
4039 mem_descr++;
4040 }
4041 kfree(phba->init_mem);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004042 kfree(phba->phwi_ctrlr->wrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304043 kfree(phba->phwi_ctrlr);
4044}
4045
4046static int beiscsi_init_controller(struct beiscsi_hba *phba)
4047{
4048 int ret = -ENOMEM;
4049
4050 ret = beiscsi_get_memory(phba);
4051 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304052 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4053 "BM_%d : beiscsi_dev_probe -"
4054 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304055 return ret;
4056 }
4057
4058 ret = hwi_init_controller(phba);
4059 if (ret)
4060 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304061 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4062 "BM_%d : Return success from beiscsi_init_controller");
4063
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304064 return 0;
4065
4066free_init:
4067 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05004068 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304069}
4070
4071static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
4072{
4073 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
4074 struct sgl_handle *psgl_handle;
4075 struct iscsi_sge *pfrag;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004076 unsigned int arr_index, i, idx;
4077 unsigned int ulp_icd_start, ulp_num = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304078
4079 phba->io_sgl_hndl_avbl = 0;
4080 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304081
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304082 mem_descr_sglh = phba->init_mem;
4083 mem_descr_sglh += HWI_MEM_SGLH;
4084 if (1 == mem_descr_sglh->num_elements) {
4085 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
4086 phba->params.ios_per_ctrl,
4087 GFP_KERNEL);
4088 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304089 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4090 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304091 return -ENOMEM;
4092 }
4093 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
4094 (phba->params.icds_per_ctrl -
4095 phba->params.ios_per_ctrl),
4096 GFP_KERNEL);
4097 if (!phba->eh_sgl_hndl_base) {
4098 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304099 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4100 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304101 return -ENOMEM;
4102 }
4103 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304104 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4105 "BM_%d : HWI_MEM_SGLH is more than one element."
4106 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304107 return -ENOMEM;
4108 }
4109
4110 arr_index = 0;
4111 idx = 0;
4112 while (idx < mem_descr_sglh->num_elements) {
4113 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
4114
4115 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
4116 sizeof(struct sgl_handle)); i++) {
4117 if (arr_index < phba->params.ios_per_ctrl) {
4118 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
4119 phba->io_sgl_hndl_avbl++;
4120 arr_index++;
4121 } else {
4122 phba->eh_sgl_hndl_base[arr_index -
4123 phba->params.ios_per_ctrl] =
4124 psgl_handle;
4125 arr_index++;
4126 phba->eh_sgl_hndl_avbl++;
4127 }
4128 psgl_handle++;
4129 }
4130 idx++;
4131 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304132 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4133 "BM_%d : phba->io_sgl_hndl_avbl=%d"
4134 "phba->eh_sgl_hndl_avbl=%d\n",
4135 phba->io_sgl_hndl_avbl,
4136 phba->eh_sgl_hndl_avbl);
4137
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304138 mem_descr_sg = phba->init_mem;
4139 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304140 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4141 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
4142 mem_descr_sg->num_elements);
4143
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004144 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++)
4145 if (test_bit(ulp_num, &phba->fw_config.ulp_supported))
4146 break;
4147
4148 ulp_icd_start = phba->fw_config.iscsi_icd_start[ulp_num];
4149
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304150 arr_index = 0;
4151 idx = 0;
4152 while (idx < mem_descr_sg->num_elements) {
4153 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
4154
4155 for (i = 0;
4156 i < (mem_descr_sg->mem_array[idx].size) /
4157 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
4158 i++) {
4159 if (arr_index < phba->params.ios_per_ctrl)
4160 psgl_handle = phba->io_sgl_hndl_base[arr_index];
4161 else
4162 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
4163 phba->params.ios_per_ctrl];
4164 psgl_handle->pfrag = pfrag;
4165 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
4166 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
4167 pfrag += phba->params.num_sge_per_io;
Jayamohan Kallickal90622db2013-09-28 15:35:47 -07004168 psgl_handle->sgl_index = ulp_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304169 }
4170 idx++;
4171 }
4172 phba->io_sgl_free_index = 0;
4173 phba->io_sgl_alloc_index = 0;
4174 phba->eh_sgl_free_index = 0;
4175 phba->eh_sgl_alloc_index = 0;
4176 return 0;
4177}
4178
4179static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
4180{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004181 int ret;
4182 uint16_t i, ulp_num;
4183 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304184
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004185 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4186 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4187 ptr_cid_info = kzalloc(sizeof(struct ulp_cid_info),
4188 GFP_KERNEL);
4189
4190 if (!ptr_cid_info) {
4191 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4192 "BM_%d : Failed to allocate memory"
4193 "for ULP_CID_INFO for ULP : %d\n",
4194 ulp_num);
4195 ret = -ENOMEM;
4196 goto free_memory;
4197
4198 }
4199
4200 /* Allocate memory for CID array */
4201 ptr_cid_info->cid_array = kzalloc(sizeof(void *) *
4202 BEISCSI_GET_CID_COUNT(phba,
4203 ulp_num), GFP_KERNEL);
4204 if (!ptr_cid_info->cid_array) {
4205 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4206 "BM_%d : Failed to allocate memory"
4207 "for CID_ARRAY for ULP : %d\n",
4208 ulp_num);
4209 kfree(ptr_cid_info);
4210 ptr_cid_info = NULL;
4211 ret = -ENOMEM;
4212
4213 goto free_memory;
4214 }
4215 ptr_cid_info->avlbl_cids = BEISCSI_GET_CID_COUNT(
4216 phba, ulp_num);
4217
4218 /* Save the cid_info_array ptr */
4219 phba->cid_array_info[ulp_num] = ptr_cid_info;
4220 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304221 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05304222 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004223 phba->params.cxns_per_ctrl, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304224 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304225 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4226 "BM_%d : Failed to allocate memory in "
4227 "hba_setup_cid_tbls\n");
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004228 ret = -ENOMEM;
4229
4230 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304231 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004232
4233 phba->conn_table = kzalloc(sizeof(struct beiscsi_conn *) *
4234 phba->params.cxns_per_ctrl, GFP_KERNEL);
4235 if (!phba->conn_table) {
4236 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4237 "BM_%d : Failed to allocate memory in"
4238 "hba_setup_cid_tbls\n");
4239
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004240 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004241 phba->ep_array = NULL;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004242 ret = -ENOMEM;
Tomas Henzl5f2d25e2014-06-06 14:06:30 +02004243
4244 goto free_memory;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304245 }
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004246
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004247 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
4248 ulp_num = phba->phwi_ctrlr->wrb_context[i].ulp_num;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004249
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004250 ptr_cid_info = phba->cid_array_info[ulp_num];
4251 ptr_cid_info->cid_array[ptr_cid_info->cid_alloc++] =
4252 phba->phwi_ctrlr->wrb_context[i].cid;
4253
4254 }
4255
4256 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4257 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4258 ptr_cid_info = phba->cid_array_info[ulp_num];
4259
4260 ptr_cid_info->cid_alloc = 0;
4261 ptr_cid_info->cid_free = 0;
4262 }
4263 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304264 return 0;
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004265
4266free_memory:
4267 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4268 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4269 ptr_cid_info = phba->cid_array_info[ulp_num];
4270
4271 if (ptr_cid_info) {
4272 kfree(ptr_cid_info->cid_array);
4273 kfree(ptr_cid_info);
4274 phba->cid_array_info[ulp_num] = NULL;
4275 }
4276 }
4277 }
4278
4279 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304280}
4281
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304282static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304283{
4284 struct be_ctrl_info *ctrl = &phba->ctrl;
4285 struct hwi_controller *phwi_ctrlr;
4286 struct hwi_context_memory *phwi_context;
4287 struct be_queue_info *eq;
4288 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304289 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304290 u32 enabled;
4291
4292 phwi_ctrlr = phba->phwi_ctrlr;
4293 phwi_context = phwi_ctrlr->phwi_ctxt;
4294
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304295 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
4296 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
4297 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304298
4299 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4300 if (!enabled) {
4301 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304302 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4303 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304304 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004305 }
4306
4307 if (!phba->msix_enabled) {
4308 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304309 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4310 "BM_%d : eq->id=%d\n", eq->id);
4311
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05004312 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4313 } else {
4314 for (i = 0; i <= phba->num_cpus; i++) {
4315 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304316 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4317 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304318 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
4319 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05304320 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304321}
4322
4323static void hwi_disable_intr(struct beiscsi_hba *phba)
4324{
4325 struct be_ctrl_info *ctrl = &phba->ctrl;
4326
4327 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
4328 u32 reg = ioread32(addr);
4329
4330 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4331 if (enabled) {
4332 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
4333 iowrite32(reg, addr);
4334 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05304335 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4336 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304337}
4338
John Soni Jose9aef4202012-08-20 23:00:08 +05304339/**
4340 * beiscsi_get_boot_info()- Get the boot session info
4341 * @phba: The device priv structure instance
4342 *
4343 * Get the boot target info and store in driver priv structure
4344 *
4345 * return values
4346 * Success: 0
4347 * Failure: Non-Zero Value
4348 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304349static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
4350{
Mike Christie0e438952012-04-03 23:41:51 -05004351 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304352 struct be_dma_mem nonemb_cmd;
John Soni Josee175def2012-10-20 04:45:40 +05304353 unsigned int tag;
John Soni Jose9aef4202012-08-20 23:00:08 +05304354 unsigned int s_handle;
Mike Christief457a462011-06-24 15:11:53 -05004355 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304356
John Soni Jose9aef4202012-08-20 23:00:08 +05304357 /* Get the session handle of the boot target */
4358 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
4359 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304360 beiscsi_log(phba, KERN_ERR,
4361 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4362 "BM_%d : No boot session\n");
John Soni Jose3efde862015-04-25 08:16:57 +05304363
4364 if (ret == -ENXIO)
4365 phba->get_boot = 0;
4366
4367
John Soni Jose9aef4202012-08-20 23:00:08 +05304368 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304369 }
John Soni Jose3efde862015-04-25 08:16:57 +05304370 phba->get_boot = 0;
Joe Perches7c845eb2014-08-08 14:24:46 -07004371 nonemb_cmd.va = pci_zalloc_consistent(phba->ctrl.pdev,
4372 sizeof(*session_resp),
4373 &nonemb_cmd.dma);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304374 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304375 beiscsi_log(phba, KERN_ERR,
4376 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4377 "BM_%d : Failed to allocate memory for"
4378 "beiscsi_get_session_info\n");
4379
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304380 return -ENOMEM;
4381 }
4382
John Soni Jose9aef4202012-08-20 23:00:08 +05304383 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05004384 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304385 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304386 beiscsi_log(phba, KERN_ERR,
4387 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
4388 "BM_%d : beiscsi_get_session_info"
4389 " Failed\n");
4390
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304391 goto boot_freemem;
John Soni Josee175def2012-10-20 04:45:40 +05304392 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304393
Jitendra Bhivare88840332016-02-04 15:49:12 +05304394 ret = beiscsi_mccq_compl_wait(phba, tag, NULL, &nonemb_cmd);
John Soni Josee175def2012-10-20 04:45:40 +05304395 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304396 beiscsi_log(phba, KERN_ERR,
4397 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
John Soni Josee175def2012-10-20 04:45:40 +05304398 "BM_%d : beiscsi_get_session_info Failed");
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05004399
4400 if (ret != -EBUSY)
4401 goto boot_freemem;
4402 else
4403 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304404 }
John Soni Josee175def2012-10-20 04:45:40 +05304405
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304406 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05004407
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304408 memcpy(&phba->boot_sess, &session_resp->session_info,
4409 sizeof(struct mgmt_session_info));
John Soni Jose3f4134c2015-04-25 08:18:13 +05304410
4411 beiscsi_logout_fw_sess(phba,
4412 phba->boot_sess.session_handle);
Mike Christief457a462011-06-24 15:11:53 -05004413 ret = 0;
4414
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304415boot_freemem:
4416 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4417 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05004418 return ret;
4419}
4420
4421static void beiscsi_boot_release(void *data)
4422{
4423 struct beiscsi_hba *phba = data;
4424
4425 scsi_host_put(phba->shost);
4426}
4427
4428static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
4429{
4430 struct iscsi_boot_kobj *boot_kobj;
4431
Jayamohan Kallickala3d313e2014-08-08 01:00:00 -04004432 /* it has been created previously */
4433 if (phba->boot_kset)
4434 return 0;
4435
Mike Christief457a462011-06-24 15:11:53 -05004436 /* get boot info using mgmt cmd */
4437 if (beiscsi_get_boot_info(phba))
4438 /* Try to see if we can carry on without this */
4439 return 0;
4440
4441 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
4442 if (!phba->boot_kset)
4443 return -ENOMEM;
4444
4445 /* get a ref because the show function will ref the phba */
4446 if (!scsi_host_get(phba->shost))
4447 goto free_kset;
4448 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
4449 beiscsi_show_boot_tgt_info,
4450 beiscsi_tgt_get_attr_visibility,
4451 beiscsi_boot_release);
4452 if (!boot_kobj)
4453 goto put_shost;
4454
4455 if (!scsi_host_get(phba->shost))
4456 goto free_kset;
4457 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
4458 beiscsi_show_boot_ini_info,
4459 beiscsi_ini_get_attr_visibility,
4460 beiscsi_boot_release);
4461 if (!boot_kobj)
4462 goto put_shost;
4463
4464 if (!scsi_host_get(phba->shost))
4465 goto free_kset;
4466 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
4467 beiscsi_show_boot_eth_info,
4468 beiscsi_eth_get_attr_visibility,
4469 beiscsi_boot_release);
4470 if (!boot_kobj)
4471 goto put_shost;
4472 return 0;
4473
4474put_shost:
4475 scsi_host_put(phba->shost);
4476free_kset:
4477 iscsi_boot_destroy_kset(phba->boot_kset);
Maurizio Lombardi84bd6492016-03-04 10:41:49 +01004478 phba->boot_kset = NULL;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05304479 return -ENOMEM;
4480}
4481
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304482static int beiscsi_init_port(struct beiscsi_hba *phba)
4483{
4484 int ret;
4485
4486 ret = beiscsi_init_controller(phba);
4487 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304488 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4489 "BM_%d : beiscsi_dev_probe - Failed in"
4490 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304491 return ret;
4492 }
4493 ret = beiscsi_init_sgl_handle(phba);
4494 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304495 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4496 "BM_%d : beiscsi_dev_probe - Failed in"
4497 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304498 goto do_cleanup_ctrlr;
4499 }
4500
4501 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304502 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4503 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304504 kfree(phba->io_sgl_hndl_base);
4505 kfree(phba->eh_sgl_hndl_base);
4506 goto do_cleanup_ctrlr;
4507 }
4508
4509 return ret;
4510
4511do_cleanup_ctrlr:
4512 hwi_cleanup(phba);
4513 return ret;
4514}
4515
4516static void hwi_purge_eq(struct beiscsi_hba *phba)
4517{
4518 struct hwi_controller *phwi_ctrlr;
4519 struct hwi_context_memory *phwi_context;
4520 struct be_queue_info *eq;
4521 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304522 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304523 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304524
4525 phwi_ctrlr = phba->phwi_ctrlr;
4526 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304527 if (phba->msix_enabled)
4528 eq_msix = 1;
4529 else
4530 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304531
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304532 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4533 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304534 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304535 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304536 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4537 & EQE_VALID_MASK) {
4538 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4539 queue_tail_inc(eq);
4540 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304541 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304542 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304543
4544 if (num_processed)
4545 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304546 }
4547}
4548
4549static void beiscsi_clean_port(struct beiscsi_hba *phba)
4550{
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004551 int mgmt_status, ulp_num;
4552 struct ulp_cid_info *ptr_cid_info = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304553
Jayamohan Kallickalbd41c2b2013-09-28 15:35:51 -07004554 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4555 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4556 mgmt_status = mgmt_epfw_cleanup(phba, ulp_num);
4557 if (mgmt_status)
4558 beiscsi_log(phba, KERN_WARNING,
4559 BEISCSI_LOG_INIT,
4560 "BM_%d : mgmt_epfw_cleanup FAILED"
4561 " for ULP_%d\n", ulp_num);
4562 }
4563 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304564
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304565 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304566 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304567 kfree(phba->io_sgl_hndl_base);
4568 kfree(phba->eh_sgl_hndl_base);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304569 kfree(phba->ep_array);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004570 kfree(phba->conn_table);
Jayamohan Kallickal0a3db7c2013-09-28 15:35:49 -07004571
4572 for (ulp_num = 0; ulp_num < BEISCSI_ULP_COUNT; ulp_num++) {
4573 if (test_bit(ulp_num, (void *)&phba->fw_config.ulp_supported)) {
4574 ptr_cid_info = phba->cid_array_info[ulp_num];
4575
4576 if (ptr_cid_info) {
4577 kfree(ptr_cid_info->cid_array);
4578 kfree(ptr_cid_info);
4579 phba->cid_array_info[ulp_num] = NULL;
4580 }
4581 }
4582 }
4583
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304584}
4585
John Soni Josed629c472012-10-20 04:42:00 +05304586/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004587 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4588 * @beiscsi_conn: ptr to the conn to be cleaned up
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004589 * @task: ptr to iscsi_task resource to be freed.
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004590 *
4591 * Free driver mgmt resources binded to CXN.
4592 **/
4593void
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004594beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn,
4595 struct iscsi_task *task)
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004596{
4597 struct beiscsi_io_task *io_task;
4598 struct beiscsi_hba *phba = beiscsi_conn->phba;
4599 struct hwi_wrb_context *pwrb_context;
4600 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004601 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4602 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004603
4604 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004605 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
4606
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004607 io_task = task->dd_data;
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004608
4609 if (io_task->pwrb_handle) {
4610 memset(io_task->pwrb_handle->pwrb, 0,
4611 sizeof(struct iscsi_wrb));
4612 free_wrb_handle(phba, pwrb_context,
4613 io_task->pwrb_handle);
4614 io_task->pwrb_handle = NULL;
4615 }
4616
4617 if (io_task->psgl_handle) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004618 free_mgmt_sgl_handle(phba,
4619 io_task->psgl_handle);
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004620 io_task->psgl_handle = NULL;
4621 }
4622
John Soni Joseeb1c4692015-04-25 08:17:45 +05304623 if (io_task->mtask_addr) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004624 pci_unmap_single(phba->pcidev,
4625 io_task->mtask_addr,
4626 io_task->mtask_data_count,
4627 PCI_DMA_TODEVICE);
John Soni Joseeb1c4692015-04-25 08:17:45 +05304628 io_task->mtask_addr = 0;
4629 }
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004630}
4631
4632/**
John Soni Josed629c472012-10-20 04:42:00 +05304633 * beiscsi_cleanup_task()- Free driver resources of the task
4634 * @task: ptr to the iscsi task
4635 *
4636 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004637static void beiscsi_cleanup_task(struct iscsi_task *task)
4638{
4639 struct beiscsi_io_task *io_task = task->dd_data;
4640 struct iscsi_conn *conn = task->conn;
4641 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4642 struct beiscsi_hba *phba = beiscsi_conn->phba;
4643 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4644 struct hwi_wrb_context *pwrb_context;
4645 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004646 uint16_t cri_index = BE_GET_CRI_FROM_CID(
4647 beiscsi_conn->beiscsi_conn_cid);
Mike Christie1282ab72012-04-18 03:06:00 -05004648
4649 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004650 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Mike Christie1282ab72012-04-18 03:06:00 -05004651
4652 if (io_task->cmd_bhs) {
4653 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4654 io_task->bhs_pa.u.a64.address);
4655 io_task->cmd_bhs = NULL;
4656 }
4657
4658 if (task->sc) {
4659 if (io_task->pwrb_handle) {
4660 free_wrb_handle(phba, pwrb_context,
4661 io_task->pwrb_handle);
4662 io_task->pwrb_handle = NULL;
4663 }
4664
4665 if (io_task->psgl_handle) {
Mike Christie1282ab72012-04-18 03:06:00 -05004666 free_io_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05004667 io_task->psgl_handle = NULL;
4668 }
Jayamohan Kallickalda334972014-01-29 02:16:44 -05004669
4670 if (io_task->scsi_cmnd) {
4671 scsi_dma_unmap(io_task->scsi_cmnd);
4672 io_task->scsi_cmnd = NULL;
4673 }
Mike Christie1282ab72012-04-18 03:06:00 -05004674 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004675 if (!beiscsi_conn->login_in_progress)
Jayamohan Kallickal4a4a11b2013-04-05 20:38:31 -07004676 beiscsi_free_mgmt_task_handles(beiscsi_conn, task);
Mike Christie1282ab72012-04-18 03:06:00 -05004677 }
4678}
4679
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304680void
4681beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4682 struct beiscsi_offload_params *params)
4683{
4684 struct wrb_handle *pwrb_handle;
John Soni Jose340c99e2015-08-20 04:44:30 +05304685 struct hwi_wrb_context *pwrb_context = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304686 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004687 struct iscsi_task *task = beiscsi_conn->task;
4688 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304689 u32 doorbell = 0;
4690
4691 /*
4692 * We can always use 0 here because it is reserved by libiscsi for
4693 * login/startup related tasks.
4694 */
Mike Christie1282ab72012-04-18 03:06:00 -05004695 beiscsi_conn->login_in_progress = 0;
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004696 spin_lock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004697 beiscsi_cleanup_task(task);
Shlomo Pongratz659743b2014-02-07 00:41:38 -06004698 spin_unlock_bh(&session->back_lock);
Mike Christie1282ab72012-04-18 03:06:00 -05004699
John Soni Jose340c99e2015-08-20 04:44:30 +05304700 pwrb_handle = alloc_wrb_handle(phba, beiscsi_conn->beiscsi_conn_cid,
4701 &pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304702
John Soni Joseacb96932012-10-20 04:44:35 +05304703 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004704 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304705 beiscsi_offload_cxn_v0(params, pwrb_handle,
John Soni Jose340c99e2015-08-20 04:44:30 +05304706 phba->init_mem,
4707 pwrb_context);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004708 else
John Soni Jose340c99e2015-08-20 04:44:30 +05304709 beiscsi_offload_cxn_v2(params, pwrb_handle,
4710 pwrb_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304711
John Soni Joseacb96932012-10-20 04:44:35 +05304712 be_dws_le_to_cpu(pwrb_handle->pwrb,
4713 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304714
4715 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304716 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304717 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304718 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004719 iowrite32(doorbell, phba->db_va +
4720 beiscsi_conn->doorbell_offset);
Jitendra Bhivarecb564c62016-01-20 14:11:00 +05304721
4722 /*
4723 * There is no completion for CONTEXT_UPDATE. The completion of next
4724 * WRB posted guarantees FW's processing and DMA'ing of it.
4725 * Use beiscsi_put_wrb_handle to put it back in the pool which makes
4726 * sure zero'ing or reuse of the WRB only after wrbs_per_cxn.
4727 */
4728 beiscsi_put_wrb_handle(pwrb_context, pwrb_handle,
4729 phba->params.wrbs_per_cxn);
4730 beiscsi_log(phba, KERN_INFO,
4731 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4732 "BM_%d : put CONTEXT_UPDATE pwrb_handle=%p free_index=0x%x wrb_handles_available=%d\n",
4733 pwrb_handle, pwrb_context->free_index,
4734 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304735}
4736
4737static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4738 int *index, int *age)
4739{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304740 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304741 if (age)
4742 *age = conn->session->age;
4743}
4744
4745/**
4746 * beiscsi_alloc_pdu - allocates pdu and related resources
4747 * @task: libiscsi task
4748 * @opcode: opcode of pdu for task
4749 *
4750 * This is called with the session lock held. It will allocate
4751 * the wrb and sgl if needed for the command. And it will prep
4752 * the pdu's itt. beiscsi_parse_pdu will later translate
4753 * the pdu itt to the libiscsi task itt.
4754 */
4755static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4756{
4757 struct beiscsi_io_task *io_task = task->dd_data;
4758 struct iscsi_conn *conn = task->conn;
4759 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4760 struct beiscsi_hba *phba = beiscsi_conn->phba;
4761 struct hwi_wrb_context *pwrb_context;
4762 struct hwi_controller *phwi_ctrlr;
4763 itt_t itt;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004764 uint16_t cri_index = 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304765 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4766 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304767
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304768 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004769 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304770 if (!io_task->cmd_bhs)
4771 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304772 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304773 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304774 io_task->conn = beiscsi_conn;
4775
4776 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4777 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304778 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004779 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304780
4781 if (task->sc) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304782 io_task->psgl_handle = alloc_io_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304783 if (!io_task->psgl_handle) {
4784 beiscsi_log(phba, KERN_ERR,
4785 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4786 "BM_%d : Alloc of IO_SGL_ICD Failed"
4787 "for the CID : %d\n",
4788 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304789 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304790 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304791 io_task->pwrb_handle = alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304792 beiscsi_conn->beiscsi_conn_cid,
4793 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304794 if (!io_task->pwrb_handle) {
4795 beiscsi_log(phba, KERN_ERR,
4796 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4797 "BM_%d : Alloc of WRB_HANDLE Failed"
4798 "for the CID : %d\n",
4799 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304800 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304801 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304802 } else {
4803 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304804 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004805 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304806 if (!beiscsi_conn->login_in_progress) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304807 io_task->psgl_handle = (struct sgl_handle *)
4808 alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304809 if (!io_task->psgl_handle) {
4810 beiscsi_log(phba, KERN_ERR,
4811 BEISCSI_LOG_IO |
4812 BEISCSI_LOG_CONFIG,
4813 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4814 "for the CID : %d\n",
4815 beiscsi_conn->
4816 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304817 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304818 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304819
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304820 beiscsi_conn->login_in_progress = 1;
4821 beiscsi_conn->plogin_sgl_handle =
4822 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304823 io_task->pwrb_handle =
4824 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304825 beiscsi_conn->beiscsi_conn_cid,
4826 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304827 if (!io_task->pwrb_handle) {
4828 beiscsi_log(phba, KERN_ERR,
4829 BEISCSI_LOG_IO |
4830 BEISCSI_LOG_CONFIG,
4831 "BM_%d : Alloc of WRB_HANDLE Failed"
4832 "for the CID : %d\n",
4833 beiscsi_conn->
4834 beiscsi_conn_cid);
4835 goto free_mgmt_hndls;
4836 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304837 beiscsi_conn->plogin_wrb_handle =
4838 io_task->pwrb_handle;
4839
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304840 } else {
4841 io_task->psgl_handle =
4842 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304843 io_task->pwrb_handle =
4844 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304845 }
4846 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304847 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
John Soni Jose8359c792012-10-20 04:43:03 +05304848 if (!io_task->psgl_handle) {
4849 beiscsi_log(phba, KERN_ERR,
4850 BEISCSI_LOG_IO |
4851 BEISCSI_LOG_CONFIG,
4852 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4853 "for the CID : %d\n",
4854 beiscsi_conn->
4855 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304856 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304857 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304858 io_task->pwrb_handle =
4859 alloc_wrb_handle(phba,
John Soni Jose340c99e2015-08-20 04:44:30 +05304860 beiscsi_conn->beiscsi_conn_cid,
4861 &io_task->pwrb_context);
John Soni Jose8359c792012-10-20 04:43:03 +05304862 if (!io_task->pwrb_handle) {
4863 beiscsi_log(phba, KERN_ERR,
4864 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4865 "BM_%d : Alloc of WRB_HANDLE Failed"
4866 "for the CID : %d\n",
4867 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304868 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304869 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304870
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304871 }
4872 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304873 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4874 wrb_index << 16) | (unsigned int)
4875 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304876 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304877
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304878 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4879 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304880
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304881free_io_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304882 free_io_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304883 goto free_hndls;
4884free_mgmt_hndls:
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304885 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004886 io_task->psgl_handle = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304887free_hndls:
4888 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickala7909b32013-04-05 20:38:32 -07004889 cri_index = BE_GET_CRI_FROM_CID(
4890 beiscsi_conn->beiscsi_conn_cid);
4891 pwrb_context = &phwi_ctrlr->wrb_context[cri_index];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304892 if (io_task->pwrb_handle)
4893 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304894 io_task->pwrb_handle = NULL;
4895 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4896 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004897 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304898 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304899}
John Soni Jose09a10932012-10-20 04:44:23 +05304900int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4901 unsigned int num_sg, unsigned int xferlen,
4902 unsigned int writedir)
4903{
4904
4905 struct beiscsi_io_task *io_task = task->dd_data;
4906 struct iscsi_conn *conn = task->conn;
4907 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4908 struct beiscsi_hba *phba = beiscsi_conn->phba;
4909 struct iscsi_wrb *pwrb = NULL;
4910 unsigned int doorbell = 0;
4911
4912 pwrb = io_task->pwrb_handle->pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304913
John Soni Jose09a10932012-10-20 04:44:23 +05304914 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4915
4916 if (writedir) {
4917 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4918 INI_WR_CMD);
4919 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4920 } else {
4921 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4922 INI_RD_CMD);
4923 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4924 }
4925
4926 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4927 type, pwrb);
4928
4929 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4930 cpu_to_be16(*(unsigned short *)
4931 &io_task->cmd_bhs->iscsi_hdr.lun));
4932 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4933 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4934 io_task->pwrb_handle->wrb_index);
4935 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4936 be32_to_cpu(task->cmdsn));
4937 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4938 io_task->psgl_handle->sgl_index);
4939
4940 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4941 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05304942 io_task->pwrb_handle->wrb_index);
4943 if (io_task->pwrb_context->plast_wrb)
4944 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
4945 io_task->pwrb_context->plast_wrb,
4946 io_task->pwrb_handle->wrb_index);
4947 io_task->pwrb_context->plast_wrb = pwrb;
John Soni Jose09a10932012-10-20 04:44:23 +05304948
4949 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4950
4951 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4952 doorbell |= (io_task->pwrb_handle->wrb_index &
4953 DB_DEF_PDU_WRB_INDEX_MASK) <<
4954 DB_DEF_PDU_WRB_INDEX_SHIFT;
4955 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07004956 iowrite32(doorbell, phba->db_va +
4957 beiscsi_conn->doorbell_offset);
John Soni Jose09a10932012-10-20 04:44:23 +05304958 return 0;
4959}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304960
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304961static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4962 unsigned int num_sg, unsigned int xferlen,
4963 unsigned int writedir)
4964{
4965
4966 struct beiscsi_io_task *io_task = task->dd_data;
4967 struct iscsi_conn *conn = task->conn;
4968 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4969 struct beiscsi_hba *phba = beiscsi_conn->phba;
4970 struct iscsi_wrb *pwrb = NULL;
4971 unsigned int doorbell = 0;
4972
4973 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304974 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4975
4976 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304977 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4978 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304979 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304980 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304981 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4982 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304983 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4984 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304985
John Soni Jose09a10932012-10-20 04:44:23 +05304986 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4987 type, pwrb);
4988
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304989 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004990 cpu_to_be16(*(unsigned short *)
4991 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304992 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4993 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4994 io_task->pwrb_handle->wrb_index);
4995 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4996 be32_to_cpu(task->cmdsn));
4997 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4998 io_task->psgl_handle->sgl_index);
4999
5000 hwi_write_sgl(pwrb, sg, num_sg, io_task);
5001
5002 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05305003 io_task->pwrb_handle->wrb_index);
5004 if (io_task->pwrb_context->plast_wrb)
5005 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
5006 io_task->pwrb_context->plast_wrb,
5007 io_task->pwrb_handle->wrb_index);
5008 io_task->pwrb_context->plast_wrb = pwrb;
5009
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305010 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
5011
5012 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05305013 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305014 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
5015 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
5016
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07005017 iowrite32(doorbell, phba->db_va +
5018 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305019 return 0;
5020}
5021
5022static int beiscsi_mtask(struct iscsi_task *task)
5023{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05305024 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305025 struct iscsi_conn *conn = task->conn;
5026 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
5027 struct beiscsi_hba *phba = beiscsi_conn->phba;
5028 struct iscsi_wrb *pwrb = NULL;
5029 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05305030 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05305031 unsigned int pwrb_typeoffset = 0;
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005032 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305033
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305034 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305035 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05305036 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05305037
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005038 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05305039 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
5040 be32_to_cpu(task->cmdsn));
5041 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
5042 io_task->pwrb_handle->wrb_index);
5043 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
5044 io_task->psgl_handle->sgl_index);
5045 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
5046 task->data_count);
5047 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05305048 io_task->pwrb_handle->wrb_index);
5049 if (io_task->pwrb_context->plast_wrb)
5050 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb,
5051 io_task->pwrb_context->plast_wrb,
5052 io_task->pwrb_handle->wrb_index);
5053 io_task->pwrb_context->plast_wrb = pwrb;
5054
John Soni Jose09a10932012-10-20 04:44:23 +05305055 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005056 } else {
5057 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
5058 be32_to_cpu(task->cmdsn));
5059 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
5060 io_task->pwrb_handle->wrb_index);
5061 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
5062 io_task->psgl_handle->sgl_index);
5063 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
5064 task->data_count);
5065 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
John Soni Jose340c99e2015-08-20 04:44:30 +05305066 io_task->pwrb_handle->wrb_index);
5067 if (io_task->pwrb_context->plast_wrb)
5068 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb,
5069 io_task->pwrb_context->plast_wrb,
5070 io_task->pwrb_handle->wrb_index);
5071 io_task->pwrb_context->plast_wrb = pwrb;
5072
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005073 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05305074 }
5075
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05305076
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305077 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
5078 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305079 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05305080 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005081 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305082 break;
5083 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07005084 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05305085 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005086 if (is_chip_be2_be3r(phba))
5087 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05305088 dmsg, pwrb, 1);
5089 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005090 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05305091 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07005092 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05305093 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005094 if (is_chip_be2_be3r(phba))
5095 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05305096 dmsg, pwrb, 0);
5097 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005098 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05305099 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07005100 }
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005101 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305102 break;
5103 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05305104 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005105 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305106 break;
5107 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05305108 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005109 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305110 break;
5111 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05305112 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005113 ret = hwi_write_buffer(pwrb, task);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305114 break;
5115
5116 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05305117 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5118 "BM_%d : opcode =%d Not supported\n",
5119 task->hdr->opcode & ISCSI_OPCODE_MASK);
5120
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305121 return -EINVAL;
5122 }
5123
Alexey Khoroshilove0493622016-01-16 00:45:34 +03005124 if (ret)
5125 return ret;
5126
John Soni Jose09a10932012-10-20 04:44:23 +05305127 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07005128 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
5129 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
5130 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305131
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305132 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05305133 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305134 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
5135 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
Jayamohan Kallickal1e4be6f2013-09-28 15:35:50 -07005136 iowrite32(doorbell, phba->db_va +
5137 beiscsi_conn->doorbell_offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305138 return 0;
5139}
5140
5141static int beiscsi_task_xmit(struct iscsi_task *task)
5142{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305143 struct beiscsi_io_task *io_task = task->dd_data;
5144 struct scsi_cmnd *sc = task->sc;
Jitendra Bhivare18683792016-02-04 15:49:18 +05305145 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305146 struct scatterlist *sg;
5147 int num_sg;
5148 unsigned int writedir = 0, xferlen = 0;
5149
Jitendra Bhivare18683792016-02-04 15:49:18 +05305150 if (!io_task->conn->login_in_progress)
5151 task->hdr->exp_statsn = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05305152
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305153 if (!sc)
5154 return beiscsi_mtask(task);
5155
5156 io_task->scsi_cmnd = sc;
5157 num_sg = scsi_dma_map(sc);
Jitendra Bhivare18683792016-02-04 15:49:18 +05305158 phba = io_task->conn->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305159 if (num_sg < 0) {
Jayamohan Kallickalafb96052013-09-28 15:35:55 -07005160 beiscsi_log(phba, KERN_ERR,
5161 BEISCSI_LOG_IO | BEISCSI_LOG_ISCSI,
5162 "BM_%d : scsi_dma_map Failed "
5163 "Driver_ITT : 0x%x ITT : 0x%x Xferlen : 0x%x\n",
5164 be32_to_cpu(io_task->cmd_bhs->iscsi_hdr.itt),
5165 io_task->libiscsi_itt, scsi_bufflen(sc));
John Soni Jose99bc5d52012-08-20 23:00:18 +05305166
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305167 return num_sg;
5168 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305169 xferlen = scsi_bufflen(sc);
5170 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05305171 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305172 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05305173 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305174 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05305175
John Soni Jose09a10932012-10-20 04:44:23 +05305176 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305177}
5178
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005179/**
5180 * beiscsi_bsg_request - handle bsg request from ISCSI transport
5181 * @job: job to handle
5182 */
5183static int beiscsi_bsg_request(struct bsg_job *job)
5184{
5185 struct Scsi_Host *shost;
5186 struct beiscsi_hba *phba;
5187 struct iscsi_bsg_request *bsg_req = job->request;
5188 int rc = -EINVAL;
5189 unsigned int tag;
5190 struct be_dma_mem nonemb_cmd;
5191 struct be_cmd_resp_hdr *resp;
5192 struct iscsi_bsg_reply *bsg_reply = job->reply;
5193 unsigned short status, extd_status;
5194
5195 shost = iscsi_job_to_shost(job);
5196 phba = iscsi_host_priv(shost);
5197
5198 switch (bsg_req->msgcode) {
5199 case ISCSI_BSG_HST_VENDOR:
5200 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
5201 job->request_payload.payload_len,
5202 &nonemb_cmd.dma);
5203 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305204 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5205 "BM_%d : Failed to allocate memory for "
5206 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05305207 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005208 }
5209 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
5210 &nonemb_cmd);
5211 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305212 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05305213 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05305214
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005215 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
5216 nonemb_cmd.va, nonemb_cmd.dma);
5217 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05305218 }
5219
5220 rc = wait_event_interruptible_timeout(
5221 phba->ctrl.mcc_wait[tag],
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305222 phba->ctrl.mcc_tag_status[tag],
John Soni Josee175def2012-10-20 04:45:40 +05305223 msecs_to_jiffies(
5224 BEISCSI_HOST_MBX_TIMEOUT));
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305225 extd_status = (phba->ctrl.mcc_tag_status[tag] &
5226 CQE_STATUS_ADDL_MASK) >> CQE_STATUS_ADDL_SHIFT;
5227 status = phba->ctrl.mcc_tag_status[tag] & CQE_STATUS_MASK;
Jitendra Bhivare090e2182016-02-04 15:49:17 +05305228 free_mcc_wrb(&phba->ctrl, tag);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005229 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
5230 sg_copy_from_buffer(job->reply_payload.sg_list,
5231 job->reply_payload.sg_cnt,
5232 nonemb_cmd.va, (resp->response_length
5233 + sizeof(*resp)));
5234 bsg_reply->reply_payload_rcv_len = resp->response_length;
5235 bsg_reply->result = status;
5236 bsg_job_done(job, bsg_reply->result,
5237 bsg_reply->reply_payload_rcv_len);
5238 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
5239 nonemb_cmd.va, nonemb_cmd.dma);
5240 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305241 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05305242 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05305243 " status = %d extd_status = %d\n",
5244 status, extd_status);
5245
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005246 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05305247 } else {
5248 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005249 }
5250 break;
5251
5252 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05305253 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
5254 "BM_%d : Unsupported bsg command: 0x%x\n",
5255 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005256 break;
5257 }
5258
5259 return rc;
5260}
5261
John Soni Jose99bc5d52012-08-20 23:00:18 +05305262void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
5263{
5264 /* Set the logging parameter */
5265 beiscsi_log_enable_init(phba, beiscsi_log_enable);
5266}
5267
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305268/*
5269 * beiscsi_quiesce()- Cleanup Driver resources
5270 * @phba: Instance Priv structure
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005271 * @unload_state:i Clean or EEH unload state
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305272 *
5273 * Free the OS and HW resources held by the driver
5274 **/
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005275static void beiscsi_quiesce(struct beiscsi_hba *phba,
5276 uint32_t unload_state)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305277{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305278 struct hwi_controller *phwi_ctrlr;
5279 struct hwi_context_memory *phwi_context;
5280 struct be_eq_obj *pbe_eq;
5281 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305282
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305283 phwi_ctrlr = phba->phwi_ctrlr;
5284 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305285 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305286 if (phba->msix_enabled) {
5287 for (i = 0; i <= phba->num_cpus; i++) {
5288 msix_vec = phba->msix_entries[i].vector;
5289 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07005290 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305291 }
5292 } else
Lars-Peter Clausene729b502016-03-04 11:15:06 +01005293 if (phba->pcidev->irq)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305294 free_irq(phba->pcidev->irq, phba);
5295 pci_disable_msix(phba->pcidev);
John Soni Jose53281ed2014-09-26 15:13:55 -04005296 cancel_delayed_work_sync(&phba->beiscsi_hw_check_task);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005297
Jens Axboe89f8b332014-03-13 09:38:42 -06005298 for (i = 0; i < phba->num_cpus; i++) {
5299 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005300 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005301 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305302
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005303 if (unload_state == BEISCSI_CLEAN_UNLOAD) {
5304 destroy_workqueue(phba->wq);
5305 beiscsi_clean_port(phba);
5306 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05305307
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005308 beiscsi_unmap_pci_function(phba);
5309 pci_free_consistent(phba->pcidev,
5310 phba->ctrl.mbox_mem_alloced.size,
5311 phba->ctrl.mbox_mem_alloced.va,
5312 phba->ctrl.mbox_mem_alloced.dma);
5313 } else {
5314 hwi_purge_eq(phba);
5315 hwi_cleanup(phba);
5316 }
John Soni Jose7a158002012-10-20 04:45:51 +05305317
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005318}
5319
5320static void beiscsi_remove(struct pci_dev *pcidev)
5321{
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005322 struct beiscsi_hba *phba = NULL;
5323
5324 phba = pci_get_drvdata(pcidev);
5325 if (!phba) {
5326 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
5327 return;
5328 }
5329
Mike Christie0e438952012-04-03 23:41:51 -05005330 beiscsi_destroy_def_ifaces(phba);
Mike Christie9d045162011-06-24 15:11:52 -05005331 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305332 iscsi_host_remove(phba->shost);
Jitendra Bhivarecdaa4de2016-01-20 14:10:51 +05305333 beiscsi_quiesce(phba, BEISCSI_CLEAN_UNLOAD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305334 pci_dev_put(phba->pcidev);
5335 iscsi_host_free(phba->shost);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005336 pci_disable_pcie_error_reporting(pcidev);
5337 pci_set_drvdata(pcidev, NULL);
John Soni Josee307f3a2015-04-25 08:17:19 +05305338 pci_release_regions(pcidev);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07005339 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305340}
5341
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305342static void beiscsi_msix_enable(struct beiscsi_hba *phba)
5343{
5344 int i, status;
5345
5346 for (i = 0; i <= phba->num_cpus; i++)
5347 phba->msix_entries[i].entry = i;
5348
Alexander Gordeeve149fc12014-08-18 08:01:48 +02005349 status = pci_enable_msix_range(phba->pcidev, phba->msix_entries,
5350 phba->num_cpus + 1, phba->num_cpus + 1);
5351 if (status > 0)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305352 phba->msix_enabled = true;
5353
5354 return;
5355}
5356
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005357static void be_eqd_update(struct beiscsi_hba *phba)
5358{
5359 struct be_set_eqd set_eqd[MAX_CPUS];
5360 struct be_aic_obj *aic;
5361 struct be_eq_obj *pbe_eq;
5362 struct hwi_controller *phwi_ctrlr;
5363 struct hwi_context_memory *phwi_context;
5364 int eqd, i, num = 0;
5365 ulong now;
5366 u32 pps, delta;
5367 unsigned int tag;
5368
5369 phwi_ctrlr = phba->phwi_ctrlr;
5370 phwi_context = phwi_ctrlr->phwi_ctxt;
5371
5372 for (i = 0; i <= phba->num_cpus; i++) {
5373 aic = &phba->aic_obj[i];
5374 pbe_eq = &phwi_context->be_eq[i];
5375 now = jiffies;
5376 if (!aic->jiffs || time_before(now, aic->jiffs) ||
5377 pbe_eq->cq_count < aic->eq_prev) {
5378 aic->jiffs = now;
5379 aic->eq_prev = pbe_eq->cq_count;
5380 continue;
5381 }
5382 delta = jiffies_to_msecs(now - aic->jiffs);
5383 pps = (((u32)(pbe_eq->cq_count - aic->eq_prev) * 1000) / delta);
5384 eqd = (pps / 1500) << 2;
5385
5386 if (eqd < 8)
5387 eqd = 0;
5388 eqd = min_t(u32, eqd, phwi_context->max_eqd);
5389 eqd = max_t(u32, eqd, phwi_context->min_eqd);
5390
5391 aic->jiffs = now;
5392 aic->eq_prev = pbe_eq->cq_count;
5393
5394 if (eqd != aic->prev_eqd) {
5395 set_eqd[num].delay_multiplier = (eqd * 65)/100;
5396 set_eqd[num].eq_id = pbe_eq->q.id;
5397 aic->prev_eqd = eqd;
5398 num++;
5399 }
5400 }
5401 if (num) {
5402 tag = be_cmd_modify_eq_delay(phba, set_eqd, num);
5403 if (tag)
Jitendra Bhivare88840332016-02-04 15:49:12 +05305404 beiscsi_mccq_compl_wait(phba, tag, NULL, NULL);
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005405 }
5406}
5407
Jayamohan Kallickala3d313e2014-08-08 01:00:00 -04005408static void be_check_boot_session(struct beiscsi_hba *phba)
5409{
5410 if (beiscsi_setup_boot_info(phba))
5411 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5412 "BM_%d : Could not set up "
5413 "iSCSI boot info on async event.\n");
5414}
5415
John Soni Jose7a158002012-10-20 04:45:51 +05305416/*
5417 * beiscsi_hw_health_check()- Check adapter health
5418 * @work: work item to check HW health
5419 *
5420 * Check if adapter in an unrecoverable state or not.
5421 **/
5422static void
5423beiscsi_hw_health_check(struct work_struct *work)
5424{
5425 struct beiscsi_hba *phba =
5426 container_of(work, struct beiscsi_hba,
5427 beiscsi_hw_check_task.work);
5428
Jayamohan Kallickal73af08e2014-05-05 21:41:26 -04005429 be_eqd_update(phba);
5430
Jayamohan Kallickala3d313e2014-08-08 01:00:00 -04005431 if (phba->state & BE_ADAPTER_CHECK_BOOT) {
John Soni Jose3efde862015-04-25 08:16:57 +05305432 if ((phba->get_boot > 0) && (!phba->boot_kset)) {
5433 phba->get_boot--;
5434 if (!(phba->get_boot % BE_GET_BOOT_TO))
5435 be_check_boot_session(phba);
5436 } else {
5437 phba->state &= ~BE_ADAPTER_CHECK_BOOT;
5438 phba->get_boot = 0;
5439 }
Jayamohan Kallickala3d313e2014-08-08 01:00:00 -04005440 }
5441
John Soni Jose7a158002012-10-20 04:45:51 +05305442 beiscsi_ue_detect(phba);
5443
5444 schedule_delayed_work(&phba->beiscsi_hw_check_task,
5445 msecs_to_jiffies(1000));
5446}
5447
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005448
5449static pci_ers_result_t beiscsi_eeh_err_detected(struct pci_dev *pdev,
5450 pci_channel_state_t state)
5451{
5452 struct beiscsi_hba *phba = NULL;
5453
5454 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5455 phba->state |= BE_ADAPTER_PCI_ERR;
5456
5457 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5458 "BM_%d : EEH error detected\n");
5459
5460 beiscsi_quiesce(phba, BEISCSI_EEH_UNLOAD);
5461
5462 if (state == pci_channel_io_perm_failure) {
5463 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5464 "BM_%d : EEH : State PERM Failure");
5465 return PCI_ERS_RESULT_DISCONNECT;
5466 }
5467
5468 pci_disable_device(pdev);
5469
5470 /* The error could cause the FW to trigger a flash debug dump.
5471 * Resetting the card while flash dump is in progress
5472 * can cause it not to recover; wait for it to finish.
5473 * Wait only for first function as it is needed only once per
5474 * adapter.
5475 **/
5476 if (pdev->devfn == 0)
5477 ssleep(30);
5478
5479 return PCI_ERS_RESULT_NEED_RESET;
5480}
5481
5482static pci_ers_result_t beiscsi_eeh_reset(struct pci_dev *pdev)
5483{
5484 struct beiscsi_hba *phba = NULL;
5485 int status = 0;
5486
5487 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5488
5489 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5490 "BM_%d : EEH Reset\n");
5491
5492 status = pci_enable_device(pdev);
5493 if (status)
5494 return PCI_ERS_RESULT_DISCONNECT;
5495
5496 pci_set_master(pdev);
5497 pci_set_power_state(pdev, PCI_D0);
5498 pci_restore_state(pdev);
5499
5500 /* Wait for the CHIP Reset to complete */
5501 status = be_chk_reset_complete(phba);
5502 if (!status) {
5503 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5504 "BM_%d : EEH Reset Completed\n");
5505 } else {
5506 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5507 "BM_%d : EEH Reset Completion Failure\n");
5508 return PCI_ERS_RESULT_DISCONNECT;
5509 }
5510
5511 pci_cleanup_aer_uncorrect_error_status(pdev);
5512 return PCI_ERS_RESULT_RECOVERED;
5513}
5514
5515static void beiscsi_eeh_resume(struct pci_dev *pdev)
5516{
5517 int ret = 0, i;
5518 struct be_eq_obj *pbe_eq;
5519 struct beiscsi_hba *phba = NULL;
5520 struct hwi_controller *phwi_ctrlr;
5521 struct hwi_context_memory *phwi_context;
5522
5523 phba = (struct beiscsi_hba *)pci_get_drvdata(pdev);
5524 pci_save_state(pdev);
5525
5526 if (enable_msix)
5527 find_num_cpus(phba);
5528 else
5529 phba->num_cpus = 1;
5530
5531 if (enable_msix) {
5532 beiscsi_msix_enable(phba);
5533 if (!phba->msix_enabled)
5534 phba->num_cpus = 1;
5535 }
5536
5537 ret = beiscsi_cmd_reset_function(phba);
5538 if (ret) {
5539 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5540 "BM_%d : Reset Failed\n");
5541 goto ret_err;
5542 }
5543
5544 ret = be_chk_reset_complete(phba);
5545 if (ret) {
5546 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5547 "BM_%d : Failed to get out of reset.\n");
5548 goto ret_err;
5549 }
5550
5551 beiscsi_get_params(phba);
5552 phba->shost->max_id = phba->params.cxns_per_ctrl;
5553 phba->shost->can_queue = phba->params.ios_per_ctrl;
5554 ret = hwi_init_controller(phba);
Nicholas Krausec6fff322016-02-27 12:43:25 -05005555 if (ret) {
5556 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5557 "BM_%d : beiscsi_eeh_resume -"
5558 "Failed to initialize beiscsi_hba.\n");
5559 goto ret_err;
5560 }
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005561
5562 for (i = 0; i < MAX_MCC_CMD; i++) {
5563 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5564 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305565 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005566 phba->ctrl.mcc_tag_available++;
5567 }
5568
5569 phwi_ctrlr = phba->phwi_ctrlr;
5570 phwi_context = phwi_ctrlr->phwi_ctxt;
5571
Jens Axboe89f8b332014-03-13 09:38:42 -06005572 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005573 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005574 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005575 be_iopoll);
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005576 }
5577
Jens Axboe89f8b332014-03-13 09:38:42 -06005578 i = (phba->msix_enabled) ? i : 0;
5579 /* Work item for MCC handling */
5580 pbe_eq = &phwi_context->be_eq[i];
5581 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
5582
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005583 ret = beiscsi_init_irqs(phba);
5584 if (ret < 0) {
5585 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5586 "BM_%d : beiscsi_eeh_resume - "
5587 "Failed to beiscsi_init_irqs\n");
5588 goto ret_err;
5589 }
5590
5591 hwi_enable_intr(phba);
5592 phba->state &= ~BE_ADAPTER_PCI_ERR;
5593
5594 return;
5595ret_err:
5596 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5597 "BM_%d : AER EEH Resume Failed\n");
5598}
5599
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08005600static int beiscsi_dev_probe(struct pci_dev *pcidev,
5601 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305602{
5603 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305604 struct hwi_controller *phwi_ctrlr;
5605 struct hwi_context_memory *phwi_context;
5606 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005607 int ret = 0, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305608
5609 ret = beiscsi_enable_pci(pcidev);
5610 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305611 dev_err(&pcidev->dev,
5612 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305613 return ret;
5614 }
5615
5616 phba = beiscsi_hba_alloc(pcidev);
5617 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305618 dev_err(&pcidev->dev,
5619 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305620 goto disable_pci;
5621 }
5622
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005623 /* Enable EEH reporting */
5624 ret = pci_enable_pcie_error_reporting(pcidev);
5625 if (ret)
5626 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
5627 "BM_%d : PCIe Error Reporting "
5628 "Enabling Failed\n");
5629
5630 pci_save_state(pcidev);
5631
John Soni Jose99bc5d52012-08-20 23:00:18 +05305632 /* Initialize Driver configuration Paramters */
5633 beiscsi_hba_attrs_init(phba);
5634
John Soni Josee175def2012-10-20 04:45:40 +05305635 phba->fw_timeout = false;
Jayamohan Kallickal6c831852013-09-28 15:35:40 -07005636 phba->mac_addr_set = false;
John Soni Josee175def2012-10-20 04:45:40 +05305637
5638
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305639 switch (pcidev->device) {
5640 case BE_DEVICE_ID1:
5641 case OC_DEVICE_ID1:
5642 case OC_DEVICE_ID2:
5643 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05305644 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305645 break;
5646 case BE_DEVICE_ID2:
5647 case OC_DEVICE_ID3:
5648 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05305649 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305650 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05305651 case OC_SKH_ID1:
5652 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05305653 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07005654 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05305655 default:
5656 phba->generation = 0;
5657 }
5658
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305659 ret = be_ctrl_init(phba, pcidev);
5660 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305661 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5662 "BM_%d : beiscsi_dev_probe-"
5663 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305664 goto hba_free;
5665 }
5666
Jitendra Bhivarecdaa4de2016-01-20 14:10:51 +05305667 /*
5668 * FUNCTION_RESET should clean up any stale info in FW for this fn
5669 */
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305670 ret = beiscsi_cmd_reset_function(phba);
5671 if (ret) {
5672 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal92665a62013-09-28 15:35:43 -07005673 "BM_%d : Reset Failed\n");
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305674 goto hba_free;
5675 }
5676 ret = be_chk_reset_complete(phba);
5677 if (ret) {
5678 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
Jayamohan Kallickal92665a62013-09-28 15:35:43 -07005679 "BM_%d : Failed to get out of reset.\n");
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05305680 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05305681 }
5682
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305683 spin_lock_init(&phba->io_sgl_lock);
5684 spin_lock_init(&phba->mgmt_sgl_lock);
5685 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal8f09a3b2013-09-28 15:35:42 -07005686 spin_lock_init(&phba->async_pdu_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305687 ret = mgmt_get_fw_config(&phba->ctrl, phba);
5688 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305689 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5690 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05305691 goto free_port;
5692 }
Jitendra Bhivare53aefe22016-01-20 14:10:53 +05305693 mgmt_get_port_name(&phba->ctrl, phba);
Jitendra Bhivare4570f162016-01-20 14:10:54 +05305694 beiscsi_get_params(phba);
Jayamohan Kallickal68c26a32013-09-28 15:35:54 -07005695
5696 if (enable_msix)
5697 find_num_cpus(phba);
5698 else
5699 phba->num_cpus = 1;
5700
5701 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5702 "BM_%d : num_cpus = %d\n",
5703 phba->num_cpus);
5704
5705 if (enable_msix) {
5706 beiscsi_msix_enable(phba);
5707 if (!phba->msix_enabled)
5708 phba->num_cpus = 1;
5709 }
5710
Jayamohan Kallickal843ae752013-09-28 15:35:44 -07005711 phba->shost->max_id = phba->params.cxns_per_ctrl;
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05305712 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305713 ret = beiscsi_init_port(phba);
5714 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305715 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5716 "BM_%d : beiscsi_dev_probe-"
5717 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305718 goto free_port;
5719 }
5720
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005721 for (i = 0; i < MAX_MCC_CMD; i++) {
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305722 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
5723 phba->ctrl.mcc_tag[i] = i + 1;
Jitendra Bhivare67296ad2016-02-04 15:49:10 +05305724 phba->ctrl.mcc_tag_status[i + 1] = 0;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305725 phba->ctrl.mcc_tag_available++;
Jayamohan Kallickal1957aa72014-01-29 02:16:39 -05005726 memset(&phba->ctrl.ptag_state[i].tag_mem_state, 0,
Jayamohan Kallickal8fc01ea2014-05-05 21:41:28 -04005727 sizeof(struct be_dma_mem));
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05305728 }
5729
5730 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
5731
John Soni Jose72fb46a2012-10-20 04:42:49 +05305732 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305733 phba->shost->host_no);
Kees Cookd8537542013-07-03 15:04:57 -07005734 phba->wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 1, phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305735 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305736 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5737 "BM_%d : beiscsi_dev_probe-"
5738 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305739 goto free_twq;
5740 }
5741
John Soni Jose7a158002012-10-20 04:45:51 +05305742 INIT_DELAYED_WORK(&phba->beiscsi_hw_check_task,
5743 beiscsi_hw_health_check);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305744
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305745 phwi_ctrlr = phba->phwi_ctrlr;
5746 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05305747
Jens Axboe89f8b332014-03-13 09:38:42 -06005748 for (i = 0; i < phba->num_cpus; i++) {
John Soni Jose72fb46a2012-10-20 04:42:49 +05305749 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005750 irq_poll_init(&pbe_eq->iopoll, be_iopoll_budget,
Jens Axboe89f8b332014-03-13 09:38:42 -06005751 be_iopoll);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305752 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05305753
Jens Axboe89f8b332014-03-13 09:38:42 -06005754 i = (phba->msix_enabled) ? i : 0;
5755 /* Work item for MCC handling */
5756 pbe_eq = &phwi_context->be_eq[i];
5757 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
5758
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305759 ret = beiscsi_init_irqs(phba);
5760 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305761 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5762 "BM_%d : beiscsi_dev_probe-"
5763 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305764 goto free_blkenbld;
5765 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305766 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05005767
Jayamohan Kallickal0598b8a2014-05-05 21:41:25 -04005768 if (iscsi_host_add(phba->shost, &phba->pcidev->dev))
5769 goto free_blkenbld;
5770
Mike Christief457a462011-06-24 15:11:53 -05005771 if (beiscsi_setup_boot_info(phba))
5772 /*
5773 * log error but continue, because we may not be using
5774 * iscsi boot.
5775 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05305776 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
5777 "BM_%d : Could not set up "
5778 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05005779
Mike Christie0e438952012-04-03 23:41:51 -05005780 beiscsi_create_def_ifaces(phba);
John Soni Jose7a158002012-10-20 04:45:51 +05305781 schedule_delayed_work(&phba->beiscsi_hw_check_task,
5782 msecs_to_jiffies(1000));
5783
John Soni Jose99bc5d52012-08-20 23:00:18 +05305784 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5785 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305786 return 0;
5787
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305788free_blkenbld:
5789 destroy_workqueue(phba->wq);
Jens Axboe89f8b332014-03-13 09:38:42 -06005790 for (i = 0; i < phba->num_cpus; i++) {
5791 pbe_eq = &phwi_context->be_eq[i];
Christoph Hellwig511cbce2015-11-10 14:56:14 +01005792 irq_poll_disable(&pbe_eq->iopoll);
Jens Axboe89f8b332014-03-13 09:38:42 -06005793 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305794free_twq:
5795 beiscsi_clean_port(phba);
5796 beiscsi_free_mem(phba);
5797free_port:
5798 pci_free_consistent(phba->pcidev,
5799 phba->ctrl.mbox_mem_alloced.size,
5800 phba->ctrl.mbox_mem_alloced.va,
5801 phba->ctrl.mbox_mem_alloced.dma);
5802 beiscsi_unmap_pci_function(phba);
5803hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305804 if (phba->msix_enabled)
5805 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305806 pci_dev_put(phba->pcidev);
5807 iscsi_host_free(phba->shost);
John Soni Jose2e7cee02015-02-12 06:45:47 +05305808 pci_set_drvdata(pcidev, NULL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305809disable_pci:
John Soni Josee307f3a2015-04-25 08:17:19 +05305810 pci_release_regions(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305811 pci_disable_device(pcidev);
5812 return ret;
5813}
5814
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005815static struct pci_error_handlers beiscsi_eeh_handlers = {
5816 .error_detected = beiscsi_eeh_err_detected,
5817 .slot_reset = beiscsi_eeh_reset,
5818 .resume = beiscsi_eeh_resume,
5819};
5820
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305821struct iscsi_transport beiscsi_iscsi_transport = {
5822 .owner = THIS_MODULE,
5823 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305824 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305825 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305826 .create_session = beiscsi_session_create,
5827 .destroy_session = beiscsi_session_destroy,
5828 .create_conn = beiscsi_conn_create,
5829 .bind_conn = beiscsi_conn_bind,
5830 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05005831 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05005832 .set_iface_param = be2iscsi_iface_set_param,
5833 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305834 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005835 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305836 .get_session_param = iscsi_session_get_param,
5837 .get_host_param = beiscsi_get_host_param,
5838 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005839 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305840 .send_pdu = iscsi_conn_send_pdu,
5841 .xmit_task = beiscsi_task_xmit,
5842 .cleanup_task = beiscsi_cleanup_task,
5843 .alloc_pdu = beiscsi_alloc_pdu,
5844 .parse_pdu_itt = beiscsi_parse_pdu,
5845 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005846 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305847 .ep_connect = beiscsi_ep_connect,
5848 .ep_poll = beiscsi_ep_poll,
5849 .ep_disconnect = beiscsi_ep_disconnect,
5850 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005851 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305852};
5853
5854static struct pci_driver beiscsi_pci_driver = {
5855 .name = DRV_NAME,
5856 .probe = beiscsi_dev_probe,
5857 .remove = beiscsi_remove,
Jayamohan Kallickal3567f362013-09-28 15:35:58 -07005858 .id_table = beiscsi_pci_id_table,
5859 .err_handler = &beiscsi_eeh_handlers
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305860};
5861
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305862
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305863static int __init beiscsi_module_init(void)
5864{
5865 int ret;
5866
5867 beiscsi_scsi_transport =
5868 iscsi_register_transport(&beiscsi_iscsi_transport);
5869 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305870 printk(KERN_ERR
5871 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305872 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305873 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305874 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5875 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305876
5877 ret = pci_register_driver(&beiscsi_pci_driver);
5878 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305879 printk(KERN_ERR
5880 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305881 goto unregister_iscsi_transport;
5882 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305883 return 0;
5884
5885unregister_iscsi_transport:
5886 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5887 return ret;
5888}
5889
5890static void __exit beiscsi_module_exit(void)
5891{
5892 pci_unregister_driver(&beiscsi_pci_driver);
5893 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5894}
5895
5896module_init(beiscsi_module_init);
5897module_exit(beiscsi_module_exit);