blob: 6be5e296c3c1a7ee09ec313185a99feabe7b0e38 [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 Gortmakeracf3368f2011-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);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530156struct device_attribute *beiscsi_attrs[] = {
157 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530158 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530159 &dev_attr_beiscsi_adapter_family,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530160 NULL,
161};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530162
John Soni Jose6763daa2012-10-20 04:41:45 +0530163static char const *cqe_desc[] = {
164 "RESERVED_DESC",
165 "SOL_CMD_COMPLETE",
166 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
167 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
168 "CXN_KILLED_BURST_LEN_MISMATCH",
169 "CXN_KILLED_AHS_RCVD",
170 "CXN_KILLED_HDR_DIGEST_ERR",
171 "CXN_KILLED_UNKNOWN_HDR",
172 "CXN_KILLED_STALE_ITT_TTT_RCVD",
173 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
174 "CXN_KILLED_RST_RCVD",
175 "CXN_KILLED_TIMED_OUT",
176 "CXN_KILLED_RST_SENT",
177 "CXN_KILLED_FIN_RCVD",
178 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
179 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
180 "CXN_KILLED_OVER_RUN_RESIDUAL",
181 "CXN_KILLED_UNDER_RUN_RESIDUAL",
182 "CMD_KILLED_INVALID_STATSN_RCVD",
183 "CMD_KILLED_INVALID_R2T_RCVD",
184 "CMD_CXN_KILLED_LUN_INVALID",
185 "CMD_CXN_KILLED_ICD_INVALID",
186 "CMD_CXN_KILLED_ITT_INVALID",
187 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
188 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
189 "CXN_INVALIDATE_NOTIFY",
190 "CXN_INVALIDATE_INDEX_NOTIFY",
191 "CMD_INVALIDATED_NOTIFY",
192 "UNSOL_HDR_NOTIFY",
193 "UNSOL_DATA_NOTIFY",
194 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
195 "DRIVERMSG_NOTIFY",
196 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
197 "SOL_CMD_KILLED_DIF_ERR",
198 "CXN_KILLED_SYN_RCVD",
199 "CXN_KILLED_IMM_DATA_RCVD"
200};
201
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530202static int beiscsi_slave_configure(struct scsi_device *sdev)
203{
204 blk_queue_max_segment_size(sdev->request_queue, 65536);
205 return 0;
206}
207
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530208static int beiscsi_eh_abort(struct scsi_cmnd *sc)
209{
210 struct iscsi_cls_session *cls_session;
211 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
212 struct beiscsi_io_task *aborted_io_task;
213 struct iscsi_conn *conn;
214 struct beiscsi_conn *beiscsi_conn;
215 struct beiscsi_hba *phba;
216 struct iscsi_session *session;
217 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530218 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530219 unsigned int cid, tag, num_invalidate;
220
221 cls_session = starget_to_session(scsi_target(sc->device));
222 session = cls_session->dd_data;
223
224 spin_lock_bh(&session->lock);
225 if (!aborted_task || !aborted_task->sc) {
226 /* we raced */
227 spin_unlock_bh(&session->lock);
228 return SUCCESS;
229 }
230
231 aborted_io_task = aborted_task->dd_data;
232 if (!aborted_io_task->scsi_cmnd) {
233 /* raced or invalid command */
234 spin_unlock_bh(&session->lock);
235 return SUCCESS;
236 }
237 spin_unlock_bh(&session->lock);
238 conn = aborted_task->conn;
239 beiscsi_conn = conn->dd_data;
240 phba = beiscsi_conn->phba;
241
242 /* invalidate iocb */
243 cid = beiscsi_conn->beiscsi_conn_cid;
244 inv_tbl = phba->inv_tbl;
245 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
246 inv_tbl->cid = cid;
247 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
248 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530249 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
250 sizeof(struct invalidate_commands_params_in),
251 &nonemb_cmd.dma);
252 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530253 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
254 "BM_%d : Failed to allocate memory for"
255 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530256 return FAILED;
257 }
258 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
259
260 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
261 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530262 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530263 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
264 "BM_%d : mgmt_invalidate_icds could not be"
265 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530266 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
267 nonemb_cmd.va, nonemb_cmd.dma);
268
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530269 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530270 }
John Soni Josee175def2012-10-20 04:45:40 +0530271
272 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530273 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
274 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530275 return iscsi_eh_abort(sc);
276}
277
278static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
279{
280 struct iscsi_task *abrt_task;
281 struct beiscsi_io_task *abrt_io_task;
282 struct iscsi_conn *conn;
283 struct beiscsi_conn *beiscsi_conn;
284 struct beiscsi_hba *phba;
285 struct iscsi_session *session;
286 struct iscsi_cls_session *cls_session;
287 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530288 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530289 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530290
291 /* invalidate iocbs */
292 cls_session = starget_to_session(scsi_target(sc->device));
293 session = cls_session->dd_data;
294 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500295 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
296 spin_unlock_bh(&session->lock);
297 return FAILED;
298 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530299 conn = session->leadconn;
300 beiscsi_conn = conn->dd_data;
301 phba = beiscsi_conn->phba;
302 cid = beiscsi_conn->beiscsi_conn_cid;
303 inv_tbl = phba->inv_tbl;
304 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
305 num_invalidate = 0;
306 for (i = 0; i < conn->session->cmds_max; i++) {
307 abrt_task = conn->session->cmds[i];
308 abrt_io_task = abrt_task->dd_data;
309 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
310 continue;
311
312 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
313 continue;
314
315 inv_tbl->cid = cid;
316 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
317 num_invalidate++;
318 inv_tbl++;
319 }
320 spin_unlock_bh(&session->lock);
321 inv_tbl = phba->inv_tbl;
322
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530323 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
324 sizeof(struct invalidate_commands_params_in),
325 &nonemb_cmd.dma);
326 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530327 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
328 "BM_%d : Failed to allocate memory for"
329 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530330 return FAILED;
331 }
332 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
333 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
334 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
335 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530336 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530337 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
338 "BM_%d : mgmt_invalidate_icds could not be"
339 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530340 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
341 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530342 return FAILED;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530343 }
John Soni Josee175def2012-10-20 04:45:40 +0530344
345 beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530346 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
347 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530348 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530349}
350
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530351static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
352{
353 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500354 struct mgmt_session_info *boot_sess = &phba->boot_sess;
355 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530356 char *str = buf;
357 int rc;
358
359 switch (type) {
360 case ISCSI_BOOT_TGT_NAME:
361 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500362 (int)strlen(boot_sess->target_name),
363 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530364 break;
365 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500366 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530367 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500368 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530369 else
370 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500371 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530372 break;
373 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500374 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530375 break;
376
377 case ISCSI_BOOT_TGT_CHAP_NAME:
378 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500379 boot_conn->negotiated_login_options.auth_data.chap.
380 target_chap_name_length,
381 (char *)&boot_conn->negotiated_login_options.
382 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530383 break;
384 case ISCSI_BOOT_TGT_CHAP_SECRET:
385 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500386 boot_conn->negotiated_login_options.auth_data.chap.
387 target_secret_length,
388 (char *)&boot_conn->negotiated_login_options.
389 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530390 break;
391 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
392 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500393 boot_conn->negotiated_login_options.auth_data.chap.
394 intr_chap_name_length,
395 (char *)&boot_conn->negotiated_login_options.
396 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530397 break;
398 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500399 rc = sprintf(str, "%.*s\n",
400 boot_conn->negotiated_login_options.auth_data.chap.
401 intr_secret_length,
402 (char *)&boot_conn->negotiated_login_options.
403 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530404 break;
405 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500406 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530407 break;
408 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500409 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530410 break;
411 default:
412 rc = -ENOSYS;
413 break;
414 }
415 return rc;
416}
417
418static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
419{
420 struct beiscsi_hba *phba = data;
421 char *str = buf;
422 int rc;
423
424 switch (type) {
425 case ISCSI_BOOT_INI_INITIATOR_NAME:
426 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
427 break;
428 default:
429 rc = -ENOSYS;
430 break;
431 }
432 return rc;
433}
434
435static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
436{
437 struct beiscsi_hba *phba = data;
438 char *str = buf;
439 int rc;
440
441 switch (type) {
442 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500443 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530444 break;
445 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500446 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530447 break;
448 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500449 rc = beiscsi_get_macaddr(str, phba);
450 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530451 default:
452 rc = -ENOSYS;
453 break;
454 }
455 return rc;
456}
457
458
Al Viro587a1f12011-07-23 23:11:19 -0400459static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530460{
Al Viro587a1f12011-07-23 23:11:19 -0400461 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530462
463 switch (type) {
464 case ISCSI_BOOT_TGT_NAME:
465 case ISCSI_BOOT_TGT_IP_ADDR:
466 case ISCSI_BOOT_TGT_PORT:
467 case ISCSI_BOOT_TGT_CHAP_NAME:
468 case ISCSI_BOOT_TGT_CHAP_SECRET:
469 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
470 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
471 case ISCSI_BOOT_TGT_NIC_ASSOC:
472 case ISCSI_BOOT_TGT_FLAGS:
473 rc = S_IRUGO;
474 break;
475 default:
476 rc = 0;
477 break;
478 }
479 return rc;
480}
481
Al Viro587a1f12011-07-23 23:11:19 -0400482static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530483{
Al Viro587a1f12011-07-23 23:11:19 -0400484 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530485
486 switch (type) {
487 case ISCSI_BOOT_INI_INITIATOR_NAME:
488 rc = S_IRUGO;
489 break;
490 default:
491 rc = 0;
492 break;
493 }
494 return rc;
495}
496
497
Al Viro587a1f12011-07-23 23:11:19 -0400498static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530499{
Al Viro587a1f12011-07-23 23:11:19 -0400500 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530501
502 switch (type) {
503 case ISCSI_BOOT_ETH_FLAGS:
504 case ISCSI_BOOT_ETH_MAC:
505 case ISCSI_BOOT_ETH_INDEX:
506 rc = S_IRUGO;
507 break;
508 default:
509 rc = 0;
510 break;
511 }
512 return rc;
513}
514
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530515/*------------------- PCI Driver operations and data ----------------- */
516static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
517 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530518 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530519 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
520 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
521 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530522 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530523 { 0 }
524};
525MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
526
John Soni Jose99bc5d52012-08-20 23:00:18 +0530527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530528static struct scsi_host_template beiscsi_sht = {
529 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500530 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530531 .proc_name = DRV_NAME,
532 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530533 .change_queue_depth = iscsi_change_queue_depth,
534 .slave_configure = beiscsi_slave_configure,
535 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530536 .eh_abort_handler = beiscsi_eh_abort,
537 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530538 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530539 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530540 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
541 .can_queue = BE2_IO_DEPTH,
542 .this_id = -1,
543 .max_sectors = BEISCSI_MAX_SECTORS,
544 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
545 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500546 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
547
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530548};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530549
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530550static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530551
552static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
553{
554 struct beiscsi_hba *phba;
555 struct Scsi_Host *shost;
556
557 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
558 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530559 dev_err(&pcidev->dev,
560 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530561 return NULL;
562 }
563 shost->dma_boundary = pcidev->dma_mask;
564 shost->max_id = BE2_MAX_SESSIONS;
565 shost->max_channel = 0;
566 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
567 shost->max_lun = BEISCSI_NUM_MAX_LUN;
568 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530569 phba = iscsi_host_priv(shost);
570 memset(phba, 0, sizeof(*phba));
571 phba->shost = shost;
572 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530573 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500574 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530575
576 if (iscsi_host_add(shost, &phba->pcidev->dev))
577 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530578
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530579 return phba;
580
581free_devices:
582 pci_dev_put(phba->pcidev);
583 iscsi_host_free(phba->shost);
584 return NULL;
585}
586
587static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
588{
589 if (phba->csr_va) {
590 iounmap(phba->csr_va);
591 phba->csr_va = NULL;
592 }
593 if (phba->db_va) {
594 iounmap(phba->db_va);
595 phba->db_va = NULL;
596 }
597 if (phba->pci_va) {
598 iounmap(phba->pci_va);
599 phba->pci_va = NULL;
600 }
601}
602
603static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
604 struct pci_dev *pcidev)
605{
606 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530607 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530608
609 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
610 pci_resource_len(pcidev, 2));
611 if (addr == NULL)
612 return -ENOMEM;
613 phba->ctrl.csr = addr;
614 phba->csr_va = addr;
615 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
616
617 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
618 if (addr == NULL)
619 goto pci_map_err;
620 phba->ctrl.db = addr;
621 phba->db_va = addr;
622 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
623
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530624 if (phba->generation == BE_GEN2)
625 pcicfg_reg = 1;
626 else
627 pcicfg_reg = 0;
628
629 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
630 pci_resource_len(pcidev, pcicfg_reg));
631
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530632 if (addr == NULL)
633 goto pci_map_err;
634 phba->ctrl.pcicfg = addr;
635 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530636 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530637 return 0;
638
639pci_map_err:
640 beiscsi_unmap_pci_function(phba);
641 return -ENOMEM;
642}
643
644static int beiscsi_enable_pci(struct pci_dev *pcidev)
645{
646 int ret;
647
648 ret = pci_enable_device(pcidev);
649 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530650 dev_err(&pcidev->dev,
651 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530652 return ret;
653 }
654
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530655 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530656 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
657 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
658 if (ret) {
659 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
660 pci_disable_device(pcidev);
661 return ret;
662 }
663 }
664 return 0;
665}
666
667static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
668{
669 struct be_ctrl_info *ctrl = &phba->ctrl;
670 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
671 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
672 int status = 0;
673
674 ctrl->pdev = pdev;
675 status = beiscsi_map_pci_bars(phba, pdev);
676 if (status)
677 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530678 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
679 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
680 mbox_mem_alloc->size,
681 &mbox_mem_alloc->dma);
682 if (!mbox_mem_alloc->va) {
683 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500684 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530685 }
686
687 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
688 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
689 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
690 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
691 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530692 spin_lock_init(&phba->ctrl.mcc_lock);
693 spin_lock_init(&phba->ctrl.mcc_cq_lock);
694
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530695 return status;
696}
697
698static void beiscsi_get_params(struct beiscsi_hba *phba)
699{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530700 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
701 - (phba->fw_config.iscsi_cid_count
702 + BE2_TMFS
703 + BE2_NOPOUT_REQ));
704 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530705 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700706 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530707 phba->params.num_sge_per_io = BE2_SGE;
708 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
709 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
710 phba->params.eq_timer = 64;
711 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530712 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
713 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530714 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
715 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530716 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
717 "BM_%d : phba->params.num_eq_entries=%d\n",
718 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530719 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530720 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
721 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530722 phba->params.wrbs_per_cxn = 256;
723}
724
725static void hwi_ring_eq_db(struct beiscsi_hba *phba,
726 unsigned int id, unsigned int clr_interrupt,
727 unsigned int num_processed,
728 unsigned char rearm, unsigned char event)
729{
730 u32 val = 0;
731 val |= id & DB_EQ_RING_ID_MASK;
732 if (rearm)
733 val |= 1 << DB_EQ_REARM_SHIFT;
734 if (clr_interrupt)
735 val |= 1 << DB_EQ_CLR_SHIFT;
736 if (event)
737 val |= 1 << DB_EQ_EVNT_SHIFT;
738 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
739 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
740}
741
742/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530743 * be_isr_mcc - The isr routine of the driver.
744 * @irq: Not used
745 * @dev_id: Pointer to host adapter structure
746 */
747static irqreturn_t be_isr_mcc(int irq, void *dev_id)
748{
749 struct beiscsi_hba *phba;
750 struct be_eq_entry *eqe = NULL;
751 struct be_queue_info *eq;
752 struct be_queue_info *mcc;
753 unsigned int num_eq_processed;
754 struct be_eq_obj *pbe_eq;
755 unsigned long flags;
756
757 pbe_eq = dev_id;
758 eq = &pbe_eq->q;
759 phba = pbe_eq->phba;
760 mcc = &phba->ctrl.mcc_obj.cq;
761 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530762
763 num_eq_processed = 0;
764
765 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
766 & EQE_VALID_MASK) {
767 if (((eqe->dw[offsetof(struct amap_eq_entry,
768 resource_id) / 32] &
769 EQE_RESID_MASK) >> 16) == mcc->id) {
770 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530771 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530772 spin_unlock_irqrestore(&phba->isr_lock, flags);
773 }
774 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
775 queue_tail_inc(eq);
776 eqe = queue_tail_node(eq);
777 num_eq_processed++;
778 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530779 if (pbe_eq->todo_mcc_cq)
780 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530781 if (num_eq_processed)
782 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
783
784 return IRQ_HANDLED;
785}
786
787/**
788 * be_isr_msix - The isr routine of the driver.
789 * @irq: Not used
790 * @dev_id: Pointer to host adapter structure
791 */
792static irqreturn_t be_isr_msix(int irq, void *dev_id)
793{
794 struct beiscsi_hba *phba;
795 struct be_eq_entry *eqe = NULL;
796 struct be_queue_info *eq;
797 struct be_queue_info *cq;
798 unsigned int num_eq_processed;
799 struct be_eq_obj *pbe_eq;
800 unsigned long flags;
801
802 pbe_eq = dev_id;
803 eq = &pbe_eq->q;
804 cq = pbe_eq->cq;
805 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530806
807 phba = pbe_eq->phba;
808 num_eq_processed = 0;
809 if (blk_iopoll_enabled) {
810 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
811 & EQE_VALID_MASK) {
812 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
813 blk_iopoll_sched(&pbe_eq->iopoll);
814
815 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
816 queue_tail_inc(eq);
817 eqe = queue_tail_node(eq);
818 num_eq_processed++;
819 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530820 } else {
821 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
822 & EQE_VALID_MASK) {
823 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530824 pbe_eq->todo_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530825 spin_unlock_irqrestore(&phba->isr_lock, flags);
826 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
827 queue_tail_inc(eq);
828 eqe = queue_tail_node(eq);
829 num_eq_processed++;
830 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530831
John Soni Jose72fb46a2012-10-20 04:42:49 +0530832 if (pbe_eq->todo_cq)
833 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530834 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530835
836 if (num_eq_processed)
837 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
838
839 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530840}
841
842/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530843 * be_isr - The isr routine of the driver.
844 * @irq: Not used
845 * @dev_id: Pointer to host adapter structure
846 */
847static irqreturn_t be_isr(int irq, void *dev_id)
848{
849 struct beiscsi_hba *phba;
850 struct hwi_controller *phwi_ctrlr;
851 struct hwi_context_memory *phwi_context;
852 struct be_eq_entry *eqe = NULL;
853 struct be_queue_info *eq;
854 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530855 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530856 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530857 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530858 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 int isr;
861
862 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700863 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530864 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
865 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
866 if (!isr)
867 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530868
869 phwi_ctrlr = phba->phwi_ctrlr;
870 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530871 pbe_eq = &phwi_context->be_eq[0];
872
873 eq = &phwi_context->be_eq[0].q;
874 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530875 index = 0;
876 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530877
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530878 num_ioeq_processed = 0;
879 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530880 if (blk_iopoll_enabled) {
881 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
882 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530883 if (((eqe->dw[offsetof(struct amap_eq_entry,
884 resource_id) / 32] &
885 EQE_RESID_MASK) >> 16) == mcc->id) {
886 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530887 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530888 spin_unlock_irqrestore(&phba->isr_lock, flags);
889 num_mcceq_processed++;
890 } else {
891 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
892 blk_iopoll_sched(&pbe_eq->iopoll);
893 num_ioeq_processed++;
894 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530895 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
896 queue_tail_inc(eq);
897 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530898 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530899 if (num_ioeq_processed || num_mcceq_processed) {
John Soni Jose72fb46a2012-10-20 04:42:49 +0530900 if (pbe_eq->todo_mcc_cq)
901 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530902
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530903 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530904 hwi_ring_eq_db(phba, eq->id, 0,
905 (num_ioeq_processed +
906 num_mcceq_processed) , 1, 1);
907 else
908 hwi_ring_eq_db(phba, eq->id, 0,
909 (num_ioeq_processed +
910 num_mcceq_processed), 0, 1);
911
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530912 return IRQ_HANDLED;
913 } else
914 return IRQ_NONE;
915 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530916 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530917 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
918 & EQE_VALID_MASK) {
919
920 if (((eqe->dw[offsetof(struct amap_eq_entry,
921 resource_id) / 32] &
922 EQE_RESID_MASK) >> 16) != cq->id) {
923 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530924 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530925 spin_unlock_irqrestore(&phba->isr_lock, flags);
926 } else {
927 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530928 pbe_eq->todo_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530929 spin_unlock_irqrestore(&phba->isr_lock, flags);
930 }
931 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
932 queue_tail_inc(eq);
933 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530934 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530935 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530936 if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
937 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530938
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530939 if (num_ioeq_processed) {
940 hwi_ring_eq_db(phba, eq->id, 0,
941 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530942 return IRQ_HANDLED;
943 } else
944 return IRQ_NONE;
945 }
946}
947
948static int beiscsi_init_irqs(struct beiscsi_hba *phba)
949{
950 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530951 struct hwi_controller *phwi_ctrlr;
952 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530953 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530954
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530955 phwi_ctrlr = phba->phwi_ctrlr;
956 phwi_context = phwi_ctrlr->phwi_ctxt;
957
958 if (phba->msix_enabled) {
959 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700960 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
961 GFP_KERNEL);
962 if (!phba->msi_name[i]) {
963 ret = -ENOMEM;
964 goto free_msix_irqs;
965 }
966
967 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
968 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530969 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700970 ret = request_irq(msix_vec, be_isr_msix, 0,
971 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530972 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530973 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530974 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
975 "BM_%d : beiscsi_init_irqs-Failed to"
976 "register msix for i = %d\n",
977 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700978 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530979 goto free_msix_irqs;
980 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530981 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700982 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
983 if (!phba->msi_name[i]) {
984 ret = -ENOMEM;
985 goto free_msix_irqs;
986 }
987 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
988 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530989 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700990 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530991 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530992 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530993 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
994 "BM_%d : beiscsi_init_irqs-"
995 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700996 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530997 goto free_msix_irqs;
998 }
999
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301000 } else {
1001 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1002 "beiscsi", phba);
1003 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301004 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1005 "BM_%d : beiscsi_init_irqs-"
1006 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301007 return ret;
1008 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301009 }
1010 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301011free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001012 for (j = i - 1; j >= 0; j--) {
1013 kfree(phba->msi_name[j]);
1014 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301015 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001016 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301017 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301018}
1019
1020static void hwi_ring_cq_db(struct beiscsi_hba *phba,
1021 unsigned int id, unsigned int num_processed,
1022 unsigned char rearm, unsigned char event)
1023{
1024 u32 val = 0;
1025 val |= id & DB_CQ_RING_ID_MASK;
1026 if (rearm)
1027 val |= 1 << DB_CQ_REARM_SHIFT;
1028 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1029 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1030}
1031
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301032static unsigned int
1033beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1034 struct beiscsi_hba *phba,
1035 unsigned short cid,
1036 struct pdu_base *ppdu,
1037 unsigned long pdu_len,
1038 void *pbuffer, unsigned long buf_len)
1039{
1040 struct iscsi_conn *conn = beiscsi_conn->conn;
1041 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301042 struct iscsi_task *task;
1043 struct beiscsi_io_task *io_task;
1044 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301045
1046 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1047 PDUBASE_OPCODE_MASK) {
1048 case ISCSI_OP_NOOP_IN:
1049 pbuffer = NULL;
1050 buf_len = 0;
1051 break;
1052 case ISCSI_OP_ASYNC_EVENT:
1053 break;
1054 case ISCSI_OP_REJECT:
1055 WARN_ON(!pbuffer);
1056 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301057 beiscsi_log(phba, KERN_ERR,
1058 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1059 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301060 break;
1061 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301062 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301063 task = conn->login_task;
1064 io_task = task->dd_data;
1065 login_hdr = (struct iscsi_hdr *)ppdu;
1066 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301067 break;
1068 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301069 beiscsi_log(phba, KERN_WARNING,
1070 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1071 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1072 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301073 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301074 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301075 return 1;
1076 }
1077
1078 spin_lock_bh(&session->lock);
1079 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1080 spin_unlock_bh(&session->lock);
1081 return 0;
1082}
1083
1084static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1085{
1086 struct sgl_handle *psgl_handle;
1087
1088 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301089 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1090 "BM_%d : In alloc_io_sgl_handle,"
1091 " io_sgl_alloc_index=%d\n",
1092 phba->io_sgl_alloc_index);
1093
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301094 psgl_handle = phba->io_sgl_hndl_base[phba->
1095 io_sgl_alloc_index];
1096 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1097 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301098 if (phba->io_sgl_alloc_index == (phba->params.
1099 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301100 phba->io_sgl_alloc_index = 0;
1101 else
1102 phba->io_sgl_alloc_index++;
1103 } else
1104 psgl_handle = NULL;
1105 return psgl_handle;
1106}
1107
1108static void
1109free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1110{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301111 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1112 "BM_%d : In free_,io_sgl_free_index=%d\n",
1113 phba->io_sgl_free_index);
1114
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301115 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1116 /*
1117 * this can happen if clean_task is called on a task that
1118 * failed in xmit_task or alloc_pdu.
1119 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301120 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1121 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1122 "value there=%p\n", phba->io_sgl_free_index,
1123 phba->io_sgl_hndl_base
1124 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301125 return;
1126 }
1127 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1128 phba->io_sgl_hndl_avbl++;
1129 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1130 phba->io_sgl_free_index = 0;
1131 else
1132 phba->io_sgl_free_index++;
1133}
1134
1135/**
1136 * alloc_wrb_handle - To allocate a wrb handle
1137 * @phba: The hba pointer
1138 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301139 *
1140 * This happens under session_lock until submission to chip
1141 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301142struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301143{
1144 struct hwi_wrb_context *pwrb_context;
1145 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301146 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301147
1148 phwi_ctrlr = phba->phwi_ctrlr;
1149 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301150 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301151 pwrb_handle = pwrb_context->pwrb_handle_base[
1152 pwrb_context->alloc_index];
1153 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301154 if (pwrb_context->alloc_index ==
1155 (phba->params.wrbs_per_cxn - 1))
1156 pwrb_context->alloc_index = 0;
1157 else
1158 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301159 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1160 pwrb_context->alloc_index];
1161 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301162 } else
1163 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301164 return pwrb_handle;
1165}
1166
1167/**
1168 * free_wrb_handle - To free the wrb handle back to pool
1169 * @phba: The hba pointer
1170 * @pwrb_context: The context to free from
1171 * @pwrb_handle: The wrb_handle to free
1172 *
1173 * This happens under session_lock until submission to chip
1174 */
1175static void
1176free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1177 struct wrb_handle *pwrb_handle)
1178{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301179 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301180 pwrb_context->wrb_handles_available++;
1181 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1182 pwrb_context->free_index = 0;
1183 else
1184 pwrb_context->free_index++;
1185
John Soni Jose99bc5d52012-08-20 23:00:18 +05301186 beiscsi_log(phba, KERN_INFO,
1187 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1188 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1189 "wrb_handles_available=%d\n",
1190 pwrb_handle, pwrb_context->free_index,
1191 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301192}
1193
1194static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1195{
1196 struct sgl_handle *psgl_handle;
1197
1198 if (phba->eh_sgl_hndl_avbl) {
1199 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1200 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301201 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1202 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1203 phba->eh_sgl_alloc_index,
1204 phba->eh_sgl_alloc_index);
1205
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301206 phba->eh_sgl_hndl_avbl--;
1207 if (phba->eh_sgl_alloc_index ==
1208 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1209 1))
1210 phba->eh_sgl_alloc_index = 0;
1211 else
1212 phba->eh_sgl_alloc_index++;
1213 } else
1214 psgl_handle = NULL;
1215 return psgl_handle;
1216}
1217
1218void
1219free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1220{
1221
John Soni Jose99bc5d52012-08-20 23:00:18 +05301222 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1223 "BM_%d : In free_mgmt_sgl_handle,"
1224 "eh_sgl_free_index=%d\n",
1225 phba->eh_sgl_free_index);
1226
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301227 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1228 /*
1229 * this can happen if clean_task is called on a task that
1230 * failed in xmit_task or alloc_pdu.
1231 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301232 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1233 "BM_%d : Double Free in eh SGL ,"
1234 "eh_sgl_free_index=%d\n",
1235 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301236 return;
1237 }
1238 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1239 phba->eh_sgl_hndl_avbl++;
1240 if (phba->eh_sgl_free_index ==
1241 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1242 phba->eh_sgl_free_index = 0;
1243 else
1244 phba->eh_sgl_free_index++;
1245}
1246
1247static void
1248be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301249 struct iscsi_task *task,
1250 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301251{
1252 struct beiscsi_io_task *io_task = task->dd_data;
1253 struct be_status_bhs *sts_bhs =
1254 (struct be_status_bhs *)io_task->cmd_bhs;
1255 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301256 unsigned char *sense;
1257 u32 resid = 0, exp_cmdsn, max_cmdsn;
1258 u8 rsp, status, flags;
1259
John Soni Jose73133262012-10-20 04:44:49 +05301260 exp_cmdsn = csol_cqe->exp_cmdsn;
1261 max_cmdsn = (csol_cqe->exp_cmdsn +
1262 csol_cqe->cmd_wnd - 1);
1263 rsp = csol_cqe->i_resp;
1264 status = csol_cqe->i_sts;
1265 flags = csol_cqe->i_flags;
1266 resid = csol_cqe->res_cnt;
1267
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001268 if (!task->sc) {
1269 if (io_task->scsi_cmnd)
1270 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301271
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001272 return;
1273 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301274 task->sc->result = (DID_OK << 16) | status;
1275 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1276 task->sc->result = DID_ERROR << 16;
1277 goto unmap;
1278 }
1279
1280 /* bidi not initially supported */
1281 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301282 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1283 task->sc->result = DID_ERROR << 16;
1284
1285 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1286 scsi_set_resid(task->sc, resid);
1287 if (!status && (scsi_bufflen(task->sc) - resid <
1288 task->sc->underflow))
1289 task->sc->result = DID_ERROR << 16;
1290 }
1291 }
1292
1293 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001294 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301295 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001296
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301297 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001298 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301299 memcpy(task->sc->sense_buffer, sense,
1300 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1301 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301302
John Soni Jose73133262012-10-20 04:44:49 +05301303 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1304 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301305unmap:
1306 scsi_dma_unmap(io_task->scsi_cmnd);
1307 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1308}
1309
1310static void
1311be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301312 struct iscsi_task *task,
1313 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301314{
1315 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301316 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301317 struct iscsi_conn *conn = beiscsi_conn->conn;
1318
1319 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301320 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301321 hdr->t2wait = 5;
1322 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301323 hdr->flags = csol_cqe->i_flags;
1324 hdr->response = csol_cqe->i_resp;
1325 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1326 hdr->max_cmdsn = (csol_cqe->exp_cmdsn + csol_cqe->cmd_wnd - 1);
1327
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301328 hdr->dlength[0] = 0;
1329 hdr->dlength[1] = 0;
1330 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301331 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301332 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1334}
1335
1336static void
1337be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301338 struct iscsi_task *task,
1339 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301340{
1341 struct iscsi_tm_rsp *hdr;
1342 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301343 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301344
1345 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301346 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301347 hdr->flags = csol_cqe->i_flags;
1348 hdr->response = csol_cqe->i_resp;
1349 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1350 hdr->max_cmdsn = (csol_cqe->exp_cmdsn +
1351 csol_cqe->cmd_wnd - 1);
1352
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301353 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301354 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1355}
1356
1357static void
1358hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1359 struct beiscsi_hba *phba, struct sol_cqe *psol)
1360{
1361 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301362 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301363 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301364 struct iscsi_task *task;
1365 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301366 struct iscsi_conn *conn = beiscsi_conn->conn;
1367 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301368 uint16_t wrb_index, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301369
1370 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001371 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301372 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1373 wrb_idx, psol);
1374 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1375 cid, psol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001376 } else {
1377 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1378 wrb_idx, psol);
1379 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1380 cid, psol);
John Soni Jose73133262012-10-20 04:44:49 +05301381 }
1382
1383 pwrb_context = &phwi_ctrlr->wrb_context[
1384 cid - phba->fw_config.iscsi_cid_start];
1385 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301386 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301387
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301388 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001389 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301390 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001391 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301392 spin_lock_bh(&session->lock);
1393 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1394 spin_unlock_bh(&session->lock);
1395}
1396
1397static void
1398be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301399 struct iscsi_task *task,
1400 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301401{
1402 struct iscsi_nopin *hdr;
1403 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301404 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301405
1406 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301407 hdr->flags = csol_cqe->i_flags;
1408 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1409 hdr->max_cmdsn = be32_to_cpu(hdr->exp_cmdsn +
1410 csol_cqe->cmd_wnd - 1);
1411
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301412 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301413 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301414 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1415}
1416
John Soni Jose73133262012-10-20 04:44:49 +05301417static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1418 struct sol_cqe *psol,
1419 struct common_sol_cqe *csol_cqe)
1420{
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001421 if (is_chip_be2_be3r(phba)) {
1422 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1423 i_exp_cmd_sn, psol);
1424 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1425 i_res_cnt, psol);
1426 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1427 i_cmd_wnd, psol);
1428 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1429 wrb_index, psol);
1430 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1431 cid, psol);
1432 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1433 hw_sts, psol);
1434 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1435 i_resp, psol);
1436 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1437 i_sts, psol);
1438 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1439 i_flags, psol);
1440 } else {
John Soni Jose73133262012-10-20 04:44:49 +05301441 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1442 i_exp_cmd_sn, psol);
1443 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1444 i_res_cnt, psol);
1445 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1446 wrb_index, psol);
1447 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1448 cid, psol);
1449 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1450 hw_sts, psol);
1451 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1452 i_cmd_wnd, psol);
1453 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1454 cmd_cmpl, psol))
1455 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1456 i_sts, psol);
1457 else
1458 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1459 i_sts, psol);
1460 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1461 u, psol))
1462 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1463
1464 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1465 o, psol))
1466 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
John Soni Jose73133262012-10-20 04:44:49 +05301467 }
1468}
1469
1470
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301471static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1472 struct beiscsi_hba *phba, struct sol_cqe *psol)
1473{
1474 struct hwi_wrb_context *pwrb_context;
1475 struct wrb_handle *pwrb_handle;
1476 struct iscsi_wrb *pwrb = NULL;
1477 struct hwi_controller *phwi_ctrlr;
1478 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301479 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301480 struct iscsi_conn *conn = beiscsi_conn->conn;
1481 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301482 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301483
1484 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301485
1486 /* Copy the elements to a common structure */
1487 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1488
1489 pwrb_context = &phwi_ctrlr->wrb_context[
1490 csol_cqe.cid - phba->fw_config.iscsi_cid_start];
1491
1492 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1493 csol_cqe.wrb_index];
1494
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301495 task = pwrb_handle->pio_handle;
1496 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301497 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301498
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301499 spin_lock_bh(&session->lock);
1500 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301501 case HWH_TYPE_IO:
1502 case HWH_TYPE_IO_RD:
1503 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301504 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301505 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301506 else
John Soni Jose73133262012-10-20 04:44:49 +05301507 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301508 break;
1509
1510 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301511 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301512 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301513 else
John Soni Jose73133262012-10-20 04:44:49 +05301514 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301515 break;
1516
1517 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301518 beiscsi_log(phba, KERN_ERR,
1519 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1520 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1521 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301522 break;
1523
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301524 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301525 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 break;
1527
1528 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301529 beiscsi_log(phba, KERN_WARNING,
1530 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1531 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1532 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301533 csol_cqe.wrb_index,
1534 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301535 break;
1536 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301537
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301538 spin_unlock_bh(&session->lock);
1539}
1540
1541static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1542 *pasync_ctx, unsigned int is_header,
1543 unsigned int host_write_ptr)
1544{
1545 if (is_header)
1546 return &pasync_ctx->async_entry[host_write_ptr].
1547 header_busy_list;
1548 else
1549 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1550}
1551
1552static struct async_pdu_handle *
1553hwi_get_async_handle(struct beiscsi_hba *phba,
1554 struct beiscsi_conn *beiscsi_conn,
1555 struct hwi_async_pdu_context *pasync_ctx,
1556 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1557{
1558 struct be_bus_address phys_addr;
1559 struct list_head *pbusy_list;
1560 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301561 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301562 unsigned int index, dpl;
1563
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001564 if (is_chip_be2_be3r(phba)) {
John Soni Jose73133262012-10-20 04:44:49 +05301565 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1566 dpl, pdpdu_cqe);
1567 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1568 index, pdpdu_cqe);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07001569 } else {
1570 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1571 dpl, pdpdu_cqe);
1572 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1573 index, pdpdu_cqe);
John Soni Jose73133262012-10-20 04:44:49 +05301574 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301575
1576 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301577 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1578 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301579 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301580 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1581 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301582
1583 phys_addr.u.a64.address =
1584 *((unsigned long long *)(&phys_addr.u.a64.address));
1585
1586 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1587 & PDUCQE_CODE_MASK) {
1588 case UNSOL_HDR_NOTIFY:
1589 is_header = 1;
1590
John Soni Jose73133262012-10-20 04:44:49 +05301591 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1592 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301593 break;
1594 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301595 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1596 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301597 break;
1598 default:
1599 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301600 beiscsi_log(phba, KERN_WARNING,
1601 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1602 "BM_%d : Unexpected code=%d\n",
1603 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1604 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301605 return NULL;
1606 }
1607
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301608 WARN_ON(list_empty(pbusy_list));
1609 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001610 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301611 break;
1612 }
1613
1614 WARN_ON(!pasync_handle);
1615
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301616 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1617 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301618 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301619 pasync_handle->buffer_len = dpl;
1620 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301621
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301622 return pasync_handle;
1623}
1624
1625static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301626hwi_update_async_writables(struct beiscsi_hba *phba,
1627 struct hwi_async_pdu_context *pasync_ctx,
1628 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301629{
1630 struct list_head *pbusy_list;
1631 struct async_pdu_handle *pasync_handle;
1632 unsigned int num_entries, writables = 0;
1633 unsigned int *pep_read_ptr, *pwritables;
1634
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001635 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301636 if (is_header) {
1637 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1638 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301639 } else {
1640 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1641 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301642 }
1643
1644 while ((*pep_read_ptr) != cq_index) {
1645 (*pep_read_ptr)++;
1646 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1647
1648 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1649 *pep_read_ptr);
1650 if (writables == 0)
1651 WARN_ON(list_empty(pbusy_list));
1652
1653 if (!list_empty(pbusy_list)) {
1654 pasync_handle = list_entry(pbusy_list->next,
1655 struct async_pdu_handle,
1656 link);
1657 WARN_ON(!pasync_handle);
1658 pasync_handle->consumed = 1;
1659 }
1660
1661 writables++;
1662 }
1663
1664 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301665 beiscsi_log(phba, KERN_ERR,
1666 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1667 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1668 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301669 WARN_ON(1);
1670 }
1671
1672 *pwritables = *pwritables + writables;
1673 return 0;
1674}
1675
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001676static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301677 unsigned int cri)
1678{
1679 struct hwi_controller *phwi_ctrlr;
1680 struct hwi_async_pdu_context *pasync_ctx;
1681 struct async_pdu_handle *pasync_handle, *tmp_handle;
1682 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301683
1684 phwi_ctrlr = phba->phwi_ctrlr;
1685 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1686
1687 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1688
1689 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1690 list_del(&pasync_handle->link);
1691
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001692 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301693 list_add_tail(&pasync_handle->link,
1694 &pasync_ctx->async_header.free_list);
1695 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301696 } else {
1697 list_add_tail(&pasync_handle->link,
1698 &pasync_ctx->async_data.free_list);
1699 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301700 }
1701 }
1702
1703 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1704 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1705 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301706}
1707
1708static struct phys_addr *
1709hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1710 unsigned int is_header, unsigned int host_write_ptr)
1711{
1712 struct phys_addr *pasync_sge = NULL;
1713
1714 if (is_header)
1715 pasync_sge = pasync_ctx->async_header.ring_base;
1716 else
1717 pasync_sge = pasync_ctx->async_data.ring_base;
1718
1719 return pasync_sge + host_write_ptr;
1720}
1721
1722static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1723 unsigned int is_header)
1724{
1725 struct hwi_controller *phwi_ctrlr;
1726 struct hwi_async_pdu_context *pasync_ctx;
1727 struct async_pdu_handle *pasync_handle;
1728 struct list_head *pfree_link, *pbusy_list;
1729 struct phys_addr *pasync_sge;
1730 unsigned int ring_id, num_entries;
1731 unsigned int host_write_num;
1732 unsigned int writables;
1733 unsigned int i = 0;
1734 u32 doorbell = 0;
1735
1736 phwi_ctrlr = phba->phwi_ctrlr;
1737 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001738 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301739
1740 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301741 writables = min(pasync_ctx->async_header.writables,
1742 pasync_ctx->async_header.free_entries);
1743 pfree_link = pasync_ctx->async_header.free_list.next;
1744 host_write_num = pasync_ctx->async_header.host_write_ptr;
1745 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1746 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301747 writables = min(pasync_ctx->async_data.writables,
1748 pasync_ctx->async_data.free_entries);
1749 pfree_link = pasync_ctx->async_data.free_list.next;
1750 host_write_num = pasync_ctx->async_data.host_write_ptr;
1751 ring_id = phwi_ctrlr->default_pdu_data.id;
1752 }
1753
1754 writables = (writables / 8) * 8;
1755 if (writables) {
1756 for (i = 0; i < writables; i++) {
1757 pbusy_list =
1758 hwi_get_async_busy_list(pasync_ctx, is_header,
1759 host_write_num);
1760 pasync_handle =
1761 list_entry(pfree_link, struct async_pdu_handle,
1762 link);
1763 WARN_ON(!pasync_handle);
1764 pasync_handle->consumed = 0;
1765
1766 pfree_link = pfree_link->next;
1767
1768 pasync_sge = hwi_get_ring_address(pasync_ctx,
1769 is_header, host_write_num);
1770
1771 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1772 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1773
1774 list_move(&pasync_handle->link, pbusy_list);
1775
1776 host_write_num++;
1777 host_write_num = host_write_num % num_entries;
1778 }
1779
1780 if (is_header) {
1781 pasync_ctx->async_header.host_write_ptr =
1782 host_write_num;
1783 pasync_ctx->async_header.free_entries -= writables;
1784 pasync_ctx->async_header.writables -= writables;
1785 pasync_ctx->async_header.busy_entries += writables;
1786 } else {
1787 pasync_ctx->async_data.host_write_ptr = host_write_num;
1788 pasync_ctx->async_data.free_entries -= writables;
1789 pasync_ctx->async_data.writables -= writables;
1790 pasync_ctx->async_data.busy_entries += writables;
1791 }
1792
1793 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1794 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1795 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1796 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1797 << DB_DEF_PDU_CQPROC_SHIFT;
1798
1799 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1800 }
1801}
1802
1803static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1804 struct beiscsi_conn *beiscsi_conn,
1805 struct i_t_dpdu_cqe *pdpdu_cqe)
1806{
1807 struct hwi_controller *phwi_ctrlr;
1808 struct hwi_async_pdu_context *pasync_ctx;
1809 struct async_pdu_handle *pasync_handle = NULL;
1810 unsigned int cq_index = -1;
1811
1812 phwi_ctrlr = phba->phwi_ctrlr;
1813 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1814
1815 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1816 pdpdu_cqe, &cq_index);
1817 BUG_ON(pasync_handle->is_header != 0);
1818 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301819 hwi_update_async_writables(phba, pasync_ctx,
1820 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301821
1822 hwi_free_async_msg(phba, pasync_handle->cri);
1823 hwi_post_async_buffers(phba, pasync_handle->is_header);
1824}
1825
1826static unsigned int
1827hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1828 struct beiscsi_hba *phba,
1829 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1830{
1831 struct list_head *plist;
1832 struct async_pdu_handle *pasync_handle;
1833 void *phdr = NULL;
1834 unsigned int hdr_len = 0, buf_len = 0;
1835 unsigned int status, index = 0, offset = 0;
1836 void *pfirst_buffer = NULL;
1837 unsigned int num_buf = 0;
1838
1839 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1840
1841 list_for_each_entry(pasync_handle, plist, link) {
1842 if (index == 0) {
1843 phdr = pasync_handle->pbuffer;
1844 hdr_len = pasync_handle->buffer_len;
1845 } else {
1846 buf_len = pasync_handle->buffer_len;
1847 if (!num_buf) {
1848 pfirst_buffer = pasync_handle->pbuffer;
1849 num_buf++;
1850 }
1851 memcpy(pfirst_buffer + offset,
1852 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001853 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301854 }
1855 index++;
1856 }
1857
1858 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301859 (beiscsi_conn->beiscsi_conn_cid -
1860 phba->fw_config.iscsi_cid_start),
1861 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001862 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301863
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001864 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301865 return 0;
1866}
1867
1868static unsigned int
1869hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1870 struct beiscsi_hba *phba,
1871 struct async_pdu_handle *pasync_handle)
1872{
1873 struct hwi_async_pdu_context *pasync_ctx;
1874 struct hwi_controller *phwi_ctrlr;
1875 unsigned int bytes_needed = 0, status = 0;
1876 unsigned short cri = pasync_handle->cri;
1877 struct pdu_base *ppdu;
1878
1879 phwi_ctrlr = phba->phwi_ctrlr;
1880 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1881
1882 list_del(&pasync_handle->link);
1883 if (pasync_handle->is_header) {
1884 pasync_ctx->async_header.busy_entries--;
1885 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1886 hwi_free_async_msg(phba, cri);
1887 BUG();
1888 }
1889
1890 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1891 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1892 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1893 (unsigned short)pasync_handle->buffer_len;
1894 list_add_tail(&pasync_handle->link,
1895 &pasync_ctx->async_entry[cri].wait_queue.list);
1896
1897 ppdu = pasync_handle->pbuffer;
1898 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1899 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1900 0xFFFF0000) | ((be16_to_cpu((ppdu->
1901 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1902 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1903
1904 if (status == 0) {
1905 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1906 bytes_needed;
1907
1908 if (bytes_needed == 0)
1909 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1910 pasync_ctx, cri);
1911 }
1912 } else {
1913 pasync_ctx->async_data.busy_entries--;
1914 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1915 list_add_tail(&pasync_handle->link,
1916 &pasync_ctx->async_entry[cri].wait_queue.
1917 list);
1918 pasync_ctx->async_entry[cri].wait_queue.
1919 bytes_received +=
1920 (unsigned short)pasync_handle->buffer_len;
1921
1922 if (pasync_ctx->async_entry[cri].wait_queue.
1923 bytes_received >=
1924 pasync_ctx->async_entry[cri].wait_queue.
1925 bytes_needed)
1926 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1927 pasync_ctx, cri);
1928 }
1929 }
1930 return status;
1931}
1932
1933static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1934 struct beiscsi_hba *phba,
1935 struct i_t_dpdu_cqe *pdpdu_cqe)
1936{
1937 struct hwi_controller *phwi_ctrlr;
1938 struct hwi_async_pdu_context *pasync_ctx;
1939 struct async_pdu_handle *pasync_handle = NULL;
1940 unsigned int cq_index = -1;
1941
1942 phwi_ctrlr = phba->phwi_ctrlr;
1943 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1944 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1945 pdpdu_cqe, &cq_index);
1946
1947 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301948 hwi_update_async_writables(phba, pasync_ctx,
1949 pasync_handle->is_header, cq_index);
1950
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301951 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1952 hwi_post_async_buffers(phba, pasync_handle->is_header);
1953}
1954
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301955static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1956{
1957 struct be_queue_info *mcc_cq;
1958 struct be_mcc_compl *mcc_compl;
1959 unsigned int num_processed = 0;
1960
1961 mcc_cq = &phba->ctrl.mcc_obj.cq;
1962 mcc_compl = queue_tail_node(mcc_cq);
1963 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1964 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1965
1966 if (num_processed >= 32) {
1967 hwi_ring_cq_db(phba, mcc_cq->id,
1968 num_processed, 0, 0);
1969 num_processed = 0;
1970 }
1971 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1972 /* Interpret flags as an async trailer */
1973 if (is_link_state_evt(mcc_compl->flags))
1974 /* Interpret compl as a async link evt */
1975 beiscsi_async_link_state_process(phba,
1976 (struct be_async_event_link_state *) mcc_compl);
1977 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301978 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1979 "BM_%d : Unsupported Async Event, flags"
1980 " = 0x%08x\n",
1981 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301982 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1983 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1984 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1985 }
1986
1987 mcc_compl->flags = 0;
1988 queue_tail_inc(mcc_cq);
1989 mcc_compl = queue_tail_node(mcc_cq);
1990 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1991 num_processed++;
1992 }
1993
1994 if (num_processed > 0)
1995 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1996
1997}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301998
John Soni Jose6763daa2012-10-20 04:41:45 +05301999/**
2000 * beiscsi_process_cq()- Process the Completion Queue
2001 * @pbe_eq: Event Q on which the Completion has come
2002 *
2003 * return
2004 * Number of Completion Entries processed.
2005 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302006static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302007{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302008 struct be_queue_info *cq;
2009 struct sol_cqe *sol;
2010 struct dmsg_cqe *dmsg;
2011 unsigned int num_processed = 0;
2012 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302013 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302014 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302015 struct beiscsi_endpoint *beiscsi_ep;
2016 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302017 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302019 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302020 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302021 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022
2023 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2024 CQE_VALID_MASK) {
2025 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2026
John Soni Jose73133262012-10-20 04:44:49 +05302027 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2028 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302029
John Soni Jose73133262012-10-20 04:44:49 +05302030 /* Get the CID */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002031 if (is_chip_be2_be3r(phba)) {
2032 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2033 } else {
John Soni Jose73133262012-10-20 04:44:49 +05302034 if ((code == DRIVERMSG_NOTIFY) ||
2035 (code == UNSOL_HDR_NOTIFY) ||
2036 (code == UNSOL_DATA_NOTIFY))
2037 cid = AMAP_GET_BITS(
2038 struct amap_i_t_dpdu_cqe_v2,
2039 cid, sol);
2040 else
2041 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2042 cid, sol);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002043 }
John Soni Jose73133262012-10-20 04:44:49 +05302044
2045 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302046 beiscsi_ep = ep->dd_data;
2047 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302048
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302049 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302050 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302051 num_processed, 0, 0);
2052 tot_nump += num_processed;
2053 num_processed = 0;
2054 }
2055
John Soni Jose0a513dd2012-08-20 23:00:55 +05302056 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302057 case SOL_CMD_COMPLETE:
2058 hwi_complete_cmd(beiscsi_conn, phba, sol);
2059 break;
2060 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302061 beiscsi_log(phba, KERN_INFO,
2062 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302063 "BM_%d : Received %s[%d] on CID : %d\n",
2064 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302065
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302066 dmsg = (struct dmsg_cqe *)sol;
2067 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2068 break;
2069 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302070 beiscsi_log(phba, KERN_INFO,
2071 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302072 "BM_%d : Received %s[%d] on CID : %d\n",
2073 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302074
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302075 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2076 (struct i_t_dpdu_cqe *)sol);
2077 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302078 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302079 beiscsi_log(phba, KERN_INFO,
2080 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302081 "BM_%d : Received %s[%d] on CID : %d\n",
2082 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302083
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302084 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2085 (struct i_t_dpdu_cqe *)sol);
2086 break;
2087 case CXN_INVALIDATE_INDEX_NOTIFY:
2088 case CMD_INVALIDATED_NOTIFY:
2089 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302090 beiscsi_log(phba, KERN_ERR,
2091 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302092 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2093 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302094 break;
2095 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2096 case CMD_KILLED_INVALID_STATSN_RCVD:
2097 case CMD_KILLED_INVALID_R2T_RCVD:
2098 case CMD_CXN_KILLED_LUN_INVALID:
2099 case CMD_CXN_KILLED_ICD_INVALID:
2100 case CMD_CXN_KILLED_ITT_INVALID:
2101 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2102 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302103 beiscsi_log(phba, KERN_ERR,
2104 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302105 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2106 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302107 break;
2108 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302109 beiscsi_log(phba, KERN_ERR,
2110 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302111 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2112 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302113 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2114 (struct i_t_dpdu_cqe *) sol);
2115 break;
2116 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2117 case CXN_KILLED_BURST_LEN_MISMATCH:
2118 case CXN_KILLED_AHS_RCVD:
2119 case CXN_KILLED_HDR_DIGEST_ERR:
2120 case CXN_KILLED_UNKNOWN_HDR:
2121 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2122 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2123 case CXN_KILLED_TIMED_OUT:
2124 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302125 case CXN_KILLED_RST_SENT:
2126 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302127 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2128 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2129 case CXN_KILLED_OVER_RUN_RESIDUAL:
2130 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2131 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302132 beiscsi_log(phba, KERN_ERR,
2133 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302134 "BM_%d : Event %s[%d] received on CID : %d\n",
2135 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302136 if (beiscsi_conn)
2137 iscsi_conn_failure(beiscsi_conn->conn,
2138 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302139 break;
2140 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302141 beiscsi_log(phba, KERN_ERR,
2142 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302143 "BM_%d : Invalid CQE Event Received Code : %d"
2144 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302145 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302146 break;
2147 }
2148
2149 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2150 queue_tail_inc(cq);
2151 sol = queue_tail_node(cq);
2152 num_processed++;
2153 }
2154
2155 if (num_processed > 0) {
2156 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302157 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302158 }
2159 return tot_nump;
2160}
2161
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302162void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302163{
2164 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302165 struct hwi_controller *phwi_ctrlr;
2166 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302167 struct beiscsi_hba *phba;
2168 struct be_eq_obj *pbe_eq =
2169 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302170
John Soni Jose72fb46a2012-10-20 04:42:49 +05302171 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302172 phwi_ctrlr = phba->phwi_ctrlr;
2173 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302174
John Soni Jose72fb46a2012-10-20 04:42:49 +05302175 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302176 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302177 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302178 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302179 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302180 }
2181
John Soni Jose72fb46a2012-10-20 04:42:49 +05302182 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302183 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302184 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302185 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302186 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302187 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302188
2189 /* rearm EQ for further interrupts */
2190 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302191}
2192
2193static int be_iopoll(struct blk_iopoll *iop, int budget)
2194{
2195 static unsigned int ret;
2196 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302197 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302198
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302199 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2200 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302201 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302202 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302203 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302204 beiscsi_log(phba, KERN_INFO,
2205 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2206 "BM_%d : rearm pbe_eq->q.id =%d\n",
2207 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302208 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302209 }
2210 return ret;
2211}
2212
2213static void
John Soni Jose09a10932012-10-20 04:44:23 +05302214hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2215 unsigned int num_sg, struct beiscsi_io_task *io_task)
2216{
2217 struct iscsi_sge *psgl;
2218 unsigned int sg_len, index;
2219 unsigned int sge_len = 0;
2220 unsigned long long addr;
2221 struct scatterlist *l_sg;
2222 unsigned int offset;
2223
2224 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2225 io_task->bhs_pa.u.a32.address_lo);
2226 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2227 io_task->bhs_pa.u.a32.address_hi);
2228
2229 l_sg = sg;
2230 for (index = 0; (index < num_sg) && (index < 2); index++,
2231 sg = sg_next(sg)) {
2232 if (index == 0) {
2233 sg_len = sg_dma_len(sg);
2234 addr = (u64) sg_dma_address(sg);
2235 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2236 sge0_addr_lo, pwrb,
2237 lower_32_bits(addr));
2238 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2239 sge0_addr_hi, pwrb,
2240 upper_32_bits(addr));
2241 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2242 sge0_len, pwrb,
2243 sg_len);
2244 sge_len = sg_len;
2245 } else {
2246 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2247 pwrb, sge_len);
2248 sg_len = sg_dma_len(sg);
2249 addr = (u64) sg_dma_address(sg);
2250 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2251 sge1_addr_lo, pwrb,
2252 lower_32_bits(addr));
2253 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2254 sge1_addr_hi, pwrb,
2255 upper_32_bits(addr));
2256 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2257 sge1_len, pwrb,
2258 sg_len);
2259 }
2260 }
2261 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2262 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2263
2264 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2265
2266 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2267 io_task->bhs_pa.u.a32.address_hi);
2268 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2269 io_task->bhs_pa.u.a32.address_lo);
2270
2271 if (num_sg == 1) {
2272 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2273 1);
2274 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2275 0);
2276 } else if (num_sg == 2) {
2277 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2278 0);
2279 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2280 1);
2281 } else {
2282 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2283 0);
2284 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2285 0);
2286 }
2287
2288 sg = l_sg;
2289 psgl++;
2290 psgl++;
2291 offset = 0;
2292 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2293 sg_len = sg_dma_len(sg);
2294 addr = (u64) sg_dma_address(sg);
2295 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2296 lower_32_bits(addr));
2297 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2298 upper_32_bits(addr));
2299 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2300 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2301 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2302 offset += sg_len;
2303 }
2304 psgl--;
2305 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2306}
2307
2308static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302309hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2310 unsigned int num_sg, struct beiscsi_io_task *io_task)
2311{
2312 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302313 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302314 unsigned int sge_len = 0;
2315 unsigned long long addr;
2316 struct scatterlist *l_sg;
2317 unsigned int offset;
2318
2319 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2320 io_task->bhs_pa.u.a32.address_lo);
2321 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2322 io_task->bhs_pa.u.a32.address_hi);
2323
2324 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302325 for (index = 0; (index < num_sg) && (index < 2); index++,
2326 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302327 if (index == 0) {
2328 sg_len = sg_dma_len(sg);
2329 addr = (u64) sg_dma_address(sg);
2330 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302331 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302332 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302333 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302334 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2335 sg_len);
2336 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302337 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302338 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2339 pwrb, sge_len);
2340 sg_len = sg_dma_len(sg);
2341 addr = (u64) sg_dma_address(sg);
2342 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302343 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302344 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302345 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302346 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2347 sg_len);
2348 }
2349 }
2350 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2351 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2352
2353 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2354
2355 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2356 io_task->bhs_pa.u.a32.address_hi);
2357 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2358 io_task->bhs_pa.u.a32.address_lo);
2359
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302360 if (num_sg == 1) {
2361 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2362 1);
2363 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2364 0);
2365 } else if (num_sg == 2) {
2366 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2367 0);
2368 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2369 1);
2370 } else {
2371 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2372 0);
2373 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2374 0);
2375 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302376 sg = l_sg;
2377 psgl++;
2378 psgl++;
2379 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302380 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302381 sg_len = sg_dma_len(sg);
2382 addr = (u64) sg_dma_address(sg);
2383 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2384 (addr & 0xFFFFFFFF));
2385 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2386 (addr >> 32));
2387 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2388 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2389 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2390 offset += sg_len;
2391 }
2392 psgl--;
2393 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2394}
2395
John Soni Josed629c472012-10-20 04:42:00 +05302396/**
2397 * hwi_write_buffer()- Populate the WRB with task info
2398 * @pwrb: ptr to the WRB entry
2399 * @task: iscsi task which is to be executed
2400 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302401static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2402{
2403 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302404 struct beiscsi_io_task *io_task = task->dd_data;
2405 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2406 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302407 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302408
2409 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2410 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2411 io_task->bhs_pa.u.a32.address_lo);
2412 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2413 io_task->bhs_pa.u.a32.address_hi);
2414
2415 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302416
2417 /* Check for the data_count */
2418 dsp_value = (task->data_count) ? 1 : 0;
2419
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002420 if (is_chip_be2_be3r(phba))
2421 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302422 pwrb, dsp_value);
2423 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07002424 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
John Soni Jose09a10932012-10-20 04:44:23 +05302425 pwrb, dsp_value);
2426
2427 /* Map addr only if there is data_count */
2428 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302429 io_task->mtask_addr = pci_map_single(phba->pcidev,
2430 task->data,
2431 task->data_count,
2432 PCI_DMA_TODEVICE);
John Soni Josed629c472012-10-20 04:42:00 +05302433 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302434 } else
John Soni Josed629c472012-10-20 04:42:00 +05302435 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302436
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302437 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302438 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302439 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302440 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302441 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2442 task->data_count);
2443
2444 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2445 } else {
2446 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302447 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302448 }
2449
2450 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2451
2452 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2453
2454 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2455 io_task->bhs_pa.u.a32.address_hi);
2456 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2457 io_task->bhs_pa.u.a32.address_lo);
2458 if (task->data) {
2459 psgl++;
2460 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2461 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2462 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2463 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2464 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2466
2467 psgl++;
2468 if (task->data) {
2469 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302470 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302471 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302472 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302473 }
2474 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2475 }
2476 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2477}
2478
2479static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2480{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302481 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302482 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2483 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2484
2485 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2486 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302487 num_async_pdu_buf_pages =
2488 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2489 phba->params.defpdu_hdr_sz);
2490 num_async_pdu_buf_sgl_pages =
2491 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2492 sizeof(struct phys_addr));
2493 num_async_pdu_data_pages =
2494 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2495 phba->params.defpdu_data_sz);
2496 num_async_pdu_data_sgl_pages =
2497 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2498 sizeof(struct phys_addr));
2499
2500 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2501
2502 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2503 BE_ISCSI_PDU_HEADER_SIZE;
2504 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2505 sizeof(struct hwi_context_memory);
2506
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302507
2508 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2509 * (phba->params.wrbs_per_cxn)
2510 * phba->params.cxns_per_ctrl;
2511 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2512 (phba->params.wrbs_per_cxn);
2513 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2514 phba->params.cxns_per_ctrl);
2515
2516 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2517 phba->params.icds_per_ctrl;
2518 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2519 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2520
2521 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2522 num_async_pdu_buf_pages * PAGE_SIZE;
2523 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2524 num_async_pdu_data_pages * PAGE_SIZE;
2525 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2526 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2527 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2528 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2529 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2530 phba->params.asyncpdus_per_ctrl *
2531 sizeof(struct async_pdu_handle);
2532 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2533 phba->params.asyncpdus_per_ctrl *
2534 sizeof(struct async_pdu_handle);
2535 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2536 sizeof(struct hwi_async_pdu_context) +
2537 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2538}
2539
2540static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2541{
2542 struct be_mem_descriptor *mem_descr;
2543 dma_addr_t bus_add;
2544 struct mem_array *mem_arr, *mem_arr_orig;
2545 unsigned int i, j, alloc_size, curr_alloc_size;
2546
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002547 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302548 if (!phba->phwi_ctrlr)
2549 return -ENOMEM;
2550
2551 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2552 GFP_KERNEL);
2553 if (!phba->init_mem) {
2554 kfree(phba->phwi_ctrlr);
2555 return -ENOMEM;
2556 }
2557
2558 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2559 GFP_KERNEL);
2560 if (!mem_arr_orig) {
2561 kfree(phba->init_mem);
2562 kfree(phba->phwi_ctrlr);
2563 return -ENOMEM;
2564 }
2565
2566 mem_descr = phba->init_mem;
2567 for (i = 0; i < SE_MEM_MAX; i++) {
2568 j = 0;
2569 mem_arr = mem_arr_orig;
2570 alloc_size = phba->mem_req[i];
2571 memset(mem_arr, 0, sizeof(struct mem_array) *
2572 BEISCSI_MAX_FRAGS_INIT);
2573 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2574 do {
2575 mem_arr->virtual_address = pci_alloc_consistent(
2576 phba->pcidev,
2577 curr_alloc_size,
2578 &bus_add);
2579 if (!mem_arr->virtual_address) {
2580 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2581 goto free_mem;
2582 if (curr_alloc_size -
2583 rounddown_pow_of_two(curr_alloc_size))
2584 curr_alloc_size = rounddown_pow_of_two
2585 (curr_alloc_size);
2586 else
2587 curr_alloc_size = curr_alloc_size / 2;
2588 } else {
2589 mem_arr->bus_address.u.
2590 a64.address = (__u64) bus_add;
2591 mem_arr->size = curr_alloc_size;
2592 alloc_size -= curr_alloc_size;
2593 curr_alloc_size = min(be_max_phys_size *
2594 1024, alloc_size);
2595 j++;
2596 mem_arr++;
2597 }
2598 } while (alloc_size);
2599 mem_descr->num_elements = j;
2600 mem_descr->size_in_bytes = phba->mem_req[i];
2601 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2602 GFP_KERNEL);
2603 if (!mem_descr->mem_array)
2604 goto free_mem;
2605
2606 memcpy(mem_descr->mem_array, mem_arr_orig,
2607 sizeof(struct mem_array) * j);
2608 mem_descr++;
2609 }
2610 kfree(mem_arr_orig);
2611 return 0;
2612free_mem:
2613 mem_descr->num_elements = j;
2614 while ((i) || (j)) {
2615 for (j = mem_descr->num_elements; j > 0; j--) {
2616 pci_free_consistent(phba->pcidev,
2617 mem_descr->mem_array[j - 1].size,
2618 mem_descr->mem_array[j - 1].
2619 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302620 (unsigned long)mem_descr->
2621 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302622 bus_address.u.a64.address);
2623 }
2624 if (i) {
2625 i--;
2626 kfree(mem_descr->mem_array);
2627 mem_descr--;
2628 }
2629 }
2630 kfree(mem_arr_orig);
2631 kfree(phba->init_mem);
2632 kfree(phba->phwi_ctrlr);
2633 return -ENOMEM;
2634}
2635
2636static int beiscsi_get_memory(struct beiscsi_hba *phba)
2637{
2638 beiscsi_find_mem_req(phba);
2639 return beiscsi_alloc_mem(phba);
2640}
2641
2642static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2643{
2644 struct pdu_data_out *pdata_out;
2645 struct pdu_nop_out *pnop_out;
2646 struct be_mem_descriptor *mem_descr;
2647
2648 mem_descr = phba->init_mem;
2649 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2650 pdata_out =
2651 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2652 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2653
2654 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2655 IIOC_SCSI_DATA);
2656
2657 pnop_out =
2658 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2659 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2660
2661 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2662 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2663 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2664 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2665}
2666
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002667static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302668{
2669 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002670 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302671 struct hwi_controller *phwi_ctrlr;
2672 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002673 struct iscsi_wrb *pwrb = NULL;
2674 unsigned int num_cxn_wrbh = 0;
2675 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302676
2677 mem_descr_wrbh = phba->init_mem;
2678 mem_descr_wrbh += HWI_MEM_WRBH;
2679
2680 mem_descr_wrb = phba->init_mem;
2681 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302682 phwi_ctrlr = phba->phwi_ctrlr;
2683
2684 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2685 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302686 pwrb_context->pwrb_handle_base =
2687 kzalloc(sizeof(struct wrb_handle *) *
2688 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002689 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302690 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2691 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002692 goto init_wrb_hndl_failed;
2693 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302694 pwrb_context->pwrb_handle_basestd =
2695 kzalloc(sizeof(struct wrb_handle *) *
2696 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002697 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302698 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2699 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002700 goto init_wrb_hndl_failed;
2701 }
2702 if (!num_cxn_wrbh) {
2703 pwrb_handle =
2704 mem_descr_wrbh->mem_array[idx].virtual_address;
2705 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2706 ((sizeof(struct wrb_handle)) *
2707 phba->params.wrbs_per_cxn));
2708 idx++;
2709 }
2710 pwrb_context->alloc_index = 0;
2711 pwrb_context->wrb_handles_available = 0;
2712 pwrb_context->free_index = 0;
2713
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302714 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302715 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2716 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2717 pwrb_context->pwrb_handle_basestd[j] =
2718 pwrb_handle;
2719 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302720 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302721 pwrb_handle++;
2722 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302723 num_cxn_wrbh--;
2724 }
2725 }
2726 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302727 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302728 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002729 if (!num_cxn_wrb) {
2730 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2731 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2732 ((sizeof(struct iscsi_wrb) *
2733 phba->params.wrbs_per_cxn));
2734 idx++;
2735 }
2736
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302737 if (num_cxn_wrb) {
2738 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2739 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2740 pwrb_handle->pwrb = pwrb;
2741 pwrb++;
2742 }
2743 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302744 }
2745 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002746 return 0;
2747init_wrb_hndl_failed:
2748 for (j = index; j > 0; j--) {
2749 pwrb_context = &phwi_ctrlr->wrb_context[j];
2750 kfree(pwrb_context->pwrb_handle_base);
2751 kfree(pwrb_context->pwrb_handle_basestd);
2752 }
2753 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302754}
2755
2756static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2757{
2758 struct hwi_controller *phwi_ctrlr;
2759 struct hba_parameters *p = &phba->params;
2760 struct hwi_async_pdu_context *pasync_ctx;
2761 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002762 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302763 struct be_mem_descriptor *mem_descr;
2764
2765 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2766 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2767
2768 phwi_ctrlr = phba->phwi_ctrlr;
2769 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2770 mem_descr->mem_array[0].virtual_address;
2771 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2772 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2773
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002774 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2775 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302776
2777 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2778 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2779 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302780 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2781 "BM_%d : hwi_init_async_pdu_ctx"
2782 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2783 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302784 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302785 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2786 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302787
2788 pasync_ctx->async_header.va_base =
2789 mem_descr->mem_array[0].virtual_address;
2790
2791 pasync_ctx->async_header.pa_base.u.a64.address =
2792 mem_descr->mem_array[0].bus_address.u.a64.address;
2793
2794 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2795 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2796 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302797 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2798 "BM_%d : hwi_init_async_pdu_ctx"
2799 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2800 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302801 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302802 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2803 "BM_%d : No Virtual address\n");
2804
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302805 pasync_ctx->async_header.ring_base =
2806 mem_descr->mem_array[0].virtual_address;
2807
2808 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2809 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2810 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302811 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2812 "BM_%d : hwi_init_async_pdu_ctx"
2813 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2814 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302815 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302816 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2817 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302818
2819 pasync_ctx->async_header.handle_base =
2820 mem_descr->mem_array[0].virtual_address;
2821 pasync_ctx->async_header.writables = 0;
2822 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2823
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302824
2825 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2826 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2827 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302828 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2829 "BM_%d : hwi_init_async_pdu_ctx"
2830 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2831 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302832 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302833 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2834 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302835
2836 pasync_ctx->async_data.ring_base =
2837 mem_descr->mem_array[0].virtual_address;
2838
2839 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2840 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2841 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302842 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2843 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302844
2845 pasync_ctx->async_data.handle_base =
2846 mem_descr->mem_array[0].virtual_address;
2847 pasync_ctx->async_data.writables = 0;
2848 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2849
2850 pasync_header_h =
2851 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2852 pasync_data_h =
2853 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2854
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002855 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2856 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2857 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302858 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2859 "BM_%d : hwi_init_async_pdu_ctx"
2860 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2861 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002862 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302863 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2864 "BM_%d : No Virtual address\n");
2865
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002866 idx = 0;
2867 pasync_ctx->async_data.va_base =
2868 mem_descr->mem_array[idx].virtual_address;
2869 pasync_ctx->async_data.pa_base.u.a64.address =
2870 mem_descr->mem_array[idx].bus_address.u.a64.address;
2871
2872 num_async_data = ((mem_descr->mem_array[idx].size) /
2873 phba->params.defpdu_data_sz);
2874 num_per_mem = 0;
2875
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302876 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2877 pasync_header_h->cri = -1;
2878 pasync_header_h->index = (char)index;
2879 INIT_LIST_HEAD(&pasync_header_h->link);
2880 pasync_header_h->pbuffer =
2881 (void *)((unsigned long)
2882 (pasync_ctx->async_header.va_base) +
2883 (p->defpdu_hdr_sz * index));
2884
2885 pasync_header_h->pa.u.a64.address =
2886 pasync_ctx->async_header.pa_base.u.a64.address +
2887 (p->defpdu_hdr_sz * index);
2888
2889 list_add_tail(&pasync_header_h->link,
2890 &pasync_ctx->async_header.free_list);
2891 pasync_header_h++;
2892 pasync_ctx->async_header.free_entries++;
2893 pasync_ctx->async_header.writables++;
2894
2895 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2896 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2897 header_busy_list);
2898 pasync_data_h->cri = -1;
2899 pasync_data_h->index = (char)index;
2900 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002901
2902 if (!num_async_data) {
2903 num_per_mem = 0;
2904 idx++;
2905 pasync_ctx->async_data.va_base =
2906 mem_descr->mem_array[idx].virtual_address;
2907 pasync_ctx->async_data.pa_base.u.a64.address =
2908 mem_descr->mem_array[idx].
2909 bus_address.u.a64.address;
2910
2911 num_async_data = ((mem_descr->mem_array[idx].size) /
2912 phba->params.defpdu_data_sz);
2913 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302914 pasync_data_h->pbuffer =
2915 (void *)((unsigned long)
2916 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002917 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302918
2919 pasync_data_h->pa.u.a64.address =
2920 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002921 (p->defpdu_data_sz * num_per_mem);
2922 num_per_mem++;
2923 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302924
2925 list_add_tail(&pasync_data_h->link,
2926 &pasync_ctx->async_data.free_list);
2927 pasync_data_h++;
2928 pasync_ctx->async_data.free_entries++;
2929 pasync_ctx->async_data.writables++;
2930
2931 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2932 }
2933
2934 pasync_ctx->async_header.host_write_ptr = 0;
2935 pasync_ctx->async_header.ep_read_ptr = -1;
2936 pasync_ctx->async_data.host_write_ptr = 0;
2937 pasync_ctx->async_data.ep_read_ptr = -1;
2938}
2939
2940static int
2941be_sgl_create_contiguous(void *virtual_address,
2942 u64 physical_address, u32 length,
2943 struct be_dma_mem *sgl)
2944{
2945 WARN_ON(!virtual_address);
2946 WARN_ON(!physical_address);
2947 WARN_ON(!length > 0);
2948 WARN_ON(!sgl);
2949
2950 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302951 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302952 sgl->size = length;
2953
2954 return 0;
2955}
2956
2957static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2958{
2959 memset(sgl, 0, sizeof(*sgl));
2960}
2961
2962static void
2963hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2964 struct mem_array *pmem, struct be_dma_mem *sgl)
2965{
2966 if (sgl->va)
2967 be_sgl_destroy_contiguous(sgl);
2968
2969 be_sgl_create_contiguous(pmem->virtual_address,
2970 pmem->bus_address.u.a64.address,
2971 pmem->size, sgl);
2972}
2973
2974static void
2975hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2976 struct mem_array *pmem, struct be_dma_mem *sgl)
2977{
2978 if (sgl->va)
2979 be_sgl_destroy_contiguous(sgl);
2980
2981 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2982 pmem->bus_address.u.a64.address,
2983 pmem->size, sgl);
2984}
2985
2986static int be_fill_queue(struct be_queue_info *q,
2987 u16 len, u16 entry_size, void *vaddress)
2988{
2989 struct be_dma_mem *mem = &q->dma_mem;
2990
2991 memset(q, 0, sizeof(*q));
2992 q->len = len;
2993 q->entry_size = entry_size;
2994 mem->size = len * entry_size;
2995 mem->va = vaddress;
2996 if (!mem->va)
2997 return -ENOMEM;
2998 memset(mem->va, 0, mem->size);
2999 return 0;
3000}
3001
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303002static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303003 struct hwi_context_memory *phwi_context)
3004{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303005 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303006 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303007 struct be_queue_info *eq;
3008 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303009 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303010 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303011
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303012 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3013 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303014
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303015 if (phba->msix_enabled)
3016 eq_for_mcc = 1;
3017 else
3018 eq_for_mcc = 0;
3019 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3020 eq = &phwi_context->be_eq[i].q;
3021 mem = &eq->dma_mem;
3022 phwi_context->be_eq[i].phba = phba;
3023 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3024 num_eq_pages * PAGE_SIZE,
3025 &paddr);
3026 if (!eq_vaddress)
3027 goto create_eq_error;
3028
3029 mem->va = eq_vaddress;
3030 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3031 sizeof(struct be_eq_entry), eq_vaddress);
3032 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303033 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3034 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303035 goto create_eq_error;
3036 }
3037
3038 mem->dma = paddr;
3039 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3040 phwi_context->cur_eqd);
3041 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303042 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3043 "BM_%d : beiscsi_cmd_eq_create"
3044 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303045 goto create_eq_error;
3046 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303047
3048 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3049 "BM_%d : eqid = %d\n",
3050 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303051 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303052 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303053create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303054 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303055 eq = &phwi_context->be_eq[i].q;
3056 mem = &eq->dma_mem;
3057 if (mem->va)
3058 pci_free_consistent(phba->pcidev, num_eq_pages
3059 * PAGE_SIZE,
3060 mem->va, mem->dma);
3061 }
3062 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303063}
3064
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303065static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303066 struct hwi_context_memory *phwi_context)
3067{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303068 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303069 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303070 struct be_queue_info *cq, *eq;
3071 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303072 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303073 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303074 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303075
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303076 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3077 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303078
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303079 for (i = 0; i < phba->num_cpus; i++) {
3080 cq = &phwi_context->be_cq[i];
3081 eq = &phwi_context->be_eq[i].q;
3082 pbe_eq = &phwi_context->be_eq[i];
3083 pbe_eq->cq = cq;
3084 pbe_eq->phba = phba;
3085 mem = &cq->dma_mem;
3086 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3087 num_cq_pages * PAGE_SIZE,
3088 &paddr);
3089 if (!cq_vaddress)
3090 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303091 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303092 sizeof(struct sol_cqe), cq_vaddress);
3093 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303094 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3095 "BM_%d : be_fill_queue Failed "
3096 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303097 goto create_cq_error;
3098 }
3099
3100 mem->dma = paddr;
3101 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3102 false, 0);
3103 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303104 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3105 "BM_%d : beiscsi_cmd_eq_create"
3106 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303107 goto create_cq_error;
3108 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303109 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3110 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3111 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303112 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303113 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303114
3115create_cq_error:
3116 for (i = 0; i < phba->num_cpus; i++) {
3117 cq = &phwi_context->be_cq[i];
3118 mem = &cq->dma_mem;
3119 if (mem->va)
3120 pci_free_consistent(phba->pcidev, num_cq_pages
3121 * PAGE_SIZE,
3122 mem->va, mem->dma);
3123 }
3124 return ret;
3125
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303126}
3127
3128static int
3129beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3130 struct hwi_context_memory *phwi_context,
3131 struct hwi_controller *phwi_ctrlr,
3132 unsigned int def_pdu_ring_sz)
3133{
3134 unsigned int idx;
3135 int ret;
3136 struct be_queue_info *dq, *cq;
3137 struct be_dma_mem *mem;
3138 struct be_mem_descriptor *mem_descr;
3139 void *dq_vaddress;
3140
3141 idx = 0;
3142 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303143 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303144 mem = &dq->dma_mem;
3145 mem_descr = phba->init_mem;
3146 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
3147 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3148 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3149 sizeof(struct phys_addr),
3150 sizeof(struct phys_addr), dq_vaddress);
3151 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303152 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3153 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303154 return ret;
3155 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303156 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3157 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303158 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3159 def_pdu_ring_sz,
3160 phba->params.defpdu_hdr_sz);
3161 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303162 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3163 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303164 return ret;
3165 }
3166 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303167 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3168 "BM_%d : iscsi def pdu id is %d\n",
3169 phwi_context->be_def_hdrq.id);
3170
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303171 hwi_post_async_buffers(phba, 1);
3172 return 0;
3173}
3174
3175static int
3176beiscsi_create_def_data(struct beiscsi_hba *phba,
3177 struct hwi_context_memory *phwi_context,
3178 struct hwi_controller *phwi_ctrlr,
3179 unsigned int def_pdu_ring_sz)
3180{
3181 unsigned int idx;
3182 int ret;
3183 struct be_queue_info *dataq, *cq;
3184 struct be_dma_mem *mem;
3185 struct be_mem_descriptor *mem_descr;
3186 void *dq_vaddress;
3187
3188 idx = 0;
3189 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303190 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303191 mem = &dataq->dma_mem;
3192 mem_descr = phba->init_mem;
3193 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3194 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3195 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3196 sizeof(struct phys_addr),
3197 sizeof(struct phys_addr), dq_vaddress);
3198 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303199 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3200 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303201 return ret;
3202 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303203 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3204 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303205 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3206 def_pdu_ring_sz,
3207 phba->params.defpdu_data_sz);
3208 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303209 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3210 "BM_%d be_cmd_create_default_pdu_queue"
3211 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303212 return ret;
3213 }
3214 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303215 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3216 "BM_%d : iscsi def data id is %d\n",
3217 phwi_context->be_def_dataq.id);
3218
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303219 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303220 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3221 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3222
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303223 return 0;
3224}
3225
3226static int
3227beiscsi_post_pages(struct beiscsi_hba *phba)
3228{
3229 struct be_mem_descriptor *mem_descr;
3230 struct mem_array *pm_arr;
3231 unsigned int page_offset, i;
3232 struct be_dma_mem sgl;
3233 int status;
3234
3235 mem_descr = phba->init_mem;
3236 mem_descr += HWI_MEM_SGE;
3237 pm_arr = mem_descr->mem_array;
3238
3239 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3240 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3241 for (i = 0; i < mem_descr->num_elements; i++) {
3242 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3243 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3244 page_offset,
3245 (pm_arr->size / PAGE_SIZE));
3246 page_offset += pm_arr->size / PAGE_SIZE;
3247 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303248 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3249 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303250 return status;
3251 }
3252 pm_arr++;
3253 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303254 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3255 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303256 return 0;
3257}
3258
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303259static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3260{
3261 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003262 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303263 pci_free_consistent(phba->pcidev, mem->size,
3264 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003265 mem->va = NULL;
3266 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303267}
3268
3269static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3270 u16 len, u16 entry_size)
3271{
3272 struct be_dma_mem *mem = &q->dma_mem;
3273
3274 memset(q, 0, sizeof(*q));
3275 q->len = len;
3276 q->entry_size = entry_size;
3277 mem->size = len * entry_size;
3278 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3279 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303280 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303281 memset(mem->va, 0, mem->size);
3282 return 0;
3283}
3284
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303285static int
3286beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3287 struct hwi_context_memory *phwi_context,
3288 struct hwi_controller *phwi_ctrlr)
3289{
3290 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3291 u64 pa_addr_lo;
3292 unsigned int idx, num, i;
3293 struct mem_array *pwrb_arr;
3294 void *wrb_vaddr;
3295 struct be_dma_mem sgl;
3296 struct be_mem_descriptor *mem_descr;
3297 int status;
3298
3299 idx = 0;
3300 mem_descr = phba->init_mem;
3301 mem_descr += HWI_MEM_WRB;
3302 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3303 GFP_KERNEL);
3304 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303305 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3306 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303307 return -ENOMEM;
3308 }
3309 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3310 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3311 num_wrb_rings = mem_descr->mem_array[idx].size /
3312 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3313
3314 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3315 if (num_wrb_rings) {
3316 pwrb_arr[num].virtual_address = wrb_vaddr;
3317 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3318 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3319 sizeof(struct iscsi_wrb);
3320 wrb_vaddr += pwrb_arr[num].size;
3321 pa_addr_lo += pwrb_arr[num].size;
3322 num_wrb_rings--;
3323 } else {
3324 idx++;
3325 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3326 pa_addr_lo = mem_descr->mem_array[idx].\
3327 bus_address.u.a64.address;
3328 num_wrb_rings = mem_descr->mem_array[idx].size /
3329 (phba->params.wrbs_per_cxn *
3330 sizeof(struct iscsi_wrb));
3331 pwrb_arr[num].virtual_address = wrb_vaddr;
3332 pwrb_arr[num].bus_address.u.a64.address\
3333 = pa_addr_lo;
3334 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3335 sizeof(struct iscsi_wrb);
3336 wrb_vaddr += pwrb_arr[num].size;
3337 pa_addr_lo += pwrb_arr[num].size;
3338 num_wrb_rings--;
3339 }
3340 }
3341 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3342 wrb_mem_index = 0;
3343 offset = 0;
3344 size = 0;
3345
3346 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3347 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3348 &phwi_context->be_wrbq[i]);
3349 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303350 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3351 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003352 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303353 return status;
3354 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303355 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3356 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303357 }
3358 kfree(pwrb_arr);
3359 return 0;
3360}
3361
3362static void free_wrb_handles(struct beiscsi_hba *phba)
3363{
3364 unsigned int index;
3365 struct hwi_controller *phwi_ctrlr;
3366 struct hwi_wrb_context *pwrb_context;
3367
3368 phwi_ctrlr = phba->phwi_ctrlr;
3369 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3370 pwrb_context = &phwi_ctrlr->wrb_context[index];
3371 kfree(pwrb_context->pwrb_handle_base);
3372 kfree(pwrb_context->pwrb_handle_basestd);
3373 }
3374}
3375
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303376static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3377{
3378 struct be_queue_info *q;
3379 struct be_ctrl_info *ctrl = &phba->ctrl;
3380
3381 q = &phba->ctrl.mcc_obj.q;
3382 if (q->created)
3383 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3384 be_queue_free(phba, q);
3385
3386 q = &phba->ctrl.mcc_obj.cq;
3387 if (q->created)
3388 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3389 be_queue_free(phba, q);
3390}
3391
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303392static void hwi_cleanup(struct beiscsi_hba *phba)
3393{
3394 struct be_queue_info *q;
3395 struct be_ctrl_info *ctrl = &phba->ctrl;
3396 struct hwi_controller *phwi_ctrlr;
3397 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303398 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303399
3400 phwi_ctrlr = phba->phwi_ctrlr;
3401 phwi_context = phwi_ctrlr->phwi_ctxt;
3402 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3403 q = &phwi_context->be_wrbq[i];
3404 if (q->created)
3405 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3406 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303407 free_wrb_handles(phba);
3408
3409 q = &phwi_context->be_def_hdrq;
3410 if (q->created)
3411 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3412
3413 q = &phwi_context->be_def_dataq;
3414 if (q->created)
3415 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3416
3417 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3418
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303419 for (i = 0; i < (phba->num_cpus); i++) {
3420 q = &phwi_context->be_cq[i];
3421 if (q->created)
3422 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3423 }
3424 if (phba->msix_enabled)
3425 eq_num = 1;
3426 else
3427 eq_num = 0;
3428 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3429 q = &phwi_context->be_eq[i].q;
3430 if (q->created)
3431 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3432 }
3433 be_mcc_queues_destroy(phba);
Jayamohan Kallickal0283fbb2013-04-05 20:38:21 -07003434 be_cmd_fw_uninit(ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303435}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303436
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303437static int be_mcc_queues_create(struct beiscsi_hba *phba,
3438 struct hwi_context_memory *phwi_context)
3439{
3440 struct be_queue_info *q, *cq;
3441 struct be_ctrl_info *ctrl = &phba->ctrl;
3442
3443 /* Alloc MCC compl queue */
3444 cq = &phba->ctrl.mcc_obj.cq;
3445 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3446 sizeof(struct be_mcc_compl)))
3447 goto err;
3448 /* Ask BE to create MCC compl queue; */
3449 if (phba->msix_enabled) {
3450 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3451 [phba->num_cpus].q, false, true, 0))
3452 goto mcc_cq_free;
3453 } else {
3454 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3455 false, true, 0))
3456 goto mcc_cq_free;
3457 }
3458
3459 /* Alloc MCC queue */
3460 q = &phba->ctrl.mcc_obj.q;
3461 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3462 goto mcc_cq_destroy;
3463
3464 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303465 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303466 goto mcc_q_free;
3467
3468 return 0;
3469
3470mcc_q_free:
3471 be_queue_free(phba, q);
3472mcc_cq_destroy:
3473 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3474mcc_cq_free:
3475 be_queue_free(phba, cq);
3476err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303477 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303478}
3479
John Soni Jose107dfcb2012-10-20 04:42:13 +05303480/**
3481 * find_num_cpus()- Get the CPU online count
3482 * @phba: ptr to priv structure
3483 *
3484 * CPU count is used for creating EQ.
3485 **/
3486static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303487{
3488 int num_cpus = 0;
3489
3490 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303491
John Soni Jose22abeef2012-10-20 04:43:32 +05303492 switch (phba->generation) {
3493 case BE_GEN2:
3494 case BE_GEN3:
3495 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3496 BEISCSI_MAX_NUM_CPUS : num_cpus;
3497 break;
3498 case BE_GEN4:
3499 phba->num_cpus = (num_cpus > OC_SKH_MAX_NUM_CPUS) ?
3500 OC_SKH_MAX_NUM_CPUS : num_cpus;
3501 break;
3502 default:
3503 phba->num_cpus = 1;
3504 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303505}
3506
3507static int hwi_init_port(struct beiscsi_hba *phba)
3508{
3509 struct hwi_controller *phwi_ctrlr;
3510 struct hwi_context_memory *phwi_context;
3511 unsigned int def_pdu_ring_sz;
3512 struct be_ctrl_info *ctrl = &phba->ctrl;
3513 int status;
3514
3515 def_pdu_ring_sz =
3516 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3517 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303518 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303519 phwi_context->max_eqd = 0;
3520 phwi_context->min_eqd = 0;
3521 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303522 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303523
3524 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303525 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303526 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3527 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303528 goto error;
3529 }
3530
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303531 status = be_mcc_queues_create(phba, phwi_context);
3532 if (status != 0)
3533 goto error;
3534
3535 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303536 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303537 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3538 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303539 goto error;
3540 }
3541
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303542 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303543 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303544 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3545 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303546 goto error;
3547 }
3548
3549 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3550 def_pdu_ring_sz);
3551 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303552 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3553 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303554 goto error;
3555 }
3556
3557 status = beiscsi_create_def_data(phba, phwi_context,
3558 phwi_ctrlr, def_pdu_ring_sz);
3559 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303560 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3561 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303562 goto error;
3563 }
3564
3565 status = beiscsi_post_pages(phba);
3566 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303567 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3568 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303569 goto error;
3570 }
3571
3572 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3573 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303574 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3575 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303576 goto error;
3577 }
3578
John Soni Jose99bc5d52012-08-20 23:00:18 +05303579 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3580 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303581 return 0;
3582
3583error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303584 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3585 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303586 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003587 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303588}
3589
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590static int hwi_init_controller(struct beiscsi_hba *phba)
3591{
3592 struct hwi_controller *phwi_ctrlr;
3593
3594 phwi_ctrlr = phba->phwi_ctrlr;
3595 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3596 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3597 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303598 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3599 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3600 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303601 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303602 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3603 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3604 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303605 return -ENOMEM;
3606 }
3607
3608 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003609 if (beiscsi_init_wrb_handle(phba))
3610 return -ENOMEM;
3611
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303612 hwi_init_async_pdu_ctx(phba);
3613 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303614 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3615 "BM_%d : hwi_init_controller failed\n");
3616
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303617 return -ENOMEM;
3618 }
3619 return 0;
3620}
3621
3622static void beiscsi_free_mem(struct beiscsi_hba *phba)
3623{
3624 struct be_mem_descriptor *mem_descr;
3625 int i, j;
3626
3627 mem_descr = phba->init_mem;
3628 i = 0;
3629 j = 0;
3630 for (i = 0; i < SE_MEM_MAX; i++) {
3631 for (j = mem_descr->num_elements; j > 0; j--) {
3632 pci_free_consistent(phba->pcidev,
3633 mem_descr->mem_array[j - 1].size,
3634 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303635 (unsigned long)mem_descr->mem_array[j - 1].
3636 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303637 }
3638 kfree(mem_descr->mem_array);
3639 mem_descr++;
3640 }
3641 kfree(phba->init_mem);
3642 kfree(phba->phwi_ctrlr);
3643}
3644
3645static int beiscsi_init_controller(struct beiscsi_hba *phba)
3646{
3647 int ret = -ENOMEM;
3648
3649 ret = beiscsi_get_memory(phba);
3650 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303651 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3652 "BM_%d : beiscsi_dev_probe -"
3653 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303654 return ret;
3655 }
3656
3657 ret = hwi_init_controller(phba);
3658 if (ret)
3659 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303660 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3661 "BM_%d : Return success from beiscsi_init_controller");
3662
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303663 return 0;
3664
3665free_init:
3666 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003667 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303668}
3669
3670static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3671{
3672 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3673 struct sgl_handle *psgl_handle;
3674 struct iscsi_sge *pfrag;
3675 unsigned int arr_index, i, idx;
3676
3677 phba->io_sgl_hndl_avbl = 0;
3678 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303679
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303680 mem_descr_sglh = phba->init_mem;
3681 mem_descr_sglh += HWI_MEM_SGLH;
3682 if (1 == mem_descr_sglh->num_elements) {
3683 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3684 phba->params.ios_per_ctrl,
3685 GFP_KERNEL);
3686 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303687 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3688 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303689 return -ENOMEM;
3690 }
3691 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3692 (phba->params.icds_per_ctrl -
3693 phba->params.ios_per_ctrl),
3694 GFP_KERNEL);
3695 if (!phba->eh_sgl_hndl_base) {
3696 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303697 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3698 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303699 return -ENOMEM;
3700 }
3701 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303702 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3703 "BM_%d : HWI_MEM_SGLH is more than one element."
3704 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303705 return -ENOMEM;
3706 }
3707
3708 arr_index = 0;
3709 idx = 0;
3710 while (idx < mem_descr_sglh->num_elements) {
3711 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3712
3713 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3714 sizeof(struct sgl_handle)); i++) {
3715 if (arr_index < phba->params.ios_per_ctrl) {
3716 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3717 phba->io_sgl_hndl_avbl++;
3718 arr_index++;
3719 } else {
3720 phba->eh_sgl_hndl_base[arr_index -
3721 phba->params.ios_per_ctrl] =
3722 psgl_handle;
3723 arr_index++;
3724 phba->eh_sgl_hndl_avbl++;
3725 }
3726 psgl_handle++;
3727 }
3728 idx++;
3729 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303730 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3731 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3732 "phba->eh_sgl_hndl_avbl=%d\n",
3733 phba->io_sgl_hndl_avbl,
3734 phba->eh_sgl_hndl_avbl);
3735
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303736 mem_descr_sg = phba->init_mem;
3737 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303738 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3739 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3740 mem_descr_sg->num_elements);
3741
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303742 arr_index = 0;
3743 idx = 0;
3744 while (idx < mem_descr_sg->num_elements) {
3745 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3746
3747 for (i = 0;
3748 i < (mem_descr_sg->mem_array[idx].size) /
3749 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3750 i++) {
3751 if (arr_index < phba->params.ios_per_ctrl)
3752 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3753 else
3754 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3755 phba->params.ios_per_ctrl];
3756 psgl_handle->pfrag = pfrag;
3757 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3758 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3759 pfrag += phba->params.num_sge_per_io;
3760 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303761 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303762 }
3763 idx++;
3764 }
3765 phba->io_sgl_free_index = 0;
3766 phba->io_sgl_alloc_index = 0;
3767 phba->eh_sgl_free_index = 0;
3768 phba->eh_sgl_alloc_index = 0;
3769 return 0;
3770}
3771
3772static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3773{
3774 int i, new_cid;
3775
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303776 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303777 GFP_KERNEL);
3778 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303779 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3780 "BM_%d : Failed to allocate memory in "
3781 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303782 return -ENOMEM;
3783 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303784 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303785 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3786 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303787 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3788 "BM_%d : Failed to allocate memory in "
3789 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303790 kfree(phba->cid_array);
3791 return -ENOMEM;
3792 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303793 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303794 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3795 phba->cid_array[i] = new_cid;
3796 new_cid += 2;
3797 }
3798 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3799 return 0;
3800}
3801
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303802static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303803{
3804 struct be_ctrl_info *ctrl = &phba->ctrl;
3805 struct hwi_controller *phwi_ctrlr;
3806 struct hwi_context_memory *phwi_context;
3807 struct be_queue_info *eq;
3808 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303809 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303810 u32 enabled;
3811
3812 phwi_ctrlr = phba->phwi_ctrlr;
3813 phwi_context = phwi_ctrlr->phwi_ctxt;
3814
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303815 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3816 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3817 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303818
3819 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3820 if (!enabled) {
3821 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303822 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3823 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303824 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003825 }
3826
3827 if (!phba->msix_enabled) {
3828 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303829 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3830 "BM_%d : eq->id=%d\n", eq->id);
3831
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003832 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3833 } else {
3834 for (i = 0; i <= phba->num_cpus; i++) {
3835 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303836 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3837 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303838 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3839 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303840 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303841}
3842
3843static void hwi_disable_intr(struct beiscsi_hba *phba)
3844{
3845 struct be_ctrl_info *ctrl = &phba->ctrl;
3846
3847 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3848 u32 reg = ioread32(addr);
3849
3850 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3851 if (enabled) {
3852 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3853 iowrite32(reg, addr);
3854 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303855 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3856 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303857}
3858
John Soni Jose9aef4202012-08-20 23:00:08 +05303859/**
3860 * beiscsi_get_boot_info()- Get the boot session info
3861 * @phba: The device priv structure instance
3862 *
3863 * Get the boot target info and store in driver priv structure
3864 *
3865 * return values
3866 * Success: 0
3867 * Failure: Non-Zero Value
3868 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303869static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3870{
Mike Christie0e438952012-04-03 23:41:51 -05003871 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303872 struct be_dma_mem nonemb_cmd;
John Soni Josee175def2012-10-20 04:45:40 +05303873 unsigned int tag;
John Soni Jose9aef4202012-08-20 23:00:08 +05303874 unsigned int s_handle;
Mike Christief457a462011-06-24 15:11:53 -05003875 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303876
John Soni Jose9aef4202012-08-20 23:00:08 +05303877 /* Get the session handle of the boot target */
3878 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3879 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303880 beiscsi_log(phba, KERN_ERR,
3881 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3882 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303883 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303884 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303885 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3886 sizeof(*session_resp),
3887 &nonemb_cmd.dma);
3888 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303889 beiscsi_log(phba, KERN_ERR,
3890 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3891 "BM_%d : Failed to allocate memory for"
3892 "beiscsi_get_session_info\n");
3893
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303894 return -ENOMEM;
3895 }
3896
3897 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303898 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003899 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303900 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303901 beiscsi_log(phba, KERN_ERR,
3902 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3903 "BM_%d : beiscsi_get_session_info"
3904 " Failed\n");
3905
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303906 goto boot_freemem;
John Soni Josee175def2012-10-20 04:45:40 +05303907 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303908
John Soni Josee175def2012-10-20 04:45:40 +05303909 ret = beiscsi_mccq_compl(phba, tag, NULL, nonemb_cmd.va);
3910 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303911 beiscsi_log(phba, KERN_ERR,
3912 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
John Soni Josee175def2012-10-20 04:45:40 +05303913 "BM_%d : beiscsi_get_session_info Failed");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303914 goto boot_freemem;
3915 }
John Soni Josee175def2012-10-20 04:45:40 +05303916
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303917 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003918
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303919 memcpy(&phba->boot_sess, &session_resp->session_info,
3920 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003921 ret = 0;
3922
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303923boot_freemem:
3924 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3925 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003926 return ret;
3927}
3928
3929static void beiscsi_boot_release(void *data)
3930{
3931 struct beiscsi_hba *phba = data;
3932
3933 scsi_host_put(phba->shost);
3934}
3935
3936static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3937{
3938 struct iscsi_boot_kobj *boot_kobj;
3939
3940 /* get boot info using mgmt cmd */
3941 if (beiscsi_get_boot_info(phba))
3942 /* Try to see if we can carry on without this */
3943 return 0;
3944
3945 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3946 if (!phba->boot_kset)
3947 return -ENOMEM;
3948
3949 /* get a ref because the show function will ref the phba */
3950 if (!scsi_host_get(phba->shost))
3951 goto free_kset;
3952 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3953 beiscsi_show_boot_tgt_info,
3954 beiscsi_tgt_get_attr_visibility,
3955 beiscsi_boot_release);
3956 if (!boot_kobj)
3957 goto put_shost;
3958
3959 if (!scsi_host_get(phba->shost))
3960 goto free_kset;
3961 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3962 beiscsi_show_boot_ini_info,
3963 beiscsi_ini_get_attr_visibility,
3964 beiscsi_boot_release);
3965 if (!boot_kobj)
3966 goto put_shost;
3967
3968 if (!scsi_host_get(phba->shost))
3969 goto free_kset;
3970 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3971 beiscsi_show_boot_eth_info,
3972 beiscsi_eth_get_attr_visibility,
3973 beiscsi_boot_release);
3974 if (!boot_kobj)
3975 goto put_shost;
3976 return 0;
3977
3978put_shost:
3979 scsi_host_put(phba->shost);
3980free_kset:
3981 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303982 return -ENOMEM;
3983}
3984
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303985static int beiscsi_init_port(struct beiscsi_hba *phba)
3986{
3987 int ret;
3988
3989 ret = beiscsi_init_controller(phba);
3990 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303991 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3992 "BM_%d : beiscsi_dev_probe - Failed in"
3993 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303994 return ret;
3995 }
3996 ret = beiscsi_init_sgl_handle(phba);
3997 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303998 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3999 "BM_%d : beiscsi_dev_probe - Failed in"
4000 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304001 goto do_cleanup_ctrlr;
4002 }
4003
4004 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304005 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4006 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304007 kfree(phba->io_sgl_hndl_base);
4008 kfree(phba->eh_sgl_hndl_base);
4009 goto do_cleanup_ctrlr;
4010 }
4011
4012 return ret;
4013
4014do_cleanup_ctrlr:
4015 hwi_cleanup(phba);
4016 return ret;
4017}
4018
4019static void hwi_purge_eq(struct beiscsi_hba *phba)
4020{
4021 struct hwi_controller *phwi_ctrlr;
4022 struct hwi_context_memory *phwi_context;
4023 struct be_queue_info *eq;
4024 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304025 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304026 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304027
4028 phwi_ctrlr = phba->phwi_ctrlr;
4029 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304030 if (phba->msix_enabled)
4031 eq_msix = 1;
4032 else
4033 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304034
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304035 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4036 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304037 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304038 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304039 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4040 & EQE_VALID_MASK) {
4041 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4042 queue_tail_inc(eq);
4043 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304044 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304045 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304046
4047 if (num_processed)
4048 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304049 }
4050}
4051
4052static void beiscsi_clean_port(struct beiscsi_hba *phba)
4053{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05304054 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304055
4056 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
4057 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304058 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4059 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304060
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304061 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304062 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304063 kfree(phba->io_sgl_hndl_base);
4064 kfree(phba->eh_sgl_hndl_base);
4065 kfree(phba->cid_array);
4066 kfree(phba->ep_array);
4067}
4068
John Soni Josed629c472012-10-20 04:42:00 +05304069/**
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004070 * beiscsi_free_mgmt_task_handles()- Free driver CXN resources
4071 * @beiscsi_conn: ptr to the conn to be cleaned up
4072 *
4073 * Free driver mgmt resources binded to CXN.
4074 **/
4075void
4076beiscsi_free_mgmt_task_handles(struct beiscsi_conn *beiscsi_conn)
4077{
4078 struct beiscsi_io_task *io_task;
4079 struct beiscsi_hba *phba = beiscsi_conn->phba;
4080 struct hwi_wrb_context *pwrb_context;
4081 struct hwi_controller *phwi_ctrlr;
4082
4083 phwi_ctrlr = phba->phwi_ctrlr;
4084 pwrb_context = &phwi_ctrlr->wrb_context
4085 [beiscsi_conn->beiscsi_conn_cid
4086 - phba->fw_config.iscsi_cid_start];
4087 io_task = beiscsi_conn->task->dd_data;
4088
4089 if (io_task->pwrb_handle) {
4090 memset(io_task->pwrb_handle->pwrb, 0,
4091 sizeof(struct iscsi_wrb));
4092 free_wrb_handle(phba, pwrb_context,
4093 io_task->pwrb_handle);
4094 io_task->pwrb_handle = NULL;
4095 }
4096
4097 if (io_task->psgl_handle) {
4098 spin_lock_bh(&phba->mgmt_sgl_lock);
4099 free_mgmt_sgl_handle(phba,
4100 io_task->psgl_handle);
4101 spin_unlock_bh(&phba->mgmt_sgl_lock);
4102 io_task->psgl_handle = NULL;
4103 }
4104
4105 if (io_task->mtask_addr)
4106 pci_unmap_single(phba->pcidev,
4107 io_task->mtask_addr,
4108 io_task->mtask_data_count,
4109 PCI_DMA_TODEVICE);
4110}
4111
4112/**
John Soni Josed629c472012-10-20 04:42:00 +05304113 * beiscsi_cleanup_task()- Free driver resources of the task
4114 * @task: ptr to the iscsi task
4115 *
4116 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004117static void beiscsi_cleanup_task(struct iscsi_task *task)
4118{
4119 struct beiscsi_io_task *io_task = task->dd_data;
4120 struct iscsi_conn *conn = task->conn;
4121 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4122 struct beiscsi_hba *phba = beiscsi_conn->phba;
4123 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4124 struct hwi_wrb_context *pwrb_context;
4125 struct hwi_controller *phwi_ctrlr;
4126
4127 phwi_ctrlr = phba->phwi_ctrlr;
4128 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
4129 - phba->fw_config.iscsi_cid_start];
4130
4131 if (io_task->cmd_bhs) {
4132 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4133 io_task->bhs_pa.u.a64.address);
4134 io_task->cmd_bhs = NULL;
4135 }
4136
4137 if (task->sc) {
4138 if (io_task->pwrb_handle) {
4139 free_wrb_handle(phba, pwrb_context,
4140 io_task->pwrb_handle);
4141 io_task->pwrb_handle = NULL;
4142 }
4143
4144 if (io_task->psgl_handle) {
4145 spin_lock(&phba->io_sgl_lock);
4146 free_io_sgl_handle(phba, io_task->psgl_handle);
4147 spin_unlock(&phba->io_sgl_lock);
4148 io_task->psgl_handle = NULL;
4149 }
4150 } else {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004151 if (!beiscsi_conn->login_in_progress)
4152 beiscsi_free_mgmt_task_handles(beiscsi_conn);
Mike Christie1282ab72012-04-18 03:06:00 -05004153 }
4154}
4155
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304156void
4157beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4158 struct beiscsi_offload_params *params)
4159{
4160 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304161 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004162 struct iscsi_task *task = beiscsi_conn->task;
4163 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304164 u32 doorbell = 0;
4165
4166 /*
4167 * We can always use 0 here because it is reserved by libiscsi for
4168 * login/startup related tasks.
4169 */
Mike Christie1282ab72012-04-18 03:06:00 -05004170 beiscsi_conn->login_in_progress = 0;
4171 spin_lock_bh(&session->lock);
4172 beiscsi_cleanup_task(task);
4173 spin_unlock_bh(&session->lock);
4174
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304175 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05304176 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304177
John Soni Joseacb96932012-10-20 04:44:35 +05304178 /* Check for the adapter family */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004179 if (is_chip_be2_be3r(phba))
John Soni Joseacb96932012-10-20 04:44:35 +05304180 beiscsi_offload_cxn_v0(params, pwrb_handle,
4181 phba->init_mem);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004182 else
4183 beiscsi_offload_cxn_v2(params, pwrb_handle);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304184
John Soni Joseacb96932012-10-20 04:44:35 +05304185 be_dws_le_to_cpu(pwrb_handle->pwrb,
4186 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304187
4188 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304189 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304190 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304191 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4192
4193 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4194}
4195
4196static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4197 int *index, int *age)
4198{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304199 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304200 if (age)
4201 *age = conn->session->age;
4202}
4203
4204/**
4205 * beiscsi_alloc_pdu - allocates pdu and related resources
4206 * @task: libiscsi task
4207 * @opcode: opcode of pdu for task
4208 *
4209 * This is called with the session lock held. It will allocate
4210 * the wrb and sgl if needed for the command. And it will prep
4211 * the pdu's itt. beiscsi_parse_pdu will later translate
4212 * the pdu itt to the libiscsi task itt.
4213 */
4214static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4215{
4216 struct beiscsi_io_task *io_task = task->dd_data;
4217 struct iscsi_conn *conn = task->conn;
4218 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4219 struct beiscsi_hba *phba = beiscsi_conn->phba;
4220 struct hwi_wrb_context *pwrb_context;
4221 struct hwi_controller *phwi_ctrlr;
4222 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304223 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4224 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304225
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304226 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004227 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304228 if (!io_task->cmd_bhs)
4229 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304230 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304231 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304232 io_task->conn = beiscsi_conn;
4233
4234 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4235 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304236 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004237 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304238
4239 if (task->sc) {
4240 spin_lock(&phba->io_sgl_lock);
4241 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4242 spin_unlock(&phba->io_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304243 if (!io_task->psgl_handle) {
4244 beiscsi_log(phba, KERN_ERR,
4245 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4246 "BM_%d : Alloc of IO_SGL_ICD Failed"
4247 "for the CID : %d\n",
4248 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304249 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304250 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304251 io_task->pwrb_handle = alloc_wrb_handle(phba,
4252 beiscsi_conn->beiscsi_conn_cid -
4253 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304254 if (!io_task->pwrb_handle) {
4255 beiscsi_log(phba, KERN_ERR,
4256 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4257 "BM_%d : Alloc of WRB_HANDLE Failed"
4258 "for the CID : %d\n",
4259 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304260 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304261 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304262 } else {
4263 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304264 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal43f388b2013-04-05 20:38:25 -07004265 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304266 if (!beiscsi_conn->login_in_progress) {
4267 spin_lock(&phba->mgmt_sgl_lock);
4268 io_task->psgl_handle = (struct sgl_handle *)
4269 alloc_mgmt_sgl_handle(phba);
4270 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304271 if (!io_task->psgl_handle) {
4272 beiscsi_log(phba, KERN_ERR,
4273 BEISCSI_LOG_IO |
4274 BEISCSI_LOG_CONFIG,
4275 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4276 "for the CID : %d\n",
4277 beiscsi_conn->
4278 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304279 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304280 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304281
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304282 beiscsi_conn->login_in_progress = 1;
4283 beiscsi_conn->plogin_sgl_handle =
4284 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304285 io_task->pwrb_handle =
4286 alloc_wrb_handle(phba,
4287 beiscsi_conn->beiscsi_conn_cid -
4288 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304289 if (!io_task->pwrb_handle) {
4290 beiscsi_log(phba, KERN_ERR,
4291 BEISCSI_LOG_IO |
4292 BEISCSI_LOG_CONFIG,
4293 "BM_%d : Alloc of WRB_HANDLE Failed"
4294 "for the CID : %d\n",
4295 beiscsi_conn->
4296 beiscsi_conn_cid);
4297 goto free_mgmt_hndls;
4298 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304299 beiscsi_conn->plogin_wrb_handle =
4300 io_task->pwrb_handle;
4301
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304302 } else {
4303 io_task->psgl_handle =
4304 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304305 io_task->pwrb_handle =
4306 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304307 }
4308 } else {
4309 spin_lock(&phba->mgmt_sgl_lock);
4310 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4311 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304312 if (!io_task->psgl_handle) {
4313 beiscsi_log(phba, KERN_ERR,
4314 BEISCSI_LOG_IO |
4315 BEISCSI_LOG_CONFIG,
4316 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4317 "for the CID : %d\n",
4318 beiscsi_conn->
4319 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304320 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304321 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304322 io_task->pwrb_handle =
4323 alloc_wrb_handle(phba,
4324 beiscsi_conn->beiscsi_conn_cid -
4325 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304326 if (!io_task->pwrb_handle) {
4327 beiscsi_log(phba, KERN_ERR,
4328 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4329 "BM_%d : Alloc of WRB_HANDLE Failed"
4330 "for the CID : %d\n",
4331 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304332 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304333 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304334
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304335 }
4336 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304337 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4338 wrb_index << 16) | (unsigned int)
4339 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304340 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304341
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304342 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4343 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304344
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304345free_io_hndls:
4346 spin_lock(&phba->io_sgl_lock);
4347 free_io_sgl_handle(phba, io_task->psgl_handle);
4348 spin_unlock(&phba->io_sgl_lock);
4349 goto free_hndls;
4350free_mgmt_hndls:
4351 spin_lock(&phba->mgmt_sgl_lock);
4352 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4353 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304354free_hndls:
4355 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304356 pwrb_context = &phwi_ctrlr->wrb_context[
4357 beiscsi_conn->beiscsi_conn_cid -
4358 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304359 if (io_task->pwrb_handle)
4360 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304361 io_task->pwrb_handle = NULL;
4362 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4363 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004364 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304365 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304366}
John Soni Jose09a10932012-10-20 04:44:23 +05304367int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4368 unsigned int num_sg, unsigned int xferlen,
4369 unsigned int writedir)
4370{
4371
4372 struct beiscsi_io_task *io_task = task->dd_data;
4373 struct iscsi_conn *conn = task->conn;
4374 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4375 struct beiscsi_hba *phba = beiscsi_conn->phba;
4376 struct iscsi_wrb *pwrb = NULL;
4377 unsigned int doorbell = 0;
4378
4379 pwrb = io_task->pwrb_handle->pwrb;
4380 memset(pwrb, 0, sizeof(*pwrb));
4381
4382 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4383 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4384
4385 if (writedir) {
4386 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4387 INI_WR_CMD);
4388 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4389 } else {
4390 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4391 INI_RD_CMD);
4392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4393 }
4394
4395 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4396 type, pwrb);
4397
4398 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4399 cpu_to_be16(*(unsigned short *)
4400 &io_task->cmd_bhs->iscsi_hdr.lun));
4401 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4402 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4403 io_task->pwrb_handle->wrb_index);
4404 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4405 be32_to_cpu(task->cmdsn));
4406 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4407 io_task->psgl_handle->sgl_index);
4408
4409 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4410 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4411 io_task->pwrb_handle->nxt_wrb_index);
4412
4413 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4414
4415 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4416 doorbell |= (io_task->pwrb_handle->wrb_index &
4417 DB_DEF_PDU_WRB_INDEX_MASK) <<
4418 DB_DEF_PDU_WRB_INDEX_SHIFT;
4419 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4420 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4421 return 0;
4422}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304423
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304424static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4425 unsigned int num_sg, unsigned int xferlen,
4426 unsigned int writedir)
4427{
4428
4429 struct beiscsi_io_task *io_task = task->dd_data;
4430 struct iscsi_conn *conn = task->conn;
4431 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4432 struct beiscsi_hba *phba = beiscsi_conn->phba;
4433 struct iscsi_wrb *pwrb = NULL;
4434 unsigned int doorbell = 0;
4435
4436 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304437 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4438 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4439
4440 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304441 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4442 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304443 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304444 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304445 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4446 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304447 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4448 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304449
John Soni Jose09a10932012-10-20 04:44:23 +05304450 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4451 type, pwrb);
4452
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304453 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004454 cpu_to_be16(*(unsigned short *)
4455 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304456 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4457 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4458 io_task->pwrb_handle->wrb_index);
4459 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4460 be32_to_cpu(task->cmdsn));
4461 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4462 io_task->psgl_handle->sgl_index);
4463
4464 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4465
4466 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4467 io_task->pwrb_handle->nxt_wrb_index);
4468 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4469
4470 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304471 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304472 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4473 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4474
4475 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4476 return 0;
4477}
4478
4479static int beiscsi_mtask(struct iscsi_task *task)
4480{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304481 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304482 struct iscsi_conn *conn = task->conn;
4483 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4484 struct beiscsi_hba *phba = beiscsi_conn->phba;
4485 struct iscsi_wrb *pwrb = NULL;
4486 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304487 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304488 unsigned int pwrb_typeoffset = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304489
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304490 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304491 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304492 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304493
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004494 if (is_chip_be2_be3r(phba)) {
John Soni Jose09a10932012-10-20 04:44:23 +05304495 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4496 be32_to_cpu(task->cmdsn));
4497 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4498 io_task->pwrb_handle->wrb_index);
4499 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4500 io_task->psgl_handle->sgl_index);
4501 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4502 task->data_count);
4503 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4504 io_task->pwrb_handle->nxt_wrb_index);
4505 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004506 } else {
4507 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4508 be32_to_cpu(task->cmdsn));
4509 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4510 io_task->pwrb_handle->wrb_index);
4511 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4512 io_task->psgl_handle->sgl_index);
4513 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4514 task->data_count);
4515 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4516 io_task->pwrb_handle->nxt_wrb_index);
4517 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
John Soni Jose09a10932012-10-20 04:44:23 +05304518 }
4519
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304520
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304521 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4522 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304523 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304524 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304525 hwi_write_buffer(pwrb, task);
4526 break;
4527 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004528 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304529 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004530 if (is_chip_be2_be3r(phba))
4531 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304532 dmsg, pwrb, 1);
4533 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004534 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304535 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004536 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304537 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004538 if (is_chip_be2_be3r(phba))
4539 AMAP_SET_BITS(struct amap_iscsi_wrb,
John Soni Jose09a10932012-10-20 04:44:23 +05304540 dmsg, pwrb, 0);
4541 else
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004542 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
John Soni Jose09a10932012-10-20 04:44:23 +05304543 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004544 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304545 hwi_write_buffer(pwrb, task);
4546 break;
4547 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304548 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304549 hwi_write_buffer(pwrb, task);
4550 break;
4551 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304552 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304553 hwi_write_buffer(pwrb, task);
4554 break;
4555 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304556 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304557 hwi_write_buffer(pwrb, task);
4558 break;
4559
4560 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304561 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4562 "BM_%d : opcode =%d Not supported\n",
4563 task->hdr->opcode & ISCSI_OPCODE_MASK);
4564
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304565 return -EINVAL;
4566 }
4567
John Soni Jose09a10932012-10-20 04:44:23 +05304568 /* Set the task type */
Jayamohan Kallickal2c9dfd32013-04-05 20:38:26 -07004569 io_task->wrb_type = (is_chip_be2_be3r(phba)) ?
4570 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb) :
4571 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304572
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304573 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304574 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304575 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4576 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4577 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4578 return 0;
4579}
4580
4581static int beiscsi_task_xmit(struct iscsi_task *task)
4582{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304583 struct beiscsi_io_task *io_task = task->dd_data;
4584 struct scsi_cmnd *sc = task->sc;
John Soni Jose09a10932012-10-20 04:44:23 +05304585 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304586 struct scatterlist *sg;
4587 int num_sg;
4588 unsigned int writedir = 0, xferlen = 0;
4589
John Soni Jose09a10932012-10-20 04:44:23 +05304590 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
4591
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304592 if (!sc)
4593 return beiscsi_mtask(task);
4594
4595 io_task->scsi_cmnd = sc;
4596 num_sg = scsi_dma_map(sc);
4597 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304598 struct iscsi_conn *conn = task->conn;
4599 struct beiscsi_hba *phba = NULL;
4600
4601 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4602 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4603 "BM_%d : scsi_dma_map Failed\n");
4604
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304605 return num_sg;
4606 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304607 xferlen = scsi_bufflen(sc);
4608 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304609 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304610 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304611 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304612 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304613
John Soni Jose09a10932012-10-20 04:44:23 +05304614 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304615}
4616
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004617/**
4618 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4619 * @job: job to handle
4620 */
4621static int beiscsi_bsg_request(struct bsg_job *job)
4622{
4623 struct Scsi_Host *shost;
4624 struct beiscsi_hba *phba;
4625 struct iscsi_bsg_request *bsg_req = job->request;
4626 int rc = -EINVAL;
4627 unsigned int tag;
4628 struct be_dma_mem nonemb_cmd;
4629 struct be_cmd_resp_hdr *resp;
4630 struct iscsi_bsg_reply *bsg_reply = job->reply;
4631 unsigned short status, extd_status;
4632
4633 shost = iscsi_job_to_shost(job);
4634 phba = iscsi_host_priv(shost);
4635
4636 switch (bsg_req->msgcode) {
4637 case ISCSI_BSG_HST_VENDOR:
4638 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4639 job->request_payload.payload_len,
4640 &nonemb_cmd.dma);
4641 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304642 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4643 "BM_%d : Failed to allocate memory for "
4644 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304645 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004646 }
4647 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4648 &nonemb_cmd);
4649 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304650 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304651 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304652
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004653 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4654 nonemb_cmd.va, nonemb_cmd.dma);
4655 return -EAGAIN;
John Soni Josee175def2012-10-20 04:45:40 +05304656 }
4657
4658 rc = wait_event_interruptible_timeout(
4659 phba->ctrl.mcc_wait[tag],
4660 phba->ctrl.mcc_numtag[tag],
4661 msecs_to_jiffies(
4662 BEISCSI_HOST_MBX_TIMEOUT));
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004663 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4664 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4665 free_mcc_tag(&phba->ctrl, tag);
4666 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4667 sg_copy_from_buffer(job->reply_payload.sg_list,
4668 job->reply_payload.sg_cnt,
4669 nonemb_cmd.va, (resp->response_length
4670 + sizeof(*resp)));
4671 bsg_reply->reply_payload_rcv_len = resp->response_length;
4672 bsg_reply->result = status;
4673 bsg_job_done(job, bsg_reply->result,
4674 bsg_reply->reply_payload_rcv_len);
4675 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4676 nonemb_cmd.va, nonemb_cmd.dma);
4677 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304678 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304679 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304680 " status = %d extd_status = %d\n",
4681 status, extd_status);
4682
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004683 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304684 } else {
4685 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004686 }
4687 break;
4688
4689 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304690 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4691 "BM_%d : Unsupported bsg command: 0x%x\n",
4692 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004693 break;
4694 }
4695
4696 return rc;
4697}
4698
John Soni Jose99bc5d52012-08-20 23:00:18 +05304699void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4700{
4701 /* Set the logging parameter */
4702 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4703}
4704
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304705/*
4706 * beiscsi_quiesce()- Cleanup Driver resources
4707 * @phba: Instance Priv structure
4708 *
4709 * Free the OS and HW resources held by the driver
4710 **/
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004711static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304712{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304713 struct hwi_controller *phwi_ctrlr;
4714 struct hwi_context_memory *phwi_context;
4715 struct be_eq_obj *pbe_eq;
4716 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304717
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304718 phwi_ctrlr = phba->phwi_ctrlr;
4719 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304720 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304721 if (phba->msix_enabled) {
4722 for (i = 0; i <= phba->num_cpus; i++) {
4723 msix_vec = phba->msix_entries[i].vector;
4724 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004725 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304726 }
4727 } else
4728 if (phba->pcidev->irq)
4729 free_irq(phba->pcidev->irq, phba);
4730 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304731 destroy_workqueue(phba->wq);
4732 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304733 for (i = 0; i < phba->num_cpus; i++) {
4734 pbe_eq = &phwi_context->be_eq[i];
4735 blk_iopoll_disable(&pbe_eq->iopoll);
4736 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304737
4738 beiscsi_clean_port(phba);
4739 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304740
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304741 beiscsi_unmap_pci_function(phba);
4742 pci_free_consistent(phba->pcidev,
4743 phba->ctrl.mbox_mem_alloced.size,
4744 phba->ctrl.mbox_mem_alloced.va,
4745 phba->ctrl.mbox_mem_alloced.dma);
John Soni Jose7a158002012-10-20 04:45:51 +05304746
4747 cancel_delayed_work_sync(&phba->beiscsi_hw_check_task);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004748}
4749
4750static void beiscsi_remove(struct pci_dev *pcidev)
4751{
4752
4753 struct beiscsi_hba *phba = NULL;
4754
4755 phba = pci_get_drvdata(pcidev);
4756 if (!phba) {
4757 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4758 return;
4759 }
4760
Mike Christie0e438952012-04-03 23:41:51 -05004761 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004762 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004763 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304764 iscsi_host_remove(phba->shost);
4765 pci_dev_put(phba->pcidev);
4766 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004767 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304768}
4769
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004770static void beiscsi_shutdown(struct pci_dev *pcidev)
4771{
4772
4773 struct beiscsi_hba *phba = NULL;
4774
4775 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4776 if (!phba) {
4777 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4778 return;
4779 }
4780
4781 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004782 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004783}
4784
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304785static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4786{
4787 int i, status;
4788
4789 for (i = 0; i <= phba->num_cpus; i++)
4790 phba->msix_entries[i].entry = i;
4791
4792 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4793 (phba->num_cpus + 1));
4794 if (!status)
4795 phba->msix_enabled = true;
4796
4797 return;
4798}
4799
John Soni Jose7a158002012-10-20 04:45:51 +05304800/*
4801 * beiscsi_hw_health_check()- Check adapter health
4802 * @work: work item to check HW health
4803 *
4804 * Check if adapter in an unrecoverable state or not.
4805 **/
4806static void
4807beiscsi_hw_health_check(struct work_struct *work)
4808{
4809 struct beiscsi_hba *phba =
4810 container_of(work, struct beiscsi_hba,
4811 beiscsi_hw_check_task.work);
4812
4813 beiscsi_ue_detect(phba);
4814
4815 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4816 msecs_to_jiffies(1000));
4817}
4818
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004819static int beiscsi_dev_probe(struct pci_dev *pcidev,
4820 const struct pci_device_id *id)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304821{
4822 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304823 struct hwi_controller *phwi_ctrlr;
4824 struct hwi_context_memory *phwi_context;
4825 struct be_eq_obj *pbe_eq;
John Soni Jose107dfcb2012-10-20 04:42:13 +05304826 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304827
4828 ret = beiscsi_enable_pci(pcidev);
4829 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304830 dev_err(&pcidev->dev,
4831 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304832 return ret;
4833 }
4834
4835 phba = beiscsi_hba_alloc(pcidev);
4836 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304837 dev_err(&pcidev->dev,
4838 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304839 goto disable_pci;
4840 }
4841
John Soni Jose99bc5d52012-08-20 23:00:18 +05304842 /* Initialize Driver configuration Paramters */
4843 beiscsi_hba_attrs_init(phba);
4844
John Soni Josee175def2012-10-20 04:45:40 +05304845 phba->fw_timeout = false;
4846
4847
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304848 switch (pcidev->device) {
4849 case BE_DEVICE_ID1:
4850 case OC_DEVICE_ID1:
4851 case OC_DEVICE_ID2:
4852 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05304853 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304854 break;
4855 case BE_DEVICE_ID2:
4856 case OC_DEVICE_ID3:
4857 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05304858 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304859 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05304860 case OC_SKH_ID1:
4861 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05304862 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalbf9131c2013-04-05 20:38:24 -07004863 break;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304864 default:
4865 phba->generation = 0;
4866 }
4867
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304868 if (enable_msix)
John Soni Jose107dfcb2012-10-20 04:42:13 +05304869 find_num_cpus(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304870 else
John Soni Jose107dfcb2012-10-20 04:42:13 +05304871 phba->num_cpus = 1;
4872
John Soni Jose99bc5d52012-08-20 23:00:18 +05304873 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4874 "BM_%d : num_cpus = %d\n",
4875 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304876
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004877 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304878 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004879 if (!phba->msix_enabled)
4880 phba->num_cpus = 1;
4881 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304882 ret = be_ctrl_init(phba, pcidev);
4883 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304884 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4885 "BM_%d : beiscsi_dev_probe-"
4886 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304887 goto hba_free;
4888 }
4889
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304890 ret = beiscsi_cmd_reset_function(phba);
4891 if (ret) {
4892 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4893 "BM_%d : Reset Failed. Aborting Crashdump\n");
4894 goto hba_free;
4895 }
4896 ret = be_chk_reset_complete(phba);
4897 if (ret) {
4898 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4899 "BM_%d : Failed to get out of reset."
4900 "Aborting Crashdump\n");
4901 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304902 }
4903
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304904 spin_lock_init(&phba->io_sgl_lock);
4905 spin_lock_init(&phba->mgmt_sgl_lock);
4906 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304907 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4908 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304909 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4910 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304911 goto free_port;
4912 }
4913 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304914 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304915 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304916 ret = beiscsi_init_port(phba);
4917 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304918 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4919 "BM_%d : beiscsi_dev_probe-"
4920 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304921 goto free_port;
4922 }
4923
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304924 for (i = 0; i < MAX_MCC_CMD ; i++) {
4925 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4926 phba->ctrl.mcc_tag[i] = i + 1;
4927 phba->ctrl.mcc_numtag[i + 1] = 0;
4928 phba->ctrl.mcc_tag_available++;
4929 }
4930
4931 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4932
John Soni Jose72fb46a2012-10-20 04:42:49 +05304933 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304934 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004935 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304936 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304937 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4938 "BM_%d : beiscsi_dev_probe-"
4939 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304940 goto free_twq;
4941 }
4942
John Soni Jose7a158002012-10-20 04:45:51 +05304943 INIT_DELAYED_WORK(&phba->beiscsi_hw_check_task,
4944 beiscsi_hw_health_check);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304945
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304946 phwi_ctrlr = phba->phwi_ctrlr;
4947 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05304948
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304949 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304950 for (i = 0; i < phba->num_cpus; i++) {
4951 pbe_eq = &phwi_context->be_eq[i];
4952 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4953 be_iopoll);
4954 blk_iopoll_enable(&pbe_eq->iopoll);
4955 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304956
4957 i = (phba->msix_enabled) ? i : 0;
4958 /* Work item for MCC handling */
4959 pbe_eq = &phwi_context->be_eq[i];
4960 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
4961 } else {
4962 if (phba->msix_enabled) {
4963 for (i = 0; i <= phba->num_cpus; i++) {
4964 pbe_eq = &phwi_context->be_eq[i];
4965 INIT_WORK(&pbe_eq->work_cqs,
4966 beiscsi_process_all_cqs);
4967 }
4968 } else {
4969 pbe_eq = &phwi_context->be_eq[0];
4970 INIT_WORK(&pbe_eq->work_cqs,
4971 beiscsi_process_all_cqs);
4972 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304973 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304974
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304975 ret = beiscsi_init_irqs(phba);
4976 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304977 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4978 "BM_%d : beiscsi_dev_probe-"
4979 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304980 goto free_blkenbld;
4981 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304982 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004983
4984 if (beiscsi_setup_boot_info(phba))
4985 /*
4986 * log error but continue, because we may not be using
4987 * iscsi boot.
4988 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304989 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4990 "BM_%d : Could not set up "
4991 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004992
Mike Christie0e438952012-04-03 23:41:51 -05004993 beiscsi_create_def_ifaces(phba);
John Soni Jose7a158002012-10-20 04:45:51 +05304994 schedule_delayed_work(&phba->beiscsi_hw_check_task,
4995 msecs_to_jiffies(1000));
4996
John Soni Jose99bc5d52012-08-20 23:00:18 +05304997 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4998 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304999 return 0;
5000
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305001free_blkenbld:
5002 destroy_workqueue(phba->wq);
5003 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305004 for (i = 0; i < phba->num_cpus; i++) {
5005 pbe_eq = &phwi_context->be_eq[i];
5006 blk_iopoll_disable(&pbe_eq->iopoll);
5007 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305008free_twq:
5009 beiscsi_clean_port(phba);
5010 beiscsi_free_mem(phba);
5011free_port:
5012 pci_free_consistent(phba->pcidev,
5013 phba->ctrl.mbox_mem_alloced.size,
5014 phba->ctrl.mbox_mem_alloced.va,
5015 phba->ctrl.mbox_mem_alloced.dma);
5016 beiscsi_unmap_pci_function(phba);
5017hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05305018 if (phba->msix_enabled)
5019 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305020 iscsi_host_remove(phba->shost);
5021 pci_dev_put(phba->pcidev);
5022 iscsi_host_free(phba->shost);
5023disable_pci:
5024 pci_disable_device(pcidev);
5025 return ret;
5026}
5027
5028struct iscsi_transport beiscsi_iscsi_transport = {
5029 .owner = THIS_MODULE,
5030 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05305031 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305032 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305033 .create_session = beiscsi_session_create,
5034 .destroy_session = beiscsi_session_destroy,
5035 .create_conn = beiscsi_conn_create,
5036 .bind_conn = beiscsi_conn_bind,
5037 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05005038 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05005039 .set_iface_param = be2iscsi_iface_set_param,
5040 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305041 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005042 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305043 .get_session_param = iscsi_session_get_param,
5044 .get_host_param = beiscsi_get_host_param,
5045 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005046 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305047 .send_pdu = iscsi_conn_send_pdu,
5048 .xmit_task = beiscsi_task_xmit,
5049 .cleanup_task = beiscsi_cleanup_task,
5050 .alloc_pdu = beiscsi_alloc_pdu,
5051 .parse_pdu_itt = beiscsi_parse_pdu,
5052 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005053 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305054 .ep_connect = beiscsi_ep_connect,
5055 .ep_poll = beiscsi_ep_poll,
5056 .ep_disconnect = beiscsi_ep_disconnect,
5057 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005058 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305059};
5060
5061static struct pci_driver beiscsi_pci_driver = {
5062 .name = DRV_NAME,
5063 .probe = beiscsi_dev_probe,
5064 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005065 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305066 .id_table = beiscsi_pci_id_table
5067};
5068
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305069
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305070static int __init beiscsi_module_init(void)
5071{
5072 int ret;
5073
5074 beiscsi_scsi_transport =
5075 iscsi_register_transport(&beiscsi_iscsi_transport);
5076 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305077 printk(KERN_ERR
5078 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305079 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305080 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305081 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5082 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305083
5084 ret = pci_register_driver(&beiscsi_pci_driver);
5085 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305086 printk(KERN_ERR
5087 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305088 goto unregister_iscsi_transport;
5089 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305090 return 0;
5091
5092unregister_iscsi_transport:
5093 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5094 return ret;
5095}
5096
5097static void __exit beiscsi_module_exit(void)
5098{
5099 pci_unregister_driver(&beiscsi_pci_driver);
5100 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5101}
5102
5103module_init(beiscsi_module_init);
5104module_exit(beiscsi_module_exit);