blob: 2d28cac34f55e3a9ee0e385e8a7a5475a7f1fd4e [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickal255fa9a2011-03-25 14:23:57 -07002 * Copyright (C) 2005 - 2011 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 Jose99bc5d52012-08-20 23:00:18 +0530155struct device_attribute *beiscsi_attrs[] = {
156 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530157 &dev_attr_beiscsi_drvr_ver,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530158 NULL,
159};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530160
John Soni Jose6763daa2012-10-20 04:41:45 +0530161static char const *cqe_desc[] = {
162 "RESERVED_DESC",
163 "SOL_CMD_COMPLETE",
164 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
165 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
166 "CXN_KILLED_BURST_LEN_MISMATCH",
167 "CXN_KILLED_AHS_RCVD",
168 "CXN_KILLED_HDR_DIGEST_ERR",
169 "CXN_KILLED_UNKNOWN_HDR",
170 "CXN_KILLED_STALE_ITT_TTT_RCVD",
171 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
172 "CXN_KILLED_RST_RCVD",
173 "CXN_KILLED_TIMED_OUT",
174 "CXN_KILLED_RST_SENT",
175 "CXN_KILLED_FIN_RCVD",
176 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
177 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
178 "CXN_KILLED_OVER_RUN_RESIDUAL",
179 "CXN_KILLED_UNDER_RUN_RESIDUAL",
180 "CMD_KILLED_INVALID_STATSN_RCVD",
181 "CMD_KILLED_INVALID_R2T_RCVD",
182 "CMD_CXN_KILLED_LUN_INVALID",
183 "CMD_CXN_KILLED_ICD_INVALID",
184 "CMD_CXN_KILLED_ITT_INVALID",
185 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
186 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
187 "CXN_INVALIDATE_NOTIFY",
188 "CXN_INVALIDATE_INDEX_NOTIFY",
189 "CMD_INVALIDATED_NOTIFY",
190 "UNSOL_HDR_NOTIFY",
191 "UNSOL_DATA_NOTIFY",
192 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
193 "DRIVERMSG_NOTIFY",
194 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
195 "SOL_CMD_KILLED_DIF_ERR",
196 "CXN_KILLED_SYN_RCVD",
197 "CXN_KILLED_IMM_DATA_RCVD"
198};
199
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530200static int beiscsi_slave_configure(struct scsi_device *sdev)
201{
202 blk_queue_max_segment_size(sdev->request_queue, 65536);
203 return 0;
204}
205
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530206static int beiscsi_eh_abort(struct scsi_cmnd *sc)
207{
208 struct iscsi_cls_session *cls_session;
209 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
210 struct beiscsi_io_task *aborted_io_task;
211 struct iscsi_conn *conn;
212 struct beiscsi_conn *beiscsi_conn;
213 struct beiscsi_hba *phba;
214 struct iscsi_session *session;
215 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530216 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530217 unsigned int cid, tag, num_invalidate;
218
219 cls_session = starget_to_session(scsi_target(sc->device));
220 session = cls_session->dd_data;
221
222 spin_lock_bh(&session->lock);
223 if (!aborted_task || !aborted_task->sc) {
224 /* we raced */
225 spin_unlock_bh(&session->lock);
226 return SUCCESS;
227 }
228
229 aborted_io_task = aborted_task->dd_data;
230 if (!aborted_io_task->scsi_cmnd) {
231 /* raced or invalid command */
232 spin_unlock_bh(&session->lock);
233 return SUCCESS;
234 }
235 spin_unlock_bh(&session->lock);
236 conn = aborted_task->conn;
237 beiscsi_conn = conn->dd_data;
238 phba = beiscsi_conn->phba;
239
240 /* invalidate iocb */
241 cid = beiscsi_conn->beiscsi_conn_cid;
242 inv_tbl = phba->inv_tbl;
243 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
244 inv_tbl->cid = cid;
245 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
246 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530247 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
248 sizeof(struct invalidate_commands_params_in),
249 &nonemb_cmd.dma);
250 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530251 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
252 "BM_%d : Failed to allocate memory for"
253 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530254 return FAILED;
255 }
256 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
257
258 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
259 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530260 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530261 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
262 "BM_%d : mgmt_invalidate_icds could not be"
263 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530264 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
265 nonemb_cmd.va, nonemb_cmd.dma);
266
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530267 return FAILED;
268 } else {
269 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
270 phba->ctrl.mcc_numtag[tag]);
271 free_mcc_tag(&phba->ctrl, tag);
272 }
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;
343 } else {
344 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
345 phba->ctrl.mcc_numtag[tag]);
346 free_mcc_tag(&phba->ctrl, tag);
347 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530348 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
349 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530350 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530351}
352
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530353static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
354{
355 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500356 struct mgmt_session_info *boot_sess = &phba->boot_sess;
357 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530358 char *str = buf;
359 int rc;
360
361 switch (type) {
362 case ISCSI_BOOT_TGT_NAME:
363 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500364 (int)strlen(boot_sess->target_name),
365 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530366 break;
367 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500368 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530369 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500370 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530371 else
372 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500373 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530374 break;
375 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500376 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530377 break;
378
379 case ISCSI_BOOT_TGT_CHAP_NAME:
380 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500381 boot_conn->negotiated_login_options.auth_data.chap.
382 target_chap_name_length,
383 (char *)&boot_conn->negotiated_login_options.
384 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530385 break;
386 case ISCSI_BOOT_TGT_CHAP_SECRET:
387 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500388 boot_conn->negotiated_login_options.auth_data.chap.
389 target_secret_length,
390 (char *)&boot_conn->negotiated_login_options.
391 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530392 break;
393 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
394 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500395 boot_conn->negotiated_login_options.auth_data.chap.
396 intr_chap_name_length,
397 (char *)&boot_conn->negotiated_login_options.
398 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530399 break;
400 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500401 rc = sprintf(str, "%.*s\n",
402 boot_conn->negotiated_login_options.auth_data.chap.
403 intr_secret_length,
404 (char *)&boot_conn->negotiated_login_options.
405 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530406 break;
407 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500408 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530409 break;
410 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500411 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530412 break;
413 default:
414 rc = -ENOSYS;
415 break;
416 }
417 return rc;
418}
419
420static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
421{
422 struct beiscsi_hba *phba = data;
423 char *str = buf;
424 int rc;
425
426 switch (type) {
427 case ISCSI_BOOT_INI_INITIATOR_NAME:
428 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
429 break;
430 default:
431 rc = -ENOSYS;
432 break;
433 }
434 return rc;
435}
436
437static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
438{
439 struct beiscsi_hba *phba = data;
440 char *str = buf;
441 int rc;
442
443 switch (type) {
444 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500445 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530446 break;
447 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500448 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530449 break;
450 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500451 rc = beiscsi_get_macaddr(str, phba);
452 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530453 default:
454 rc = -ENOSYS;
455 break;
456 }
457 return rc;
458}
459
460
Al Viro587a1f12011-07-23 23:11:19 -0400461static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530462{
Al Viro587a1f12011-07-23 23:11:19 -0400463 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464
465 switch (type) {
466 case ISCSI_BOOT_TGT_NAME:
467 case ISCSI_BOOT_TGT_IP_ADDR:
468 case ISCSI_BOOT_TGT_PORT:
469 case ISCSI_BOOT_TGT_CHAP_NAME:
470 case ISCSI_BOOT_TGT_CHAP_SECRET:
471 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
472 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
473 case ISCSI_BOOT_TGT_NIC_ASSOC:
474 case ISCSI_BOOT_TGT_FLAGS:
475 rc = S_IRUGO;
476 break;
477 default:
478 rc = 0;
479 break;
480 }
481 return rc;
482}
483
Al Viro587a1f12011-07-23 23:11:19 -0400484static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530485{
Al Viro587a1f12011-07-23 23:11:19 -0400486 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530487
488 switch (type) {
489 case ISCSI_BOOT_INI_INITIATOR_NAME:
490 rc = S_IRUGO;
491 break;
492 default:
493 rc = 0;
494 break;
495 }
496 return rc;
497}
498
499
Al Viro587a1f12011-07-23 23:11:19 -0400500static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530501{
Al Viro587a1f12011-07-23 23:11:19 -0400502 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530503
504 switch (type) {
505 case ISCSI_BOOT_ETH_FLAGS:
506 case ISCSI_BOOT_ETH_MAC:
507 case ISCSI_BOOT_ETH_INDEX:
508 rc = S_IRUGO;
509 break;
510 default:
511 rc = 0;
512 break;
513 }
514 return rc;
515}
516
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530517/*------------------- PCI Driver operations and data ----------------- */
518static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
519 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530520 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530521 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
522 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
523 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530524 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530525 { 0 }
526};
527MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
528
John Soni Jose99bc5d52012-08-20 23:00:18 +0530529
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530530static struct scsi_host_template beiscsi_sht = {
531 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500532 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530533 .proc_name = DRV_NAME,
534 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530535 .change_queue_depth = iscsi_change_queue_depth,
536 .slave_configure = beiscsi_slave_configure,
537 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530538 .eh_abort_handler = beiscsi_eh_abort,
539 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530540 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530541 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530542 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
543 .can_queue = BE2_IO_DEPTH,
544 .this_id = -1,
545 .max_sectors = BEISCSI_MAX_SECTORS,
546 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
547 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500548 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
549
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530550};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530551
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530552static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530553
554static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
555{
556 struct beiscsi_hba *phba;
557 struct Scsi_Host *shost;
558
559 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
560 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530561 dev_err(&pcidev->dev,
562 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530563 return NULL;
564 }
565 shost->dma_boundary = pcidev->dma_mask;
566 shost->max_id = BE2_MAX_SESSIONS;
567 shost->max_channel = 0;
568 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
569 shost->max_lun = BEISCSI_NUM_MAX_LUN;
570 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530571 phba = iscsi_host_priv(shost);
572 memset(phba, 0, sizeof(*phba));
573 phba->shost = shost;
574 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530575 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500576 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530577
578 if (iscsi_host_add(shost, &phba->pcidev->dev))
579 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530580
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530581 return phba;
582
583free_devices:
584 pci_dev_put(phba->pcidev);
585 iscsi_host_free(phba->shost);
586 return NULL;
587}
588
589static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
590{
591 if (phba->csr_va) {
592 iounmap(phba->csr_va);
593 phba->csr_va = NULL;
594 }
595 if (phba->db_va) {
596 iounmap(phba->db_va);
597 phba->db_va = NULL;
598 }
599 if (phba->pci_va) {
600 iounmap(phba->pci_va);
601 phba->pci_va = NULL;
602 }
603}
604
605static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
606 struct pci_dev *pcidev)
607{
608 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530609 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530610
611 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
612 pci_resource_len(pcidev, 2));
613 if (addr == NULL)
614 return -ENOMEM;
615 phba->ctrl.csr = addr;
616 phba->csr_va = addr;
617 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
618
619 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
620 if (addr == NULL)
621 goto pci_map_err;
622 phba->ctrl.db = addr;
623 phba->db_va = addr;
624 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
625
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530626 if (phba->generation == BE_GEN2)
627 pcicfg_reg = 1;
628 else
629 pcicfg_reg = 0;
630
631 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
632 pci_resource_len(pcidev, pcicfg_reg));
633
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530634 if (addr == NULL)
635 goto pci_map_err;
636 phba->ctrl.pcicfg = addr;
637 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530638 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530639 return 0;
640
641pci_map_err:
642 beiscsi_unmap_pci_function(phba);
643 return -ENOMEM;
644}
645
646static int beiscsi_enable_pci(struct pci_dev *pcidev)
647{
648 int ret;
649
650 ret = pci_enable_device(pcidev);
651 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530652 dev_err(&pcidev->dev,
653 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530654 return ret;
655 }
656
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530657 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530658 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
659 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
660 if (ret) {
661 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
662 pci_disable_device(pcidev);
663 return ret;
664 }
665 }
666 return 0;
667}
668
669static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
670{
671 struct be_ctrl_info *ctrl = &phba->ctrl;
672 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
673 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
674 int status = 0;
675
676 ctrl->pdev = pdev;
677 status = beiscsi_map_pci_bars(phba, pdev);
678 if (status)
679 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530680 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
681 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
682 mbox_mem_alloc->size,
683 &mbox_mem_alloc->dma);
684 if (!mbox_mem_alloc->va) {
685 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500686 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530687 }
688
689 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
690 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
691 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
692 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
693 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530694 spin_lock_init(&phba->ctrl.mcc_lock);
695 spin_lock_init(&phba->ctrl.mcc_cq_lock);
696
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530697 return status;
698}
699
700static void beiscsi_get_params(struct beiscsi_hba *phba)
701{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530702 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
703 - (phba->fw_config.iscsi_cid_count
704 + BE2_TMFS
705 + BE2_NOPOUT_REQ));
706 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530707 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700708 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530709 phba->params.num_sge_per_io = BE2_SGE;
710 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
711 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
712 phba->params.eq_timer = 64;
713 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530714 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
715 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530716 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
717 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530718 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
719 "BM_%d : phba->params.num_eq_entries=%d\n",
720 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530721 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530722 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
723 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530724 phba->params.wrbs_per_cxn = 256;
725}
726
727static void hwi_ring_eq_db(struct beiscsi_hba *phba,
728 unsigned int id, unsigned int clr_interrupt,
729 unsigned int num_processed,
730 unsigned char rearm, unsigned char event)
731{
732 u32 val = 0;
733 val |= id & DB_EQ_RING_ID_MASK;
734 if (rearm)
735 val |= 1 << DB_EQ_REARM_SHIFT;
736 if (clr_interrupt)
737 val |= 1 << DB_EQ_CLR_SHIFT;
738 if (event)
739 val |= 1 << DB_EQ_EVNT_SHIFT;
740 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
741 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
742}
743
744/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530745 * be_isr_mcc - The isr routine of the driver.
746 * @irq: Not used
747 * @dev_id: Pointer to host adapter structure
748 */
749static irqreturn_t be_isr_mcc(int irq, void *dev_id)
750{
751 struct beiscsi_hba *phba;
752 struct be_eq_entry *eqe = NULL;
753 struct be_queue_info *eq;
754 struct be_queue_info *mcc;
755 unsigned int num_eq_processed;
756 struct be_eq_obj *pbe_eq;
757 unsigned long flags;
758
759 pbe_eq = dev_id;
760 eq = &pbe_eq->q;
761 phba = pbe_eq->phba;
762 mcc = &phba->ctrl.mcc_obj.cq;
763 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530764
765 num_eq_processed = 0;
766
767 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
768 & EQE_VALID_MASK) {
769 if (((eqe->dw[offsetof(struct amap_eq_entry,
770 resource_id) / 32] &
771 EQE_RESID_MASK) >> 16) == mcc->id) {
772 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530773 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530774 spin_unlock_irqrestore(&phba->isr_lock, flags);
775 }
776 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
777 queue_tail_inc(eq);
778 eqe = queue_tail_node(eq);
779 num_eq_processed++;
780 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530781 if (pbe_eq->todo_mcc_cq)
782 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530783 if (num_eq_processed)
784 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
785
786 return IRQ_HANDLED;
787}
788
789/**
790 * be_isr_msix - The isr routine of the driver.
791 * @irq: Not used
792 * @dev_id: Pointer to host adapter structure
793 */
794static irqreturn_t be_isr_msix(int irq, void *dev_id)
795{
796 struct beiscsi_hba *phba;
797 struct be_eq_entry *eqe = NULL;
798 struct be_queue_info *eq;
799 struct be_queue_info *cq;
800 unsigned int num_eq_processed;
801 struct be_eq_obj *pbe_eq;
802 unsigned long flags;
803
804 pbe_eq = dev_id;
805 eq = &pbe_eq->q;
806 cq = pbe_eq->cq;
807 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530808
809 phba = pbe_eq->phba;
810 num_eq_processed = 0;
811 if (blk_iopoll_enabled) {
812 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
813 & EQE_VALID_MASK) {
814 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
815 blk_iopoll_sched(&pbe_eq->iopoll);
816
817 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
818 queue_tail_inc(eq);
819 eqe = queue_tail_node(eq);
820 num_eq_processed++;
821 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530822 } else {
823 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
824 & EQE_VALID_MASK) {
825 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530826 pbe_eq->todo_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530827 spin_unlock_irqrestore(&phba->isr_lock, flags);
828 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
829 queue_tail_inc(eq);
830 eqe = queue_tail_node(eq);
831 num_eq_processed++;
832 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530833
John Soni Jose72fb46a2012-10-20 04:42:49 +0530834 if (pbe_eq->todo_cq)
835 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530836 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530837
838 if (num_eq_processed)
839 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
840
841 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530842}
843
844/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530845 * be_isr - The isr routine of the driver.
846 * @irq: Not used
847 * @dev_id: Pointer to host adapter structure
848 */
849static irqreturn_t be_isr(int irq, void *dev_id)
850{
851 struct beiscsi_hba *phba;
852 struct hwi_controller *phwi_ctrlr;
853 struct hwi_context_memory *phwi_context;
854 struct be_eq_entry *eqe = NULL;
855 struct be_queue_info *eq;
856 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530857 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530858 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530861 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530862 int isr;
863
864 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700865 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
867 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
868 if (!isr)
869 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530870
871 phwi_ctrlr = phba->phwi_ctrlr;
872 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530873 pbe_eq = &phwi_context->be_eq[0];
874
875 eq = &phwi_context->be_eq[0].q;
876 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530877 index = 0;
878 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530879
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530880 num_ioeq_processed = 0;
881 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530882 if (blk_iopoll_enabled) {
883 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
884 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530885 if (((eqe->dw[offsetof(struct amap_eq_entry,
886 resource_id) / 32] &
887 EQE_RESID_MASK) >> 16) == mcc->id) {
888 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530889 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530890 spin_unlock_irqrestore(&phba->isr_lock, flags);
891 num_mcceq_processed++;
892 } else {
893 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
894 blk_iopoll_sched(&pbe_eq->iopoll);
895 num_ioeq_processed++;
896 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530897 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
898 queue_tail_inc(eq);
899 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530900 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530901 if (num_ioeq_processed || num_mcceq_processed) {
John Soni Jose72fb46a2012-10-20 04:42:49 +0530902 if (pbe_eq->todo_mcc_cq)
903 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530904
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530905 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530906 hwi_ring_eq_db(phba, eq->id, 0,
907 (num_ioeq_processed +
908 num_mcceq_processed) , 1, 1);
909 else
910 hwi_ring_eq_db(phba, eq->id, 0,
911 (num_ioeq_processed +
912 num_mcceq_processed), 0, 1);
913
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530914 return IRQ_HANDLED;
915 } else
916 return IRQ_NONE;
917 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530918 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530919 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
920 & EQE_VALID_MASK) {
921
922 if (((eqe->dw[offsetof(struct amap_eq_entry,
923 resource_id) / 32] &
924 EQE_RESID_MASK) >> 16) != cq->id) {
925 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530926 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530927 spin_unlock_irqrestore(&phba->isr_lock, flags);
928 } else {
929 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530930 pbe_eq->todo_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530931 spin_unlock_irqrestore(&phba->isr_lock, flags);
932 }
933 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
934 queue_tail_inc(eq);
935 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530936 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530937 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530938 if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
939 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530940
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530941 if (num_ioeq_processed) {
942 hwi_ring_eq_db(phba, eq->id, 0,
943 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530944 return IRQ_HANDLED;
945 } else
946 return IRQ_NONE;
947 }
948}
949
950static int beiscsi_init_irqs(struct beiscsi_hba *phba)
951{
952 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530953 struct hwi_controller *phwi_ctrlr;
954 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530955 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530956
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530957 phwi_ctrlr = phba->phwi_ctrlr;
958 phwi_context = phwi_ctrlr->phwi_ctxt;
959
960 if (phba->msix_enabled) {
961 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700962 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
963 GFP_KERNEL);
964 if (!phba->msi_name[i]) {
965 ret = -ENOMEM;
966 goto free_msix_irqs;
967 }
968
969 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
970 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530971 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700972 ret = request_irq(msix_vec, be_isr_msix, 0,
973 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530974 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530975 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530976 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
977 "BM_%d : beiscsi_init_irqs-Failed to"
978 "register msix for i = %d\n",
979 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700980 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530981 goto free_msix_irqs;
982 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530983 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700984 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
985 if (!phba->msi_name[i]) {
986 ret = -ENOMEM;
987 goto free_msix_irqs;
988 }
989 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
990 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530991 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700992 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530993 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530994 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530995 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
996 "BM_%d : beiscsi_init_irqs-"
997 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700998 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530999 goto free_msix_irqs;
1000 }
1001
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301002 } else {
1003 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1004 "beiscsi", phba);
1005 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301006 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1007 "BM_%d : beiscsi_init_irqs-"
1008 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301009 return ret;
1010 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301011 }
1012 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301013free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001014 for (j = i - 1; j >= 0; j--) {
1015 kfree(phba->msi_name[j]);
1016 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301017 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001018 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301019 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301020}
1021
1022static void hwi_ring_cq_db(struct beiscsi_hba *phba,
1023 unsigned int id, unsigned int num_processed,
1024 unsigned char rearm, unsigned char event)
1025{
1026 u32 val = 0;
1027 val |= id & DB_CQ_RING_ID_MASK;
1028 if (rearm)
1029 val |= 1 << DB_CQ_REARM_SHIFT;
1030 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1031 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1032}
1033
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301034static unsigned int
1035beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1036 struct beiscsi_hba *phba,
1037 unsigned short cid,
1038 struct pdu_base *ppdu,
1039 unsigned long pdu_len,
1040 void *pbuffer, unsigned long buf_len)
1041{
1042 struct iscsi_conn *conn = beiscsi_conn->conn;
1043 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301044 struct iscsi_task *task;
1045 struct beiscsi_io_task *io_task;
1046 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301047
1048 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1049 PDUBASE_OPCODE_MASK) {
1050 case ISCSI_OP_NOOP_IN:
1051 pbuffer = NULL;
1052 buf_len = 0;
1053 break;
1054 case ISCSI_OP_ASYNC_EVENT:
1055 break;
1056 case ISCSI_OP_REJECT:
1057 WARN_ON(!pbuffer);
1058 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301059 beiscsi_log(phba, KERN_ERR,
1060 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1061 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301062 break;
1063 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301064 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301065 task = conn->login_task;
1066 io_task = task->dd_data;
1067 login_hdr = (struct iscsi_hdr *)ppdu;
1068 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301069 break;
1070 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301071 beiscsi_log(phba, KERN_WARNING,
1072 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1073 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1074 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301075 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301076 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301077 return 1;
1078 }
1079
1080 spin_lock_bh(&session->lock);
1081 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1082 spin_unlock_bh(&session->lock);
1083 return 0;
1084}
1085
1086static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1087{
1088 struct sgl_handle *psgl_handle;
1089
1090 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301091 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1092 "BM_%d : In alloc_io_sgl_handle,"
1093 " io_sgl_alloc_index=%d\n",
1094 phba->io_sgl_alloc_index);
1095
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301096 psgl_handle = phba->io_sgl_hndl_base[phba->
1097 io_sgl_alloc_index];
1098 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1099 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301100 if (phba->io_sgl_alloc_index == (phba->params.
1101 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301102 phba->io_sgl_alloc_index = 0;
1103 else
1104 phba->io_sgl_alloc_index++;
1105 } else
1106 psgl_handle = NULL;
1107 return psgl_handle;
1108}
1109
1110static void
1111free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1112{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301113 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1114 "BM_%d : In free_,io_sgl_free_index=%d\n",
1115 phba->io_sgl_free_index);
1116
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301117 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1118 /*
1119 * this can happen if clean_task is called on a task that
1120 * failed in xmit_task or alloc_pdu.
1121 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301122 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1123 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1124 "value there=%p\n", phba->io_sgl_free_index,
1125 phba->io_sgl_hndl_base
1126 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301127 return;
1128 }
1129 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1130 phba->io_sgl_hndl_avbl++;
1131 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1132 phba->io_sgl_free_index = 0;
1133 else
1134 phba->io_sgl_free_index++;
1135}
1136
1137/**
1138 * alloc_wrb_handle - To allocate a wrb handle
1139 * @phba: The hba pointer
1140 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301141 *
1142 * This happens under session_lock until submission to chip
1143 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301144struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301145{
1146 struct hwi_wrb_context *pwrb_context;
1147 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301148 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301149
1150 phwi_ctrlr = phba->phwi_ctrlr;
1151 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301152 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301153 pwrb_handle = pwrb_context->pwrb_handle_base[
1154 pwrb_context->alloc_index];
1155 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301156 if (pwrb_context->alloc_index ==
1157 (phba->params.wrbs_per_cxn - 1))
1158 pwrb_context->alloc_index = 0;
1159 else
1160 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301161 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1162 pwrb_context->alloc_index];
1163 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301164 } else
1165 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301166 return pwrb_handle;
1167}
1168
1169/**
1170 * free_wrb_handle - To free the wrb handle back to pool
1171 * @phba: The hba pointer
1172 * @pwrb_context: The context to free from
1173 * @pwrb_handle: The wrb_handle to free
1174 *
1175 * This happens under session_lock until submission to chip
1176 */
1177static void
1178free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1179 struct wrb_handle *pwrb_handle)
1180{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301181 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301182 pwrb_context->wrb_handles_available++;
1183 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1184 pwrb_context->free_index = 0;
1185 else
1186 pwrb_context->free_index++;
1187
John Soni Jose99bc5d52012-08-20 23:00:18 +05301188 beiscsi_log(phba, KERN_INFO,
1189 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1190 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1191 "wrb_handles_available=%d\n",
1192 pwrb_handle, pwrb_context->free_index,
1193 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301194}
1195
1196static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1197{
1198 struct sgl_handle *psgl_handle;
1199
1200 if (phba->eh_sgl_hndl_avbl) {
1201 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1202 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301203 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1204 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1205 phba->eh_sgl_alloc_index,
1206 phba->eh_sgl_alloc_index);
1207
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301208 phba->eh_sgl_hndl_avbl--;
1209 if (phba->eh_sgl_alloc_index ==
1210 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1211 1))
1212 phba->eh_sgl_alloc_index = 0;
1213 else
1214 phba->eh_sgl_alloc_index++;
1215 } else
1216 psgl_handle = NULL;
1217 return psgl_handle;
1218}
1219
1220void
1221free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1222{
1223
John Soni Jose99bc5d52012-08-20 23:00:18 +05301224 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1225 "BM_%d : In free_mgmt_sgl_handle,"
1226 "eh_sgl_free_index=%d\n",
1227 phba->eh_sgl_free_index);
1228
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301229 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1230 /*
1231 * this can happen if clean_task is called on a task that
1232 * failed in xmit_task or alloc_pdu.
1233 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301234 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1235 "BM_%d : Double Free in eh SGL ,"
1236 "eh_sgl_free_index=%d\n",
1237 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301238 return;
1239 }
1240 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1241 phba->eh_sgl_hndl_avbl++;
1242 if (phba->eh_sgl_free_index ==
1243 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1244 phba->eh_sgl_free_index = 0;
1245 else
1246 phba->eh_sgl_free_index++;
1247}
1248
1249static void
1250be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301251 struct iscsi_task *task,
1252 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301253{
1254 struct beiscsi_io_task *io_task = task->dd_data;
1255 struct be_status_bhs *sts_bhs =
1256 (struct be_status_bhs *)io_task->cmd_bhs;
1257 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301258 unsigned char *sense;
1259 u32 resid = 0, exp_cmdsn, max_cmdsn;
1260 u8 rsp, status, flags;
1261
John Soni Jose73133262012-10-20 04:44:49 +05301262 exp_cmdsn = csol_cqe->exp_cmdsn;
1263 max_cmdsn = (csol_cqe->exp_cmdsn +
1264 csol_cqe->cmd_wnd - 1);
1265 rsp = csol_cqe->i_resp;
1266 status = csol_cqe->i_sts;
1267 flags = csol_cqe->i_flags;
1268 resid = csol_cqe->res_cnt;
1269
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001270 if (!task->sc) {
1271 if (io_task->scsi_cmnd)
1272 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301273
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001274 return;
1275 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301276 task->sc->result = (DID_OK << 16) | status;
1277 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1278 task->sc->result = DID_ERROR << 16;
1279 goto unmap;
1280 }
1281
1282 /* bidi not initially supported */
1283 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301284 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1285 task->sc->result = DID_ERROR << 16;
1286
1287 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1288 scsi_set_resid(task->sc, resid);
1289 if (!status && (scsi_bufflen(task->sc) - resid <
1290 task->sc->underflow))
1291 task->sc->result = DID_ERROR << 16;
1292 }
1293 }
1294
1295 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001296 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301297 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001298
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301299 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001300 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301301 memcpy(task->sc->sense_buffer, sense,
1302 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1303 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301304
John Soni Jose73133262012-10-20 04:44:49 +05301305 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1306 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301307unmap:
1308 scsi_dma_unmap(io_task->scsi_cmnd);
1309 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1310}
1311
1312static void
1313be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301314 struct iscsi_task *task,
1315 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301316{
1317 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301318 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301319 struct iscsi_conn *conn = beiscsi_conn->conn;
1320
1321 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301322 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301323 hdr->t2wait = 5;
1324 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301325 hdr->flags = csol_cqe->i_flags;
1326 hdr->response = csol_cqe->i_resp;
1327 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1328 hdr->max_cmdsn = (csol_cqe->exp_cmdsn + csol_cqe->cmd_wnd - 1);
1329
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301330 hdr->dlength[0] = 0;
1331 hdr->dlength[1] = 0;
1332 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301333 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301334 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1336}
1337
1338static void
1339be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301340 struct iscsi_task *task,
1341 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301342{
1343 struct iscsi_tm_rsp *hdr;
1344 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301345 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301346
1347 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301348 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301349 hdr->flags = csol_cqe->i_flags;
1350 hdr->response = csol_cqe->i_resp;
1351 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1352 hdr->max_cmdsn = (csol_cqe->exp_cmdsn +
1353 csol_cqe->cmd_wnd - 1);
1354
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301355 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301356 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1357}
1358
1359static void
1360hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1361 struct beiscsi_hba *phba, struct sol_cqe *psol)
1362{
1363 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301364 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301365 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301366 struct iscsi_task *task;
1367 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301368 struct iscsi_conn *conn = beiscsi_conn->conn;
1369 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301370 uint16_t wrb_index, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301371
1372 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301373 if (chip_skh_r(phba->pcidev)) {
1374 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1375 wrb_idx, psol);
1376 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1377 cid, psol);
1378 } else {
1379 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1380 wrb_idx, psol);
1381 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1382 cid, psol);
1383 }
1384
1385 pwrb_context = &phwi_ctrlr->wrb_context[
1386 cid - phba->fw_config.iscsi_cid_start];
1387 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301388 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301389
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301390 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001391 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301392 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001393 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394 spin_lock_bh(&session->lock);
1395 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1396 spin_unlock_bh(&session->lock);
1397}
1398
1399static void
1400be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301401 struct iscsi_task *task,
1402 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301403{
1404 struct iscsi_nopin *hdr;
1405 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301406 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301407
1408 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301409 hdr->flags = csol_cqe->i_flags;
1410 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1411 hdr->max_cmdsn = be32_to_cpu(hdr->exp_cmdsn +
1412 csol_cqe->cmd_wnd - 1);
1413
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301414 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301415 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301416 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1417}
1418
John Soni Jose73133262012-10-20 04:44:49 +05301419static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1420 struct sol_cqe *psol,
1421 struct common_sol_cqe *csol_cqe)
1422{
1423 if (chip_skh_r(phba->pcidev)) {
1424 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1425 i_exp_cmd_sn, psol);
1426 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1427 i_res_cnt, psol);
1428 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1429 wrb_index, psol);
1430 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1431 cid, psol);
1432 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1433 hw_sts, psol);
1434 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1435 i_cmd_wnd, psol);
1436 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1437 cmd_cmpl, psol))
1438 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1439 i_sts, psol);
1440 else
1441 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1442 i_sts, psol);
1443 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1444 u, psol))
1445 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1446
1447 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1448 o, psol))
1449 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
1450 } else {
1451 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1452 i_exp_cmd_sn, psol);
1453 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1454 i_res_cnt, psol);
1455 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1456 i_cmd_wnd, psol);
1457 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1458 wrb_index, psol);
1459 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1460 cid, psol);
1461 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1462 hw_sts, psol);
1463 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1464 i_resp, psol);
1465 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1466 i_sts, psol);
1467 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1468 i_flags, psol);
1469 }
1470}
1471
1472
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301473static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1474 struct beiscsi_hba *phba, struct sol_cqe *psol)
1475{
1476 struct hwi_wrb_context *pwrb_context;
1477 struct wrb_handle *pwrb_handle;
1478 struct iscsi_wrb *pwrb = NULL;
1479 struct hwi_controller *phwi_ctrlr;
1480 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301481 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301482 struct iscsi_conn *conn = beiscsi_conn->conn;
1483 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301484 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301485
1486 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301487
1488 /* Copy the elements to a common structure */
1489 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1490
1491 pwrb_context = &phwi_ctrlr->wrb_context[
1492 csol_cqe.cid - phba->fw_config.iscsi_cid_start];
1493
1494 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1495 csol_cqe.wrb_index];
1496
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301497 task = pwrb_handle->pio_handle;
1498 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301499 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301500
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301501 spin_lock_bh(&session->lock);
1502 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301503 case HWH_TYPE_IO:
1504 case HWH_TYPE_IO_RD:
1505 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301506 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301507 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301508 else
John Soni Jose73133262012-10-20 04:44:49 +05301509 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301510 break;
1511
1512 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301513 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301514 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301515 else
John Soni Jose73133262012-10-20 04:44:49 +05301516 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301517 break;
1518
1519 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301520 beiscsi_log(phba, KERN_ERR,
1521 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1522 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1523 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301524 break;
1525
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301527 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301528 break;
1529
1530 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301531 beiscsi_log(phba, KERN_WARNING,
1532 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1533 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1534 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301535 csol_cqe.wrb_index,
1536 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301537 break;
1538 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301539
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301540 spin_unlock_bh(&session->lock);
1541}
1542
1543static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1544 *pasync_ctx, unsigned int is_header,
1545 unsigned int host_write_ptr)
1546{
1547 if (is_header)
1548 return &pasync_ctx->async_entry[host_write_ptr].
1549 header_busy_list;
1550 else
1551 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1552}
1553
1554static struct async_pdu_handle *
1555hwi_get_async_handle(struct beiscsi_hba *phba,
1556 struct beiscsi_conn *beiscsi_conn,
1557 struct hwi_async_pdu_context *pasync_ctx,
1558 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1559{
1560 struct be_bus_address phys_addr;
1561 struct list_head *pbusy_list;
1562 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301563 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301564 unsigned int index, dpl;
1565
1566 if (chip_skh_r(phba->pcidev)) {
1567 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1568 dpl, pdpdu_cqe);
1569 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1570 index, pdpdu_cqe);
1571 } else {
1572 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1573 dpl, pdpdu_cqe);
1574 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1575 index, pdpdu_cqe);
1576 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301577
1578 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301579 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1580 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301581 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301582 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1583 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301584
1585 phys_addr.u.a64.address =
1586 *((unsigned long long *)(&phys_addr.u.a64.address));
1587
1588 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1589 & PDUCQE_CODE_MASK) {
1590 case UNSOL_HDR_NOTIFY:
1591 is_header = 1;
1592
John Soni Jose73133262012-10-20 04:44:49 +05301593 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1594 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301595 break;
1596 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301597 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1598 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301599 break;
1600 default:
1601 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301602 beiscsi_log(phba, KERN_WARNING,
1603 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1604 "BM_%d : Unexpected code=%d\n",
1605 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1606 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301607 return NULL;
1608 }
1609
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301610 WARN_ON(list_empty(pbusy_list));
1611 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001612 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301613 break;
1614 }
1615
1616 WARN_ON(!pasync_handle);
1617
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301618 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1619 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301620 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301621 pasync_handle->buffer_len = dpl;
1622 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301623
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301624 return pasync_handle;
1625}
1626
1627static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301628hwi_update_async_writables(struct beiscsi_hba *phba,
1629 struct hwi_async_pdu_context *pasync_ctx,
1630 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301631{
1632 struct list_head *pbusy_list;
1633 struct async_pdu_handle *pasync_handle;
1634 unsigned int num_entries, writables = 0;
1635 unsigned int *pep_read_ptr, *pwritables;
1636
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001637 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301638 if (is_header) {
1639 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1640 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301641 } else {
1642 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1643 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301644 }
1645
1646 while ((*pep_read_ptr) != cq_index) {
1647 (*pep_read_ptr)++;
1648 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1649
1650 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1651 *pep_read_ptr);
1652 if (writables == 0)
1653 WARN_ON(list_empty(pbusy_list));
1654
1655 if (!list_empty(pbusy_list)) {
1656 pasync_handle = list_entry(pbusy_list->next,
1657 struct async_pdu_handle,
1658 link);
1659 WARN_ON(!pasync_handle);
1660 pasync_handle->consumed = 1;
1661 }
1662
1663 writables++;
1664 }
1665
1666 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301667 beiscsi_log(phba, KERN_ERR,
1668 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1669 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1670 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301671 WARN_ON(1);
1672 }
1673
1674 *pwritables = *pwritables + writables;
1675 return 0;
1676}
1677
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001678static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301679 unsigned int cri)
1680{
1681 struct hwi_controller *phwi_ctrlr;
1682 struct hwi_async_pdu_context *pasync_ctx;
1683 struct async_pdu_handle *pasync_handle, *tmp_handle;
1684 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301685
1686 phwi_ctrlr = phba->phwi_ctrlr;
1687 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1688
1689 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1690
1691 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1692 list_del(&pasync_handle->link);
1693
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001694 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301695 list_add_tail(&pasync_handle->link,
1696 &pasync_ctx->async_header.free_list);
1697 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301698 } else {
1699 list_add_tail(&pasync_handle->link,
1700 &pasync_ctx->async_data.free_list);
1701 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301702 }
1703 }
1704
1705 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1706 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1707 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301708}
1709
1710static struct phys_addr *
1711hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1712 unsigned int is_header, unsigned int host_write_ptr)
1713{
1714 struct phys_addr *pasync_sge = NULL;
1715
1716 if (is_header)
1717 pasync_sge = pasync_ctx->async_header.ring_base;
1718 else
1719 pasync_sge = pasync_ctx->async_data.ring_base;
1720
1721 return pasync_sge + host_write_ptr;
1722}
1723
1724static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1725 unsigned int is_header)
1726{
1727 struct hwi_controller *phwi_ctrlr;
1728 struct hwi_async_pdu_context *pasync_ctx;
1729 struct async_pdu_handle *pasync_handle;
1730 struct list_head *pfree_link, *pbusy_list;
1731 struct phys_addr *pasync_sge;
1732 unsigned int ring_id, num_entries;
1733 unsigned int host_write_num;
1734 unsigned int writables;
1735 unsigned int i = 0;
1736 u32 doorbell = 0;
1737
1738 phwi_ctrlr = phba->phwi_ctrlr;
1739 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001740 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301741
1742 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301743 writables = min(pasync_ctx->async_header.writables,
1744 pasync_ctx->async_header.free_entries);
1745 pfree_link = pasync_ctx->async_header.free_list.next;
1746 host_write_num = pasync_ctx->async_header.host_write_ptr;
1747 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1748 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301749 writables = min(pasync_ctx->async_data.writables,
1750 pasync_ctx->async_data.free_entries);
1751 pfree_link = pasync_ctx->async_data.free_list.next;
1752 host_write_num = pasync_ctx->async_data.host_write_ptr;
1753 ring_id = phwi_ctrlr->default_pdu_data.id;
1754 }
1755
1756 writables = (writables / 8) * 8;
1757 if (writables) {
1758 for (i = 0; i < writables; i++) {
1759 pbusy_list =
1760 hwi_get_async_busy_list(pasync_ctx, is_header,
1761 host_write_num);
1762 pasync_handle =
1763 list_entry(pfree_link, struct async_pdu_handle,
1764 link);
1765 WARN_ON(!pasync_handle);
1766 pasync_handle->consumed = 0;
1767
1768 pfree_link = pfree_link->next;
1769
1770 pasync_sge = hwi_get_ring_address(pasync_ctx,
1771 is_header, host_write_num);
1772
1773 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1774 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1775
1776 list_move(&pasync_handle->link, pbusy_list);
1777
1778 host_write_num++;
1779 host_write_num = host_write_num % num_entries;
1780 }
1781
1782 if (is_header) {
1783 pasync_ctx->async_header.host_write_ptr =
1784 host_write_num;
1785 pasync_ctx->async_header.free_entries -= writables;
1786 pasync_ctx->async_header.writables -= writables;
1787 pasync_ctx->async_header.busy_entries += writables;
1788 } else {
1789 pasync_ctx->async_data.host_write_ptr = host_write_num;
1790 pasync_ctx->async_data.free_entries -= writables;
1791 pasync_ctx->async_data.writables -= writables;
1792 pasync_ctx->async_data.busy_entries += writables;
1793 }
1794
1795 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1796 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1797 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1798 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1799 << DB_DEF_PDU_CQPROC_SHIFT;
1800
1801 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1802 }
1803}
1804
1805static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1806 struct beiscsi_conn *beiscsi_conn,
1807 struct i_t_dpdu_cqe *pdpdu_cqe)
1808{
1809 struct hwi_controller *phwi_ctrlr;
1810 struct hwi_async_pdu_context *pasync_ctx;
1811 struct async_pdu_handle *pasync_handle = NULL;
1812 unsigned int cq_index = -1;
1813
1814 phwi_ctrlr = phba->phwi_ctrlr;
1815 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1816
1817 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1818 pdpdu_cqe, &cq_index);
1819 BUG_ON(pasync_handle->is_header != 0);
1820 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301821 hwi_update_async_writables(phba, pasync_ctx,
1822 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301823
1824 hwi_free_async_msg(phba, pasync_handle->cri);
1825 hwi_post_async_buffers(phba, pasync_handle->is_header);
1826}
1827
1828static unsigned int
1829hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1830 struct beiscsi_hba *phba,
1831 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1832{
1833 struct list_head *plist;
1834 struct async_pdu_handle *pasync_handle;
1835 void *phdr = NULL;
1836 unsigned int hdr_len = 0, buf_len = 0;
1837 unsigned int status, index = 0, offset = 0;
1838 void *pfirst_buffer = NULL;
1839 unsigned int num_buf = 0;
1840
1841 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1842
1843 list_for_each_entry(pasync_handle, plist, link) {
1844 if (index == 0) {
1845 phdr = pasync_handle->pbuffer;
1846 hdr_len = pasync_handle->buffer_len;
1847 } else {
1848 buf_len = pasync_handle->buffer_len;
1849 if (!num_buf) {
1850 pfirst_buffer = pasync_handle->pbuffer;
1851 num_buf++;
1852 }
1853 memcpy(pfirst_buffer + offset,
1854 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001855 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301856 }
1857 index++;
1858 }
1859
1860 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301861 (beiscsi_conn->beiscsi_conn_cid -
1862 phba->fw_config.iscsi_cid_start),
1863 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001864 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301865
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001866 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867 return 0;
1868}
1869
1870static unsigned int
1871hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1872 struct beiscsi_hba *phba,
1873 struct async_pdu_handle *pasync_handle)
1874{
1875 struct hwi_async_pdu_context *pasync_ctx;
1876 struct hwi_controller *phwi_ctrlr;
1877 unsigned int bytes_needed = 0, status = 0;
1878 unsigned short cri = pasync_handle->cri;
1879 struct pdu_base *ppdu;
1880
1881 phwi_ctrlr = phba->phwi_ctrlr;
1882 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1883
1884 list_del(&pasync_handle->link);
1885 if (pasync_handle->is_header) {
1886 pasync_ctx->async_header.busy_entries--;
1887 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1888 hwi_free_async_msg(phba, cri);
1889 BUG();
1890 }
1891
1892 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1893 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1894 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1895 (unsigned short)pasync_handle->buffer_len;
1896 list_add_tail(&pasync_handle->link,
1897 &pasync_ctx->async_entry[cri].wait_queue.list);
1898
1899 ppdu = pasync_handle->pbuffer;
1900 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1901 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1902 0xFFFF0000) | ((be16_to_cpu((ppdu->
1903 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1904 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1905
1906 if (status == 0) {
1907 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1908 bytes_needed;
1909
1910 if (bytes_needed == 0)
1911 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1912 pasync_ctx, cri);
1913 }
1914 } else {
1915 pasync_ctx->async_data.busy_entries--;
1916 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1917 list_add_tail(&pasync_handle->link,
1918 &pasync_ctx->async_entry[cri].wait_queue.
1919 list);
1920 pasync_ctx->async_entry[cri].wait_queue.
1921 bytes_received +=
1922 (unsigned short)pasync_handle->buffer_len;
1923
1924 if (pasync_ctx->async_entry[cri].wait_queue.
1925 bytes_received >=
1926 pasync_ctx->async_entry[cri].wait_queue.
1927 bytes_needed)
1928 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1929 pasync_ctx, cri);
1930 }
1931 }
1932 return status;
1933}
1934
1935static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1936 struct beiscsi_hba *phba,
1937 struct i_t_dpdu_cqe *pdpdu_cqe)
1938{
1939 struct hwi_controller *phwi_ctrlr;
1940 struct hwi_async_pdu_context *pasync_ctx;
1941 struct async_pdu_handle *pasync_handle = NULL;
1942 unsigned int cq_index = -1;
1943
1944 phwi_ctrlr = phba->phwi_ctrlr;
1945 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1946 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1947 pdpdu_cqe, &cq_index);
1948
1949 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301950 hwi_update_async_writables(phba, pasync_ctx,
1951 pasync_handle->is_header, cq_index);
1952
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301953 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1954 hwi_post_async_buffers(phba, pasync_handle->is_header);
1955}
1956
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301957static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1958{
1959 struct be_queue_info *mcc_cq;
1960 struct be_mcc_compl *mcc_compl;
1961 unsigned int num_processed = 0;
1962
1963 mcc_cq = &phba->ctrl.mcc_obj.cq;
1964 mcc_compl = queue_tail_node(mcc_cq);
1965 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1966 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1967
1968 if (num_processed >= 32) {
1969 hwi_ring_cq_db(phba, mcc_cq->id,
1970 num_processed, 0, 0);
1971 num_processed = 0;
1972 }
1973 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1974 /* Interpret flags as an async trailer */
1975 if (is_link_state_evt(mcc_compl->flags))
1976 /* Interpret compl as a async link evt */
1977 beiscsi_async_link_state_process(phba,
1978 (struct be_async_event_link_state *) mcc_compl);
1979 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301980 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1981 "BM_%d : Unsupported Async Event, flags"
1982 " = 0x%08x\n",
1983 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301984 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1985 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1986 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1987 }
1988
1989 mcc_compl->flags = 0;
1990 queue_tail_inc(mcc_cq);
1991 mcc_compl = queue_tail_node(mcc_cq);
1992 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1993 num_processed++;
1994 }
1995
1996 if (num_processed > 0)
1997 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1998
1999}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302000
John Soni Jose6763daa2012-10-20 04:41:45 +05302001/**
2002 * beiscsi_process_cq()- Process the Completion Queue
2003 * @pbe_eq: Event Q on which the Completion has come
2004 *
2005 * return
2006 * Number of Completion Entries processed.
2007 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302008static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302009{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302010 struct be_queue_info *cq;
2011 struct sol_cqe *sol;
2012 struct dmsg_cqe *dmsg;
2013 unsigned int num_processed = 0;
2014 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302015 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302016 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302017 struct beiscsi_endpoint *beiscsi_ep;
2018 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302019 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302020
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302021 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302023 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024
2025 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2026 CQE_VALID_MASK) {
2027 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2028
John Soni Jose73133262012-10-20 04:44:49 +05302029 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2030 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302031
John Soni Jose73133262012-10-20 04:44:49 +05302032 /* Get the CID */
2033 if (chip_skh_r(phba->pcidev)) {
2034 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);
2043 } else
2044 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2045
2046 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05302047 beiscsi_ep = ep->dd_data;
2048 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302049
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302050 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302051 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302052 num_processed, 0, 0);
2053 tot_nump += num_processed;
2054 num_processed = 0;
2055 }
2056
John Soni Jose0a513dd2012-08-20 23:00:55 +05302057 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302058 case SOL_CMD_COMPLETE:
2059 hwi_complete_cmd(beiscsi_conn, phba, sol);
2060 break;
2061 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302062 beiscsi_log(phba, KERN_INFO,
2063 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302064 "BM_%d : Received %s[%d] on CID : %d\n",
2065 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302066
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302067 dmsg = (struct dmsg_cqe *)sol;
2068 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2069 break;
2070 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302071 beiscsi_log(phba, KERN_INFO,
2072 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302073 "BM_%d : Received %s[%d] on CID : %d\n",
2074 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302075
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302076 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2077 (struct i_t_dpdu_cqe *)sol);
2078 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302079 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302080 beiscsi_log(phba, KERN_INFO,
2081 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302082 "BM_%d : Received %s[%d] on CID : %d\n",
2083 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302084
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302085 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2086 (struct i_t_dpdu_cqe *)sol);
2087 break;
2088 case CXN_INVALIDATE_INDEX_NOTIFY:
2089 case CMD_INVALIDATED_NOTIFY:
2090 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302091 beiscsi_log(phba, KERN_ERR,
2092 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302093 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2094 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302095 break;
2096 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2097 case CMD_KILLED_INVALID_STATSN_RCVD:
2098 case CMD_KILLED_INVALID_R2T_RCVD:
2099 case CMD_CXN_KILLED_LUN_INVALID:
2100 case CMD_CXN_KILLED_ICD_INVALID:
2101 case CMD_CXN_KILLED_ITT_INVALID:
2102 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2103 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302104 beiscsi_log(phba, KERN_ERR,
2105 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302106 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2107 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302108 break;
2109 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302110 beiscsi_log(phba, KERN_ERR,
2111 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302112 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2113 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302114 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2115 (struct i_t_dpdu_cqe *) sol);
2116 break;
2117 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2118 case CXN_KILLED_BURST_LEN_MISMATCH:
2119 case CXN_KILLED_AHS_RCVD:
2120 case CXN_KILLED_HDR_DIGEST_ERR:
2121 case CXN_KILLED_UNKNOWN_HDR:
2122 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2123 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2124 case CXN_KILLED_TIMED_OUT:
2125 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302126 case CXN_KILLED_RST_SENT:
2127 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302128 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2129 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2130 case CXN_KILLED_OVER_RUN_RESIDUAL:
2131 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2132 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302133 beiscsi_log(phba, KERN_ERR,
2134 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302135 "BM_%d : Event %s[%d] received on CID : %d\n",
2136 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302137 if (beiscsi_conn)
2138 iscsi_conn_failure(beiscsi_conn->conn,
2139 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302140 break;
2141 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302142 beiscsi_log(phba, KERN_ERR,
2143 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302144 "BM_%d : Invalid CQE Event Received Code : %d"
2145 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302146 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302147 break;
2148 }
2149
2150 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2151 queue_tail_inc(cq);
2152 sol = queue_tail_node(cq);
2153 num_processed++;
2154 }
2155
2156 if (num_processed > 0) {
2157 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302158 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302159 }
2160 return tot_nump;
2161}
2162
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302163void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302164{
2165 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302166 struct hwi_controller *phwi_ctrlr;
2167 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302168 struct beiscsi_hba *phba;
2169 struct be_eq_obj *pbe_eq =
2170 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302171
John Soni Jose72fb46a2012-10-20 04:42:49 +05302172 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302173 phwi_ctrlr = phba->phwi_ctrlr;
2174 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302175
John Soni Jose72fb46a2012-10-20 04:42:49 +05302176 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302177 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302178 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302179 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302180 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302181 }
2182
John Soni Jose72fb46a2012-10-20 04:42:49 +05302183 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302184 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302185 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302186 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302187 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302188 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302189
2190 /* rearm EQ for further interrupts */
2191 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302192}
2193
2194static int be_iopoll(struct blk_iopoll *iop, int budget)
2195{
2196 static unsigned int ret;
2197 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302198 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302199
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302200 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2201 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302202 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302203 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302204 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302205 beiscsi_log(phba, KERN_INFO,
2206 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2207 "BM_%d : rearm pbe_eq->q.id =%d\n",
2208 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302209 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302210 }
2211 return ret;
2212}
2213
2214static void
John Soni Jose09a10932012-10-20 04:44:23 +05302215hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2216 unsigned int num_sg, struct beiscsi_io_task *io_task)
2217{
2218 struct iscsi_sge *psgl;
2219 unsigned int sg_len, index;
2220 unsigned int sge_len = 0;
2221 unsigned long long addr;
2222 struct scatterlist *l_sg;
2223 unsigned int offset;
2224
2225 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2226 io_task->bhs_pa.u.a32.address_lo);
2227 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2228 io_task->bhs_pa.u.a32.address_hi);
2229
2230 l_sg = sg;
2231 for (index = 0; (index < num_sg) && (index < 2); index++,
2232 sg = sg_next(sg)) {
2233 if (index == 0) {
2234 sg_len = sg_dma_len(sg);
2235 addr = (u64) sg_dma_address(sg);
2236 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2237 sge0_addr_lo, pwrb,
2238 lower_32_bits(addr));
2239 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2240 sge0_addr_hi, pwrb,
2241 upper_32_bits(addr));
2242 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2243 sge0_len, pwrb,
2244 sg_len);
2245 sge_len = sg_len;
2246 } else {
2247 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2248 pwrb, sge_len);
2249 sg_len = sg_dma_len(sg);
2250 addr = (u64) sg_dma_address(sg);
2251 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2252 sge1_addr_lo, pwrb,
2253 lower_32_bits(addr));
2254 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2255 sge1_addr_hi, pwrb,
2256 upper_32_bits(addr));
2257 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2258 sge1_len, pwrb,
2259 sg_len);
2260 }
2261 }
2262 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2263 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2264
2265 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2266
2267 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2268 io_task->bhs_pa.u.a32.address_hi);
2269 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2270 io_task->bhs_pa.u.a32.address_lo);
2271
2272 if (num_sg == 1) {
2273 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2274 1);
2275 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2276 0);
2277 } else if (num_sg == 2) {
2278 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2279 0);
2280 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2281 1);
2282 } else {
2283 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2284 0);
2285 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2286 0);
2287 }
2288
2289 sg = l_sg;
2290 psgl++;
2291 psgl++;
2292 offset = 0;
2293 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2294 sg_len = sg_dma_len(sg);
2295 addr = (u64) sg_dma_address(sg);
2296 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2297 lower_32_bits(addr));
2298 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2299 upper_32_bits(addr));
2300 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2301 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2302 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2303 offset += sg_len;
2304 }
2305 psgl--;
2306 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2307}
2308
2309static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302310hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2311 unsigned int num_sg, struct beiscsi_io_task *io_task)
2312{
2313 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302314 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302315 unsigned int sge_len = 0;
2316 unsigned long long addr;
2317 struct scatterlist *l_sg;
2318 unsigned int offset;
2319
2320 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2321 io_task->bhs_pa.u.a32.address_lo);
2322 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2323 io_task->bhs_pa.u.a32.address_hi);
2324
2325 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302326 for (index = 0; (index < num_sg) && (index < 2); index++,
2327 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302328 if (index == 0) {
2329 sg_len = sg_dma_len(sg);
2330 addr = (u64) sg_dma_address(sg);
2331 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302332 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302333 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302334 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302335 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2336 sg_len);
2337 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302338 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302339 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2340 pwrb, sge_len);
2341 sg_len = sg_dma_len(sg);
2342 addr = (u64) sg_dma_address(sg);
2343 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302344 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302345 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302346 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302347 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2348 sg_len);
2349 }
2350 }
2351 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2352 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2353
2354 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2355
2356 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2357 io_task->bhs_pa.u.a32.address_hi);
2358 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2359 io_task->bhs_pa.u.a32.address_lo);
2360
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302361 if (num_sg == 1) {
2362 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2363 1);
2364 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2365 0);
2366 } else if (num_sg == 2) {
2367 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2368 0);
2369 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2370 1);
2371 } else {
2372 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2373 0);
2374 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2375 0);
2376 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302377 sg = l_sg;
2378 psgl++;
2379 psgl++;
2380 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302381 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302382 sg_len = sg_dma_len(sg);
2383 addr = (u64) sg_dma_address(sg);
2384 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2385 (addr & 0xFFFFFFFF));
2386 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2387 (addr >> 32));
2388 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2389 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2390 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2391 offset += sg_len;
2392 }
2393 psgl--;
2394 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2395}
2396
John Soni Josed629c472012-10-20 04:42:00 +05302397/**
2398 * hwi_write_buffer()- Populate the WRB with task info
2399 * @pwrb: ptr to the WRB entry
2400 * @task: iscsi task which is to be executed
2401 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302402static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2403{
2404 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302405 struct beiscsi_io_task *io_task = task->dd_data;
2406 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2407 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302408 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302409
2410 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2411 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2412 io_task->bhs_pa.u.a32.address_lo);
2413 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2414 io_task->bhs_pa.u.a32.address_hi);
2415
2416 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302417
2418 /* Check for the data_count */
2419 dsp_value = (task->data_count) ? 1 : 0;
2420
2421 if (chip_skh_r(phba->pcidev))
2422 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
2423 pwrb, dsp_value);
2424 else
2425 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
2426 pwrb, dsp_value);
2427
2428 /* Map addr only if there is data_count */
2429 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302430 io_task->mtask_addr = pci_map_single(phba->pcidev,
2431 task->data,
2432 task->data_count,
2433 PCI_DMA_TODEVICE);
John Soni Josed629c472012-10-20 04:42:00 +05302434 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302435 } else
John Soni Josed629c472012-10-20 04:42:00 +05302436 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302437
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302438 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302439 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302440 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302441 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302442 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2443 task->data_count);
2444
2445 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2446 } else {
2447 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302448 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302449 }
2450
2451 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2452
2453 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2454
2455 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2456 io_task->bhs_pa.u.a32.address_hi);
2457 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2458 io_task->bhs_pa.u.a32.address_lo);
2459 if (task->data) {
2460 psgl++;
2461 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2462 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2463 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2464 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2466 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2467
2468 psgl++;
2469 if (task->data) {
2470 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302471 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302472 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302473 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302474 }
2475 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2476 }
2477 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2478}
2479
2480static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2481{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302482 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302483 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2484 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2485
2486 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2487 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302488 num_async_pdu_buf_pages =
2489 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2490 phba->params.defpdu_hdr_sz);
2491 num_async_pdu_buf_sgl_pages =
2492 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2493 sizeof(struct phys_addr));
2494 num_async_pdu_data_pages =
2495 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2496 phba->params.defpdu_data_sz);
2497 num_async_pdu_data_sgl_pages =
2498 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2499 sizeof(struct phys_addr));
2500
2501 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2502
2503 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2504 BE_ISCSI_PDU_HEADER_SIZE;
2505 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2506 sizeof(struct hwi_context_memory);
2507
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302508
2509 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2510 * (phba->params.wrbs_per_cxn)
2511 * phba->params.cxns_per_ctrl;
2512 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2513 (phba->params.wrbs_per_cxn);
2514 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2515 phba->params.cxns_per_ctrl);
2516
2517 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2518 phba->params.icds_per_ctrl;
2519 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2520 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2521
2522 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2523 num_async_pdu_buf_pages * PAGE_SIZE;
2524 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2525 num_async_pdu_data_pages * PAGE_SIZE;
2526 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2527 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2528 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2529 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2530 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2531 phba->params.asyncpdus_per_ctrl *
2532 sizeof(struct async_pdu_handle);
2533 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2534 phba->params.asyncpdus_per_ctrl *
2535 sizeof(struct async_pdu_handle);
2536 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2537 sizeof(struct hwi_async_pdu_context) +
2538 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2539}
2540
2541static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2542{
2543 struct be_mem_descriptor *mem_descr;
2544 dma_addr_t bus_add;
2545 struct mem_array *mem_arr, *mem_arr_orig;
2546 unsigned int i, j, alloc_size, curr_alloc_size;
2547
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002548 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302549 if (!phba->phwi_ctrlr)
2550 return -ENOMEM;
2551
2552 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2553 GFP_KERNEL);
2554 if (!phba->init_mem) {
2555 kfree(phba->phwi_ctrlr);
2556 return -ENOMEM;
2557 }
2558
2559 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2560 GFP_KERNEL);
2561 if (!mem_arr_orig) {
2562 kfree(phba->init_mem);
2563 kfree(phba->phwi_ctrlr);
2564 return -ENOMEM;
2565 }
2566
2567 mem_descr = phba->init_mem;
2568 for (i = 0; i < SE_MEM_MAX; i++) {
2569 j = 0;
2570 mem_arr = mem_arr_orig;
2571 alloc_size = phba->mem_req[i];
2572 memset(mem_arr, 0, sizeof(struct mem_array) *
2573 BEISCSI_MAX_FRAGS_INIT);
2574 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2575 do {
2576 mem_arr->virtual_address = pci_alloc_consistent(
2577 phba->pcidev,
2578 curr_alloc_size,
2579 &bus_add);
2580 if (!mem_arr->virtual_address) {
2581 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2582 goto free_mem;
2583 if (curr_alloc_size -
2584 rounddown_pow_of_two(curr_alloc_size))
2585 curr_alloc_size = rounddown_pow_of_two
2586 (curr_alloc_size);
2587 else
2588 curr_alloc_size = curr_alloc_size / 2;
2589 } else {
2590 mem_arr->bus_address.u.
2591 a64.address = (__u64) bus_add;
2592 mem_arr->size = curr_alloc_size;
2593 alloc_size -= curr_alloc_size;
2594 curr_alloc_size = min(be_max_phys_size *
2595 1024, alloc_size);
2596 j++;
2597 mem_arr++;
2598 }
2599 } while (alloc_size);
2600 mem_descr->num_elements = j;
2601 mem_descr->size_in_bytes = phba->mem_req[i];
2602 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2603 GFP_KERNEL);
2604 if (!mem_descr->mem_array)
2605 goto free_mem;
2606
2607 memcpy(mem_descr->mem_array, mem_arr_orig,
2608 sizeof(struct mem_array) * j);
2609 mem_descr++;
2610 }
2611 kfree(mem_arr_orig);
2612 return 0;
2613free_mem:
2614 mem_descr->num_elements = j;
2615 while ((i) || (j)) {
2616 for (j = mem_descr->num_elements; j > 0; j--) {
2617 pci_free_consistent(phba->pcidev,
2618 mem_descr->mem_array[j - 1].size,
2619 mem_descr->mem_array[j - 1].
2620 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302621 (unsigned long)mem_descr->
2622 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302623 bus_address.u.a64.address);
2624 }
2625 if (i) {
2626 i--;
2627 kfree(mem_descr->mem_array);
2628 mem_descr--;
2629 }
2630 }
2631 kfree(mem_arr_orig);
2632 kfree(phba->init_mem);
2633 kfree(phba->phwi_ctrlr);
2634 return -ENOMEM;
2635}
2636
2637static int beiscsi_get_memory(struct beiscsi_hba *phba)
2638{
2639 beiscsi_find_mem_req(phba);
2640 return beiscsi_alloc_mem(phba);
2641}
2642
2643static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2644{
2645 struct pdu_data_out *pdata_out;
2646 struct pdu_nop_out *pnop_out;
2647 struct be_mem_descriptor *mem_descr;
2648
2649 mem_descr = phba->init_mem;
2650 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2651 pdata_out =
2652 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2653 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2654
2655 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2656 IIOC_SCSI_DATA);
2657
2658 pnop_out =
2659 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2660 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2661
2662 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2663 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2664 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2665 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2666}
2667
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002668static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302669{
2670 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002671 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302672 struct hwi_controller *phwi_ctrlr;
2673 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002674 struct iscsi_wrb *pwrb = NULL;
2675 unsigned int num_cxn_wrbh = 0;
2676 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302677
2678 mem_descr_wrbh = phba->init_mem;
2679 mem_descr_wrbh += HWI_MEM_WRBH;
2680
2681 mem_descr_wrb = phba->init_mem;
2682 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302683 phwi_ctrlr = phba->phwi_ctrlr;
2684
2685 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2686 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302687 pwrb_context->pwrb_handle_base =
2688 kzalloc(sizeof(struct wrb_handle *) *
2689 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002690 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302691 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2692 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002693 goto init_wrb_hndl_failed;
2694 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302695 pwrb_context->pwrb_handle_basestd =
2696 kzalloc(sizeof(struct wrb_handle *) *
2697 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002698 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302699 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2700 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002701 goto init_wrb_hndl_failed;
2702 }
2703 if (!num_cxn_wrbh) {
2704 pwrb_handle =
2705 mem_descr_wrbh->mem_array[idx].virtual_address;
2706 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2707 ((sizeof(struct wrb_handle)) *
2708 phba->params.wrbs_per_cxn));
2709 idx++;
2710 }
2711 pwrb_context->alloc_index = 0;
2712 pwrb_context->wrb_handles_available = 0;
2713 pwrb_context->free_index = 0;
2714
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302715 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2717 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2718 pwrb_context->pwrb_handle_basestd[j] =
2719 pwrb_handle;
2720 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302721 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302722 pwrb_handle++;
2723 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302724 num_cxn_wrbh--;
2725 }
2726 }
2727 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302728 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302729 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002730 if (!num_cxn_wrb) {
2731 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2732 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2733 ((sizeof(struct iscsi_wrb) *
2734 phba->params.wrbs_per_cxn));
2735 idx++;
2736 }
2737
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302738 if (num_cxn_wrb) {
2739 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2740 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2741 pwrb_handle->pwrb = pwrb;
2742 pwrb++;
2743 }
2744 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302745 }
2746 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002747 return 0;
2748init_wrb_hndl_failed:
2749 for (j = index; j > 0; j--) {
2750 pwrb_context = &phwi_ctrlr->wrb_context[j];
2751 kfree(pwrb_context->pwrb_handle_base);
2752 kfree(pwrb_context->pwrb_handle_basestd);
2753 }
2754 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302755}
2756
2757static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2758{
2759 struct hwi_controller *phwi_ctrlr;
2760 struct hba_parameters *p = &phba->params;
2761 struct hwi_async_pdu_context *pasync_ctx;
2762 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002763 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302764 struct be_mem_descriptor *mem_descr;
2765
2766 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2767 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2768
2769 phwi_ctrlr = phba->phwi_ctrlr;
2770 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2771 mem_descr->mem_array[0].virtual_address;
2772 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2773 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2774
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002775 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2776 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302777
2778 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2779 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2780 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302781 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2782 "BM_%d : hwi_init_async_pdu_ctx"
2783 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2784 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302785 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302786 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2787 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302788
2789 pasync_ctx->async_header.va_base =
2790 mem_descr->mem_array[0].virtual_address;
2791
2792 pasync_ctx->async_header.pa_base.u.a64.address =
2793 mem_descr->mem_array[0].bus_address.u.a64.address;
2794
2795 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2796 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2797 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302798 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2799 "BM_%d : hwi_init_async_pdu_ctx"
2800 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2801 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302802 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302803 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2804 "BM_%d : No Virtual address\n");
2805
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302806 pasync_ctx->async_header.ring_base =
2807 mem_descr->mem_array[0].virtual_address;
2808
2809 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2810 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2811 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302812 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2813 "BM_%d : hwi_init_async_pdu_ctx"
2814 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2815 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302816 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302817 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2818 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302819
2820 pasync_ctx->async_header.handle_base =
2821 mem_descr->mem_array[0].virtual_address;
2822 pasync_ctx->async_header.writables = 0;
2823 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2824
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302825
2826 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2827 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2828 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302829 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2830 "BM_%d : hwi_init_async_pdu_ctx"
2831 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2832 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302833 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302834 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2835 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302836
2837 pasync_ctx->async_data.ring_base =
2838 mem_descr->mem_array[0].virtual_address;
2839
2840 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2841 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2842 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302843 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2844 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302845
2846 pasync_ctx->async_data.handle_base =
2847 mem_descr->mem_array[0].virtual_address;
2848 pasync_ctx->async_data.writables = 0;
2849 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2850
2851 pasync_header_h =
2852 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2853 pasync_data_h =
2854 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2855
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002856 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2857 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2858 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302859 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2860 "BM_%d : hwi_init_async_pdu_ctx"
2861 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2862 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002863 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302864 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2865 "BM_%d : No Virtual address\n");
2866
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002867 idx = 0;
2868 pasync_ctx->async_data.va_base =
2869 mem_descr->mem_array[idx].virtual_address;
2870 pasync_ctx->async_data.pa_base.u.a64.address =
2871 mem_descr->mem_array[idx].bus_address.u.a64.address;
2872
2873 num_async_data = ((mem_descr->mem_array[idx].size) /
2874 phba->params.defpdu_data_sz);
2875 num_per_mem = 0;
2876
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302877 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2878 pasync_header_h->cri = -1;
2879 pasync_header_h->index = (char)index;
2880 INIT_LIST_HEAD(&pasync_header_h->link);
2881 pasync_header_h->pbuffer =
2882 (void *)((unsigned long)
2883 (pasync_ctx->async_header.va_base) +
2884 (p->defpdu_hdr_sz * index));
2885
2886 pasync_header_h->pa.u.a64.address =
2887 pasync_ctx->async_header.pa_base.u.a64.address +
2888 (p->defpdu_hdr_sz * index);
2889
2890 list_add_tail(&pasync_header_h->link,
2891 &pasync_ctx->async_header.free_list);
2892 pasync_header_h++;
2893 pasync_ctx->async_header.free_entries++;
2894 pasync_ctx->async_header.writables++;
2895
2896 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2897 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2898 header_busy_list);
2899 pasync_data_h->cri = -1;
2900 pasync_data_h->index = (char)index;
2901 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002902
2903 if (!num_async_data) {
2904 num_per_mem = 0;
2905 idx++;
2906 pasync_ctx->async_data.va_base =
2907 mem_descr->mem_array[idx].virtual_address;
2908 pasync_ctx->async_data.pa_base.u.a64.address =
2909 mem_descr->mem_array[idx].
2910 bus_address.u.a64.address;
2911
2912 num_async_data = ((mem_descr->mem_array[idx].size) /
2913 phba->params.defpdu_data_sz);
2914 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302915 pasync_data_h->pbuffer =
2916 (void *)((unsigned long)
2917 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002918 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302919
2920 pasync_data_h->pa.u.a64.address =
2921 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002922 (p->defpdu_data_sz * num_per_mem);
2923 num_per_mem++;
2924 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302925
2926 list_add_tail(&pasync_data_h->link,
2927 &pasync_ctx->async_data.free_list);
2928 pasync_data_h++;
2929 pasync_ctx->async_data.free_entries++;
2930 pasync_ctx->async_data.writables++;
2931
2932 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2933 }
2934
2935 pasync_ctx->async_header.host_write_ptr = 0;
2936 pasync_ctx->async_header.ep_read_ptr = -1;
2937 pasync_ctx->async_data.host_write_ptr = 0;
2938 pasync_ctx->async_data.ep_read_ptr = -1;
2939}
2940
2941static int
2942be_sgl_create_contiguous(void *virtual_address,
2943 u64 physical_address, u32 length,
2944 struct be_dma_mem *sgl)
2945{
2946 WARN_ON(!virtual_address);
2947 WARN_ON(!physical_address);
2948 WARN_ON(!length > 0);
2949 WARN_ON(!sgl);
2950
2951 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302952 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302953 sgl->size = length;
2954
2955 return 0;
2956}
2957
2958static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2959{
2960 memset(sgl, 0, sizeof(*sgl));
2961}
2962
2963static void
2964hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2965 struct mem_array *pmem, struct be_dma_mem *sgl)
2966{
2967 if (sgl->va)
2968 be_sgl_destroy_contiguous(sgl);
2969
2970 be_sgl_create_contiguous(pmem->virtual_address,
2971 pmem->bus_address.u.a64.address,
2972 pmem->size, sgl);
2973}
2974
2975static void
2976hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2977 struct mem_array *pmem, struct be_dma_mem *sgl)
2978{
2979 if (sgl->va)
2980 be_sgl_destroy_contiguous(sgl);
2981
2982 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2983 pmem->bus_address.u.a64.address,
2984 pmem->size, sgl);
2985}
2986
2987static int be_fill_queue(struct be_queue_info *q,
2988 u16 len, u16 entry_size, void *vaddress)
2989{
2990 struct be_dma_mem *mem = &q->dma_mem;
2991
2992 memset(q, 0, sizeof(*q));
2993 q->len = len;
2994 q->entry_size = entry_size;
2995 mem->size = len * entry_size;
2996 mem->va = vaddress;
2997 if (!mem->va)
2998 return -ENOMEM;
2999 memset(mem->va, 0, mem->size);
3000 return 0;
3001}
3002
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303003static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303004 struct hwi_context_memory *phwi_context)
3005{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303006 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303007 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303008 struct be_queue_info *eq;
3009 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303010 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303011 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303012
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303013 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3014 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303015
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303016 if (phba->msix_enabled)
3017 eq_for_mcc = 1;
3018 else
3019 eq_for_mcc = 0;
3020 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3021 eq = &phwi_context->be_eq[i].q;
3022 mem = &eq->dma_mem;
3023 phwi_context->be_eq[i].phba = phba;
3024 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3025 num_eq_pages * PAGE_SIZE,
3026 &paddr);
3027 if (!eq_vaddress)
3028 goto create_eq_error;
3029
3030 mem->va = eq_vaddress;
3031 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3032 sizeof(struct be_eq_entry), eq_vaddress);
3033 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303034 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3035 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303036 goto create_eq_error;
3037 }
3038
3039 mem->dma = paddr;
3040 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3041 phwi_context->cur_eqd);
3042 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303043 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3044 "BM_%d : beiscsi_cmd_eq_create"
3045 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303046 goto create_eq_error;
3047 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303048
3049 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3050 "BM_%d : eqid = %d\n",
3051 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303052 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303053 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303054create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303055 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303056 eq = &phwi_context->be_eq[i].q;
3057 mem = &eq->dma_mem;
3058 if (mem->va)
3059 pci_free_consistent(phba->pcidev, num_eq_pages
3060 * PAGE_SIZE,
3061 mem->va, mem->dma);
3062 }
3063 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303064}
3065
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303066static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303067 struct hwi_context_memory *phwi_context)
3068{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303069 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303070 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303071 struct be_queue_info *cq, *eq;
3072 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303073 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303074 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303075 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303076
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303077 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3078 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303079
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303080 for (i = 0; i < phba->num_cpus; i++) {
3081 cq = &phwi_context->be_cq[i];
3082 eq = &phwi_context->be_eq[i].q;
3083 pbe_eq = &phwi_context->be_eq[i];
3084 pbe_eq->cq = cq;
3085 pbe_eq->phba = phba;
3086 mem = &cq->dma_mem;
3087 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3088 num_cq_pages * PAGE_SIZE,
3089 &paddr);
3090 if (!cq_vaddress)
3091 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303092 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303093 sizeof(struct sol_cqe), cq_vaddress);
3094 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303095 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3096 "BM_%d : be_fill_queue Failed "
3097 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303098 goto create_cq_error;
3099 }
3100
3101 mem->dma = paddr;
3102 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3103 false, 0);
3104 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303105 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3106 "BM_%d : beiscsi_cmd_eq_create"
3107 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303108 goto create_cq_error;
3109 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303110 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3111 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3112 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303113 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303114 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303115
3116create_cq_error:
3117 for (i = 0; i < phba->num_cpus; i++) {
3118 cq = &phwi_context->be_cq[i];
3119 mem = &cq->dma_mem;
3120 if (mem->va)
3121 pci_free_consistent(phba->pcidev, num_cq_pages
3122 * PAGE_SIZE,
3123 mem->va, mem->dma);
3124 }
3125 return ret;
3126
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303127}
3128
3129static int
3130beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3131 struct hwi_context_memory *phwi_context,
3132 struct hwi_controller *phwi_ctrlr,
3133 unsigned int def_pdu_ring_sz)
3134{
3135 unsigned int idx;
3136 int ret;
3137 struct be_queue_info *dq, *cq;
3138 struct be_dma_mem *mem;
3139 struct be_mem_descriptor *mem_descr;
3140 void *dq_vaddress;
3141
3142 idx = 0;
3143 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303144 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303145 mem = &dq->dma_mem;
3146 mem_descr = phba->init_mem;
3147 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
3148 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3149 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3150 sizeof(struct phys_addr),
3151 sizeof(struct phys_addr), dq_vaddress);
3152 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303153 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3154 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303155 return ret;
3156 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303157 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3158 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303159 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3160 def_pdu_ring_sz,
3161 phba->params.defpdu_hdr_sz);
3162 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303163 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3164 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303165 return ret;
3166 }
3167 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303168 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3169 "BM_%d : iscsi def pdu id is %d\n",
3170 phwi_context->be_def_hdrq.id);
3171
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303172 hwi_post_async_buffers(phba, 1);
3173 return 0;
3174}
3175
3176static int
3177beiscsi_create_def_data(struct beiscsi_hba *phba,
3178 struct hwi_context_memory *phwi_context,
3179 struct hwi_controller *phwi_ctrlr,
3180 unsigned int def_pdu_ring_sz)
3181{
3182 unsigned int idx;
3183 int ret;
3184 struct be_queue_info *dataq, *cq;
3185 struct be_dma_mem *mem;
3186 struct be_mem_descriptor *mem_descr;
3187 void *dq_vaddress;
3188
3189 idx = 0;
3190 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303191 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303192 mem = &dataq->dma_mem;
3193 mem_descr = phba->init_mem;
3194 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3195 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3196 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3197 sizeof(struct phys_addr),
3198 sizeof(struct phys_addr), dq_vaddress);
3199 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303200 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3201 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303202 return ret;
3203 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303204 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3205 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303206 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3207 def_pdu_ring_sz,
3208 phba->params.defpdu_data_sz);
3209 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303210 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3211 "BM_%d be_cmd_create_default_pdu_queue"
3212 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303213 return ret;
3214 }
3215 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303216 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3217 "BM_%d : iscsi def data id is %d\n",
3218 phwi_context->be_def_dataq.id);
3219
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303220 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303221 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3222 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3223
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303224 return 0;
3225}
3226
3227static int
3228beiscsi_post_pages(struct beiscsi_hba *phba)
3229{
3230 struct be_mem_descriptor *mem_descr;
3231 struct mem_array *pm_arr;
3232 unsigned int page_offset, i;
3233 struct be_dma_mem sgl;
3234 int status;
3235
3236 mem_descr = phba->init_mem;
3237 mem_descr += HWI_MEM_SGE;
3238 pm_arr = mem_descr->mem_array;
3239
3240 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3241 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3242 for (i = 0; i < mem_descr->num_elements; i++) {
3243 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3244 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3245 page_offset,
3246 (pm_arr->size / PAGE_SIZE));
3247 page_offset += pm_arr->size / PAGE_SIZE;
3248 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303249 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3250 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303251 return status;
3252 }
3253 pm_arr++;
3254 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303255 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3256 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303257 return 0;
3258}
3259
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303260static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3261{
3262 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003263 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303264 pci_free_consistent(phba->pcidev, mem->size,
3265 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003266 mem->va = NULL;
3267 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303268}
3269
3270static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3271 u16 len, u16 entry_size)
3272{
3273 struct be_dma_mem *mem = &q->dma_mem;
3274
3275 memset(q, 0, sizeof(*q));
3276 q->len = len;
3277 q->entry_size = entry_size;
3278 mem->size = len * entry_size;
3279 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3280 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303281 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303282 memset(mem->va, 0, mem->size);
3283 return 0;
3284}
3285
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303286static int
3287beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3288 struct hwi_context_memory *phwi_context,
3289 struct hwi_controller *phwi_ctrlr)
3290{
3291 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3292 u64 pa_addr_lo;
3293 unsigned int idx, num, i;
3294 struct mem_array *pwrb_arr;
3295 void *wrb_vaddr;
3296 struct be_dma_mem sgl;
3297 struct be_mem_descriptor *mem_descr;
3298 int status;
3299
3300 idx = 0;
3301 mem_descr = phba->init_mem;
3302 mem_descr += HWI_MEM_WRB;
3303 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3304 GFP_KERNEL);
3305 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303306 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3307 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303308 return -ENOMEM;
3309 }
3310 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3311 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3312 num_wrb_rings = mem_descr->mem_array[idx].size /
3313 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3314
3315 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3316 if (num_wrb_rings) {
3317 pwrb_arr[num].virtual_address = wrb_vaddr;
3318 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3319 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3320 sizeof(struct iscsi_wrb);
3321 wrb_vaddr += pwrb_arr[num].size;
3322 pa_addr_lo += pwrb_arr[num].size;
3323 num_wrb_rings--;
3324 } else {
3325 idx++;
3326 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3327 pa_addr_lo = mem_descr->mem_array[idx].\
3328 bus_address.u.a64.address;
3329 num_wrb_rings = mem_descr->mem_array[idx].size /
3330 (phba->params.wrbs_per_cxn *
3331 sizeof(struct iscsi_wrb));
3332 pwrb_arr[num].virtual_address = wrb_vaddr;
3333 pwrb_arr[num].bus_address.u.a64.address\
3334 = pa_addr_lo;
3335 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3336 sizeof(struct iscsi_wrb);
3337 wrb_vaddr += pwrb_arr[num].size;
3338 pa_addr_lo += pwrb_arr[num].size;
3339 num_wrb_rings--;
3340 }
3341 }
3342 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3343 wrb_mem_index = 0;
3344 offset = 0;
3345 size = 0;
3346
3347 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3348 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3349 &phwi_context->be_wrbq[i]);
3350 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303351 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3352 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003353 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303354 return status;
3355 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303356 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3357 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303358 }
3359 kfree(pwrb_arr);
3360 return 0;
3361}
3362
3363static void free_wrb_handles(struct beiscsi_hba *phba)
3364{
3365 unsigned int index;
3366 struct hwi_controller *phwi_ctrlr;
3367 struct hwi_wrb_context *pwrb_context;
3368
3369 phwi_ctrlr = phba->phwi_ctrlr;
3370 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3371 pwrb_context = &phwi_ctrlr->wrb_context[index];
3372 kfree(pwrb_context->pwrb_handle_base);
3373 kfree(pwrb_context->pwrb_handle_basestd);
3374 }
3375}
3376
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303377static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3378{
3379 struct be_queue_info *q;
3380 struct be_ctrl_info *ctrl = &phba->ctrl;
3381
3382 q = &phba->ctrl.mcc_obj.q;
3383 if (q->created)
3384 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3385 be_queue_free(phba, q);
3386
3387 q = &phba->ctrl.mcc_obj.cq;
3388 if (q->created)
3389 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3390 be_queue_free(phba, q);
3391}
3392
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303393static void hwi_cleanup(struct beiscsi_hba *phba)
3394{
3395 struct be_queue_info *q;
3396 struct be_ctrl_info *ctrl = &phba->ctrl;
3397 struct hwi_controller *phwi_ctrlr;
3398 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303399 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303400
3401 phwi_ctrlr = phba->phwi_ctrlr;
3402 phwi_context = phwi_ctrlr->phwi_ctxt;
3403 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3404 q = &phwi_context->be_wrbq[i];
3405 if (q->created)
3406 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3407 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303408 free_wrb_handles(phba);
3409
3410 q = &phwi_context->be_def_hdrq;
3411 if (q->created)
3412 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3413
3414 q = &phwi_context->be_def_dataq;
3415 if (q->created)
3416 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3417
3418 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3419
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303420 for (i = 0; i < (phba->num_cpus); i++) {
3421 q = &phwi_context->be_cq[i];
3422 if (q->created)
3423 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3424 }
3425 if (phba->msix_enabled)
3426 eq_num = 1;
3427 else
3428 eq_num = 0;
3429 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3430 q = &phwi_context->be_eq[i].q;
3431 if (q->created)
3432 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3433 }
3434 be_mcc_queues_destroy(phba);
3435}
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_mcc_wrb *wrb;
3873 struct be_dma_mem nonemb_cmd;
3874 unsigned int tag, wrb_num;
3875 unsigned short status, extd_status;
John Soni Jose9aef4202012-08-20 23:00:08 +05303876 unsigned int s_handle;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303877 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003878 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303879
John Soni Jose9aef4202012-08-20 23:00:08 +05303880 /* Get the session handle of the boot target */
3881 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3882 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303883 beiscsi_log(phba, KERN_ERR,
3884 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3885 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303886 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303887 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303888 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3889 sizeof(*session_resp),
3890 &nonemb_cmd.dma);
3891 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303892 beiscsi_log(phba, KERN_ERR,
3893 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3894 "BM_%d : Failed to allocate memory for"
3895 "beiscsi_get_session_info\n");
3896
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303897 return -ENOMEM;
3898 }
3899
3900 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303901 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003902 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303903 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303904 beiscsi_log(phba, KERN_ERR,
3905 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3906 "BM_%d : beiscsi_get_session_info"
3907 " Failed\n");
3908
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303909 goto boot_freemem;
3910 } else
3911 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3912 phba->ctrl.mcc_numtag[tag]);
3913
3914 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3915 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3916 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3917 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303918 beiscsi_log(phba, KERN_ERR,
3919 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3920 "BM_%d : beiscsi_get_session_info Failed"
3921 " status = %d extd_status = %d\n",
3922 status, extd_status);
3923
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303924 free_mcc_tag(&phba->ctrl, tag);
3925 goto boot_freemem;
3926 }
3927 wrb = queue_get_wrb(mccq, wrb_num);
3928 free_mcc_tag(&phba->ctrl, tag);
3929 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003930
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303931 memcpy(&phba->boot_sess, &session_resp->session_info,
3932 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003933 ret = 0;
3934
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303935boot_freemem:
3936 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3937 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003938 return ret;
3939}
3940
3941static void beiscsi_boot_release(void *data)
3942{
3943 struct beiscsi_hba *phba = data;
3944
3945 scsi_host_put(phba->shost);
3946}
3947
3948static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3949{
3950 struct iscsi_boot_kobj *boot_kobj;
3951
3952 /* get boot info using mgmt cmd */
3953 if (beiscsi_get_boot_info(phba))
3954 /* Try to see if we can carry on without this */
3955 return 0;
3956
3957 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3958 if (!phba->boot_kset)
3959 return -ENOMEM;
3960
3961 /* get a ref because the show function will ref the phba */
3962 if (!scsi_host_get(phba->shost))
3963 goto free_kset;
3964 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3965 beiscsi_show_boot_tgt_info,
3966 beiscsi_tgt_get_attr_visibility,
3967 beiscsi_boot_release);
3968 if (!boot_kobj)
3969 goto put_shost;
3970
3971 if (!scsi_host_get(phba->shost))
3972 goto free_kset;
3973 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3974 beiscsi_show_boot_ini_info,
3975 beiscsi_ini_get_attr_visibility,
3976 beiscsi_boot_release);
3977 if (!boot_kobj)
3978 goto put_shost;
3979
3980 if (!scsi_host_get(phba->shost))
3981 goto free_kset;
3982 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3983 beiscsi_show_boot_eth_info,
3984 beiscsi_eth_get_attr_visibility,
3985 beiscsi_boot_release);
3986 if (!boot_kobj)
3987 goto put_shost;
3988 return 0;
3989
3990put_shost:
3991 scsi_host_put(phba->shost);
3992free_kset:
3993 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303994 return -ENOMEM;
3995}
3996
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303997static int beiscsi_init_port(struct beiscsi_hba *phba)
3998{
3999 int ret;
4000
4001 ret = beiscsi_init_controller(phba);
4002 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304003 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4004 "BM_%d : beiscsi_dev_probe - Failed in"
4005 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304006 return ret;
4007 }
4008 ret = beiscsi_init_sgl_handle(phba);
4009 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304010 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4011 "BM_%d : beiscsi_dev_probe - Failed in"
4012 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304013 goto do_cleanup_ctrlr;
4014 }
4015
4016 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304017 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4018 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304019 kfree(phba->io_sgl_hndl_base);
4020 kfree(phba->eh_sgl_hndl_base);
4021 goto do_cleanup_ctrlr;
4022 }
4023
4024 return ret;
4025
4026do_cleanup_ctrlr:
4027 hwi_cleanup(phba);
4028 return ret;
4029}
4030
4031static void hwi_purge_eq(struct beiscsi_hba *phba)
4032{
4033 struct hwi_controller *phwi_ctrlr;
4034 struct hwi_context_memory *phwi_context;
4035 struct be_queue_info *eq;
4036 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304037 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304038 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304039
4040 phwi_ctrlr = phba->phwi_ctrlr;
4041 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304042 if (phba->msix_enabled)
4043 eq_msix = 1;
4044 else
4045 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304046
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304047 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4048 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304049 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304050 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304051 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4052 & EQE_VALID_MASK) {
4053 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4054 queue_tail_inc(eq);
4055 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304056 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304057 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304058
4059 if (num_processed)
4060 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304061 }
4062}
4063
4064static void beiscsi_clean_port(struct beiscsi_hba *phba)
4065{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05304066 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304067
4068 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
4069 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304070 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4071 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304072
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304073 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304074 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304075 kfree(phba->io_sgl_hndl_base);
4076 kfree(phba->eh_sgl_hndl_base);
4077 kfree(phba->cid_array);
4078 kfree(phba->ep_array);
4079}
4080
John Soni Josed629c472012-10-20 04:42:00 +05304081/**
4082 * beiscsi_cleanup_task()- Free driver resources of the task
4083 * @task: ptr to the iscsi task
4084 *
4085 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004086static void beiscsi_cleanup_task(struct iscsi_task *task)
4087{
4088 struct beiscsi_io_task *io_task = task->dd_data;
4089 struct iscsi_conn *conn = task->conn;
4090 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4091 struct beiscsi_hba *phba = beiscsi_conn->phba;
4092 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4093 struct hwi_wrb_context *pwrb_context;
4094 struct hwi_controller *phwi_ctrlr;
4095
4096 phwi_ctrlr = phba->phwi_ctrlr;
4097 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
4098 - phba->fw_config.iscsi_cid_start];
4099
4100 if (io_task->cmd_bhs) {
4101 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4102 io_task->bhs_pa.u.a64.address);
4103 io_task->cmd_bhs = NULL;
4104 }
4105
4106 if (task->sc) {
4107 if (io_task->pwrb_handle) {
4108 free_wrb_handle(phba, pwrb_context,
4109 io_task->pwrb_handle);
4110 io_task->pwrb_handle = NULL;
4111 }
4112
4113 if (io_task->psgl_handle) {
4114 spin_lock(&phba->io_sgl_lock);
4115 free_io_sgl_handle(phba, io_task->psgl_handle);
4116 spin_unlock(&phba->io_sgl_lock);
4117 io_task->psgl_handle = NULL;
4118 }
4119 } else {
4120 if (!beiscsi_conn->login_in_progress) {
4121 if (io_task->pwrb_handle) {
4122 free_wrb_handle(phba, pwrb_context,
4123 io_task->pwrb_handle);
4124 io_task->pwrb_handle = NULL;
4125 }
4126 if (io_task->psgl_handle) {
4127 spin_lock(&phba->mgmt_sgl_lock);
4128 free_mgmt_sgl_handle(phba,
4129 io_task->psgl_handle);
4130 spin_unlock(&phba->mgmt_sgl_lock);
4131 io_task->psgl_handle = NULL;
4132 }
John Soni Josed629c472012-10-20 04:42:00 +05304133 if (io_task->mtask_addr) {
4134 pci_unmap_single(phba->pcidev,
4135 io_task->mtask_addr,
4136 io_task->mtask_data_count,
4137 PCI_DMA_TODEVICE);
4138 io_task->mtask_addr = 0;
4139 }
Mike Christie1282ab72012-04-18 03:06:00 -05004140 }
4141 }
4142}
4143
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304144void
4145beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4146 struct beiscsi_offload_params *params)
4147{
4148 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304149 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004150 struct iscsi_task *task = beiscsi_conn->task;
4151 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304152 u32 doorbell = 0;
4153
4154 /*
4155 * We can always use 0 here because it is reserved by libiscsi for
4156 * login/startup related tasks.
4157 */
Mike Christie1282ab72012-04-18 03:06:00 -05004158 beiscsi_conn->login_in_progress = 0;
4159 spin_lock_bh(&session->lock);
4160 beiscsi_cleanup_task(task);
4161 spin_unlock_bh(&session->lock);
4162
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304163 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05304164 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304165
John Soni Joseacb96932012-10-20 04:44:35 +05304166 /* Check for the adapter family */
4167 if (chip_skh_r(phba->pcidev))
4168 beiscsi_offload_cxn_v2(params, pwrb_handle);
4169 else
4170 beiscsi_offload_cxn_v0(params, pwrb_handle,
4171 phba->init_mem);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304172
John Soni Joseacb96932012-10-20 04:44:35 +05304173 be_dws_le_to_cpu(pwrb_handle->pwrb,
4174 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304175
4176 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304177 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304178 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304179 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4180
4181 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4182}
4183
4184static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4185 int *index, int *age)
4186{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304187 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304188 if (age)
4189 *age = conn->session->age;
4190}
4191
4192/**
4193 * beiscsi_alloc_pdu - allocates pdu and related resources
4194 * @task: libiscsi task
4195 * @opcode: opcode of pdu for task
4196 *
4197 * This is called with the session lock held. It will allocate
4198 * the wrb and sgl if needed for the command. And it will prep
4199 * the pdu's itt. beiscsi_parse_pdu will later translate
4200 * the pdu itt to the libiscsi task itt.
4201 */
4202static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4203{
4204 struct beiscsi_io_task *io_task = task->dd_data;
4205 struct iscsi_conn *conn = task->conn;
4206 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4207 struct beiscsi_hba *phba = beiscsi_conn->phba;
4208 struct hwi_wrb_context *pwrb_context;
4209 struct hwi_controller *phwi_ctrlr;
4210 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304211 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4212 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304213
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304214 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004215 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304216 if (!io_task->cmd_bhs)
4217 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304218 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304219 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304220 io_task->conn = beiscsi_conn;
4221
4222 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4223 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304224 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004225 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304226
4227 if (task->sc) {
4228 spin_lock(&phba->io_sgl_lock);
4229 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4230 spin_unlock(&phba->io_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304231 if (!io_task->psgl_handle) {
4232 beiscsi_log(phba, KERN_ERR,
4233 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4234 "BM_%d : Alloc of IO_SGL_ICD Failed"
4235 "for the CID : %d\n",
4236 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304237 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304238 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304239 io_task->pwrb_handle = alloc_wrb_handle(phba,
4240 beiscsi_conn->beiscsi_conn_cid -
4241 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304242 if (!io_task->pwrb_handle) {
4243 beiscsi_log(phba, KERN_ERR,
4244 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4245 "BM_%d : Alloc of WRB_HANDLE Failed"
4246 "for the CID : %d\n",
4247 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304248 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304249 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304250 } else {
4251 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304252 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304253 if (!beiscsi_conn->login_in_progress) {
4254 spin_lock(&phba->mgmt_sgl_lock);
4255 io_task->psgl_handle = (struct sgl_handle *)
4256 alloc_mgmt_sgl_handle(phba);
4257 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304258 if (!io_task->psgl_handle) {
4259 beiscsi_log(phba, KERN_ERR,
4260 BEISCSI_LOG_IO |
4261 BEISCSI_LOG_CONFIG,
4262 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4263 "for the CID : %d\n",
4264 beiscsi_conn->
4265 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304266 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304267 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304268
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304269 beiscsi_conn->login_in_progress = 1;
4270 beiscsi_conn->plogin_sgl_handle =
4271 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304272 io_task->pwrb_handle =
4273 alloc_wrb_handle(phba,
4274 beiscsi_conn->beiscsi_conn_cid -
4275 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304276 if (!io_task->pwrb_handle) {
4277 beiscsi_log(phba, KERN_ERR,
4278 BEISCSI_LOG_IO |
4279 BEISCSI_LOG_CONFIG,
4280 "BM_%d : Alloc of WRB_HANDLE Failed"
4281 "for the CID : %d\n",
4282 beiscsi_conn->
4283 beiscsi_conn_cid);
4284 goto free_mgmt_hndls;
4285 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304286 beiscsi_conn->plogin_wrb_handle =
4287 io_task->pwrb_handle;
4288
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304289 } else {
4290 io_task->psgl_handle =
4291 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304292 io_task->pwrb_handle =
4293 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304294 }
Mike Christie1282ab72012-04-18 03:06:00 -05004295 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304296 } else {
4297 spin_lock(&phba->mgmt_sgl_lock);
4298 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4299 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304300 if (!io_task->psgl_handle) {
4301 beiscsi_log(phba, KERN_ERR,
4302 BEISCSI_LOG_IO |
4303 BEISCSI_LOG_CONFIG,
4304 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4305 "for the CID : %d\n",
4306 beiscsi_conn->
4307 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304308 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304309 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304310 io_task->pwrb_handle =
4311 alloc_wrb_handle(phba,
4312 beiscsi_conn->beiscsi_conn_cid -
4313 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304314 if (!io_task->pwrb_handle) {
4315 beiscsi_log(phba, KERN_ERR,
4316 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4317 "BM_%d : Alloc of WRB_HANDLE Failed"
4318 "for the CID : %d\n",
4319 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304320 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304321 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304322
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304323 }
4324 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304325 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4326 wrb_index << 16) | (unsigned int)
4327 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304328 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304329
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304330 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4331 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304332
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304333free_io_hndls:
4334 spin_lock(&phba->io_sgl_lock);
4335 free_io_sgl_handle(phba, io_task->psgl_handle);
4336 spin_unlock(&phba->io_sgl_lock);
4337 goto free_hndls;
4338free_mgmt_hndls:
4339 spin_lock(&phba->mgmt_sgl_lock);
4340 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4341 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304342free_hndls:
4343 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304344 pwrb_context = &phwi_ctrlr->wrb_context[
4345 beiscsi_conn->beiscsi_conn_cid -
4346 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304347 if (io_task->pwrb_handle)
4348 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304349 io_task->pwrb_handle = NULL;
4350 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4351 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004352 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304353 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304354}
John Soni Jose09a10932012-10-20 04:44:23 +05304355int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4356 unsigned int num_sg, unsigned int xferlen,
4357 unsigned int writedir)
4358{
4359
4360 struct beiscsi_io_task *io_task = task->dd_data;
4361 struct iscsi_conn *conn = task->conn;
4362 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4363 struct beiscsi_hba *phba = beiscsi_conn->phba;
4364 struct iscsi_wrb *pwrb = NULL;
4365 unsigned int doorbell = 0;
4366
4367 pwrb = io_task->pwrb_handle->pwrb;
4368 memset(pwrb, 0, sizeof(*pwrb));
4369
4370 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4371 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4372
4373 if (writedir) {
4374 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4375 INI_WR_CMD);
4376 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4377 } else {
4378 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4379 INI_RD_CMD);
4380 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4381 }
4382
4383 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4384 type, pwrb);
4385
4386 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4387 cpu_to_be16(*(unsigned short *)
4388 &io_task->cmd_bhs->iscsi_hdr.lun));
4389 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4390 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4391 io_task->pwrb_handle->wrb_index);
4392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4393 be32_to_cpu(task->cmdsn));
4394 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4395 io_task->psgl_handle->sgl_index);
4396
4397 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4398 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4399 io_task->pwrb_handle->nxt_wrb_index);
4400
4401 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4402
4403 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4404 doorbell |= (io_task->pwrb_handle->wrb_index &
4405 DB_DEF_PDU_WRB_INDEX_MASK) <<
4406 DB_DEF_PDU_WRB_INDEX_SHIFT;
4407 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4408 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4409 return 0;
4410}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304411
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304412static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4413 unsigned int num_sg, unsigned int xferlen,
4414 unsigned int writedir)
4415{
4416
4417 struct beiscsi_io_task *io_task = task->dd_data;
4418 struct iscsi_conn *conn = task->conn;
4419 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4420 struct beiscsi_hba *phba = beiscsi_conn->phba;
4421 struct iscsi_wrb *pwrb = NULL;
4422 unsigned int doorbell = 0;
4423
4424 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304425 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4426 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4427
4428 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304429 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4430 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304431 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304432 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304433 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4434 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304435 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4436 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304437
John Soni Jose09a10932012-10-20 04:44:23 +05304438 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4439 type, pwrb);
4440
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304441 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004442 cpu_to_be16(*(unsigned short *)
4443 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304444 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4445 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4446 io_task->pwrb_handle->wrb_index);
4447 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4448 be32_to_cpu(task->cmdsn));
4449 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4450 io_task->psgl_handle->sgl_index);
4451
4452 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4453
4454 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4455 io_task->pwrb_handle->nxt_wrb_index);
4456 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4457
4458 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304459 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304460 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4461 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4462
4463 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4464 return 0;
4465}
4466
4467static int beiscsi_mtask(struct iscsi_task *task)
4468{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304469 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304470 struct iscsi_conn *conn = task->conn;
4471 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4472 struct beiscsi_hba *phba = beiscsi_conn->phba;
4473 struct iscsi_wrb *pwrb = NULL;
4474 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304475 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304476 unsigned int pwrb_typeoffset = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304477
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304478 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304479 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304480 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304481
4482 if (chip_skh_r(phba->pcidev)) {
4483 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4484 be32_to_cpu(task->cmdsn));
4485 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4486 io_task->pwrb_handle->wrb_index);
4487 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4488 io_task->psgl_handle->sgl_index);
4489 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4490 task->data_count);
4491 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4492 io_task->pwrb_handle->nxt_wrb_index);
4493 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
4494 } else {
4495 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;
4506 }
4507
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304508
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304509 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4510 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304511 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304512 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304513 hwi_write_buffer(pwrb, task);
4514 break;
4515 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004516 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304517 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4518 if (chip_skh_r(phba->pcidev))
4519 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4520 dmsg, pwrb, 1);
4521 else
4522 AMAP_SET_BITS(struct amap_iscsi_wrb,
4523 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004524 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304525 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
4526 if (chip_skh_r(phba->pcidev))
4527 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4528 dmsg, pwrb, 0);
4529 else
4530 AMAP_SET_BITS(struct amap_iscsi_wrb,
4531 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004532 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304533 hwi_write_buffer(pwrb, task);
4534 break;
4535 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304536 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304537 hwi_write_buffer(pwrb, task);
4538 break;
4539 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304540 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304541 hwi_write_buffer(pwrb, task);
4542 break;
4543 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304544 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304545 hwi_write_buffer(pwrb, task);
4546 break;
4547
4548 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304549 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4550 "BM_%d : opcode =%d Not supported\n",
4551 task->hdr->opcode & ISCSI_OPCODE_MASK);
4552
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304553 return -EINVAL;
4554 }
4555
John Soni Jose09a10932012-10-20 04:44:23 +05304556 /* Set the task type */
4557 io_task->wrb_type = (chip_skh_r(phba->pcidev)) ?
4558 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb) :
4559 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304560
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304561 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304562 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304563 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4564 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4565 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4566 return 0;
4567}
4568
4569static int beiscsi_task_xmit(struct iscsi_task *task)
4570{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304571 struct beiscsi_io_task *io_task = task->dd_data;
4572 struct scsi_cmnd *sc = task->sc;
John Soni Jose09a10932012-10-20 04:44:23 +05304573 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304574 struct scatterlist *sg;
4575 int num_sg;
4576 unsigned int writedir = 0, xferlen = 0;
4577
John Soni Jose09a10932012-10-20 04:44:23 +05304578 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
4579
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304580 if (!sc)
4581 return beiscsi_mtask(task);
4582
4583 io_task->scsi_cmnd = sc;
4584 num_sg = scsi_dma_map(sc);
4585 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304586 struct iscsi_conn *conn = task->conn;
4587 struct beiscsi_hba *phba = NULL;
4588
4589 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4590 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4591 "BM_%d : scsi_dma_map Failed\n");
4592
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304593 return num_sg;
4594 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304595 xferlen = scsi_bufflen(sc);
4596 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304597 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304598 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304599 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304600 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304601
John Soni Jose09a10932012-10-20 04:44:23 +05304602 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304603}
4604
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004605/**
4606 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4607 * @job: job to handle
4608 */
4609static int beiscsi_bsg_request(struct bsg_job *job)
4610{
4611 struct Scsi_Host *shost;
4612 struct beiscsi_hba *phba;
4613 struct iscsi_bsg_request *bsg_req = job->request;
4614 int rc = -EINVAL;
4615 unsigned int tag;
4616 struct be_dma_mem nonemb_cmd;
4617 struct be_cmd_resp_hdr *resp;
4618 struct iscsi_bsg_reply *bsg_reply = job->reply;
4619 unsigned short status, extd_status;
4620
4621 shost = iscsi_job_to_shost(job);
4622 phba = iscsi_host_priv(shost);
4623
4624 switch (bsg_req->msgcode) {
4625 case ISCSI_BSG_HST_VENDOR:
4626 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4627 job->request_payload.payload_len,
4628 &nonemb_cmd.dma);
4629 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304630 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4631 "BM_%d : Failed to allocate memory for "
4632 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304633 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004634 }
4635 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4636 &nonemb_cmd);
4637 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304638 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304639 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304640
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004641 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4642 nonemb_cmd.va, nonemb_cmd.dma);
4643 return -EAGAIN;
4644 } else
4645 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
4646 phba->ctrl.mcc_numtag[tag]);
4647 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4648 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4649 free_mcc_tag(&phba->ctrl, tag);
4650 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4651 sg_copy_from_buffer(job->reply_payload.sg_list,
4652 job->reply_payload.sg_cnt,
4653 nonemb_cmd.va, (resp->response_length
4654 + sizeof(*resp)));
4655 bsg_reply->reply_payload_rcv_len = resp->response_length;
4656 bsg_reply->result = status;
4657 bsg_job_done(job, bsg_reply->result,
4658 bsg_reply->reply_payload_rcv_len);
4659 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4660 nonemb_cmd.va, nonemb_cmd.dma);
4661 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304662 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304663 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304664 " status = %d extd_status = %d\n",
4665 status, extd_status);
4666
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004667 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304668 } else {
4669 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004670 }
4671 break;
4672
4673 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304674 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4675 "BM_%d : Unsupported bsg command: 0x%x\n",
4676 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004677 break;
4678 }
4679
4680 return rc;
4681}
4682
John Soni Jose99bc5d52012-08-20 23:00:18 +05304683void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4684{
4685 /* Set the logging parameter */
4686 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4687}
4688
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304689/*
4690 * beiscsi_quiesce()- Cleanup Driver resources
4691 * @phba: Instance Priv structure
4692 *
4693 * Free the OS and HW resources held by the driver
4694 **/
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004695static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304696{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304697 struct hwi_controller *phwi_ctrlr;
4698 struct hwi_context_memory *phwi_context;
4699 struct be_eq_obj *pbe_eq;
4700 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304701
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304702 phwi_ctrlr = phba->phwi_ctrlr;
4703 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304704 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304705 if (phba->msix_enabled) {
4706 for (i = 0; i <= phba->num_cpus; i++) {
4707 msix_vec = phba->msix_entries[i].vector;
4708 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004709 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304710 }
4711 } else
4712 if (phba->pcidev->irq)
4713 free_irq(phba->pcidev->irq, phba);
4714 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304715 destroy_workqueue(phba->wq);
4716 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304717 for (i = 0; i < phba->num_cpus; i++) {
4718 pbe_eq = &phwi_context->be_eq[i];
4719 blk_iopoll_disable(&pbe_eq->iopoll);
4720 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304721
4722 beiscsi_clean_port(phba);
4723 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304724
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304725 beiscsi_unmap_pci_function(phba);
4726 pci_free_consistent(phba->pcidev,
4727 phba->ctrl.mbox_mem_alloced.size,
4728 phba->ctrl.mbox_mem_alloced.va,
4729 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004730}
4731
4732static void beiscsi_remove(struct pci_dev *pcidev)
4733{
4734
4735 struct beiscsi_hba *phba = NULL;
4736
4737 phba = pci_get_drvdata(pcidev);
4738 if (!phba) {
4739 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4740 return;
4741 }
4742
Mike Christie0e438952012-04-03 23:41:51 -05004743 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004744 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004745 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304746 iscsi_host_remove(phba->shost);
4747 pci_dev_put(phba->pcidev);
4748 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004749 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304750}
4751
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004752static void beiscsi_shutdown(struct pci_dev *pcidev)
4753{
4754
4755 struct beiscsi_hba *phba = NULL;
4756
4757 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4758 if (!phba) {
4759 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4760 return;
4761 }
4762
4763 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004764 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004765}
4766
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304767static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4768{
4769 int i, status;
4770
4771 for (i = 0; i <= phba->num_cpus; i++)
4772 phba->msix_entries[i].entry = i;
4773
4774 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4775 (phba->num_cpus + 1));
4776 if (!status)
4777 phba->msix_enabled = true;
4778
4779 return;
4780}
4781
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304782static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4783 const struct pci_device_id *id)
4784{
4785 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304786 struct hwi_controller *phwi_ctrlr;
4787 struct hwi_context_memory *phwi_context;
4788 struct be_eq_obj *pbe_eq;
John Soni Jose107dfcb2012-10-20 04:42:13 +05304789 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304790
4791 ret = beiscsi_enable_pci(pcidev);
4792 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304793 dev_err(&pcidev->dev,
4794 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304795 return ret;
4796 }
4797
4798 phba = beiscsi_hba_alloc(pcidev);
4799 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304800 dev_err(&pcidev->dev,
4801 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304802 goto disable_pci;
4803 }
4804
John Soni Jose99bc5d52012-08-20 23:00:18 +05304805 /* Initialize Driver configuration Paramters */
4806 beiscsi_hba_attrs_init(phba);
4807
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304808 switch (pcidev->device) {
4809 case BE_DEVICE_ID1:
4810 case OC_DEVICE_ID1:
4811 case OC_DEVICE_ID2:
4812 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05304813 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304814 break;
4815 case BE_DEVICE_ID2:
4816 case OC_DEVICE_ID3:
4817 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05304818 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304819 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05304820 case OC_SKH_ID1:
4821 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05304822 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304823 default:
4824 phba->generation = 0;
4825 }
4826
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304827 if (enable_msix)
John Soni Jose107dfcb2012-10-20 04:42:13 +05304828 find_num_cpus(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304829 else
John Soni Jose107dfcb2012-10-20 04:42:13 +05304830 phba->num_cpus = 1;
4831
John Soni Jose99bc5d52012-08-20 23:00:18 +05304832 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4833 "BM_%d : num_cpus = %d\n",
4834 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304835
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004836 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304837 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004838 if (!phba->msix_enabled)
4839 phba->num_cpus = 1;
4840 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304841 ret = be_ctrl_init(phba, pcidev);
4842 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304843 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4844 "BM_%d : beiscsi_dev_probe-"
4845 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304846 goto hba_free;
4847 }
4848
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304849 ret = beiscsi_cmd_reset_function(phba);
4850 if (ret) {
4851 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4852 "BM_%d : Reset Failed. Aborting Crashdump\n");
4853 goto hba_free;
4854 }
4855 ret = be_chk_reset_complete(phba);
4856 if (ret) {
4857 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4858 "BM_%d : Failed to get out of reset."
4859 "Aborting Crashdump\n");
4860 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304861 }
4862
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304863 spin_lock_init(&phba->io_sgl_lock);
4864 spin_lock_init(&phba->mgmt_sgl_lock);
4865 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304866 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4867 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304868 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4869 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304870 goto free_port;
4871 }
4872 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304873 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304874 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304875 ret = beiscsi_init_port(phba);
4876 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304877 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4878 "BM_%d : beiscsi_dev_probe-"
4879 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304880 goto free_port;
4881 }
4882
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304883 for (i = 0; i < MAX_MCC_CMD ; i++) {
4884 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4885 phba->ctrl.mcc_tag[i] = i + 1;
4886 phba->ctrl.mcc_numtag[i + 1] = 0;
4887 phba->ctrl.mcc_tag_available++;
4888 }
4889
4890 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4891
John Soni Jose72fb46a2012-10-20 04:42:49 +05304892 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304893 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004894 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304895 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304896 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4897 "BM_%d : beiscsi_dev_probe-"
4898 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304899 goto free_twq;
4900 }
4901
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304902
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304903 phwi_ctrlr = phba->phwi_ctrlr;
4904 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05304905
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304906 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304907 for (i = 0; i < phba->num_cpus; i++) {
4908 pbe_eq = &phwi_context->be_eq[i];
4909 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4910 be_iopoll);
4911 blk_iopoll_enable(&pbe_eq->iopoll);
4912 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304913
4914 i = (phba->msix_enabled) ? i : 0;
4915 /* Work item for MCC handling */
4916 pbe_eq = &phwi_context->be_eq[i];
4917 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
4918 } else {
4919 if (phba->msix_enabled) {
4920 for (i = 0; i <= phba->num_cpus; i++) {
4921 pbe_eq = &phwi_context->be_eq[i];
4922 INIT_WORK(&pbe_eq->work_cqs,
4923 beiscsi_process_all_cqs);
4924 }
4925 } else {
4926 pbe_eq = &phwi_context->be_eq[0];
4927 INIT_WORK(&pbe_eq->work_cqs,
4928 beiscsi_process_all_cqs);
4929 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304930 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304931
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304932 ret = beiscsi_init_irqs(phba);
4933 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304934 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4935 "BM_%d : beiscsi_dev_probe-"
4936 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304937 goto free_blkenbld;
4938 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304939 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004940
4941 if (beiscsi_setup_boot_info(phba))
4942 /*
4943 * log error but continue, because we may not be using
4944 * iscsi boot.
4945 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304946 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4947 "BM_%d : Could not set up "
4948 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004949
Mike Christie0e438952012-04-03 23:41:51 -05004950 beiscsi_create_def_ifaces(phba);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304951 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4952 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304953 return 0;
4954
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304955free_blkenbld:
4956 destroy_workqueue(phba->wq);
4957 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304958 for (i = 0; i < phba->num_cpus; i++) {
4959 pbe_eq = &phwi_context->be_eq[i];
4960 blk_iopoll_disable(&pbe_eq->iopoll);
4961 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304962free_twq:
4963 beiscsi_clean_port(phba);
4964 beiscsi_free_mem(phba);
4965free_port:
4966 pci_free_consistent(phba->pcidev,
4967 phba->ctrl.mbox_mem_alloced.size,
4968 phba->ctrl.mbox_mem_alloced.va,
4969 phba->ctrl.mbox_mem_alloced.dma);
4970 beiscsi_unmap_pci_function(phba);
4971hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304972 if (phba->msix_enabled)
4973 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304974 iscsi_host_remove(phba->shost);
4975 pci_dev_put(phba->pcidev);
4976 iscsi_host_free(phba->shost);
4977disable_pci:
4978 pci_disable_device(pcidev);
4979 return ret;
4980}
4981
4982struct iscsi_transport beiscsi_iscsi_transport = {
4983 .owner = THIS_MODULE,
4984 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304985 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304986 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304987 .create_session = beiscsi_session_create,
4988 .destroy_session = beiscsi_session_destroy,
4989 .create_conn = beiscsi_conn_create,
4990 .bind_conn = beiscsi_conn_bind,
4991 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004992 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05004993 .set_iface_param = be2iscsi_iface_set_param,
4994 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304995 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004996 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304997 .get_session_param = iscsi_session_get_param,
4998 .get_host_param = beiscsi_get_host_param,
4999 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005000 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305001 .send_pdu = iscsi_conn_send_pdu,
5002 .xmit_task = beiscsi_task_xmit,
5003 .cleanup_task = beiscsi_cleanup_task,
5004 .alloc_pdu = beiscsi_alloc_pdu,
5005 .parse_pdu_itt = beiscsi_parse_pdu,
5006 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005007 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305008 .ep_connect = beiscsi_ep_connect,
5009 .ep_poll = beiscsi_ep_poll,
5010 .ep_disconnect = beiscsi_ep_disconnect,
5011 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005012 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305013};
5014
5015static struct pci_driver beiscsi_pci_driver = {
5016 .name = DRV_NAME,
5017 .probe = beiscsi_dev_probe,
5018 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005019 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305020 .id_table = beiscsi_pci_id_table
5021};
5022
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305023
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305024static int __init beiscsi_module_init(void)
5025{
5026 int ret;
5027
5028 beiscsi_scsi_transport =
5029 iscsi_register_transport(&beiscsi_iscsi_transport);
5030 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305031 printk(KERN_ERR
5032 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305033 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305034 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305035 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5036 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305037
5038 ret = pci_register_driver(&beiscsi_pci_driver);
5039 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305040 printk(KERN_ERR
5041 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305042 goto unregister_iscsi_transport;
5043 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305044 return 0;
5045
5046unregister_iscsi_transport:
5047 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5048 return ret;
5049}
5050
5051static void __exit beiscsi_module_exit(void)
5052{
5053 pci_unregister_driver(&beiscsi_pci_driver);
5054 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5055}
5056
5057module_init(beiscsi_module_init);
5058module_exit(beiscsi_module_exit);