blob: adc662dc4ac883a7a65995dab45fc48c2f49dd1a [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
John Soni Jose5faf17b2012-10-20 04:45:27 +05302 * Copyright (C) 2005 - 2012 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 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070010 * Written by: Jayamohan Kallickal (jayamohan.kallickal@emulex.com)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053011 *
12 * Contact Information:
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070013 * linux-drivers@emulex.com
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053014 *
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -070015 * Emulex
16 * 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 Gortmakeracf33682011-05-27 09:47:43 -040030#include <linux/module.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050031#include <linux/bsg-lib.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053032
33#include <scsi/libiscsi.h>
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -050034#include <scsi/scsi_bsg_iscsi.h>
35#include <scsi/scsi_netlink.h>
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053036#include <scsi/scsi_transport_iscsi.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_cmnd.h>
39#include <scsi/scsi_device.h>
40#include <scsi/scsi_host.h>
41#include <scsi/scsi.h>
42#include "be_main.h"
43#include "be_iscsi.h"
44#include "be_mgmt.h"
John Soni Jose0a513dd2012-08-20 23:00:55 +053045#include "be_cmds.h"
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053046
47static unsigned int be_iopoll_budget = 10;
48static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053049static unsigned int enable_msix = 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053050
51MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
52MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050053MODULE_VERSION(BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050054MODULE_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"
152 "\t\t\t\tConfiguration Path : 0x20\n");
153
John Soni Jose5cac7592012-10-20 04:42:25 +0530154DEVICE_ATTR(beiscsi_drvr_ver, S_IRUGO, beiscsi_drvr_ver_disp, NULL);
John Soni Jose26000db2012-10-20 04:45:06 +0530155DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700156DEVICE_ATTR(beiscsi_fw_ver, S_IRUGO, beiscsi_fw_ver_disp, NULL);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530157struct device_attribute *beiscsi_attrs[] = {
158 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530159 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530160 &dev_attr_beiscsi_adapter_family,
Jayamohan Kallickal22661e22013-04-05 20:38:28 -0700161 &dev_attr_beiscsi_fw_ver,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530162 NULL,
163};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530164
John Soni Jose6763daa2012-10-20 04:41:45 +0530165static char const *cqe_desc[] = {
166 "RESERVED_DESC",
167 "SOL_CMD_COMPLETE",
168 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
169 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
170 "CXN_KILLED_BURST_LEN_MISMATCH",
171 "CXN_KILLED_AHS_RCVD",
172 "CXN_KILLED_HDR_DIGEST_ERR",
173 "CXN_KILLED_UNKNOWN_HDR",
174 "CXN_KILLED_STALE_ITT_TTT_RCVD",
175 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
176 "CXN_KILLED_RST_RCVD",
177 "CXN_KILLED_TIMED_OUT",
178 "CXN_KILLED_RST_SENT",
179 "CXN_KILLED_FIN_RCVD",
180 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
181 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
182 "CXN_KILLED_OVER_RUN_RESIDUAL",
183 "CXN_KILLED_UNDER_RUN_RESIDUAL",
184 "CMD_KILLED_INVALID_STATSN_RCVD",
185 "CMD_KILLED_INVALID_R2T_RCVD",
186 "CMD_CXN_KILLED_LUN_INVALID",
187 "CMD_CXN_KILLED_ICD_INVALID",
188 "CMD_CXN_KILLED_ITT_INVALID",
189 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
190 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
191 "CXN_INVALIDATE_NOTIFY",
192 "CXN_INVALIDATE_INDEX_NOTIFY",
193 "CMD_INVALIDATED_NOTIFY",
194 "UNSOL_HDR_NOTIFY",
195 "UNSOL_DATA_NOTIFY",
196 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
197 "DRIVERMSG_NOTIFY",
198 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
199 "SOL_CMD_KILLED_DIF_ERR",
200 "CXN_KILLED_SYN_RCVD",
201 "CXN_KILLED_IMM_DATA_RCVD"
202};
203
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530204static int beiscsi_slave_configure(struct scsi_device *sdev)
205{
206 blk_queue_max_segment_size(sdev->request_queue, 65536);
207 return 0;
208}
209
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530210static int beiscsi_eh_abort(struct scsi_cmnd *sc)
211{
212 struct iscsi_cls_session *cls_session;
213 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
214 struct beiscsi_io_task *aborted_io_task;
215 struct iscsi_conn *conn;
216 struct beiscsi_conn *beiscsi_conn;
217 struct beiscsi_hba *phba;
218 struct iscsi_session *session;
219 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530220 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530221 unsigned int cid, tag, num_invalidate;
222
223 cls_session = starget_to_session(scsi_target(sc->device));
224 session = cls_session->dd_data;
225
226 spin_lock_bh(&session->lock);
227 if (!aborted_task || !aborted_task->sc) {
228 /* we raced */
229 spin_unlock_bh(&session->lock);
230 return SUCCESS;
231 }
232
233 aborted_io_task = aborted_task->dd_data;
234 if (!aborted_io_task->scsi_cmnd) {
235 /* raced or invalid command */
236 spin_unlock_bh(&session->lock);
237 return SUCCESS;
238 }
239 spin_unlock_bh(&session->lock);
240 conn = aborted_task->conn;
241 beiscsi_conn = conn->dd_data;
242 phba = beiscsi_conn->phba;
243
244 /* invalidate iocb */
245 cid = beiscsi_conn->beiscsi_conn_cid;
246 inv_tbl = phba->inv_tbl;
247 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
248 inv_tbl->cid = cid;
249 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
250 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530251 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
252 sizeof(struct invalidate_commands_params_in),
253 &nonemb_cmd.dma);
254 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530255 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
256 "BM_%d : Failed to allocate memory for"
257 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530258 return FAILED;
259 }
260 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
261
262 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
263 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530264 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530265 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
266 "BM_%d : mgmt_invalidate_icds could not be"
267 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530268 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
269 nonemb_cmd.va, nonemb_cmd.dma);
270
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530271 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530272 }
John Soni Josee175def2012-10-20 04:45:40 +0530273
274 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530275 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
276 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530277 return iscsi_eh_abort(sc);
278}
279
280static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
281{
282 struct iscsi_task *abrt_task;
283 struct beiscsi_io_task *abrt_io_task;
284 struct iscsi_conn *conn;
285 struct beiscsi_conn *beiscsi_conn;
286 struct beiscsi_hba *phba;
287 struct iscsi_session *session;
288 struct iscsi_cls_session *cls_session;
289 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530290 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530291 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530292
293 /* invalidate iocbs */
294 cls_session = starget_to_session(scsi_target(sc->device));
295 session = cls_session->dd_data;
296 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500297 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
298 spin_unlock_bh(&session->lock);
299 return FAILED;
300 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530301 conn = session->leadconn;
302 beiscsi_conn = conn->dd_data;
303 phba = beiscsi_conn->phba;
304 cid = beiscsi_conn->beiscsi_conn_cid;
305 inv_tbl = phba->inv_tbl;
306 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
307 num_invalidate = 0;
308 for (i = 0; i < conn->session->cmds_max; i++) {
309 abrt_task = conn->session->cmds[i];
310 abrt_io_task = abrt_task->dd_data;
311 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
312 continue;
313
314 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
315 continue;
316
317 inv_tbl->cid = cid;
318 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
319 num_invalidate++;
320 inv_tbl++;
321 }
322 spin_unlock_bh(&session->lock);
323 inv_tbl = phba->inv_tbl;
324
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530325 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
326 sizeof(struct invalidate_commands_params_in),
327 &nonemb_cmd.dma);
328 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530329 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
330 "BM_%d : Failed to allocate memory for"
331 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530332 return FAILED;
333 }
334 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
335 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
336 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
337 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530338 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530339 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
340 "BM_%d : mgmt_invalidate_icds could not be"
341 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530342 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
343 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530344 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530345 }
John Soni Josee175def2012-10-20 04:45:40 +0530346
347 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530348 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
349 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530350 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530351}
352
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530353static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
354{
355 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500356 struct mgmt_session_info *boot_sess = &phba->boot_sess;
357 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530358 char *str = buf;
359 int rc;
360
361 switch (type) {
362 case ISCSI_BOOT_TGT_NAME:
363 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500364 (int)strlen(boot_sess->target_name),
365 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530366 break;
367 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500368 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530369 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500370 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530371 else
372 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500373 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530374 break;
375 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500376 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530377 break;
378
379 case ISCSI_BOOT_TGT_CHAP_NAME:
380 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500381 boot_conn->negotiated_login_options.auth_data.chap.
382 target_chap_name_length,
383 (char *)&boot_conn->negotiated_login_options.
384 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530385 break;
386 case ISCSI_BOOT_TGT_CHAP_SECRET:
387 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500388 boot_conn->negotiated_login_options.auth_data.chap.
389 target_secret_length,
390 (char *)&boot_conn->negotiated_login_options.
391 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530392 break;
393 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
394 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500395 boot_conn->negotiated_login_options.auth_data.chap.
396 intr_chap_name_length,
397 (char *)&boot_conn->negotiated_login_options.
398 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530399 break;
400 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500401 rc = sprintf(str, "%.*s\n",
402 boot_conn->negotiated_login_options.auth_data.chap.
403 intr_secret_length,
404 (char *)&boot_conn->negotiated_login_options.
405 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530406 break;
407 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500408 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530409 break;
410 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500411 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530412 break;
413 default:
414 rc = -ENOSYS;
415 break;
416 }
417 return rc;
418}
419
420static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
421{
422 struct beiscsi_hba *phba = data;
423 char *str = buf;
424 int rc;
425
426 switch (type) {
427 case ISCSI_BOOT_INI_INITIATOR_NAME:
428 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
429 break;
430 default:
431 rc = -ENOSYS;
432 break;
433 }
434 return rc;
435}
436
437static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
438{
439 struct beiscsi_hba *phba = data;
440 char *str = buf;
441 int rc;
442
443 switch (type) {
444 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500445 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530446 break;
447 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500448 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530449 break;
450 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500451 rc = beiscsi_get_macaddr(str, phba);
452 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530453 default:
454 rc = -ENOSYS;
455 break;
456 }
457 return rc;
458}
459
460
Al Viro587a1f12011-07-23 23:11:19 -0400461static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530462{
Al Viro587a1f12011-07-23 23:11:19 -0400463 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464
465 switch (type) {
466 case ISCSI_BOOT_TGT_NAME:
467 case ISCSI_BOOT_TGT_IP_ADDR:
468 case ISCSI_BOOT_TGT_PORT:
469 case ISCSI_BOOT_TGT_CHAP_NAME:
470 case ISCSI_BOOT_TGT_CHAP_SECRET:
471 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
472 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
473 case ISCSI_BOOT_TGT_NIC_ASSOC:
474 case ISCSI_BOOT_TGT_FLAGS:
475 rc = S_IRUGO;
476 break;
477 default:
478 rc = 0;
479 break;
480 }
481 return rc;
482}
483
Al Viro587a1f12011-07-23 23:11:19 -0400484static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530485{
Al Viro587a1f12011-07-23 23:11:19 -0400486 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530487
488 switch (type) {
489 case ISCSI_BOOT_INI_INITIATOR_NAME:
490 rc = S_IRUGO;
491 break;
492 default:
493 rc = 0;
494 break;
495 }
496 return rc;
497}
498
499
Al Viro587a1f12011-07-23 23:11:19 -0400500static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530501{
Al Viro587a1f12011-07-23 23:11:19 -0400502 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530503
504 switch (type) {
505 case ISCSI_BOOT_ETH_FLAGS:
506 case ISCSI_BOOT_ETH_MAC:
507 case ISCSI_BOOT_ETH_INDEX:
508 rc = S_IRUGO;
509 break;
510 default:
511 rc = 0;
512 break;
513 }
514 return rc;
515}
516
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530517/*------------------- PCI Driver operations and data ----------------- */
518static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
519 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530520 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530521 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
522 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
523 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530524 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530525 { 0 }
526};
527MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
528
John Soni Jose99bc5d52012-08-20 23:00:18 +0530529
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530530static struct scsi_host_template beiscsi_sht = {
531 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500532 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530533 .proc_name = DRV_NAME,
534 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530535 .change_queue_depth = iscsi_change_queue_depth,
536 .slave_configure = beiscsi_slave_configure,
537 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530538 .eh_abort_handler = beiscsi_eh_abort,
539 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530540 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530541 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530542 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
543 .can_queue = BE2_IO_DEPTH,
544 .this_id = -1,
545 .max_sectors = BEISCSI_MAX_SECTORS,
546 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
547 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500548 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
549
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530550};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530551
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530552static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530553
554static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
555{
556 struct beiscsi_hba *phba;
557 struct Scsi_Host *shost;
558
559 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
560 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530561 dev_err(&pcidev->dev,
562 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530563 return NULL;
564 }
565 shost->dma_boundary = pcidev->dma_mask;
566 shost->max_id = BE2_MAX_SESSIONS;
567 shost->max_channel = 0;
568 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
569 shost->max_lun = BEISCSI_NUM_MAX_LUN;
570 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530571 phba = iscsi_host_priv(shost);
572 memset(phba, 0, sizeof(*phba));
573 phba->shost = shost;
574 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530575 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500576 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530577
578 if (iscsi_host_add(shost, &phba->pcidev->dev))
579 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530580
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530581 return phba;
582
583free_devices:
584 pci_dev_put(phba->pcidev);
585 iscsi_host_free(phba->shost);
586 return NULL;
587}
588
589static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
590{
591 if (phba->csr_va) {
592 iounmap(phba->csr_va);
593 phba->csr_va = NULL;
594 }
595 if (phba->db_va) {
596 iounmap(phba->db_va);
597 phba->db_va = NULL;
598 }
599 if (phba->pci_va) {
600 iounmap(phba->pci_va);
601 phba->pci_va = NULL;
602 }
603}
604
605static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
606 struct pci_dev *pcidev)
607{
608 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530609 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530610
611 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
612 pci_resource_len(pcidev, 2));
613 if (addr == NULL)
614 return -ENOMEM;
615 phba->ctrl.csr = addr;
616 phba->csr_va = addr;
617 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
618
619 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
620 if (addr == NULL)
621 goto pci_map_err;
622 phba->ctrl.db = addr;
623 phba->db_va = addr;
624 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
625
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530626 if (phba->generation == BE_GEN2)
627 pcicfg_reg = 1;
628 else
629 pcicfg_reg = 0;
630
631 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
632 pci_resource_len(pcidev, pcicfg_reg));
633
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530634 if (addr == NULL)
635 goto pci_map_err;
636 phba->ctrl.pcicfg = addr;
637 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530638 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530639 return 0;
640
641pci_map_err:
642 beiscsi_unmap_pci_function(phba);
643 return -ENOMEM;
644}
645
646static int beiscsi_enable_pci(struct pci_dev *pcidev)
647{
648 int ret;
649
650 ret = pci_enable_device(pcidev);
651 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530652 dev_err(&pcidev->dev,
653 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530654 return ret;
655 }
656
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530657 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530658 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
659 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
660 if (ret) {
661 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
662 pci_disable_device(pcidev);
663 return ret;
664 }
665 }
666 return 0;
667}
668
669static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
670{
671 struct be_ctrl_info *ctrl = &phba->ctrl;
672 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
673 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
674 int status = 0;
675
676 ctrl->pdev = pdev;
677 status = beiscsi_map_pci_bars(phba, pdev);
678 if (status)
679 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530680 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
681 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
682 mbox_mem_alloc->size,
683 &mbox_mem_alloc->dma);
684 if (!mbox_mem_alloc->va) {
685 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500686 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530687 }
688
689 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
690 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
691 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
692 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
693 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530694 spin_lock_init(&phba->ctrl.mcc_lock);
695 spin_lock_init(&phba->ctrl.mcc_cq_lock);
696
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530697 return status;
698}
699
700static void beiscsi_get_params(struct beiscsi_hba *phba)
701{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530702 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
703 - (phba->fw_config.iscsi_cid_count
704 + BE2_TMFS
705 + BE2_NOPOUT_REQ));
706 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530707 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700708 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530709 phba->params.num_sge_per_io = BE2_SGE;
710 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
711 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
712 phba->params.eq_timer = 64;
713 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530714 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
715 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530716 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
717 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530718 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
719 "BM_%d : phba->params.num_eq_entries=%d\n",
720 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530721 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530722 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
723 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530724 phba->params.wrbs_per_cxn = 256;
725}
726
727static void hwi_ring_eq_db(struct beiscsi_hba *phba,
728 unsigned int id, unsigned int clr_interrupt,
729 unsigned int num_processed,
730 unsigned char rearm, unsigned char event)
731{
732 u32 val = 0;
733 val |= id & DB_EQ_RING_ID_MASK;
734 if (rearm)
735 val |= 1 << DB_EQ_REARM_SHIFT;
736 if (clr_interrupt)
737 val |= 1 << DB_EQ_CLR_SHIFT;
738 if (event)
739 val |= 1 << DB_EQ_EVNT_SHIFT;
740 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
741 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
742}
743
744/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530745 * be_isr_mcc - The isr routine of the driver.
746 * @irq: Not used
747 * @dev_id: Pointer to host adapter structure
748 */
749static irqreturn_t be_isr_mcc(int irq, void *dev_id)
750{
751 struct beiscsi_hba *phba;
752 struct be_eq_entry *eqe = NULL;
753 struct be_queue_info *eq;
754 struct be_queue_info *mcc;
755 unsigned int num_eq_processed;
756 struct be_eq_obj *pbe_eq;
757 unsigned long flags;
758
759 pbe_eq = dev_id;
760 eq = &pbe_eq->q;
761 phba = pbe_eq->phba;
762 mcc = &phba->ctrl.mcc_obj.cq;
763 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530764
765 num_eq_processed = 0;
766
767 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
768 & EQE_VALID_MASK) {
769 if (((eqe->dw[offsetof(struct amap_eq_entry,
770 resource_id) / 32] &
771 EQE_RESID_MASK) >> 16) == mcc->id) {
772 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530773 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530774 spin_unlock_irqrestore(&phba->isr_lock, flags);
775 }
776 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
777 queue_tail_inc(eq);
778 eqe = queue_tail_node(eq);
779 num_eq_processed++;
780 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530781 if (pbe_eq->todo_mcc_cq)
782 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530783 if (num_eq_processed)
784 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
785
786 return IRQ_HANDLED;
787}
788
789/**
790 * be_isr_msix - The isr routine of the driver.
791 * @irq: Not used
792 * @dev_id: Pointer to host adapter structure
793 */
794static irqreturn_t be_isr_msix(int irq, void *dev_id)
795{
796 struct beiscsi_hba *phba;
797 struct be_eq_entry *eqe = NULL;
798 struct be_queue_info *eq;
799 struct be_queue_info *cq;
800 unsigned int num_eq_processed;
801 struct be_eq_obj *pbe_eq;
802 unsigned long flags;
803
804 pbe_eq = dev_id;
805 eq = &pbe_eq->q;
806 cq = pbe_eq->cq;
807 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530808
809 phba = pbe_eq->phba;
810 num_eq_processed = 0;
811 if (blk_iopoll_enabled) {
812 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
813 & EQE_VALID_MASK) {
814 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
815 blk_iopoll_sched(&pbe_eq->iopoll);
816
817 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
818 queue_tail_inc(eq);
819 eqe = queue_tail_node(eq);
820 num_eq_processed++;
821 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530822 } else {
823 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
824 & EQE_VALID_MASK) {
825 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530826 pbe_eq->todo_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530827 spin_unlock_irqrestore(&phba->isr_lock, flags);
828 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
829 queue_tail_inc(eq);
830 eqe = queue_tail_node(eq);
831 num_eq_processed++;
832 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530833
John Soni Jose72fb46a2012-10-20 04:42:49 +0530834 if (pbe_eq->todo_cq)
835 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530836 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530837
838 if (num_eq_processed)
839 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
840
841 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530842}
843
844/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530845 * be_isr - The isr routine of the driver.
846 * @irq: Not used
847 * @dev_id: Pointer to host adapter structure
848 */
849static irqreturn_t be_isr(int irq, void *dev_id)
850{
851 struct beiscsi_hba *phba;
852 struct hwi_controller *phwi_ctrlr;
853 struct hwi_context_memory *phwi_context;
854 struct be_eq_entry *eqe = NULL;
855 struct be_queue_info *eq;
856 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530857 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530858 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530861 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530862 int isr;
863
864 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700865 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
867 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
868 if (!isr)
869 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530870
871 phwi_ctrlr = phba->phwi_ctrlr;
872 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530873 pbe_eq = &phwi_context->be_eq[0];
874
875 eq = &phwi_context->be_eq[0].q;
876 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530877 index = 0;
878 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530879
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530880 num_ioeq_processed = 0;
881 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530882 if (blk_iopoll_enabled) {
883 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
884 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530885 if (((eqe->dw[offsetof(struct amap_eq_entry,
886 resource_id) / 32] &
887 EQE_RESID_MASK) >> 16) == mcc->id) {
888 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530889 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530890 spin_unlock_irqrestore(&phba->isr_lock, flags);
891 num_mcceq_processed++;
892 } else {
893 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
894 blk_iopoll_sched(&pbe_eq->iopoll);
895 num_ioeq_processed++;
896 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530897 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
898 queue_tail_inc(eq);
899 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530900 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530901 if (num_ioeq_processed || num_mcceq_processed) {
John Soni Jose72fb46a2012-10-20 04:42:49 +0530902 if (pbe_eq->todo_mcc_cq)
903 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530904
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530905 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530906 hwi_ring_eq_db(phba, eq->id, 0,
907 (num_ioeq_processed +
908 num_mcceq_processed) , 1, 1);
909 else
910 hwi_ring_eq_db(phba, eq->id, 0,
911 (num_ioeq_processed +
912 num_mcceq_processed), 0, 1);
913
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530914 return IRQ_HANDLED;
915 } else
916 return IRQ_NONE;
917 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530918 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530919 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
920 & EQE_VALID_MASK) {
921
922 if (((eqe->dw[offsetof(struct amap_eq_entry,
923 resource_id) / 32] &
924 EQE_RESID_MASK) >> 16) != cq->id) {
925 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530926 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530927 spin_unlock_irqrestore(&phba->isr_lock, flags);
928 } else {
929 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530930 pbe_eq->todo_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530931 spin_unlock_irqrestore(&phba->isr_lock, flags);
932 }
933 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
934 queue_tail_inc(eq);
935 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530936 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530937 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530938 if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
939 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530940
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530941 if (num_ioeq_processed) {
942 hwi_ring_eq_db(phba, eq->id, 0,
943 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530944 return IRQ_HANDLED;
945 } else
946 return IRQ_NONE;
947 }
948}
949
950static int beiscsi_init_irqs(struct beiscsi_hba *phba)
951{
952 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530953 struct hwi_controller *phwi_ctrlr;
954 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530955 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530956
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530957 phwi_ctrlr = phba->phwi_ctrlr;
958 phwi_context = phwi_ctrlr->phwi_ctxt;
959
960 if (phba->msix_enabled) {
961 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700962 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
963 GFP_KERNEL);
964 if (!phba->msi_name[i]) {
965 ret = -ENOMEM;
966 goto free_msix_irqs;
967 }
968
969 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
970 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530971 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700972 ret = request_irq(msix_vec, be_isr_msix, 0,
973 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530974 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530975 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530976 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
977 "BM_%d : beiscsi_init_irqs-Failed to"
978 "register msix for i = %d\n",
979 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700980 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530981 goto free_msix_irqs;
982 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530983 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700984 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
985 if (!phba->msi_name[i]) {
986 ret = -ENOMEM;
987 goto free_msix_irqs;
988 }
989 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
990 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530991 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700992 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530993 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530994 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530995 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
996 "BM_%d : beiscsi_init_irqs-"
997 "Failed to register beiscsi_msix_mcc\n");
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 }
1001
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301002 } else {
1003 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1004 "beiscsi", phba);
1005 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301006 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1007 "BM_%d : beiscsi_init_irqs-"
1008 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301009 return ret;
1010 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301011 }
1012 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301013free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001014 for (j = i - 1; j >= 0; j--) {
1015 kfree(phba->msi_name[j]);
1016 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301017 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001018 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301019 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301020}
1021
1022static void hwi_ring_cq_db(struct beiscsi_hba *phba,
1023 unsigned int id, unsigned int num_processed,
1024 unsigned char rearm, unsigned char event)
1025{
1026 u32 val = 0;
1027 val |= id & DB_CQ_RING_ID_MASK;
1028 if (rearm)
1029 val |= 1 << DB_CQ_REARM_SHIFT;
1030 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1031 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1032}
1033
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301034static unsigned int
1035beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1036 struct beiscsi_hba *phba,
1037 unsigned short cid,
1038 struct pdu_base *ppdu,
1039 unsigned long pdu_len,
1040 void *pbuffer, unsigned long buf_len)
1041{
1042 struct iscsi_conn *conn = beiscsi_conn->conn;
1043 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301044 struct iscsi_task *task;
1045 struct beiscsi_io_task *io_task;
1046 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301047
1048 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1049 PDUBASE_OPCODE_MASK) {
1050 case ISCSI_OP_NOOP_IN:
1051 pbuffer = NULL;
1052 buf_len = 0;
1053 break;
1054 case ISCSI_OP_ASYNC_EVENT:
1055 break;
1056 case ISCSI_OP_REJECT:
1057 WARN_ON(!pbuffer);
1058 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301059 beiscsi_log(phba, KERN_ERR,
1060 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1061 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301062 break;
1063 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301064 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301065 task = conn->login_task;
1066 io_task = task->dd_data;
1067 login_hdr = (struct iscsi_hdr *)ppdu;
1068 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301069 break;
1070 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301071 beiscsi_log(phba, KERN_WARNING,
1072 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1073 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1074 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301075 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301076 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301077 return 1;
1078 }
1079
1080 spin_lock_bh(&session->lock);
1081 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1082 spin_unlock_bh(&session->lock);
1083 return 0;
1084}
1085
1086static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1087{
1088 struct sgl_handle *psgl_handle;
1089
1090 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301091 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1092 "BM_%d : In alloc_io_sgl_handle,"
1093 " io_sgl_alloc_index=%d\n",
1094 phba->io_sgl_alloc_index);
1095
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301096 psgl_handle = phba->io_sgl_hndl_base[phba->
1097 io_sgl_alloc_index];
1098 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1099 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301100 if (phba->io_sgl_alloc_index == (phba->params.
1101 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301102 phba->io_sgl_alloc_index = 0;
1103 else
1104 phba->io_sgl_alloc_index++;
1105 } else
1106 psgl_handle = NULL;
1107 return psgl_handle;
1108}
1109
1110static void
1111free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1112{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301113 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1114 "BM_%d : In free_,io_sgl_free_index=%d\n",
1115 phba->io_sgl_free_index);
1116
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301117 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1118 /*
1119 * this can happen if clean_task is called on a task that
1120 * failed in xmit_task or alloc_pdu.
1121 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301122 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1123 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1124 "value there=%p\n", phba->io_sgl_free_index,
1125 phba->io_sgl_hndl_base
1126 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301127 return;
1128 }
1129 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1130 phba->io_sgl_hndl_avbl++;
1131 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1132 phba->io_sgl_free_index = 0;
1133 else
1134 phba->io_sgl_free_index++;
1135}
1136
1137/**
1138 * alloc_wrb_handle - To allocate a wrb handle
1139 * @phba: The hba pointer
1140 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301141 *
1142 * This happens under session_lock until submission to chip
1143 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301144struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301145{
1146 struct hwi_wrb_context *pwrb_context;
1147 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301148 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301149
1150 phwi_ctrlr = phba->phwi_ctrlr;
1151 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301152 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301153 pwrb_handle = pwrb_context->pwrb_handle_base[
1154 pwrb_context->alloc_index];
1155 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301156 if (pwrb_context->alloc_index ==
1157 (phba->params.wrbs_per_cxn - 1))
1158 pwrb_context->alloc_index = 0;
1159 else
1160 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301161 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1162 pwrb_context->alloc_index];
1163 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301164 } else
1165 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301166 return pwrb_handle;
1167}
1168
1169/**
1170 * free_wrb_handle - To free the wrb handle back to pool
1171 * @phba: The hba pointer
1172 * @pwrb_context: The context to free from
1173 * @pwrb_handle: The wrb_handle to free
1174 *
1175 * This happens under session_lock until submission to chip
1176 */
1177static void
1178free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1179 struct wrb_handle *pwrb_handle)
1180{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301181 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301182 pwrb_context->wrb_handles_available++;
1183 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1184 pwrb_context->free_index = 0;
1185 else
1186 pwrb_context->free_index++;
1187
John Soni Jose99bc5d52012-08-20 23:00:18 +05301188 beiscsi_log(phba, KERN_INFO,
1189 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1190 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1191 "wrb_handles_available=%d\n",
1192 pwrb_handle, pwrb_context->free_index,
1193 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301194}
1195
1196static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1197{
1198 struct sgl_handle *psgl_handle;
1199
1200 if (phba->eh_sgl_hndl_avbl) {
1201 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1202 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301203 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1204 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1205 phba->eh_sgl_alloc_index,
1206 phba->eh_sgl_alloc_index);
1207
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301208 phba->eh_sgl_hndl_avbl--;
1209 if (phba->eh_sgl_alloc_index ==
1210 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1211 1))
1212 phba->eh_sgl_alloc_index = 0;
1213 else
1214 phba->eh_sgl_alloc_index++;
1215 } else
1216 psgl_handle = NULL;
1217 return psgl_handle;
1218}
1219
1220void
1221free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1222{
1223
John Soni Jose99bc5d52012-08-20 23:00:18 +05301224 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1225 "BM_%d : In free_mgmt_sgl_handle,"
1226 "eh_sgl_free_index=%d\n",
1227 phba->eh_sgl_free_index);
1228
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301229 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1230 /*
1231 * this can happen if clean_task is called on a task that
1232 * failed in xmit_task or alloc_pdu.
1233 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301234 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1235 "BM_%d : Double Free in eh SGL ,"
1236 "eh_sgl_free_index=%d\n",
1237 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301238 return;
1239 }
1240 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1241 phba->eh_sgl_hndl_avbl++;
1242 if (phba->eh_sgl_free_index ==
1243 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1244 phba->eh_sgl_free_index = 0;
1245 else
1246 phba->eh_sgl_free_index++;
1247}
1248
1249static void
1250be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301251 struct iscsi_task *task,
1252 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301253{
1254 struct beiscsi_io_task *io_task = task->dd_data;
1255 struct be_status_bhs *sts_bhs =
1256 (struct be_status_bhs *)io_task->cmd_bhs;
1257 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301258 unsigned char *sense;
1259 u32 resid = 0, exp_cmdsn, max_cmdsn;
1260 u8 rsp, status, flags;
1261
John Soni Jose73133262012-10-20 04:44:49 +05301262 exp_cmdsn = csol_cqe->exp_cmdsn;
1263 max_cmdsn = (csol_cqe->exp_cmdsn +
1264 csol_cqe->cmd_wnd - 1);
1265 rsp = csol_cqe->i_resp;
1266 status = csol_cqe->i_sts;
1267 flags = csol_cqe->i_flags;
1268 resid = csol_cqe->res_cnt;
1269
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001270 if (!task->sc) {
1271 if (io_task->scsi_cmnd)
1272 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301273
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001274 return;
1275 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301276 task->sc->result = (DID_OK << 16) | status;
1277 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1278 task->sc->result = DID_ERROR << 16;
1279 goto unmap;
1280 }
1281
1282 /* bidi not initially supported */
1283 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301284 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1285 task->sc->result = DID_ERROR << 16;
1286
1287 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1288 scsi_set_resid(task->sc, resid);
1289 if (!status && (scsi_bufflen(task->sc) - resid <
1290 task->sc->underflow))
1291 task->sc->result = DID_ERROR << 16;
1292 }
1293 }
1294
1295 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001296 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301297 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001298
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301299 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001300 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301301 memcpy(task->sc->sense_buffer, sense,
1302 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1303 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301304
John Soni Jose73133262012-10-20 04:44:49 +05301305 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1306 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301307unmap:
1308 scsi_dma_unmap(io_task->scsi_cmnd);
1309 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1310}
1311
1312static void
1313be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301314 struct iscsi_task *task,
1315 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301316{
1317 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301318 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301319 struct iscsi_conn *conn = beiscsi_conn->conn;
1320
1321 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301322 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301323 hdr->t2wait = 5;
1324 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301325 hdr->flags = csol_cqe->i_flags;
1326 hdr->response = csol_cqe->i_resp;
1327 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1328 hdr->max_cmdsn = (csol_cqe->exp_cmdsn + csol_cqe->cmd_wnd - 1);
1329
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301330 hdr->dlength[0] = 0;
1331 hdr->dlength[1] = 0;
1332 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301334 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1336}
1337
1338static void
1339be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301340 struct iscsi_task *task,
1341 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301342{
1343 struct iscsi_tm_rsp *hdr;
1344 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301345 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301346
1347 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301348 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301349 hdr->flags = csol_cqe->i_flags;
1350 hdr->response = csol_cqe->i_resp;
1351 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1352 hdr->max_cmdsn = (csol_cqe->exp_cmdsn +
1353 csol_cqe->cmd_wnd - 1);
1354
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301355 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301356 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1357}
1358
1359static void
1360hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1361 struct beiscsi_hba *phba, struct sol_cqe *psol)
1362{
1363 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301364 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301365 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301366 struct iscsi_task *task;
1367 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301368 struct iscsi_conn *conn = beiscsi_conn->conn;
1369 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301370 uint16_t wrb_index, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301371
1372 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001373 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301374 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1375 wrb_idx, psol);
1376 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1377 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001378 } else {
1379 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1380 wrb_idx, psol);
1381 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1382 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301383 }
1384
1385 pwrb_context = &phwi_ctrlr->wrb_context[
1386 cid - phba->fw_config.iscsi_cid_start];
1387 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301388 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301389
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301390 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001391 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301392 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001393 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394 spin_lock_bh(&session->lock);
1395 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1396 spin_unlock_bh(&session->lock);
1397}
1398
1399static void
1400be_complete_nopin_resp(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_nopin *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_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301409 hdr->flags = csol_cqe->i_flags;
1410 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1411 hdr->max_cmdsn = be32_to_cpu(hdr->exp_cmdsn +
1412 csol_cqe->cmd_wnd - 1);
1413
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301414 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301415 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301416 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1417}
1418
John Soni Jose73133262012-10-20 04:44:49 +05301419static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1420 struct sol_cqe *psol,
1421 struct common_sol_cqe *csol_cqe)
1422{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001423 if (is_chip_be2_be3r(phba)) {
1424 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1425 i_exp_cmd_sn, psol);
1426 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1427 i_res_cnt, psol);
1428 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1429 i_cmd_wnd, psol);
1430 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1431 wrb_index, psol);
1432 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1433 cid, psol);
1434 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1435 hw_sts, psol);
1436 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1437 i_resp, psol);
1438 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1439 i_sts, psol);
1440 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1441 i_flags, psol);
1442 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301443 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1444 i_exp_cmd_sn, psol);
1445 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1446 i_res_cnt, psol);
1447 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1448 wrb_index, psol);
1449 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1450 cid, psol);
1451 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1452 hw_sts, psol);
1453 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1454 i_cmd_wnd, psol);
1455 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1456 cmd_cmpl, psol))
1457 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1458 i_sts, psol);
1459 else
1460 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1461 i_sts, psol);
1462 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1463 u, psol))
1464 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1465
1466 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1467 o, psol))
1468 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301469 }
1470}
1471
1472
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301473static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1474 struct beiscsi_hba *phba, struct sol_cqe *psol)
1475{
1476 struct hwi_wrb_context *pwrb_context;
1477 struct wrb_handle *pwrb_handle;
1478 struct iscsi_wrb *pwrb = NULL;
1479 struct hwi_controller *phwi_ctrlr;
1480 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301481 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301482 struct iscsi_conn *conn = beiscsi_conn->conn;
1483 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301484 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301485
1486 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301487
1488 /* Copy the elements to a common structure */
1489 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1490
1491 pwrb_context = &phwi_ctrlr->wrb_context[
1492 csol_cqe.cid - phba->fw_config.iscsi_cid_start];
1493
1494 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1495 csol_cqe.wrb_index];
1496
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301497 task = pwrb_handle->pio_handle;
1498 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301499 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301500
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301501 spin_lock_bh(&session->lock);
1502 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301503 case HWH_TYPE_IO:
1504 case HWH_TYPE_IO_RD:
1505 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301506 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301507 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301508 else
John Soni Jose73133262012-10-20 04:44:49 +05301509 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301510 break;
1511
1512 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301513 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301514 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301515 else
John Soni Jose73133262012-10-20 04:44:49 +05301516 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301517 break;
1518
1519 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301520 beiscsi_log(phba, KERN_ERR,
1521 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1522 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1523 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301524 break;
1525
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301527 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301528 break;
1529
1530 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301531 beiscsi_log(phba, KERN_WARNING,
1532 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1533 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1534 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301535 csol_cqe.wrb_index,
1536 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301537 break;
1538 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301539
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301540 spin_unlock_bh(&session->lock);
1541}
1542
1543static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1544 *pasync_ctx, unsigned int is_header,
1545 unsigned int host_write_ptr)
1546{
1547 if (is_header)
1548 return &pasync_ctx->async_entry[host_write_ptr].
1549 header_busy_list;
1550 else
1551 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1552}
1553
1554static struct async_pdu_handle *
1555hwi_get_async_handle(struct beiscsi_hba *phba,
1556 struct beiscsi_conn *beiscsi_conn,
1557 struct hwi_async_pdu_context *pasync_ctx,
1558 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1559{
1560 struct be_bus_address phys_addr;
1561 struct list_head *pbusy_list;
1562 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301563 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301564 unsigned int index, dpl;
1565
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001566 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301567 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1568 dpl, pdpdu_cqe);
1569 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1570 index, pdpdu_cqe);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001571 } else {
1572 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1573 dpl, pdpdu_cqe);
1574 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1575 index, pdpdu_cqe);
John Soni Jose73133262012-10-20 04:44:49 +05301576 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301577
1578 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301579 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1580 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301581 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301582 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1583 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301584
1585 phys_addr.u.a64.address =
1586 *((unsigned long long *)(&phys_addr.u.a64.address));
1587
1588 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1589 & PDUCQE_CODE_MASK) {
1590 case UNSOL_HDR_NOTIFY:
1591 is_header = 1;
1592
John Soni Jose73133262012-10-20 04:44:49 +05301593 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1594 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301595 break;
1596 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301597 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1598 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301599 break;
1600 default:
1601 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301602 beiscsi_log(phba, KERN_WARNING,
1603 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1604 "BM_%d : Unexpected code=%d\n",
1605 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1606 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301607 return NULL;
1608 }
1609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301610 WARN_ON(list_empty(pbusy_list));
1611 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001612 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301613 break;
1614 }
1615
1616 WARN_ON(!pasync_handle);
1617
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301618 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1619 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301620 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301621 pasync_handle->buffer_len = dpl;
1622 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301623
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301624 return pasync_handle;
1625}
1626
1627static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301628hwi_update_async_writables(struct beiscsi_hba *phba,
1629 struct hwi_async_pdu_context *pasync_ctx,
1630 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301631{
1632 struct list_head *pbusy_list;
1633 struct async_pdu_handle *pasync_handle;
1634 unsigned int num_entries, writables = 0;
1635 unsigned int *pep_read_ptr, *pwritables;
1636
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001637 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301638 if (is_header) {
1639 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1640 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301641 } else {
1642 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1643 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301644 }
1645
1646 while ((*pep_read_ptr) != cq_index) {
1647 (*pep_read_ptr)++;
1648 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1649
1650 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1651 *pep_read_ptr);
1652 if (writables == 0)
1653 WARN_ON(list_empty(pbusy_list));
1654
1655 if (!list_empty(pbusy_list)) {
1656 pasync_handle = list_entry(pbusy_list->next,
1657 struct async_pdu_handle,
1658 link);
1659 WARN_ON(!pasync_handle);
1660 pasync_handle->consumed = 1;
1661 }
1662
1663 writables++;
1664 }
1665
1666 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301667 beiscsi_log(phba, KERN_ERR,
1668 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1669 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1670 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301671 WARN_ON(1);
1672 }
1673
1674 *pwritables = *pwritables + writables;
1675 return 0;
1676}
1677
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001678static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301679 unsigned int cri)
1680{
1681 struct hwi_controller *phwi_ctrlr;
1682 struct hwi_async_pdu_context *pasync_ctx;
1683 struct async_pdu_handle *pasync_handle, *tmp_handle;
1684 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301685
1686 phwi_ctrlr = phba->phwi_ctrlr;
1687 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1688
1689 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1690
1691 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1692 list_del(&pasync_handle->link);
1693
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001694 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301695 list_add_tail(&pasync_handle->link,
1696 &pasync_ctx->async_header.free_list);
1697 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301698 } else {
1699 list_add_tail(&pasync_handle->link,
1700 &pasync_ctx->async_data.free_list);
1701 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301702 }
1703 }
1704
1705 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1706 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1707 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301708}
1709
1710static struct phys_addr *
1711hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1712 unsigned int is_header, unsigned int host_write_ptr)
1713{
1714 struct phys_addr *pasync_sge = NULL;
1715
1716 if (is_header)
1717 pasync_sge = pasync_ctx->async_header.ring_base;
1718 else
1719 pasync_sge = pasync_ctx->async_data.ring_base;
1720
1721 return pasync_sge + host_write_ptr;
1722}
1723
1724static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1725 unsigned int is_header)
1726{
1727 struct hwi_controller *phwi_ctrlr;
1728 struct hwi_async_pdu_context *pasync_ctx;
1729 struct async_pdu_handle *pasync_handle;
1730 struct list_head *pfree_link, *pbusy_list;
1731 struct phys_addr *pasync_sge;
1732 unsigned int ring_id, num_entries;
1733 unsigned int host_write_num;
1734 unsigned int writables;
1735 unsigned int i = 0;
1736 u32 doorbell = 0;
1737
1738 phwi_ctrlr = phba->phwi_ctrlr;
1739 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001740 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301741
1742 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301743 writables = min(pasync_ctx->async_header.writables,
1744 pasync_ctx->async_header.free_entries);
1745 pfree_link = pasync_ctx->async_header.free_list.next;
1746 host_write_num = pasync_ctx->async_header.host_write_ptr;
1747 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1748 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301749 writables = min(pasync_ctx->async_data.writables,
1750 pasync_ctx->async_data.free_entries);
1751 pfree_link = pasync_ctx->async_data.free_list.next;
1752 host_write_num = pasync_ctx->async_data.host_write_ptr;
1753 ring_id = phwi_ctrlr->default_pdu_data.id;
1754 }
1755
1756 writables = (writables / 8) * 8;
1757 if (writables) {
1758 for (i = 0; i < writables; i++) {
1759 pbusy_list =
1760 hwi_get_async_busy_list(pasync_ctx, is_header,
1761 host_write_num);
1762 pasync_handle =
1763 list_entry(pfree_link, struct async_pdu_handle,
1764 link);
1765 WARN_ON(!pasync_handle);
1766 pasync_handle->consumed = 0;
1767
1768 pfree_link = pfree_link->next;
1769
1770 pasync_sge = hwi_get_ring_address(pasync_ctx,
1771 is_header, host_write_num);
1772
1773 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1774 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1775
1776 list_move(&pasync_handle->link, pbusy_list);
1777
1778 host_write_num++;
1779 host_write_num = host_write_num % num_entries;
1780 }
1781
1782 if (is_header) {
1783 pasync_ctx->async_header.host_write_ptr =
1784 host_write_num;
1785 pasync_ctx->async_header.free_entries -= writables;
1786 pasync_ctx->async_header.writables -= writables;
1787 pasync_ctx->async_header.busy_entries += writables;
1788 } else {
1789 pasync_ctx->async_data.host_write_ptr = host_write_num;
1790 pasync_ctx->async_data.free_entries -= writables;
1791 pasync_ctx->async_data.writables -= writables;
1792 pasync_ctx->async_data.busy_entries += writables;
1793 }
1794
1795 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1796 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1797 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1798 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1799 << DB_DEF_PDU_CQPROC_SHIFT;
1800
1801 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1802 }
1803}
1804
1805static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1806 struct beiscsi_conn *beiscsi_conn,
1807 struct i_t_dpdu_cqe *pdpdu_cqe)
1808{
1809 struct hwi_controller *phwi_ctrlr;
1810 struct hwi_async_pdu_context *pasync_ctx;
1811 struct async_pdu_handle *pasync_handle = NULL;
1812 unsigned int cq_index = -1;
1813
1814 phwi_ctrlr = phba->phwi_ctrlr;
1815 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1816
1817 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1818 pdpdu_cqe, &cq_index);
1819 BUG_ON(pasync_handle->is_header != 0);
1820 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301821 hwi_update_async_writables(phba, pasync_ctx,
1822 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301823
1824 hwi_free_async_msg(phba, pasync_handle->cri);
1825 hwi_post_async_buffers(phba, pasync_handle->is_header);
1826}
1827
1828static unsigned int
1829hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1830 struct beiscsi_hba *phba,
1831 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1832{
1833 struct list_head *plist;
1834 struct async_pdu_handle *pasync_handle;
1835 void *phdr = NULL;
1836 unsigned int hdr_len = 0, buf_len = 0;
1837 unsigned int status, index = 0, offset = 0;
1838 void *pfirst_buffer = NULL;
1839 unsigned int num_buf = 0;
1840
1841 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1842
1843 list_for_each_entry(pasync_handle, plist, link) {
1844 if (index == 0) {
1845 phdr = pasync_handle->pbuffer;
1846 hdr_len = pasync_handle->buffer_len;
1847 } else {
1848 buf_len = pasync_handle->buffer_len;
1849 if (!num_buf) {
1850 pfirst_buffer = pasync_handle->pbuffer;
1851 num_buf++;
1852 }
1853 memcpy(pfirst_buffer + offset,
1854 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001855 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301856 }
1857 index++;
1858 }
1859
1860 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301861 (beiscsi_conn->beiscsi_conn_cid -
1862 phba->fw_config.iscsi_cid_start),
1863 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001864 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301865
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001866 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867 return 0;
1868}
1869
1870static unsigned int
1871hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1872 struct beiscsi_hba *phba,
1873 struct async_pdu_handle *pasync_handle)
1874{
1875 struct hwi_async_pdu_context *pasync_ctx;
1876 struct hwi_controller *phwi_ctrlr;
1877 unsigned int bytes_needed = 0, status = 0;
1878 unsigned short cri = pasync_handle->cri;
1879 struct pdu_base *ppdu;
1880
1881 phwi_ctrlr = phba->phwi_ctrlr;
1882 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1883
1884 list_del(&pasync_handle->link);
1885 if (pasync_handle->is_header) {
1886 pasync_ctx->async_header.busy_entries--;
1887 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1888 hwi_free_async_msg(phba, cri);
1889 BUG();
1890 }
1891
1892 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1893 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1894 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1895 (unsigned short)pasync_handle->buffer_len;
1896 list_add_tail(&pasync_handle->link,
1897 &pasync_ctx->async_entry[cri].wait_queue.list);
1898
1899 ppdu = pasync_handle->pbuffer;
1900 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1901 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1902 0xFFFF0000) | ((be16_to_cpu((ppdu->
1903 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1904 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1905
1906 if (status == 0) {
1907 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1908 bytes_needed;
1909
1910 if (bytes_needed == 0)
1911 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1912 pasync_ctx, cri);
1913 }
1914 } else {
1915 pasync_ctx->async_data.busy_entries--;
1916 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1917 list_add_tail(&pasync_handle->link,
1918 &pasync_ctx->async_entry[cri].wait_queue.
1919 list);
1920 pasync_ctx->async_entry[cri].wait_queue.
1921 bytes_received +=
1922 (unsigned short)pasync_handle->buffer_len;
1923
1924 if (pasync_ctx->async_entry[cri].wait_queue.
1925 bytes_received >=
1926 pasync_ctx->async_entry[cri].wait_queue.
1927 bytes_needed)
1928 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1929 pasync_ctx, cri);
1930 }
1931 }
1932 return status;
1933}
1934
1935static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1936 struct beiscsi_hba *phba,
1937 struct i_t_dpdu_cqe *pdpdu_cqe)
1938{
1939 struct hwi_controller *phwi_ctrlr;
1940 struct hwi_async_pdu_context *pasync_ctx;
1941 struct async_pdu_handle *pasync_handle = NULL;
1942 unsigned int cq_index = -1;
1943
1944 phwi_ctrlr = phba->phwi_ctrlr;
1945 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1946 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1947 pdpdu_cqe, &cq_index);
1948
1949 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301950 hwi_update_async_writables(phba, pasync_ctx,
1951 pasync_handle->is_header, cq_index);
1952
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301953 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1954 hwi_post_async_buffers(phba, pasync_handle->is_header);
1955}
1956
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301957static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1958{
1959 struct be_queue_info *mcc_cq;
1960 struct be_mcc_compl *mcc_compl;
1961 unsigned int num_processed = 0;
1962
1963 mcc_cq = &phba->ctrl.mcc_obj.cq;
1964 mcc_compl = queue_tail_node(mcc_cq);
1965 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1966 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1967
1968 if (num_processed >= 32) {
1969 hwi_ring_cq_db(phba, mcc_cq->id,
1970 num_processed, 0, 0);
1971 num_processed = 0;
1972 }
1973 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1974 /* Interpret flags as an async trailer */
1975 if (is_link_state_evt(mcc_compl->flags))
1976 /* Interpret compl as a async link evt */
1977 beiscsi_async_link_state_process(phba,
1978 (struct be_async_event_link_state *) mcc_compl);
1979 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301980 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1981 "BM_%d : Unsupported Async Event, flags"
1982 " = 0x%08x\n",
1983 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301984 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1985 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1986 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1987 }
1988
1989 mcc_compl->flags = 0;
1990 queue_tail_inc(mcc_cq);
1991 mcc_compl = queue_tail_node(mcc_cq);
1992 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1993 num_processed++;
1994 }
1995
1996 if (num_processed > 0)
1997 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1998
1999}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302000
John Soni Jose6763daa2012-10-20 04:41:45 +05302001/**
2002 * beiscsi_process_cq()- Process the Completion Queue
2003 * @pbe_eq: Event Q on which the Completion has come
2004 *
2005 * return
2006 * Number of Completion Entries processed.
2007 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302008static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302009{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302010 struct be_queue_info *cq;
2011 struct sol_cqe *sol;
2012 struct dmsg_cqe *dmsg;
2013 unsigned int num_processed = 0;
2014 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302015 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302016 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302017 struct beiscsi_endpoint *beiscsi_ep;
2018 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302019 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302020
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302021 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302023 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024
2025 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2026 CQE_VALID_MASK) {
2027 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2028
John Soni Jose73133262012-10-20 04:44:49 +05302029 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2030 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302031
John Soni Jose73133262012-10-20 04:44:49 +05302032 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002033 if (is_chip_be2_be3r(phba)) {
2034 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2035 } else {
John Soni Jose73133262012-10-20 04:44:49 +05302036 if ((code == DRIVERMSG_NOTIFY) ||
2037 (code == UNSOL_HDR_NOTIFY) ||
2038 (code == UNSOL_DATA_NOTIFY))
2039 cid = AMAP_GET_BITS(
2040 struct amap_i_t_dpdu_cqe_v2,
2041 cid, sol);
2042 else
2043 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2044 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002045 }
John Soni Jose73133262012-10-20 04:44:49 +05302046
2047 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302048 beiscsi_ep = ep->dd_data;
2049 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302050
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302051 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302052 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302053 num_processed, 0, 0);
2054 tot_nump += num_processed;
2055 num_processed = 0;
2056 }
2057
John Soni Jose0a513dd2012-08-20 23:00:55 +05302058 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302059 case SOL_CMD_COMPLETE:
2060 hwi_complete_cmd(beiscsi_conn, phba, sol);
2061 break;
2062 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302063 beiscsi_log(phba, KERN_INFO,
2064 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302065 "BM_%d : Received %s[%d] on CID : %d\n",
2066 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302067
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302068 dmsg = (struct dmsg_cqe *)sol;
2069 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2070 break;
2071 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302072 beiscsi_log(phba, KERN_INFO,
2073 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302074 "BM_%d : Received %s[%d] on CID : %d\n",
2075 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302076
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302077 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2078 (struct i_t_dpdu_cqe *)sol);
2079 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302080 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302081 beiscsi_log(phba, KERN_INFO,
2082 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302083 "BM_%d : Received %s[%d] on CID : %d\n",
2084 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302085
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302086 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2087 (struct i_t_dpdu_cqe *)sol);
2088 break;
2089 case CXN_INVALIDATE_INDEX_NOTIFY:
2090 case CMD_INVALIDATED_NOTIFY:
2091 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302092 beiscsi_log(phba, KERN_ERR,
2093 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302094 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2095 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302096 break;
2097 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2098 case CMD_KILLED_INVALID_STATSN_RCVD:
2099 case CMD_KILLED_INVALID_R2T_RCVD:
2100 case CMD_CXN_KILLED_LUN_INVALID:
2101 case CMD_CXN_KILLED_ICD_INVALID:
2102 case CMD_CXN_KILLED_ITT_INVALID:
2103 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2104 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302105 beiscsi_log(phba, KERN_ERR,
2106 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302107 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2108 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302109 break;
2110 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302111 beiscsi_log(phba, KERN_ERR,
2112 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302113 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2114 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302115 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2116 (struct i_t_dpdu_cqe *) sol);
2117 break;
2118 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2119 case CXN_KILLED_BURST_LEN_MISMATCH:
2120 case CXN_KILLED_AHS_RCVD:
2121 case CXN_KILLED_HDR_DIGEST_ERR:
2122 case CXN_KILLED_UNKNOWN_HDR:
2123 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2124 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2125 case CXN_KILLED_TIMED_OUT:
2126 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302127 case CXN_KILLED_RST_SENT:
2128 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302129 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2130 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2131 case CXN_KILLED_OVER_RUN_RESIDUAL:
2132 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2133 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302134 beiscsi_log(phba, KERN_ERR,
2135 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302136 "BM_%d : Event %s[%d] received on CID : %d\n",
2137 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302138 if (beiscsi_conn)
2139 iscsi_conn_failure(beiscsi_conn->conn,
2140 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302141 break;
2142 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302143 beiscsi_log(phba, KERN_ERR,
2144 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302145 "BM_%d : Invalid CQE Event Received Code : %d"
2146 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302147 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302148 break;
2149 }
2150
2151 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2152 queue_tail_inc(cq);
2153 sol = queue_tail_node(cq);
2154 num_processed++;
2155 }
2156
2157 if (num_processed > 0) {
2158 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302159 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302160 }
2161 return tot_nump;
2162}
2163
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302164void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302165{
2166 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302167 struct hwi_controller *phwi_ctrlr;
2168 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302169 struct beiscsi_hba *phba;
2170 struct be_eq_obj *pbe_eq =
2171 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302172
John Soni Jose72fb46a2012-10-20 04:42:49 +05302173 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302174 phwi_ctrlr = phba->phwi_ctrlr;
2175 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302176
John Soni Jose72fb46a2012-10-20 04:42:49 +05302177 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302178 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302179 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302180 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302181 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302182 }
2183
John Soni Jose72fb46a2012-10-20 04:42:49 +05302184 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302185 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302186 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302187 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302188 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302189 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302190
2191 /* rearm EQ for further interrupts */
2192 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302193}
2194
2195static int be_iopoll(struct blk_iopoll *iop, int budget)
2196{
2197 static unsigned int ret;
2198 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302199 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302200
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302201 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2202 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302203 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302204 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302205 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302206 beiscsi_log(phba, KERN_INFO,
2207 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2208 "BM_%d : rearm pbe_eq->q.id =%d\n",
2209 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302210 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302211 }
2212 return ret;
2213}
2214
2215static void
John Soni Jose09a10932012-10-20 04:44:23 +05302216hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2217 unsigned int num_sg, struct beiscsi_io_task *io_task)
2218{
2219 struct iscsi_sge *psgl;
2220 unsigned int sg_len, index;
2221 unsigned int sge_len = 0;
2222 unsigned long long addr;
2223 struct scatterlist *l_sg;
2224 unsigned int offset;
2225
2226 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2227 io_task->bhs_pa.u.a32.address_lo);
2228 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2229 io_task->bhs_pa.u.a32.address_hi);
2230
2231 l_sg = sg;
2232 for (index = 0; (index < num_sg) && (index < 2); index++,
2233 sg = sg_next(sg)) {
2234 if (index == 0) {
2235 sg_len = sg_dma_len(sg);
2236 addr = (u64) sg_dma_address(sg);
2237 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2238 sge0_addr_lo, pwrb,
2239 lower_32_bits(addr));
2240 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2241 sge0_addr_hi, pwrb,
2242 upper_32_bits(addr));
2243 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2244 sge0_len, pwrb,
2245 sg_len);
2246 sge_len = sg_len;
2247 } else {
2248 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2249 pwrb, sge_len);
2250 sg_len = sg_dma_len(sg);
2251 addr = (u64) sg_dma_address(sg);
2252 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2253 sge1_addr_lo, pwrb,
2254 lower_32_bits(addr));
2255 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2256 sge1_addr_hi, pwrb,
2257 upper_32_bits(addr));
2258 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2259 sge1_len, pwrb,
2260 sg_len);
2261 }
2262 }
2263 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2264 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2265
2266 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2267
2268 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2269 io_task->bhs_pa.u.a32.address_hi);
2270 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2271 io_task->bhs_pa.u.a32.address_lo);
2272
2273 if (num_sg == 1) {
2274 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2275 1);
2276 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2277 0);
2278 } else if (num_sg == 2) {
2279 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2280 0);
2281 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2282 1);
2283 } else {
2284 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2285 0);
2286 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2287 0);
2288 }
2289
2290 sg = l_sg;
2291 psgl++;
2292 psgl++;
2293 offset = 0;
2294 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2295 sg_len = sg_dma_len(sg);
2296 addr = (u64) sg_dma_address(sg);
2297 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2298 lower_32_bits(addr));
2299 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2300 upper_32_bits(addr));
2301 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2302 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2303 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2304 offset += sg_len;
2305 }
2306 psgl--;
2307 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2308}
2309
2310static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302311hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2312 unsigned int num_sg, struct beiscsi_io_task *io_task)
2313{
2314 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302315 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302316 unsigned int sge_len = 0;
2317 unsigned long long addr;
2318 struct scatterlist *l_sg;
2319 unsigned int offset;
2320
2321 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2322 io_task->bhs_pa.u.a32.address_lo);
2323 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2324 io_task->bhs_pa.u.a32.address_hi);
2325
2326 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302327 for (index = 0; (index < num_sg) && (index < 2); index++,
2328 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302329 if (index == 0) {
2330 sg_len = sg_dma_len(sg);
2331 addr = (u64) sg_dma_address(sg);
2332 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302333 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302334 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302335 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302336 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2337 sg_len);
2338 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302339 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302340 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2341 pwrb, sge_len);
2342 sg_len = sg_dma_len(sg);
2343 addr = (u64) sg_dma_address(sg);
2344 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302345 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302346 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302347 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302348 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2349 sg_len);
2350 }
2351 }
2352 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2353 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2354
2355 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2356
2357 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2358 io_task->bhs_pa.u.a32.address_hi);
2359 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2360 io_task->bhs_pa.u.a32.address_lo);
2361
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302362 if (num_sg == 1) {
2363 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2364 1);
2365 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2366 0);
2367 } else if (num_sg == 2) {
2368 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2369 0);
2370 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2371 1);
2372 } else {
2373 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2374 0);
2375 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2376 0);
2377 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302378 sg = l_sg;
2379 psgl++;
2380 psgl++;
2381 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302382 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302383 sg_len = sg_dma_len(sg);
2384 addr = (u64) sg_dma_address(sg);
2385 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2386 (addr & 0xFFFFFFFF));
2387 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2388 (addr >> 32));
2389 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2390 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2391 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2392 offset += sg_len;
2393 }
2394 psgl--;
2395 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2396}
2397
John Soni Josed629c472012-10-20 04:42:00 +05302398/**
2399 * hwi_write_buffer()- Populate the WRB with task info
2400 * @pwrb: ptr to the WRB entry
2401 * @task: iscsi task which is to be executed
2402 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302403static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2404{
2405 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302406 struct beiscsi_io_task *io_task = task->dd_data;
2407 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2408 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302409 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302410
2411 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2412 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2413 io_task->bhs_pa.u.a32.address_lo);
2414 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2415 io_task->bhs_pa.u.a32.address_hi);
2416
2417 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302418
2419 /* Check for the data_count */
2420 dsp_value = (task->data_count) ? 1 : 0;
2421
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002422 if (is_chip_be2_be3r(phba))
2423 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302424 pwrb, dsp_value);
2425 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002426 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302427 pwrb, dsp_value);
2428
2429 /* Map addr only if there is data_count */
2430 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302431 io_task->mtask_addr = pci_map_single(phba->pcidev,
2432 task->data,
2433 task->data_count,
2434 PCI_DMA_TODEVICE);
John Soni Josed629c472012-10-20 04:42:00 +05302435 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302436 } else
John Soni Josed629c472012-10-20 04:42:00 +05302437 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302438
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302439 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302440 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302441 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302442 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302443 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2444 task->data_count);
2445
2446 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2447 } else {
2448 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302449 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302450 }
2451
2452 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2453
2454 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2455
2456 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2457 io_task->bhs_pa.u.a32.address_hi);
2458 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2459 io_task->bhs_pa.u.a32.address_lo);
2460 if (task->data) {
2461 psgl++;
2462 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2463 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2464 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2466 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2467 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2468
2469 psgl++;
2470 if (task->data) {
2471 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302472 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302473 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302474 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302475 }
2476 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2477 }
2478 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2479}
2480
2481static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2482{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302483 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302484 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2485 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2486
2487 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2488 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302489 num_async_pdu_buf_pages =
2490 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2491 phba->params.defpdu_hdr_sz);
2492 num_async_pdu_buf_sgl_pages =
2493 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2494 sizeof(struct phys_addr));
2495 num_async_pdu_data_pages =
2496 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2497 phba->params.defpdu_data_sz);
2498 num_async_pdu_data_sgl_pages =
2499 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2500 sizeof(struct phys_addr));
2501
2502 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2503
2504 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2505 BE_ISCSI_PDU_HEADER_SIZE;
2506 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2507 sizeof(struct hwi_context_memory);
2508
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302509
2510 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2511 * (phba->params.wrbs_per_cxn)
2512 * phba->params.cxns_per_ctrl;
2513 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2514 (phba->params.wrbs_per_cxn);
2515 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2516 phba->params.cxns_per_ctrl);
2517
2518 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2519 phba->params.icds_per_ctrl;
2520 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2521 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2522
2523 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2524 num_async_pdu_buf_pages * PAGE_SIZE;
2525 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2526 num_async_pdu_data_pages * PAGE_SIZE;
2527 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2528 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2529 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2530 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2531 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2532 phba->params.asyncpdus_per_ctrl *
2533 sizeof(struct async_pdu_handle);
2534 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2535 phba->params.asyncpdus_per_ctrl *
2536 sizeof(struct async_pdu_handle);
2537 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2538 sizeof(struct hwi_async_pdu_context) +
2539 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2540}
2541
2542static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2543{
2544 struct be_mem_descriptor *mem_descr;
2545 dma_addr_t bus_add;
2546 struct mem_array *mem_arr, *mem_arr_orig;
2547 unsigned int i, j, alloc_size, curr_alloc_size;
2548
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002549 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302550 if (!phba->phwi_ctrlr)
2551 return -ENOMEM;
2552
2553 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2554 GFP_KERNEL);
2555 if (!phba->init_mem) {
2556 kfree(phba->phwi_ctrlr);
2557 return -ENOMEM;
2558 }
2559
2560 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2561 GFP_KERNEL);
2562 if (!mem_arr_orig) {
2563 kfree(phba->init_mem);
2564 kfree(phba->phwi_ctrlr);
2565 return -ENOMEM;
2566 }
2567
2568 mem_descr = phba->init_mem;
2569 for (i = 0; i < SE_MEM_MAX; i++) {
2570 j = 0;
2571 mem_arr = mem_arr_orig;
2572 alloc_size = phba->mem_req[i];
2573 memset(mem_arr, 0, sizeof(struct mem_array) *
2574 BEISCSI_MAX_FRAGS_INIT);
2575 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2576 do {
2577 mem_arr->virtual_address = pci_alloc_consistent(
2578 phba->pcidev,
2579 curr_alloc_size,
2580 &bus_add);
2581 if (!mem_arr->virtual_address) {
2582 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2583 goto free_mem;
2584 if (curr_alloc_size -
2585 rounddown_pow_of_two(curr_alloc_size))
2586 curr_alloc_size = rounddown_pow_of_two
2587 (curr_alloc_size);
2588 else
2589 curr_alloc_size = curr_alloc_size / 2;
2590 } else {
2591 mem_arr->bus_address.u.
2592 a64.address = (__u64) bus_add;
2593 mem_arr->size = curr_alloc_size;
2594 alloc_size -= curr_alloc_size;
2595 curr_alloc_size = min(be_max_phys_size *
2596 1024, alloc_size);
2597 j++;
2598 mem_arr++;
2599 }
2600 } while (alloc_size);
2601 mem_descr->num_elements = j;
2602 mem_descr->size_in_bytes = phba->mem_req[i];
2603 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2604 GFP_KERNEL);
2605 if (!mem_descr->mem_array)
2606 goto free_mem;
2607
2608 memcpy(mem_descr->mem_array, mem_arr_orig,
2609 sizeof(struct mem_array) * j);
2610 mem_descr++;
2611 }
2612 kfree(mem_arr_orig);
2613 return 0;
2614free_mem:
2615 mem_descr->num_elements = j;
2616 while ((i) || (j)) {
2617 for (j = mem_descr->num_elements; j > 0; j--) {
2618 pci_free_consistent(phba->pcidev,
2619 mem_descr->mem_array[j - 1].size,
2620 mem_descr->mem_array[j - 1].
2621 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302622 (unsigned long)mem_descr->
2623 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302624 bus_address.u.a64.address);
2625 }
2626 if (i) {
2627 i--;
2628 kfree(mem_descr->mem_array);
2629 mem_descr--;
2630 }
2631 }
2632 kfree(mem_arr_orig);
2633 kfree(phba->init_mem);
2634 kfree(phba->phwi_ctrlr);
2635 return -ENOMEM;
2636}
2637
2638static int beiscsi_get_memory(struct beiscsi_hba *phba)
2639{
2640 beiscsi_find_mem_req(phba);
2641 return beiscsi_alloc_mem(phba);
2642}
2643
2644static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2645{
2646 struct pdu_data_out *pdata_out;
2647 struct pdu_nop_out *pnop_out;
2648 struct be_mem_descriptor *mem_descr;
2649
2650 mem_descr = phba->init_mem;
2651 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2652 pdata_out =
2653 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2654 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2655
2656 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2657 IIOC_SCSI_DATA);
2658
2659 pnop_out =
2660 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2661 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2662
2663 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2664 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2665 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2666 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2667}
2668
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002669static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302670{
2671 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002672 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302673 struct hwi_controller *phwi_ctrlr;
2674 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002675 struct iscsi_wrb *pwrb = NULL;
2676 unsigned int num_cxn_wrbh = 0;
2677 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302678
2679 mem_descr_wrbh = phba->init_mem;
2680 mem_descr_wrbh += HWI_MEM_WRBH;
2681
2682 mem_descr_wrb = phba->init_mem;
2683 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302684 phwi_ctrlr = phba->phwi_ctrlr;
2685
2686 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2687 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302688 pwrb_context->pwrb_handle_base =
2689 kzalloc(sizeof(struct wrb_handle *) *
2690 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002691 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302692 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2693 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002694 goto init_wrb_hndl_failed;
2695 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302696 pwrb_context->pwrb_handle_basestd =
2697 kzalloc(sizeof(struct wrb_handle *) *
2698 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002699 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302700 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2701 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002702 goto init_wrb_hndl_failed;
2703 }
2704 if (!num_cxn_wrbh) {
2705 pwrb_handle =
2706 mem_descr_wrbh->mem_array[idx].virtual_address;
2707 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2708 ((sizeof(struct wrb_handle)) *
2709 phba->params.wrbs_per_cxn));
2710 idx++;
2711 }
2712 pwrb_context->alloc_index = 0;
2713 pwrb_context->wrb_handles_available = 0;
2714 pwrb_context->free_index = 0;
2715
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302717 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2718 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2719 pwrb_context->pwrb_handle_basestd[j] =
2720 pwrb_handle;
2721 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302722 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302723 pwrb_handle++;
2724 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302725 num_cxn_wrbh--;
2726 }
2727 }
2728 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302729 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302730 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002731 if (!num_cxn_wrb) {
2732 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2733 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2734 ((sizeof(struct iscsi_wrb) *
2735 phba->params.wrbs_per_cxn));
2736 idx++;
2737 }
2738
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302739 if (num_cxn_wrb) {
2740 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2741 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2742 pwrb_handle->pwrb = pwrb;
2743 pwrb++;
2744 }
2745 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302746 }
2747 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002748 return 0;
2749init_wrb_hndl_failed:
2750 for (j = index; j > 0; j--) {
2751 pwrb_context = &phwi_ctrlr->wrb_context[j];
2752 kfree(pwrb_context->pwrb_handle_base);
2753 kfree(pwrb_context->pwrb_handle_basestd);
2754 }
2755 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302756}
2757
2758static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2759{
2760 struct hwi_controller *phwi_ctrlr;
2761 struct hba_parameters *p = &phba->params;
2762 struct hwi_async_pdu_context *pasync_ctx;
2763 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002764 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302765 struct be_mem_descriptor *mem_descr;
2766
2767 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2768 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2769
2770 phwi_ctrlr = phba->phwi_ctrlr;
2771 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2772 mem_descr->mem_array[0].virtual_address;
2773 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2774 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2775
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002776 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2777 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302778
2779 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2780 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2781 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302782 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2783 "BM_%d : hwi_init_async_pdu_ctx"
2784 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2785 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302786 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302787 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2788 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302789
2790 pasync_ctx->async_header.va_base =
2791 mem_descr->mem_array[0].virtual_address;
2792
2793 pasync_ctx->async_header.pa_base.u.a64.address =
2794 mem_descr->mem_array[0].bus_address.u.a64.address;
2795
2796 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2797 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2798 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302799 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2800 "BM_%d : hwi_init_async_pdu_ctx"
2801 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2802 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302803 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302804 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2805 "BM_%d : No Virtual address\n");
2806
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302807 pasync_ctx->async_header.ring_base =
2808 mem_descr->mem_array[0].virtual_address;
2809
2810 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2811 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2812 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302813 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2814 "BM_%d : hwi_init_async_pdu_ctx"
2815 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2816 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302817 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302818 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2819 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302820
2821 pasync_ctx->async_header.handle_base =
2822 mem_descr->mem_array[0].virtual_address;
2823 pasync_ctx->async_header.writables = 0;
2824 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2825
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302826
2827 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2828 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2829 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302830 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2831 "BM_%d : hwi_init_async_pdu_ctx"
2832 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2833 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302834 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302835 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2836 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302837
2838 pasync_ctx->async_data.ring_base =
2839 mem_descr->mem_array[0].virtual_address;
2840
2841 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2842 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2843 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302844 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2845 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302846
2847 pasync_ctx->async_data.handle_base =
2848 mem_descr->mem_array[0].virtual_address;
2849 pasync_ctx->async_data.writables = 0;
2850 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2851
2852 pasync_header_h =
2853 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2854 pasync_data_h =
2855 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2856
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002857 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2858 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2859 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302860 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2861 "BM_%d : hwi_init_async_pdu_ctx"
2862 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2863 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002864 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302865 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2866 "BM_%d : No Virtual address\n");
2867
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002868 idx = 0;
2869 pasync_ctx->async_data.va_base =
2870 mem_descr->mem_array[idx].virtual_address;
2871 pasync_ctx->async_data.pa_base.u.a64.address =
2872 mem_descr->mem_array[idx].bus_address.u.a64.address;
2873
2874 num_async_data = ((mem_descr->mem_array[idx].size) /
2875 phba->params.defpdu_data_sz);
2876 num_per_mem = 0;
2877
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302878 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2879 pasync_header_h->cri = -1;
2880 pasync_header_h->index = (char)index;
2881 INIT_LIST_HEAD(&pasync_header_h->link);
2882 pasync_header_h->pbuffer =
2883 (void *)((unsigned long)
2884 (pasync_ctx->async_header.va_base) +
2885 (p->defpdu_hdr_sz * index));
2886
2887 pasync_header_h->pa.u.a64.address =
2888 pasync_ctx->async_header.pa_base.u.a64.address +
2889 (p->defpdu_hdr_sz * index);
2890
2891 list_add_tail(&pasync_header_h->link,
2892 &pasync_ctx->async_header.free_list);
2893 pasync_header_h++;
2894 pasync_ctx->async_header.free_entries++;
2895 pasync_ctx->async_header.writables++;
2896
2897 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2898 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2899 header_busy_list);
2900 pasync_data_h->cri = -1;
2901 pasync_data_h->index = (char)index;
2902 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002903
2904 if (!num_async_data) {
2905 num_per_mem = 0;
2906 idx++;
2907 pasync_ctx->async_data.va_base =
2908 mem_descr->mem_array[idx].virtual_address;
2909 pasync_ctx->async_data.pa_base.u.a64.address =
2910 mem_descr->mem_array[idx].
2911 bus_address.u.a64.address;
2912
2913 num_async_data = ((mem_descr->mem_array[idx].size) /
2914 phba->params.defpdu_data_sz);
2915 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302916 pasync_data_h->pbuffer =
2917 (void *)((unsigned long)
2918 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002919 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302920
2921 pasync_data_h->pa.u.a64.address =
2922 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002923 (p->defpdu_data_sz * num_per_mem);
2924 num_per_mem++;
2925 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302926
2927 list_add_tail(&pasync_data_h->link,
2928 &pasync_ctx->async_data.free_list);
2929 pasync_data_h++;
2930 pasync_ctx->async_data.free_entries++;
2931 pasync_ctx->async_data.writables++;
2932
2933 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2934 }
2935
2936 pasync_ctx->async_header.host_write_ptr = 0;
2937 pasync_ctx->async_header.ep_read_ptr = -1;
2938 pasync_ctx->async_data.host_write_ptr = 0;
2939 pasync_ctx->async_data.ep_read_ptr = -1;
2940}
2941
2942static int
2943be_sgl_create_contiguous(void *virtual_address,
2944 u64 physical_address, u32 length,
2945 struct be_dma_mem *sgl)
2946{
2947 WARN_ON(!virtual_address);
2948 WARN_ON(!physical_address);
2949 WARN_ON(!length > 0);
2950 WARN_ON(!sgl);
2951
2952 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302953 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302954 sgl->size = length;
2955
2956 return 0;
2957}
2958
2959static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2960{
2961 memset(sgl, 0, sizeof(*sgl));
2962}
2963
2964static void
2965hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2966 struct mem_array *pmem, struct be_dma_mem *sgl)
2967{
2968 if (sgl->va)
2969 be_sgl_destroy_contiguous(sgl);
2970
2971 be_sgl_create_contiguous(pmem->virtual_address,
2972 pmem->bus_address.u.a64.address,
2973 pmem->size, sgl);
2974}
2975
2976static void
2977hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2978 struct mem_array *pmem, struct be_dma_mem *sgl)
2979{
2980 if (sgl->va)
2981 be_sgl_destroy_contiguous(sgl);
2982
2983 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2984 pmem->bus_address.u.a64.address,
2985 pmem->size, sgl);
2986}
2987
2988static int be_fill_queue(struct be_queue_info *q,
2989 u16 len, u16 entry_size, void *vaddress)
2990{
2991 struct be_dma_mem *mem = &q->dma_mem;
2992
2993 memset(q, 0, sizeof(*q));
2994 q->len = len;
2995 q->entry_size = entry_size;
2996 mem->size = len * entry_size;
2997 mem->va = vaddress;
2998 if (!mem->va)
2999 return -ENOMEM;
3000 memset(mem->va, 0, mem->size);
3001 return 0;
3002}
3003
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303004static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303005 struct hwi_context_memory *phwi_context)
3006{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303007 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303008 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303009 struct be_queue_info *eq;
3010 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303011 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303012 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303013
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303014 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3015 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303016
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303017 if (phba->msix_enabled)
3018 eq_for_mcc = 1;
3019 else
3020 eq_for_mcc = 0;
3021 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3022 eq = &phwi_context->be_eq[i].q;
3023 mem = &eq->dma_mem;
3024 phwi_context->be_eq[i].phba = phba;
3025 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3026 num_eq_pages * PAGE_SIZE,
3027 &paddr);
3028 if (!eq_vaddress)
3029 goto create_eq_error;
3030
3031 mem->va = eq_vaddress;
3032 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3033 sizeof(struct be_eq_entry), eq_vaddress);
3034 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303035 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3036 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303037 goto create_eq_error;
3038 }
3039
3040 mem->dma = paddr;
3041 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3042 phwi_context->cur_eqd);
3043 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303044 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3045 "BM_%d : beiscsi_cmd_eq_create"
3046 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303047 goto create_eq_error;
3048 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303049
3050 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3051 "BM_%d : eqid = %d\n",
3052 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303053 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303054 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303055create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303056 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303057 eq = &phwi_context->be_eq[i].q;
3058 mem = &eq->dma_mem;
3059 if (mem->va)
3060 pci_free_consistent(phba->pcidev, num_eq_pages
3061 * PAGE_SIZE,
3062 mem->va, mem->dma);
3063 }
3064 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303065}
3066
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303067static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303068 struct hwi_context_memory *phwi_context)
3069{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303070 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303071 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303072 struct be_queue_info *cq, *eq;
3073 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303074 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303075 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303076 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303078 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3079 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303080
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303081 for (i = 0; i < phba->num_cpus; i++) {
3082 cq = &phwi_context->be_cq[i];
3083 eq = &phwi_context->be_eq[i].q;
3084 pbe_eq = &phwi_context->be_eq[i];
3085 pbe_eq->cq = cq;
3086 pbe_eq->phba = phba;
3087 mem = &cq->dma_mem;
3088 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3089 num_cq_pages * PAGE_SIZE,
3090 &paddr);
3091 if (!cq_vaddress)
3092 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303093 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303094 sizeof(struct sol_cqe), cq_vaddress);
3095 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303096 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3097 "BM_%d : be_fill_queue Failed "
3098 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303099 goto create_cq_error;
3100 }
3101
3102 mem->dma = paddr;
3103 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3104 false, 0);
3105 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303106 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3107 "BM_%d : beiscsi_cmd_eq_create"
3108 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303109 goto create_cq_error;
3110 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303111 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3112 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3113 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303114 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303115 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303116
3117create_cq_error:
3118 for (i = 0; i < phba->num_cpus; i++) {
3119 cq = &phwi_context->be_cq[i];
3120 mem = &cq->dma_mem;
3121 if (mem->va)
3122 pci_free_consistent(phba->pcidev, num_cq_pages
3123 * PAGE_SIZE,
3124 mem->va, mem->dma);
3125 }
3126 return ret;
3127
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303128}
3129
3130static int
3131beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3132 struct hwi_context_memory *phwi_context,
3133 struct hwi_controller *phwi_ctrlr,
3134 unsigned int def_pdu_ring_sz)
3135{
3136 unsigned int idx;
3137 int ret;
3138 struct be_queue_info *dq, *cq;
3139 struct be_dma_mem *mem;
3140 struct be_mem_descriptor *mem_descr;
3141 void *dq_vaddress;
3142
3143 idx = 0;
3144 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303145 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303146 mem = &dq->dma_mem;
3147 mem_descr = phba->init_mem;
3148 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
3149 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3150 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3151 sizeof(struct phys_addr),
3152 sizeof(struct phys_addr), dq_vaddress);
3153 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303154 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3155 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303156 return ret;
3157 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303158 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3159 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303160 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3161 def_pdu_ring_sz,
3162 phba->params.defpdu_hdr_sz);
3163 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303164 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3165 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303166 return ret;
3167 }
3168 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303169 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3170 "BM_%d : iscsi def pdu id is %d\n",
3171 phwi_context->be_def_hdrq.id);
3172
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303173 hwi_post_async_buffers(phba, 1);
3174 return 0;
3175}
3176
3177static int
3178beiscsi_create_def_data(struct beiscsi_hba *phba,
3179 struct hwi_context_memory *phwi_context,
3180 struct hwi_controller *phwi_ctrlr,
3181 unsigned int def_pdu_ring_sz)
3182{
3183 unsigned int idx;
3184 int ret;
3185 struct be_queue_info *dataq, *cq;
3186 struct be_dma_mem *mem;
3187 struct be_mem_descriptor *mem_descr;
3188 void *dq_vaddress;
3189
3190 idx = 0;
3191 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303192 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303193 mem = &dataq->dma_mem;
3194 mem_descr = phba->init_mem;
3195 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3196 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3197 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3198 sizeof(struct phys_addr),
3199 sizeof(struct phys_addr), dq_vaddress);
3200 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303201 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3202 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303203 return ret;
3204 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303205 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3206 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303207 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3208 def_pdu_ring_sz,
3209 phba->params.defpdu_data_sz);
3210 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303211 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3212 "BM_%d be_cmd_create_default_pdu_queue"
3213 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303214 return ret;
3215 }
3216 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303217 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3218 "BM_%d : iscsi def data id is %d\n",
3219 phwi_context->be_def_dataq.id);
3220
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303221 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303222 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3223 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3224
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303225 return 0;
3226}
3227
3228static int
3229beiscsi_post_pages(struct beiscsi_hba *phba)
3230{
3231 struct be_mem_descriptor *mem_descr;
3232 struct mem_array *pm_arr;
3233 unsigned int page_offset, i;
3234 struct be_dma_mem sgl;
3235 int status;
3236
3237 mem_descr = phba->init_mem;
3238 mem_descr += HWI_MEM_SGE;
3239 pm_arr = mem_descr->mem_array;
3240
3241 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3242 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3243 for (i = 0; i < mem_descr->num_elements; i++) {
3244 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3245 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3246 page_offset,
3247 (pm_arr->size / PAGE_SIZE));
3248 page_offset += pm_arr->size / PAGE_SIZE;
3249 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303250 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3251 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303252 return status;
3253 }
3254 pm_arr++;
3255 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303256 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3257 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303258 return 0;
3259}
3260
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303261static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3262{
3263 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003264 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303265 pci_free_consistent(phba->pcidev, mem->size,
3266 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003267 mem->va = NULL;
3268 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303269}
3270
3271static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3272 u16 len, u16 entry_size)
3273{
3274 struct be_dma_mem *mem = &q->dma_mem;
3275
3276 memset(q, 0, sizeof(*q));
3277 q->len = len;
3278 q->entry_size = entry_size;
3279 mem->size = len * entry_size;
3280 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3281 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303282 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303283 memset(mem->va, 0, mem->size);
3284 return 0;
3285}
3286
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303287static int
3288beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3289 struct hwi_context_memory *phwi_context,
3290 struct hwi_controller *phwi_ctrlr)
3291{
3292 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3293 u64 pa_addr_lo;
3294 unsigned int idx, num, i;
3295 struct mem_array *pwrb_arr;
3296 void *wrb_vaddr;
3297 struct be_dma_mem sgl;
3298 struct be_mem_descriptor *mem_descr;
3299 int status;
3300
3301 idx = 0;
3302 mem_descr = phba->init_mem;
3303 mem_descr += HWI_MEM_WRB;
3304 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3305 GFP_KERNEL);
3306 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303307 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3308 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303309 return -ENOMEM;
3310 }
3311 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3312 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3313 num_wrb_rings = mem_descr->mem_array[idx].size /
3314 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3315
3316 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3317 if (num_wrb_rings) {
3318 pwrb_arr[num].virtual_address = wrb_vaddr;
3319 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3320 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3321 sizeof(struct iscsi_wrb);
3322 wrb_vaddr += pwrb_arr[num].size;
3323 pa_addr_lo += pwrb_arr[num].size;
3324 num_wrb_rings--;
3325 } else {
3326 idx++;
3327 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3328 pa_addr_lo = mem_descr->mem_array[idx].\
3329 bus_address.u.a64.address;
3330 num_wrb_rings = mem_descr->mem_array[idx].size /
3331 (phba->params.wrbs_per_cxn *
3332 sizeof(struct iscsi_wrb));
3333 pwrb_arr[num].virtual_address = wrb_vaddr;
3334 pwrb_arr[num].bus_address.u.a64.address\
3335 = pa_addr_lo;
3336 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3337 sizeof(struct iscsi_wrb);
3338 wrb_vaddr += pwrb_arr[num].size;
3339 pa_addr_lo += pwrb_arr[num].size;
3340 num_wrb_rings--;
3341 }
3342 }
3343 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3344 wrb_mem_index = 0;
3345 offset = 0;
3346 size = 0;
3347
3348 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3349 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3350 &phwi_context->be_wrbq[i]);
3351 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303352 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3353 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003354 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303355 return status;
3356 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303357 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3358 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303359 }
3360 kfree(pwrb_arr);
3361 return 0;
3362}
3363
3364static void free_wrb_handles(struct beiscsi_hba *phba)
3365{
3366 unsigned int index;
3367 struct hwi_controller *phwi_ctrlr;
3368 struct hwi_wrb_context *pwrb_context;
3369
3370 phwi_ctrlr = phba->phwi_ctrlr;
3371 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3372 pwrb_context = &phwi_ctrlr->wrb_context[index];
3373 kfree(pwrb_context->pwrb_handle_base);
3374 kfree(pwrb_context->pwrb_handle_basestd);
3375 }
3376}
3377
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303378static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3379{
3380 struct be_queue_info *q;
3381 struct be_ctrl_info *ctrl = &phba->ctrl;
3382
3383 q = &phba->ctrl.mcc_obj.q;
3384 if (q->created)
3385 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3386 be_queue_free(phba, q);
3387
3388 q = &phba->ctrl.mcc_obj.cq;
3389 if (q->created)
3390 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3391 be_queue_free(phba, q);
3392}
3393
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303394static void hwi_cleanup(struct beiscsi_hba *phba)
3395{
3396 struct be_queue_info *q;
3397 struct be_ctrl_info *ctrl = &phba->ctrl;
3398 struct hwi_controller *phwi_ctrlr;
3399 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303400 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303401
3402 phwi_ctrlr = phba->phwi_ctrlr;
3403 phwi_context = phwi_ctrlr->phwi_ctxt;
3404 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3405 q = &phwi_context->be_wrbq[i];
3406 if (q->created)
3407 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3408 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303409 free_wrb_handles(phba);
3410
3411 q = &phwi_context->be_def_hdrq;
3412 if (q->created)
3413 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3414
3415 q = &phwi_context->be_def_dataq;
3416 if (q->created)
3417 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3418
3419 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3420
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303421 for (i = 0; i < (phba->num_cpus); i++) {
3422 q = &phwi_context->be_cq[i];
3423 if (q->created)
3424 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3425 }
3426 if (phba->msix_enabled)
3427 eq_num = 1;
3428 else
3429 eq_num = 0;
3430 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3431 q = &phwi_context->be_eq[i].q;
3432 if (q->created)
3433 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3434 }
3435 be_mcc_queues_destroy(phba);
Jayamohan Kallickal0283fbb2013-04-05 20:38:21 -07003436 be_cmd_fw_uninit(ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303437}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303438
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303439static int be_mcc_queues_create(struct beiscsi_hba *phba,
3440 struct hwi_context_memory *phwi_context)
3441{
3442 struct be_queue_info *q, *cq;
3443 struct be_ctrl_info *ctrl = &phba->ctrl;
3444
3445 /* Alloc MCC compl queue */
3446 cq = &phba->ctrl.mcc_obj.cq;
3447 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3448 sizeof(struct be_mcc_compl)))
3449 goto err;
3450 /* Ask BE to create MCC compl queue; */
3451 if (phba->msix_enabled) {
3452 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3453 [phba->num_cpus].q, false, true, 0))
3454 goto mcc_cq_free;
3455 } else {
3456 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3457 false, true, 0))
3458 goto mcc_cq_free;
3459 }
3460
3461 /* Alloc MCC queue */
3462 q = &phba->ctrl.mcc_obj.q;
3463 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3464 goto mcc_cq_destroy;
3465
3466 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303467 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303468 goto mcc_q_free;
3469
3470 return 0;
3471
3472mcc_q_free:
3473 be_queue_free(phba, q);
3474mcc_cq_destroy:
3475 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3476mcc_cq_free:
3477 be_queue_free(phba, cq);
3478err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303479 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303480}
3481
John Soni Jose107dfcb2012-10-20 04:42:13 +05303482/**
3483 * find_num_cpus()- Get the CPU online count
3484 * @phba: ptr to priv structure
3485 *
3486 * CPU count is used for creating EQ.
3487 **/
3488static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303489{
3490 int num_cpus = 0;
3491
3492 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303493
John Soni Jose22abeef2012-10-20 04:43:32 +05303494 switch (phba->generation) {
3495 case BE_GEN2:
3496 case BE_GEN3:
3497 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3498 BEISCSI_MAX_NUM_CPUS : num_cpus;
3499 break;
3500 case BE_GEN4:
3501 phba->num_cpus = (num_cpus > OC_SKH_MAX_NUM_CPUS) ?
3502 OC_SKH_MAX_NUM_CPUS : num_cpus;
3503 break;
3504 default:
3505 phba->num_cpus = 1;
3506 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303507}
3508
3509static int hwi_init_port(struct beiscsi_hba *phba)
3510{
3511 struct hwi_controller *phwi_ctrlr;
3512 struct hwi_context_memory *phwi_context;
3513 unsigned int def_pdu_ring_sz;
3514 struct be_ctrl_info *ctrl = &phba->ctrl;
3515 int status;
3516
3517 def_pdu_ring_sz =
3518 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3519 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303520 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303521 phwi_context->max_eqd = 0;
3522 phwi_context->min_eqd = 0;
3523 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303524 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303525
3526 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303527 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303528 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3529 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303530 goto error;
3531 }
3532
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303533 status = be_mcc_queues_create(phba, phwi_context);
3534 if (status != 0)
3535 goto error;
3536
3537 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303538 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303539 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3540 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303541 goto error;
3542 }
3543
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303544 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303545 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303546 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3547 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303548 goto error;
3549 }
3550
3551 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3552 def_pdu_ring_sz);
3553 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303554 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3555 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303556 goto error;
3557 }
3558
3559 status = beiscsi_create_def_data(phba, phwi_context,
3560 phwi_ctrlr, def_pdu_ring_sz);
3561 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303562 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3563 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303564 goto error;
3565 }
3566
3567 status = beiscsi_post_pages(phba);
3568 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303569 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3570 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303571 goto error;
3572 }
3573
3574 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3575 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303576 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3577 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303578 goto error;
3579 }
3580
John Soni Jose99bc5d52012-08-20 23:00:18 +05303581 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3582 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303583 return 0;
3584
3585error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303586 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3587 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303588 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003589 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590}
3591
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303592static int hwi_init_controller(struct beiscsi_hba *phba)
3593{
3594 struct hwi_controller *phwi_ctrlr;
3595
3596 phwi_ctrlr = phba->phwi_ctrlr;
3597 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3598 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3599 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303600 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3601 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3602 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303603 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303604 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3605 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3606 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303607 return -ENOMEM;
3608 }
3609
3610 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003611 if (beiscsi_init_wrb_handle(phba))
3612 return -ENOMEM;
3613
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303614 hwi_init_async_pdu_ctx(phba);
3615 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303616 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3617 "BM_%d : hwi_init_controller failed\n");
3618
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303619 return -ENOMEM;
3620 }
3621 return 0;
3622}
3623
3624static void beiscsi_free_mem(struct beiscsi_hba *phba)
3625{
3626 struct be_mem_descriptor *mem_descr;
3627 int i, j;
3628
3629 mem_descr = phba->init_mem;
3630 i = 0;
3631 j = 0;
3632 for (i = 0; i < SE_MEM_MAX; i++) {
3633 for (j = mem_descr->num_elements; j > 0; j--) {
3634 pci_free_consistent(phba->pcidev,
3635 mem_descr->mem_array[j - 1].size,
3636 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303637 (unsigned long)mem_descr->mem_array[j - 1].
3638 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303639 }
3640 kfree(mem_descr->mem_array);
3641 mem_descr++;
3642 }
3643 kfree(phba->init_mem);
3644 kfree(phba->phwi_ctrlr);
3645}
3646
3647static int beiscsi_init_controller(struct beiscsi_hba *phba)
3648{
3649 int ret = -ENOMEM;
3650
3651 ret = beiscsi_get_memory(phba);
3652 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303653 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3654 "BM_%d : beiscsi_dev_probe -"
3655 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303656 return ret;
3657 }
3658
3659 ret = hwi_init_controller(phba);
3660 if (ret)
3661 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303662 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3663 "BM_%d : Return success from beiscsi_init_controller");
3664
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303665 return 0;
3666
3667free_init:
3668 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003669 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303670}
3671
3672static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3673{
3674 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3675 struct sgl_handle *psgl_handle;
3676 struct iscsi_sge *pfrag;
3677 unsigned int arr_index, i, idx;
3678
3679 phba->io_sgl_hndl_avbl = 0;
3680 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303681
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303682 mem_descr_sglh = phba->init_mem;
3683 mem_descr_sglh += HWI_MEM_SGLH;
3684 if (1 == mem_descr_sglh->num_elements) {
3685 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3686 phba->params.ios_per_ctrl,
3687 GFP_KERNEL);
3688 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303689 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3690 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303691 return -ENOMEM;
3692 }
3693 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3694 (phba->params.icds_per_ctrl -
3695 phba->params.ios_per_ctrl),
3696 GFP_KERNEL);
3697 if (!phba->eh_sgl_hndl_base) {
3698 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303699 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3700 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303701 return -ENOMEM;
3702 }
3703 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303704 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3705 "BM_%d : HWI_MEM_SGLH is more than one element."
3706 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303707 return -ENOMEM;
3708 }
3709
3710 arr_index = 0;
3711 idx = 0;
3712 while (idx < mem_descr_sglh->num_elements) {
3713 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3714
3715 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3716 sizeof(struct sgl_handle)); i++) {
3717 if (arr_index < phba->params.ios_per_ctrl) {
3718 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3719 phba->io_sgl_hndl_avbl++;
3720 arr_index++;
3721 } else {
3722 phba->eh_sgl_hndl_base[arr_index -
3723 phba->params.ios_per_ctrl] =
3724 psgl_handle;
3725 arr_index++;
3726 phba->eh_sgl_hndl_avbl++;
3727 }
3728 psgl_handle++;
3729 }
3730 idx++;
3731 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303732 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3733 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3734 "phba->eh_sgl_hndl_avbl=%d\n",
3735 phba->io_sgl_hndl_avbl,
3736 phba->eh_sgl_hndl_avbl);
3737
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303738 mem_descr_sg = phba->init_mem;
3739 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303740 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3741 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3742 mem_descr_sg->num_elements);
3743
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303744 arr_index = 0;
3745 idx = 0;
3746 while (idx < mem_descr_sg->num_elements) {
3747 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3748
3749 for (i = 0;
3750 i < (mem_descr_sg->mem_array[idx].size) /
3751 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3752 i++) {
3753 if (arr_index < phba->params.ios_per_ctrl)
3754 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3755 else
3756 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3757 phba->params.ios_per_ctrl];
3758 psgl_handle->pfrag = pfrag;
3759 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3760 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3761 pfrag += phba->params.num_sge_per_io;
3762 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303763 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303764 }
3765 idx++;
3766 }
3767 phba->io_sgl_free_index = 0;
3768 phba->io_sgl_alloc_index = 0;
3769 phba->eh_sgl_free_index = 0;
3770 phba->eh_sgl_alloc_index = 0;
3771 return 0;
3772}
3773
3774static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3775{
3776 int i, new_cid;
3777
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303778 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303779 GFP_KERNEL);
3780 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303781 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3782 "BM_%d : Failed to allocate memory in "
3783 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303784 return -ENOMEM;
3785 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303786 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303787 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3788 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303789 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3790 "BM_%d : Failed to allocate memory in "
3791 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303792 kfree(phba->cid_array);
3793 return -ENOMEM;
3794 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303795 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303796 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3797 phba->cid_array[i] = new_cid;
3798 new_cid += 2;
3799 }
3800 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3801 return 0;
3802}
3803
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303804static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303805{
3806 struct be_ctrl_info *ctrl = &phba->ctrl;
3807 struct hwi_controller *phwi_ctrlr;
3808 struct hwi_context_memory *phwi_context;
3809 struct be_queue_info *eq;
3810 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303811 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303812 u32 enabled;
3813
3814 phwi_ctrlr = phba->phwi_ctrlr;
3815 phwi_context = phwi_ctrlr->phwi_ctxt;
3816
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303817 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3818 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3819 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303820
3821 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3822 if (!enabled) {
3823 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303824 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3825 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303826 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003827 }
3828
3829 if (!phba->msix_enabled) {
3830 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303831 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3832 "BM_%d : eq->id=%d\n", eq->id);
3833
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003834 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3835 } else {
3836 for (i = 0; i <= phba->num_cpus; i++) {
3837 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303838 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3839 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303840 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3841 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303842 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303843}
3844
3845static void hwi_disable_intr(struct beiscsi_hba *phba)
3846{
3847 struct be_ctrl_info *ctrl = &phba->ctrl;
3848
3849 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3850 u32 reg = ioread32(addr);
3851
3852 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3853 if (enabled) {
3854 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3855 iowrite32(reg, addr);
3856 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303857 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3858 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303859}
3860
John Soni Jose9aef4202012-08-20 23:00:08 +05303861/**
3862 * beiscsi_get_boot_info()- Get the boot session info
3863 * @phba: The device priv structure instance
3864 *
3865 * Get the boot target info and store in driver priv structure
3866 *
3867 * return values
3868 * Success: 0
3869 * Failure: Non-Zero Value
3870 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303871static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3872{
Mike Christie0e438952012-04-03 23:41:51 -05003873 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303874 struct be_dma_mem nonemb_cmd;
John Soni Josee175def2012-10-20 04:45:40 +05303875 unsigned int tag;
John Soni Jose9aef4202012-08-20 23:00:08 +05303876 unsigned int s_handle;
Mike Christief457a462011-06-24 15:11:53 -05003877 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303878
John Soni Jose9aef4202012-08-20 23:00:08 +05303879 /* Get the session handle of the boot target */
3880 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3881 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303882 beiscsi_log(phba, KERN_ERR,
3883 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3884 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303885 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303886 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303887 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3888 sizeof(*session_resp),
3889 &nonemb_cmd.dma);
3890 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303891 beiscsi_log(phba, KERN_ERR,
3892 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3893 "BM_%d : Failed to allocate memory for"
3894 "beiscsi_get_session_info\n");
3895
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303896 return -ENOMEM;
3897 }
3898
3899 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303900 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003901 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303902 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303903 beiscsi_log(phba, KERN_ERR,
3904 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3905 "BM_%d : beiscsi_get_session_info"
3906 " Failed\n");
3907
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303908 goto boot_freemem;
John Soni Josee175def2012-10-20 04:45:40 +05303909 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303910
John Soni Josee175def2012-10-20 04:45:40 +05303911 ret = beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
3912 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303913 beiscsi_log(phba, KERN_ERR,
3914 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
John Soni Josee175def2012-10-20 04:45:40 +05303915 "BM_%d : beiscsi_get_session_info Failed");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303916 goto boot_freemem;
3917 }
John Soni Josee175def2012-10-20 04:45:40 +05303918
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303919 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003920
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303921 memcpy(&phba->boot_sess, &session_resp->session_info,
3922 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003923 ret = 0;
3924
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303925boot_freemem:
3926 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3927 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003928 return ret;
3929}
3930
3931static void beiscsi_boot_release(void *data)
3932{
3933 struct beiscsi_hba *phba = data;
3934
3935 scsi_host_put(phba->shost);
3936}
3937
3938static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3939{
3940 struct iscsi_boot_kobj *boot_kobj;
3941
3942 /* get boot info using mgmt cmd */
3943 if (beiscsi_get_boot_info(phba))
3944 /* Try to see if we can carry on without this */
3945 return 0;
3946
3947 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3948 if (!phba->boot_kset)
3949 return -ENOMEM;
3950
3951 /* get a ref because the show function will ref the phba */
3952 if (!scsi_host_get(phba->shost))
3953 goto free_kset;
3954 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3955 beiscsi_show_boot_tgt_info,
3956 beiscsi_tgt_get_attr_visibility,
3957 beiscsi_boot_release);
3958 if (!boot_kobj)
3959 goto put_shost;
3960
3961 if (!scsi_host_get(phba->shost))
3962 goto free_kset;
3963 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3964 beiscsi_show_boot_ini_info,
3965 beiscsi_ini_get_attr_visibility,
3966 beiscsi_boot_release);
3967 if (!boot_kobj)
3968 goto put_shost;
3969
3970 if (!scsi_host_get(phba->shost))
3971 goto free_kset;
3972 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3973 beiscsi_show_boot_eth_info,
3974 beiscsi_eth_get_attr_visibility,
3975 beiscsi_boot_release);
3976 if (!boot_kobj)
3977 goto put_shost;
3978 return 0;
3979
3980put_shost:
3981 scsi_host_put(phba->shost);
3982free_kset:
3983 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303984 return -ENOMEM;
3985}
3986
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303987static int beiscsi_init_port(struct beiscsi_hba *phba)
3988{
3989 int ret;
3990
3991 ret = beiscsi_init_controller(phba);
3992 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303993 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3994 "BM_%d : beiscsi_dev_probe - Failed in"
3995 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303996 return ret;
3997 }
3998 ret = beiscsi_init_sgl_handle(phba);
3999 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304000 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4001 "BM_%d : beiscsi_dev_probe - Failed in"
4002 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304003 goto do_cleanup_ctrlr;
4004 }
4005
4006 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304007 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4008 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304009 kfree(phba->io_sgl_hndl_base);
4010 kfree(phba->eh_sgl_hndl_base);
4011 goto do_cleanup_ctrlr;
4012 }
4013
4014 return ret;
4015
4016do_cleanup_ctrlr:
4017 hwi_cleanup(phba);
4018 return ret;
4019}
4020
4021static void hwi_purge_eq(struct beiscsi_hba *phba)
4022{
4023 struct hwi_controller *phwi_ctrlr;
4024 struct hwi_context_memory *phwi_context;
4025 struct be_queue_info *eq;
4026 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304027 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304028 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304029
4030 phwi_ctrlr = phba->phwi_ctrlr;
4031 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304032 if (phba->msix_enabled)
4033 eq_msix = 1;
4034 else
4035 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304036
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304037 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4038 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304039 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304040 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304041 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4042 & EQE_VALID_MASK) {
4043 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4044 queue_tail_inc(eq);
4045 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304046 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304047 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304048
4049 if (num_processed)
4050 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304051 }
4052}
4053
4054static void beiscsi_clean_port(struct beiscsi_hba *phba)
4055{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05304056 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304057
4058 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
4059 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304060 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4061 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304062
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304063 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304064 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304065 kfree(phba->io_sgl_hndl_base);
4066 kfree(phba->eh_sgl_hndl_base);
4067 kfree(phba->cid_array);
4068 kfree(phba->ep_array);
4069}
4070
John Soni Josed629c472012-10-20 04:42:00 +05304071/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004072 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4073 * @beiscsi_conn: ptr to the conn to be cleaned up
4074 *
4075 * Free driver mgmt resources binded to CXN.
4076 **/
4077void
4078beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn)
4079{
4080 struct beiscsi_io_task *io_task;
4081 struct beiscsi_hba *phba = beiscsi_conn->phba;
4082 struct hwi_wrb_context *pwrb_context;
4083 struct hwi_controller *phwi_ctrlr;
4084
4085 phwi_ctrlr = phba->phwi_ctrlr;
4086 pwrb_context = &phwi_ctrlr->wrb_context
4087 [beiscsi_conn->beiscsi_conn_cid
4088 - phba->fw_config.iscsi_cid_start];
4089 io_task = beiscsi_conn->task->dd_data;
4090
4091 if (io_task->pwrb_handle) {
4092 memset(io_task->pwrb_handle->pwrb, 0,
4093 sizeof(struct iscsi_wrb));
4094 free_wrb_handle(phba, pwrb_context,
4095 io_task->pwrb_handle);
4096 io_task->pwrb_handle = NULL;
4097 }
4098
4099 if (io_task->psgl_handle) {
4100 spin_lock_bh(&phba->mgmt_sgl_lock);
4101 free_mgmt_sgl_handle(phba,
4102 io_task->psgl_handle);
4103 spin_unlock_bh(&phba->mgmt_sgl_lock);
4104 io_task->psgl_handle = NULL;
4105 }
4106
4107 if (io_task->mtask_addr)
4108 pci_unmap_single(phba->pcidev,
4109 io_task->mtask_addr,
4110 io_task->mtask_data_count,
4111 PCI_DMA_TODEVICE);
4112}
4113
4114/**
John Soni Josed629c472012-10-20 04:42:00 +05304115 * beiscsi_cleanup_task()- Free driver resources of the task
4116 * @task: ptr to the iscsi task
4117 *
4118 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004119static void beiscsi_cleanup_task(struct iscsi_task *task)
4120{
4121 struct beiscsi_io_task *io_task = task->dd_data;
4122 struct iscsi_conn *conn = task->conn;
4123 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4124 struct beiscsi_hba *phba = beiscsi_conn->phba;
4125 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4126 struct hwi_wrb_context *pwrb_context;
4127 struct hwi_controller *phwi_ctrlr;
4128
4129 phwi_ctrlr = phba->phwi_ctrlr;
4130 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
4131 - phba->fw_config.iscsi_cid_start];
4132
4133 if (io_task->cmd_bhs) {
4134 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4135 io_task->bhs_pa.u.a64.address);
4136 io_task->cmd_bhs = NULL;
4137 }
4138
4139 if (task->sc) {
4140 if (io_task->pwrb_handle) {
4141 free_wrb_handle(phba, pwrb_context,
4142 io_task->pwrb_handle);
4143 io_task->pwrb_handle = NULL;
4144 }
4145
4146 if (io_task->psgl_handle) {
4147 spin_lock(&phba->io_sgl_lock);
4148 free_io_sgl_handle(phba, io_task->psgl_handle);
4149 spin_unlock(&phba->io_sgl_lock);
4150 io_task->psgl_handle = NULL;
4151 }
4152 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004153 if (!beiscsi_conn->login_in_progress)
4154 beiscsi_free_mgmt_task_handles(beiscsi_conn);
Mike Christie1282ab72012-04-18 03:06:00 -05004155 }
4156}
4157
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304158void
4159beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4160 struct beiscsi_offload_params *params)
4161{
4162 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304163 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004164 struct iscsi_task *task = beiscsi_conn->task;
4165 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304166 u32 doorbell = 0;
4167
4168 /*
4169 * We can always use 0 here because it is reserved by libiscsi for
4170 * login/startup related tasks.
4171 */
Mike Christie1282ab72012-04-18 03:06:00 -05004172 beiscsi_conn->login_in_progress = 0;
4173 spin_lock_bh(&session->lock);
4174 beiscsi_cleanup_task(task);
4175 spin_unlock_bh(&session->lock);
4176
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304177 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05304178 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304179
John Soni Joseacb96932012-10-20 04:44:35 +05304180 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004181 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304182 beiscsi_offload_cxn_v0(params, pwrb_handle,
4183 phba->init_mem);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004184 else
4185 beiscsi_offload_cxn_v2(params, pwrb_handle);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304186
John Soni Joseacb96932012-10-20 04:44:35 +05304187 be_dws_le_to_cpu(pwrb_handle->pwrb,
4188 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304189
4190 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304191 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304192 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304193 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4194
4195 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4196}
4197
4198static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4199 int *index, int *age)
4200{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304201 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304202 if (age)
4203 *age = conn->session->age;
4204}
4205
4206/**
4207 * beiscsi_alloc_pdu - allocates pdu and related resources
4208 * @task: libiscsi task
4209 * @opcode: opcode of pdu for task
4210 *
4211 * This is called with the session lock held. It will allocate
4212 * the wrb and sgl if needed for the command. And it will prep
4213 * the pdu's itt. beiscsi_parse_pdu will later translate
4214 * the pdu itt to the libiscsi task itt.
4215 */
4216static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4217{
4218 struct beiscsi_io_task *io_task = task->dd_data;
4219 struct iscsi_conn *conn = task->conn;
4220 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4221 struct beiscsi_hba *phba = beiscsi_conn->phba;
4222 struct hwi_wrb_context *pwrb_context;
4223 struct hwi_controller *phwi_ctrlr;
4224 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304225 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4226 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304227
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304228 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004229 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304230 if (!io_task->cmd_bhs)
4231 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304232 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304233 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304234 io_task->conn = beiscsi_conn;
4235
4236 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4237 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304238 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004239 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304240
4241 if (task->sc) {
4242 spin_lock(&phba->io_sgl_lock);
4243 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4244 spin_unlock(&phba->io_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304245 if (!io_task->psgl_handle) {
4246 beiscsi_log(phba, KERN_ERR,
4247 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4248 "BM_%d : Alloc of IO_SGL_ICD Failed"
4249 "for the CID : %d\n",
4250 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304251 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304252 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304253 io_task->pwrb_handle = alloc_wrb_handle(phba,
4254 beiscsi_conn->beiscsi_conn_cid -
4255 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304256 if (!io_task->pwrb_handle) {
4257 beiscsi_log(phba, KERN_ERR,
4258 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4259 "BM_%d : Alloc of WRB_HANDLE Failed"
4260 "for the CID : %d\n",
4261 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304262 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304263 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304264 } else {
4265 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304266 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004267 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304268 if (!beiscsi_conn->login_in_progress) {
4269 spin_lock(&phba->mgmt_sgl_lock);
4270 io_task->psgl_handle = (struct sgl_handle *)
4271 alloc_mgmt_sgl_handle(phba);
4272 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304273 if (!io_task->psgl_handle) {
4274 beiscsi_log(phba, KERN_ERR,
4275 BEISCSI_LOG_IO |
4276 BEISCSI_LOG_CONFIG,
4277 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4278 "for the CID : %d\n",
4279 beiscsi_conn->
4280 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304281 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304282 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304283
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304284 beiscsi_conn->login_in_progress = 1;
4285 beiscsi_conn->plogin_sgl_handle =
4286 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304287 io_task->pwrb_handle =
4288 alloc_wrb_handle(phba,
4289 beiscsi_conn->beiscsi_conn_cid -
4290 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304291 if (!io_task->pwrb_handle) {
4292 beiscsi_log(phba, KERN_ERR,
4293 BEISCSI_LOG_IO |
4294 BEISCSI_LOG_CONFIG,
4295 "BM_%d : Alloc of WRB_HANDLE Failed"
4296 "for the CID : %d\n",
4297 beiscsi_conn->
4298 beiscsi_conn_cid);
4299 goto free_mgmt_hndls;
4300 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304301 beiscsi_conn->plogin_wrb_handle =
4302 io_task->pwrb_handle;
4303
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304304 } else {
4305 io_task->psgl_handle =
4306 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304307 io_task->pwrb_handle =
4308 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304309 }
4310 } else {
4311 spin_lock(&phba->mgmt_sgl_lock);
4312 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4313 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304314 if (!io_task->psgl_handle) {
4315 beiscsi_log(phba, KERN_ERR,
4316 BEISCSI_LOG_IO |
4317 BEISCSI_LOG_CONFIG,
4318 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4319 "for the CID : %d\n",
4320 beiscsi_conn->
4321 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304322 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304323 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304324 io_task->pwrb_handle =
4325 alloc_wrb_handle(phba,
4326 beiscsi_conn->beiscsi_conn_cid -
4327 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304328 if (!io_task->pwrb_handle) {
4329 beiscsi_log(phba, KERN_ERR,
4330 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4331 "BM_%d : Alloc of WRB_HANDLE Failed"
4332 "for the CID : %d\n",
4333 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304334 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304335 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304336
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304337 }
4338 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304339 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4340 wrb_index << 16) | (unsigned int)
4341 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304342 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304343
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304344 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4345 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304346
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304347free_io_hndls:
4348 spin_lock(&phba->io_sgl_lock);
4349 free_io_sgl_handle(phba, io_task->psgl_handle);
4350 spin_unlock(&phba->io_sgl_lock);
4351 goto free_hndls;
4352free_mgmt_hndls:
4353 spin_lock(&phba->mgmt_sgl_lock);
4354 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4355 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304356free_hndls:
4357 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304358 pwrb_context = &phwi_ctrlr->wrb_context[
4359 beiscsi_conn->beiscsi_conn_cid -
4360 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304361 if (io_task->pwrb_handle)
4362 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304363 io_task->pwrb_handle = NULL;
4364 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4365 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004366 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304367 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304368}
John Soni Jose09a10932012-10-20 04:44:23 +05304369int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4370 unsigned int num_sg, unsigned int xferlen,
4371 unsigned int writedir)
4372{
4373
4374 struct beiscsi_io_task *io_task = task->dd_data;
4375 struct iscsi_conn *conn = task->conn;
4376 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4377 struct beiscsi_hba *phba = beiscsi_conn->phba;
4378 struct iscsi_wrb *pwrb = NULL;
4379 unsigned int doorbell = 0;
4380
4381 pwrb = io_task->pwrb_handle->pwrb;
4382 memset(pwrb, 0, sizeof(*pwrb));
4383
4384 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4385 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4386
4387 if (writedir) {
4388 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4389 INI_WR_CMD);
4390 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4391 } else {
4392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4393 INI_RD_CMD);
4394 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4395 }
4396
4397 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4398 type, pwrb);
4399
4400 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4401 cpu_to_be16(*(unsigned short *)
4402 &io_task->cmd_bhs->iscsi_hdr.lun));
4403 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4404 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4405 io_task->pwrb_handle->wrb_index);
4406 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4407 be32_to_cpu(task->cmdsn));
4408 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4409 io_task->psgl_handle->sgl_index);
4410
4411 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4412 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4413 io_task->pwrb_handle->nxt_wrb_index);
4414
4415 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4416
4417 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4418 doorbell |= (io_task->pwrb_handle->wrb_index &
4419 DB_DEF_PDU_WRB_INDEX_MASK) <<
4420 DB_DEF_PDU_WRB_INDEX_SHIFT;
4421 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4422 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4423 return 0;
4424}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304425
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304426static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4427 unsigned int num_sg, unsigned int xferlen,
4428 unsigned int writedir)
4429{
4430
4431 struct beiscsi_io_task *io_task = task->dd_data;
4432 struct iscsi_conn *conn = task->conn;
4433 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4434 struct beiscsi_hba *phba = beiscsi_conn->phba;
4435 struct iscsi_wrb *pwrb = NULL;
4436 unsigned int doorbell = 0;
4437
4438 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304439 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4440 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4441
4442 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304443 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4444 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304445 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304446 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304447 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4448 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304449 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4450 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304451
John Soni Jose09a10932012-10-20 04:44:23 +05304452 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4453 type, pwrb);
4454
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304455 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004456 cpu_to_be16(*(unsigned short *)
4457 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304458 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4459 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4460 io_task->pwrb_handle->wrb_index);
4461 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4462 be32_to_cpu(task->cmdsn));
4463 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4464 io_task->psgl_handle->sgl_index);
4465
4466 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4467
4468 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4469 io_task->pwrb_handle->nxt_wrb_index);
4470 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4471
4472 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304473 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304474 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4475 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4476
4477 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4478 return 0;
4479}
4480
4481static int beiscsi_mtask(struct iscsi_task *task)
4482{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304483 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304484 struct iscsi_conn *conn = task->conn;
4485 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4486 struct beiscsi_hba *phba = beiscsi_conn->phba;
4487 struct iscsi_wrb *pwrb = NULL;
4488 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304489 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304490 unsigned int pwrb_typeoffset = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304491
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304492 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304493 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304494 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304495
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004496 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304497 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4498 be32_to_cpu(task->cmdsn));
4499 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4500 io_task->pwrb_handle->wrb_index);
4501 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4502 io_task->psgl_handle->sgl_index);
4503 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4504 task->data_count);
4505 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4506 io_task->pwrb_handle->nxt_wrb_index);
4507 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004508 } else {
4509 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4510 be32_to_cpu(task->cmdsn));
4511 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4512 io_task->pwrb_handle->wrb_index);
4513 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4514 io_task->psgl_handle->sgl_index);
4515 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4516 task->data_count);
4517 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4518 io_task->pwrb_handle->nxt_wrb_index);
4519 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304520 }
4521
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304522
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304523 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4524 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304525 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304526 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304527 hwi_write_buffer(pwrb, task);
4528 break;
4529 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004530 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304531 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004532 if (is_chip_be2_be3r(phba))
4533 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304534 dmsg, pwrb, 1);
4535 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004536 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304537 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004538 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304539 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004540 if (is_chip_be2_be3r(phba))
4541 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304542 dmsg, pwrb, 0);
4543 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004544 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304545 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004546 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304547 hwi_write_buffer(pwrb, task);
4548 break;
4549 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304550 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304551 hwi_write_buffer(pwrb, task);
4552 break;
4553 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304554 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304555 hwi_write_buffer(pwrb, task);
4556 break;
4557 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304558 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304559 hwi_write_buffer(pwrb, task);
4560 break;
4561
4562 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304563 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4564 "BM_%d : opcode =%d Not supported\n",
4565 task->hdr->opcode & ISCSI_OPCODE_MASK);
4566
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304567 return -EINVAL;
4568 }
4569
John Soni Jose09a10932012-10-20 04:44:23 +05304570 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004571 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4572 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4573 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304574
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304575 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304576 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304577 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4578 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4579 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4580 return 0;
4581}
4582
4583static int beiscsi_task_xmit(struct iscsi_task *task)
4584{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304585 struct beiscsi_io_task *io_task = task->dd_data;
4586 struct scsi_cmnd *sc = task->sc;
John Soni Jose09a10932012-10-20 04:44:23 +05304587 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304588 struct scatterlist *sg;
4589 int num_sg;
4590 unsigned int writedir = 0, xferlen = 0;
4591
John Soni Jose09a10932012-10-20 04:44:23 +05304592 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
4593
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304594 if (!sc)
4595 return beiscsi_mtask(task);
4596
4597 io_task->scsi_cmnd = sc;
4598 num_sg = scsi_dma_map(sc);
4599 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304600 struct iscsi_conn *conn = task->conn;
4601 struct beiscsi_hba *phba = NULL;
4602
4603 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4604 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4605 "BM_%d : scsi_dma_map Failed\n");
4606
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304607 return num_sg;
4608 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304609 xferlen = scsi_bufflen(sc);
4610 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304611 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304612 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304613 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304614 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304615
John Soni Jose09a10932012-10-20 04:44:23 +05304616 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304617}
4618
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004619/**
4620 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4621 * @job: job to handle
4622 */
4623static int beiscsi_bsg_request(struct bsg_job *job)
4624{
4625 struct Scsi_Host *shost;
4626 struct beiscsi_hba *phba;
4627 struct iscsi_bsg_request *bsg_req = job->request;
4628 int rc = -EINVAL;
4629 unsigned int tag;
4630 struct be_dma_mem nonemb_cmd;
4631 struct be_cmd_resp_hdr *resp;
4632 struct iscsi_bsg_reply *bsg_reply = job->reply;
4633 unsigned short status, extd_status;
4634
4635 shost = iscsi_job_to_shost(job);
4636 phba = iscsi_host_priv(shost);
4637
4638 switch (bsg_req->msgcode) {
4639 case ISCSI_BSG_HST_VENDOR:
4640 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4641 job->request_payload.payload_len,
4642 &nonemb_cmd.dma);
4643 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304644 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4645 "BM_%d : Failed to allocate memory for "
4646 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304647 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004648 }
4649 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4650 &nonemb_cmd);
4651 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304652 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304653 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304654
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004655 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4656 nonemb_cmd.va, nonemb_cmd.dma);
4657 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304658 }
4659
4660 rc = wait_event_interruptible_timeout(
4661 phba->ctrl.mcc_wait[tag],
4662 phba->ctrl.mcc_numtag[tag],
4663 msecs_to_jiffies(
4664 BEISCSI_HOST_MBX_TIMEOUT));
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004665 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4666 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4667 free_mcc_tag(&phba->ctrl, tag);
4668 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4669 sg_copy_from_buffer(job->reply_payload.sg_list,
4670 job->reply_payload.sg_cnt,
4671 nonemb_cmd.va, (resp->response_length
4672 + sizeof(*resp)));
4673 bsg_reply->reply_payload_rcv_len = resp->response_length;
4674 bsg_reply->result = status;
4675 bsg_job_done(job, bsg_reply->result,
4676 bsg_reply->reply_payload_rcv_len);
4677 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4678 nonemb_cmd.va, nonemb_cmd.dma);
4679 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304680 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304681 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304682 " status = %d extd_status = %d\n",
4683 status, extd_status);
4684
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004685 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304686 } else {
4687 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004688 }
4689 break;
4690
4691 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304692 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4693 "BM_%d : Unsupported bsg command: 0x%x\n",
4694 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004695 break;
4696 }
4697
4698 return rc;
4699}
4700
John Soni Jose99bc5d52012-08-20 23:00:18 +05304701void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4702{
4703 /* Set the logging parameter */
4704 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4705}
4706
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304707/*
4708 * beiscsi_quiesce()- Cleanup Driver resources
4709 * @phba: Instance Priv structure
4710 *
4711 * Free the OS and HW resources held by the driver
4712 **/
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004713static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304714{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304715 struct hwi_controller *phwi_ctrlr;
4716 struct hwi_context_memory *phwi_context;
4717 struct be_eq_obj *pbe_eq;
4718 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304719
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304720 phwi_ctrlr = phba->phwi_ctrlr;
4721 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304723 if (phba->msix_enabled) {
4724 for (i = 0; i <= phba->num_cpus; i++) {
4725 msix_vec = phba->msix_entries[i].vector;
4726 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004727 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304728 }
4729 } else
4730 if (phba->pcidev->irq)
4731 free_irq(phba->pcidev->irq, phba);
4732 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304733 destroy_workqueue(phba->wq);
4734 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304735 for (i = 0; i < phba->num_cpus; i++) {
4736 pbe_eq = &phwi_context->be_eq[i];
4737 blk_iopoll_disable(&pbe_eq->iopoll);
4738 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304739
4740 beiscsi_clean_port(phba);
4741 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304742
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304743 beiscsi_unmap_pci_function(phba);
4744 pci_free_consistent(phba->pcidev,
4745 phba->ctrl.mbox_mem_alloced.size,
4746 phba->ctrl.mbox_mem_alloced.va,
4747 phba->ctrl.mbox_mem_alloced.dma);
John Soni Jose7a158002012-10-20 04:45:51 +05304748
4749 cancel_delayed_work_sync(&phba->beiscsi_hw_check_task);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004750}
4751
4752static void beiscsi_remove(struct pci_dev *pcidev)
4753{
4754
4755 struct beiscsi_hba *phba = NULL;
4756
4757 phba = pci_get_drvdata(pcidev);
4758 if (!phba) {
4759 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4760 return;
4761 }
4762
Mike Christie0e438952012-04-03 23:41:51 -05004763 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004764 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004765 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304766 iscsi_host_remove(phba->shost);
4767 pci_dev_put(phba->pcidev);
4768 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004769 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304770}
4771
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004772static void beiscsi_shutdown(struct pci_dev *pcidev)
4773{
4774
4775 struct beiscsi_hba *phba = NULL;
4776
4777 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4778 if (!phba) {
4779 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4780 return;
4781 }
4782
4783 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004784 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004785}
4786
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304787static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4788{
4789 int i, status;
4790
4791 for (i = 0; i <= phba->num_cpus; i++)
4792 phba->msix_entries[i].entry = i;
4793
4794 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4795 (phba->num_cpus + 1));
4796 if (!status)
4797 phba->msix_enabled = true;
4798
4799 return;
4800}
4801
John Soni Jose7a158002012-10-20 04:45:51 +05304802/*
4803 * beiscsi_hw_health_check()- Check adapter health
4804 * @work: work item to check HW health
4805 *
4806 * Check if adapter in an unrecoverable state or not.
4807 **/
4808static void
4809beiscsi_hw_health_check(struct work_struct *work)
4810{
4811 struct beiscsi_hba *phba =
4812 container_of(work, struct beiscsi_hba,
4813 beiscsi_hw_check_task.work);
4814
4815 beiscsi_ue_detect(phba);
4816
4817 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4818 msecs_to_jiffies(1000));
4819}
4820
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004821static int beiscsi_dev_probe(struct pci_dev *pcidev,
4822 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304823{
4824 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304825 struct hwi_controller *phwi_ctrlr;
4826 struct hwi_context_memory *phwi_context;
4827 struct be_eq_obj *pbe_eq;
John Soni Jose107dfcb2012-10-20 04:42:13 +05304828 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304829
4830 ret = beiscsi_enable_pci(pcidev);
4831 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304832 dev_err(&pcidev->dev,
4833 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304834 return ret;
4835 }
4836
4837 phba = beiscsi_hba_alloc(pcidev);
4838 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304839 dev_err(&pcidev->dev,
4840 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304841 goto disable_pci;
4842 }
4843
John Soni Jose99bc5d52012-08-20 23:00:18 +05304844 /* Initialize Driver configuration Paramters */
4845 beiscsi_hba_attrs_init(phba);
4846
John Soni Josee175def2012-10-20 04:45:40 +05304847 phba->fw_timeout = false;
4848
4849
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304850 switch (pcidev->device) {
4851 case BE_DEVICE_ID1:
4852 case OC_DEVICE_ID1:
4853 case OC_DEVICE_ID2:
4854 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05304855 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304856 break;
4857 case BE_DEVICE_ID2:
4858 case OC_DEVICE_ID3:
4859 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05304860 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304861 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05304862 case OC_SKH_ID1:
4863 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05304864 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07004865 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304866 default:
4867 phba->generation = 0;
4868 }
4869
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304870 if (enable_msix)
John Soni Jose107dfcb2012-10-20 04:42:13 +05304871 find_num_cpus(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304872 else
John Soni Jose107dfcb2012-10-20 04:42:13 +05304873 phba->num_cpus = 1;
4874
John Soni Jose99bc5d52012-08-20 23:00:18 +05304875 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4876 "BM_%d : num_cpus = %d\n",
4877 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304878
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004879 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304880 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004881 if (!phba->msix_enabled)
4882 phba->num_cpus = 1;
4883 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304884 ret = be_ctrl_init(phba, pcidev);
4885 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304886 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4887 "BM_%d : beiscsi_dev_probe-"
4888 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304889 goto hba_free;
4890 }
4891
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304892 ret = beiscsi_cmd_reset_function(phba);
4893 if (ret) {
4894 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4895 "BM_%d : Reset Failed. Aborting Crashdump\n");
4896 goto hba_free;
4897 }
4898 ret = be_chk_reset_complete(phba);
4899 if (ret) {
4900 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4901 "BM_%d : Failed to get out of reset."
4902 "Aborting Crashdump\n");
4903 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304904 }
4905
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304906 spin_lock_init(&phba->io_sgl_lock);
4907 spin_lock_init(&phba->mgmt_sgl_lock);
4908 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304909 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4910 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304911 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4912 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304913 goto free_port;
4914 }
4915 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304916 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304917 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304918 ret = beiscsi_init_port(phba);
4919 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304920 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4921 "BM_%d : beiscsi_dev_probe-"
4922 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304923 goto free_port;
4924 }
4925
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304926 for (i = 0; i < MAX_MCC_CMD ; i++) {
4927 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4928 phba->ctrl.mcc_tag[i] = i + 1;
4929 phba->ctrl.mcc_numtag[i + 1] = 0;
4930 phba->ctrl.mcc_tag_available++;
4931 }
4932
4933 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4934
John Soni Jose72fb46a2012-10-20 04:42:49 +05304935 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304936 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004937 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304938 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304939 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4940 "BM_%d : beiscsi_dev_probe-"
4941 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304942 goto free_twq;
4943 }
4944
John Soni Jose7a158002012-10-20 04:45:51 +05304945 INIT_DELAYED_WORK(&phba->beiscsi_hw_check_task,
4946 beiscsi_hw_health_check);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304947
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304948 phwi_ctrlr = phba->phwi_ctrlr;
4949 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05304950
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304951 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304952 for (i = 0; i < phba->num_cpus; i++) {
4953 pbe_eq = &phwi_context->be_eq[i];
4954 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4955 be_iopoll);
4956 blk_iopoll_enable(&pbe_eq->iopoll);
4957 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304958
4959 i = (phba->msix_enabled) ? i : 0;
4960 /* Work item for MCC handling */
4961 pbe_eq = &phwi_context->be_eq[i];
4962 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
4963 } else {
4964 if (phba->msix_enabled) {
4965 for (i = 0; i <= phba->num_cpus; i++) {
4966 pbe_eq = &phwi_context->be_eq[i];
4967 INIT_WORK(&pbe_eq->work_cqs,
4968 beiscsi_process_all_cqs);
4969 }
4970 } else {
4971 pbe_eq = &phwi_context->be_eq[0];
4972 INIT_WORK(&pbe_eq->work_cqs,
4973 beiscsi_process_all_cqs);
4974 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304975 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304976
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304977 ret = beiscsi_init_irqs(phba);
4978 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304979 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4980 "BM_%d : beiscsi_dev_probe-"
4981 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304982 goto free_blkenbld;
4983 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304984 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004985
4986 if (beiscsi_setup_boot_info(phba))
4987 /*
4988 * log error but continue, because we may not be using
4989 * iscsi boot.
4990 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304991 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4992 "BM_%d : Could not set up "
4993 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004994
Mike Christie0e438952012-04-03 23:41:51 -05004995 beiscsi_create_def_ifaces(phba);
John Soni Jose7a158002012-10-20 04:45:51 +05304996 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4997 msecs_to_jiffies(1000));
4998
John Soni Jose99bc5d52012-08-20 23:00:18 +05304999 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
5000 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305001 return 0;
5002
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305003free_blkenbld:
5004 destroy_workqueue(phba->wq);
5005 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305006 for (i = 0; i < phba->num_cpus; i++) {
5007 pbe_eq = &phwi_context->be_eq[i];
5008 blk_iopoll_disable(&pbe_eq->iopoll);
5009 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305010free_twq:
5011 beiscsi_clean_port(phba);
5012 beiscsi_free_mem(phba);
5013free_port:
5014 pci_free_consistent(phba->pcidev,
5015 phba->ctrl.mbox_mem_alloced.size,
5016 phba->ctrl.mbox_mem_alloced.va,
5017 phba->ctrl.mbox_mem_alloced.dma);
5018 beiscsi_unmap_pci_function(phba);
5019hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305020 if (phba->msix_enabled)
5021 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305022 iscsi_host_remove(phba->shost);
5023 pci_dev_put(phba->pcidev);
5024 iscsi_host_free(phba->shost);
5025disable_pci:
5026 pci_disable_device(pcidev);
5027 return ret;
5028}
5029
5030struct iscsi_transport beiscsi_iscsi_transport = {
5031 .owner = THIS_MODULE,
5032 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305033 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305034 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305035 .create_session = beiscsi_session_create,
5036 .destroy_session = beiscsi_session_destroy,
5037 .create_conn = beiscsi_conn_create,
5038 .bind_conn = beiscsi_conn_bind,
5039 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05005040 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05005041 .set_iface_param = be2iscsi_iface_set_param,
5042 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305043 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005044 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305045 .get_session_param = iscsi_session_get_param,
5046 .get_host_param = beiscsi_get_host_param,
5047 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005048 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305049 .send_pdu = iscsi_conn_send_pdu,
5050 .xmit_task = beiscsi_task_xmit,
5051 .cleanup_task = beiscsi_cleanup_task,
5052 .alloc_pdu = beiscsi_alloc_pdu,
5053 .parse_pdu_itt = beiscsi_parse_pdu,
5054 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005055 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305056 .ep_connect = beiscsi_ep_connect,
5057 .ep_poll = beiscsi_ep_poll,
5058 .ep_disconnect = beiscsi_ep_disconnect,
5059 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005060 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305061};
5062
5063static struct pci_driver beiscsi_pci_driver = {
5064 .name = DRV_NAME,
5065 .probe = beiscsi_dev_probe,
5066 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005067 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305068 .id_table = beiscsi_pci_id_table
5069};
5070
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305071
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305072static int __init beiscsi_module_init(void)
5073{
5074 int ret;
5075
5076 beiscsi_scsi_transport =
5077 iscsi_register_transport(&beiscsi_iscsi_transport);
5078 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305079 printk(KERN_ERR
5080 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305081 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305082 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305083 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5084 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305085
5086 ret = pci_register_driver(&beiscsi_pci_driver);
5087 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305088 printk(KERN_ERR
5089 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305090 goto unregister_iscsi_transport;
5091 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305092 return 0;
5093
5094unregister_iscsi_transport:
5095 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5096 return ret;
5097}
5098
5099static void __exit beiscsi_module_exit(void)
5100{
5101 pci_unregister_driver(&beiscsi_pci_driver);
5102 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5103}
5104
5105module_init(beiscsi_module_init);
5106module_exit(beiscsi_module_exit);