blob: ea510ef656f047d911e30e022b319b9d5aa54549 [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 Jose26000db2012-10-20 04:45:06 +0530155DEVICE_ATTR(beiscsi_adapter_family, S_IRUGO, beiscsi_adap_family_disp, NULL);
John Soni Jose99bc5d52012-08-20 23:00:18 +0530156struct device_attribute *beiscsi_attrs[] = {
157 &dev_attr_beiscsi_log_enable,
John Soni Jose5cac7592012-10-20 04:42:25 +0530158 &dev_attr_beiscsi_drvr_ver,
John Soni Jose26000db2012-10-20 04:45:06 +0530159 &dev_attr_beiscsi_adapter_family,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530160 NULL,
161};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530162
John Soni Jose6763daa2012-10-20 04:41:45 +0530163static char const *cqe_desc[] = {
164 "RESERVED_DESC",
165 "SOL_CMD_COMPLETE",
166 "SOL_CMD_KILLED_DATA_DIGEST_ERR",
167 "CXN_KILLED_PDU_SIZE_EXCEEDS_DSL",
168 "CXN_KILLED_BURST_LEN_MISMATCH",
169 "CXN_KILLED_AHS_RCVD",
170 "CXN_KILLED_HDR_DIGEST_ERR",
171 "CXN_KILLED_UNKNOWN_HDR",
172 "CXN_KILLED_STALE_ITT_TTT_RCVD",
173 "CXN_KILLED_INVALID_ITT_TTT_RCVD",
174 "CXN_KILLED_RST_RCVD",
175 "CXN_KILLED_TIMED_OUT",
176 "CXN_KILLED_RST_SENT",
177 "CXN_KILLED_FIN_RCVD",
178 "CXN_KILLED_BAD_UNSOL_PDU_RCVD",
179 "CXN_KILLED_BAD_WRB_INDEX_ERROR",
180 "CXN_KILLED_OVER_RUN_RESIDUAL",
181 "CXN_KILLED_UNDER_RUN_RESIDUAL",
182 "CMD_KILLED_INVALID_STATSN_RCVD",
183 "CMD_KILLED_INVALID_R2T_RCVD",
184 "CMD_CXN_KILLED_LUN_INVALID",
185 "CMD_CXN_KILLED_ICD_INVALID",
186 "CMD_CXN_KILLED_ITT_INVALID",
187 "CMD_CXN_KILLED_SEQ_OUTOFORDER",
188 "CMD_CXN_KILLED_INVALID_DATASN_RCVD",
189 "CXN_INVALIDATE_NOTIFY",
190 "CXN_INVALIDATE_INDEX_NOTIFY",
191 "CMD_INVALIDATED_NOTIFY",
192 "UNSOL_HDR_NOTIFY",
193 "UNSOL_DATA_NOTIFY",
194 "UNSOL_DATA_DIGEST_ERROR_NOTIFY",
195 "DRIVERMSG_NOTIFY",
196 "CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN",
197 "SOL_CMD_KILLED_DIF_ERR",
198 "CXN_KILLED_SYN_RCVD",
199 "CXN_KILLED_IMM_DATA_RCVD"
200};
201
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530202static int beiscsi_slave_configure(struct scsi_device *sdev)
203{
204 blk_queue_max_segment_size(sdev->request_queue, 65536);
205 return 0;
206}
207
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530208static int beiscsi_eh_abort(struct scsi_cmnd *sc)
209{
210 struct iscsi_cls_session *cls_session;
211 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
212 struct beiscsi_io_task *aborted_io_task;
213 struct iscsi_conn *conn;
214 struct beiscsi_conn *beiscsi_conn;
215 struct beiscsi_hba *phba;
216 struct iscsi_session *session;
217 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530218 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530219 unsigned int cid, tag, num_invalidate;
220
221 cls_session = starget_to_session(scsi_target(sc->device));
222 session = cls_session->dd_data;
223
224 spin_lock_bh(&session->lock);
225 if (!aborted_task || !aborted_task->sc) {
226 /* we raced */
227 spin_unlock_bh(&session->lock);
228 return SUCCESS;
229 }
230
231 aborted_io_task = aborted_task->dd_data;
232 if (!aborted_io_task->scsi_cmnd) {
233 /* raced or invalid command */
234 spin_unlock_bh(&session->lock);
235 return SUCCESS;
236 }
237 spin_unlock_bh(&session->lock);
238 conn = aborted_task->conn;
239 beiscsi_conn = conn->dd_data;
240 phba = beiscsi_conn->phba;
241
242 /* invalidate iocb */
243 cid = beiscsi_conn->beiscsi_conn_cid;
244 inv_tbl = phba->inv_tbl;
245 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
246 inv_tbl->cid = cid;
247 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
248 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530249 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
250 sizeof(struct invalidate_commands_params_in),
251 &nonemb_cmd.dma);
252 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530253 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
254 "BM_%d : Failed to allocate memory for"
255 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530256 return FAILED;
257 }
258 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
259
260 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
261 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530262 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530263 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
264 "BM_%d : mgmt_invalidate_icds could not be"
265 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530266 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
267 nonemb_cmd.va, nonemb_cmd.dma);
268
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530269 return FAILED;
270 } else {
271 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
272 phba->ctrl.mcc_numtag[tag]);
273 free_mcc_tag(&phba->ctrl, tag);
274 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530275 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
276 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530277 return iscsi_eh_abort(sc);
278}
279
280static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
281{
282 struct iscsi_task *abrt_task;
283 struct beiscsi_io_task *abrt_io_task;
284 struct iscsi_conn *conn;
285 struct beiscsi_conn *beiscsi_conn;
286 struct beiscsi_hba *phba;
287 struct iscsi_session *session;
288 struct iscsi_cls_session *cls_session;
289 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530290 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530291 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530292
293 /* invalidate iocbs */
294 cls_session = starget_to_session(scsi_target(sc->device));
295 session = cls_session->dd_data;
296 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500297 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
298 spin_unlock_bh(&session->lock);
299 return FAILED;
300 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530301 conn = session->leadconn;
302 beiscsi_conn = conn->dd_data;
303 phba = beiscsi_conn->phba;
304 cid = beiscsi_conn->beiscsi_conn_cid;
305 inv_tbl = phba->inv_tbl;
306 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
307 num_invalidate = 0;
308 for (i = 0; i < conn->session->cmds_max; i++) {
309 abrt_task = conn->session->cmds[i];
310 abrt_io_task = abrt_task->dd_data;
311 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
312 continue;
313
314 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
315 continue;
316
317 inv_tbl->cid = cid;
318 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
319 num_invalidate++;
320 inv_tbl++;
321 }
322 spin_unlock_bh(&session->lock);
323 inv_tbl = phba->inv_tbl;
324
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530325 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
326 sizeof(struct invalidate_commands_params_in),
327 &nonemb_cmd.dma);
328 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530329 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
330 "BM_%d : Failed to allocate memory for"
331 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530332 return FAILED;
333 }
334 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
335 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
336 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
337 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530338 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530339 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
340 "BM_%d : mgmt_invalidate_icds could not be"
341 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530342 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
343 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530344 return FAILED;
345 } else {
346 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
347 phba->ctrl.mcc_numtag[tag]);
348 free_mcc_tag(&phba->ctrl, tag);
349 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530350 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
351 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530352 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530353}
354
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530355static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
356{
357 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500358 struct mgmt_session_info *boot_sess = &phba->boot_sess;
359 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530360 char *str = buf;
361 int rc;
362
363 switch (type) {
364 case ISCSI_BOOT_TGT_NAME:
365 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500366 (int)strlen(boot_sess->target_name),
367 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530368 break;
369 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500370 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530371 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500372 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530373 else
374 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500375 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530376 break;
377 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500378 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530379 break;
380
381 case ISCSI_BOOT_TGT_CHAP_NAME:
382 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500383 boot_conn->negotiated_login_options.auth_data.chap.
384 target_chap_name_length,
385 (char *)&boot_conn->negotiated_login_options.
386 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530387 break;
388 case ISCSI_BOOT_TGT_CHAP_SECRET:
389 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500390 boot_conn->negotiated_login_options.auth_data.chap.
391 target_secret_length,
392 (char *)&boot_conn->negotiated_login_options.
393 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530394 break;
395 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
396 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500397 boot_conn->negotiated_login_options.auth_data.chap.
398 intr_chap_name_length,
399 (char *)&boot_conn->negotiated_login_options.
400 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530401 break;
402 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500403 rc = sprintf(str, "%.*s\n",
404 boot_conn->negotiated_login_options.auth_data.chap.
405 intr_secret_length,
406 (char *)&boot_conn->negotiated_login_options.
407 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530408 break;
409 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500410 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530411 break;
412 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500413 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530414 break;
415 default:
416 rc = -ENOSYS;
417 break;
418 }
419 return rc;
420}
421
422static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
423{
424 struct beiscsi_hba *phba = data;
425 char *str = buf;
426 int rc;
427
428 switch (type) {
429 case ISCSI_BOOT_INI_INITIATOR_NAME:
430 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
431 break;
432 default:
433 rc = -ENOSYS;
434 break;
435 }
436 return rc;
437}
438
439static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
440{
441 struct beiscsi_hba *phba = data;
442 char *str = buf;
443 int rc;
444
445 switch (type) {
446 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500447 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530448 break;
449 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500450 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530451 break;
452 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500453 rc = beiscsi_get_macaddr(str, phba);
454 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530455 default:
456 rc = -ENOSYS;
457 break;
458 }
459 return rc;
460}
461
462
Al Viro587a1f12011-07-23 23:11:19 -0400463static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464{
Al Viro587a1f12011-07-23 23:11:19 -0400465 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530466
467 switch (type) {
468 case ISCSI_BOOT_TGT_NAME:
469 case ISCSI_BOOT_TGT_IP_ADDR:
470 case ISCSI_BOOT_TGT_PORT:
471 case ISCSI_BOOT_TGT_CHAP_NAME:
472 case ISCSI_BOOT_TGT_CHAP_SECRET:
473 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
474 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
475 case ISCSI_BOOT_TGT_NIC_ASSOC:
476 case ISCSI_BOOT_TGT_FLAGS:
477 rc = S_IRUGO;
478 break;
479 default:
480 rc = 0;
481 break;
482 }
483 return rc;
484}
485
Al Viro587a1f12011-07-23 23:11:19 -0400486static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530487{
Al Viro587a1f12011-07-23 23:11:19 -0400488 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530489
490 switch (type) {
491 case ISCSI_BOOT_INI_INITIATOR_NAME:
492 rc = S_IRUGO;
493 break;
494 default:
495 rc = 0;
496 break;
497 }
498 return rc;
499}
500
501
Al Viro587a1f12011-07-23 23:11:19 -0400502static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530503{
Al Viro587a1f12011-07-23 23:11:19 -0400504 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530505
506 switch (type) {
507 case ISCSI_BOOT_ETH_FLAGS:
508 case ISCSI_BOOT_ETH_MAC:
509 case ISCSI_BOOT_ETH_INDEX:
510 rc = S_IRUGO;
511 break;
512 default:
513 rc = 0;
514 break;
515 }
516 return rc;
517}
518
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530519/*------------------- PCI Driver operations and data ----------------- */
520static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
521 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530522 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530523 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
524 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
525 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
John Soni Jose139a1b12012-10-20 04:43:20 +0530526 { PCI_DEVICE(ELX_VENDOR_ID, OC_SKH_ID1) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530527 { 0 }
528};
529MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
530
John Soni Jose99bc5d52012-08-20 23:00:18 +0530531
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530532static struct scsi_host_template beiscsi_sht = {
533 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500534 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530535 .proc_name = DRV_NAME,
536 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530537 .change_queue_depth = iscsi_change_queue_depth,
538 .slave_configure = beiscsi_slave_configure,
539 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530540 .eh_abort_handler = beiscsi_eh_abort,
541 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530542 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530543 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530544 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
545 .can_queue = BE2_IO_DEPTH,
546 .this_id = -1,
547 .max_sectors = BEISCSI_MAX_SECTORS,
548 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
549 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500550 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
551
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530552};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530553
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530554static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530555
556static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
557{
558 struct beiscsi_hba *phba;
559 struct Scsi_Host *shost;
560
561 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
562 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530563 dev_err(&pcidev->dev,
564 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530565 return NULL;
566 }
567 shost->dma_boundary = pcidev->dma_mask;
568 shost->max_id = BE2_MAX_SESSIONS;
569 shost->max_channel = 0;
570 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
571 shost->max_lun = BEISCSI_NUM_MAX_LUN;
572 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530573 phba = iscsi_host_priv(shost);
574 memset(phba, 0, sizeof(*phba));
575 phba->shost = shost;
576 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530577 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500578 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530579
580 if (iscsi_host_add(shost, &phba->pcidev->dev))
581 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530582
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530583 return phba;
584
585free_devices:
586 pci_dev_put(phba->pcidev);
587 iscsi_host_free(phba->shost);
588 return NULL;
589}
590
591static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
592{
593 if (phba->csr_va) {
594 iounmap(phba->csr_va);
595 phba->csr_va = NULL;
596 }
597 if (phba->db_va) {
598 iounmap(phba->db_va);
599 phba->db_va = NULL;
600 }
601 if (phba->pci_va) {
602 iounmap(phba->pci_va);
603 phba->pci_va = NULL;
604 }
605}
606
607static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
608 struct pci_dev *pcidev)
609{
610 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530611 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530612
613 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
614 pci_resource_len(pcidev, 2));
615 if (addr == NULL)
616 return -ENOMEM;
617 phba->ctrl.csr = addr;
618 phba->csr_va = addr;
619 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
620
621 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
622 if (addr == NULL)
623 goto pci_map_err;
624 phba->ctrl.db = addr;
625 phba->db_va = addr;
626 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
627
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530628 if (phba->generation == BE_GEN2)
629 pcicfg_reg = 1;
630 else
631 pcicfg_reg = 0;
632
633 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
634 pci_resource_len(pcidev, pcicfg_reg));
635
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530636 if (addr == NULL)
637 goto pci_map_err;
638 phba->ctrl.pcicfg = addr;
639 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530640 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530641 return 0;
642
643pci_map_err:
644 beiscsi_unmap_pci_function(phba);
645 return -ENOMEM;
646}
647
648static int beiscsi_enable_pci(struct pci_dev *pcidev)
649{
650 int ret;
651
652 ret = pci_enable_device(pcidev);
653 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530654 dev_err(&pcidev->dev,
655 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530656 return ret;
657 }
658
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530659 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530660 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
661 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
662 if (ret) {
663 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
664 pci_disable_device(pcidev);
665 return ret;
666 }
667 }
668 return 0;
669}
670
671static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
672{
673 struct be_ctrl_info *ctrl = &phba->ctrl;
674 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
675 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
676 int status = 0;
677
678 ctrl->pdev = pdev;
679 status = beiscsi_map_pci_bars(phba, pdev);
680 if (status)
681 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530682 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
683 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
684 mbox_mem_alloc->size,
685 &mbox_mem_alloc->dma);
686 if (!mbox_mem_alloc->va) {
687 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500688 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530689 }
690
691 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
692 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
693 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
694 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
695 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530696 spin_lock_init(&phba->ctrl.mcc_lock);
697 spin_lock_init(&phba->ctrl.mcc_cq_lock);
698
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530699 return status;
700}
701
702static void beiscsi_get_params(struct beiscsi_hba *phba)
703{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530704 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
705 - (phba->fw_config.iscsi_cid_count
706 + BE2_TMFS
707 + BE2_NOPOUT_REQ));
708 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530709 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700710 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530711 phba->params.num_sge_per_io = BE2_SGE;
712 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
713 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
714 phba->params.eq_timer = 64;
715 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530716 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
717 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530718 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
719 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530720 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
721 "BM_%d : phba->params.num_eq_entries=%d\n",
722 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530723 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530724 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
725 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530726 phba->params.wrbs_per_cxn = 256;
727}
728
729static void hwi_ring_eq_db(struct beiscsi_hba *phba,
730 unsigned int id, unsigned int clr_interrupt,
731 unsigned int num_processed,
732 unsigned char rearm, unsigned char event)
733{
734 u32 val = 0;
735 val |= id & DB_EQ_RING_ID_MASK;
736 if (rearm)
737 val |= 1 << DB_EQ_REARM_SHIFT;
738 if (clr_interrupt)
739 val |= 1 << DB_EQ_CLR_SHIFT;
740 if (event)
741 val |= 1 << DB_EQ_EVNT_SHIFT;
742 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
743 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
744}
745
746/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530747 * be_isr_mcc - The isr routine of the driver.
748 * @irq: Not used
749 * @dev_id: Pointer to host adapter structure
750 */
751static irqreturn_t be_isr_mcc(int irq, void *dev_id)
752{
753 struct beiscsi_hba *phba;
754 struct be_eq_entry *eqe = NULL;
755 struct be_queue_info *eq;
756 struct be_queue_info *mcc;
757 unsigned int num_eq_processed;
758 struct be_eq_obj *pbe_eq;
759 unsigned long flags;
760
761 pbe_eq = dev_id;
762 eq = &pbe_eq->q;
763 phba = pbe_eq->phba;
764 mcc = &phba->ctrl.mcc_obj.cq;
765 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530766
767 num_eq_processed = 0;
768
769 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
770 & EQE_VALID_MASK) {
771 if (((eqe->dw[offsetof(struct amap_eq_entry,
772 resource_id) / 32] &
773 EQE_RESID_MASK) >> 16) == mcc->id) {
774 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530775 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530776 spin_unlock_irqrestore(&phba->isr_lock, flags);
777 }
778 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
779 queue_tail_inc(eq);
780 eqe = queue_tail_node(eq);
781 num_eq_processed++;
782 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530783 if (pbe_eq->todo_mcc_cq)
784 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530785 if (num_eq_processed)
786 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
787
788 return IRQ_HANDLED;
789}
790
791/**
792 * be_isr_msix - The isr routine of the driver.
793 * @irq: Not used
794 * @dev_id: Pointer to host adapter structure
795 */
796static irqreturn_t be_isr_msix(int irq, void *dev_id)
797{
798 struct beiscsi_hba *phba;
799 struct be_eq_entry *eqe = NULL;
800 struct be_queue_info *eq;
801 struct be_queue_info *cq;
802 unsigned int num_eq_processed;
803 struct be_eq_obj *pbe_eq;
804 unsigned long flags;
805
806 pbe_eq = dev_id;
807 eq = &pbe_eq->q;
808 cq = pbe_eq->cq;
809 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530810
811 phba = pbe_eq->phba;
812 num_eq_processed = 0;
813 if (blk_iopoll_enabled) {
814 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
815 & EQE_VALID_MASK) {
816 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
817 blk_iopoll_sched(&pbe_eq->iopoll);
818
819 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
820 queue_tail_inc(eq);
821 eqe = queue_tail_node(eq);
822 num_eq_processed++;
823 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530824 } else {
825 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
826 & EQE_VALID_MASK) {
827 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530828 pbe_eq->todo_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530829 spin_unlock_irqrestore(&phba->isr_lock, flags);
830 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
831 queue_tail_inc(eq);
832 eqe = queue_tail_node(eq);
833 num_eq_processed++;
834 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530835
John Soni Jose72fb46a2012-10-20 04:42:49 +0530836 if (pbe_eq->todo_cq)
837 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530838 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530839
840 if (num_eq_processed)
841 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
842
843 return IRQ_HANDLED;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530844}
845
846/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530847 * be_isr - The isr routine of the driver.
848 * @irq: Not used
849 * @dev_id: Pointer to host adapter structure
850 */
851static irqreturn_t be_isr(int irq, void *dev_id)
852{
853 struct beiscsi_hba *phba;
854 struct hwi_controller *phwi_ctrlr;
855 struct hwi_context_memory *phwi_context;
856 struct be_eq_entry *eqe = NULL;
857 struct be_queue_info *eq;
858 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530859 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530861 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530862 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530863 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530864 int isr;
865
866 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700867 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530868 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
869 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
870 if (!isr)
871 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530872
873 phwi_ctrlr = phba->phwi_ctrlr;
874 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530875 pbe_eq = &phwi_context->be_eq[0];
876
877 eq = &phwi_context->be_eq[0].q;
878 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530879 index = 0;
880 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530881
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530882 num_ioeq_processed = 0;
883 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530884 if (blk_iopoll_enabled) {
885 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
886 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530887 if (((eqe->dw[offsetof(struct amap_eq_entry,
888 resource_id) / 32] &
889 EQE_RESID_MASK) >> 16) == mcc->id) {
890 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530891 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530892 spin_unlock_irqrestore(&phba->isr_lock, flags);
893 num_mcceq_processed++;
894 } else {
895 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
896 blk_iopoll_sched(&pbe_eq->iopoll);
897 num_ioeq_processed++;
898 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530899 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
900 queue_tail_inc(eq);
901 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530902 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530903 if (num_ioeq_processed || num_mcceq_processed) {
John Soni Jose72fb46a2012-10-20 04:42:49 +0530904 if (pbe_eq->todo_mcc_cq)
905 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530906
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530907 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530908 hwi_ring_eq_db(phba, eq->id, 0,
909 (num_ioeq_processed +
910 num_mcceq_processed) , 1, 1);
911 else
912 hwi_ring_eq_db(phba, eq->id, 0,
913 (num_ioeq_processed +
914 num_mcceq_processed), 0, 1);
915
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530916 return IRQ_HANDLED;
917 } else
918 return IRQ_NONE;
919 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530920 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530921 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
922 & EQE_VALID_MASK) {
923
924 if (((eqe->dw[offsetof(struct amap_eq_entry,
925 resource_id) / 32] &
926 EQE_RESID_MASK) >> 16) != cq->id) {
927 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530928 pbe_eq->todo_mcc_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530929 spin_unlock_irqrestore(&phba->isr_lock, flags);
930 } else {
931 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +0530932 pbe_eq->todo_cq = true;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530933 spin_unlock_irqrestore(&phba->isr_lock, flags);
934 }
935 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
936 queue_tail_inc(eq);
937 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530938 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530939 }
John Soni Jose72fb46a2012-10-20 04:42:49 +0530940 if (pbe_eq->todo_cq || pbe_eq->todo_mcc_cq)
941 queue_work(phba->wq, &pbe_eq->work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530942
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530943 if (num_ioeq_processed) {
944 hwi_ring_eq_db(phba, eq->id, 0,
945 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530946 return IRQ_HANDLED;
947 } else
948 return IRQ_NONE;
949 }
950}
951
952static int beiscsi_init_irqs(struct beiscsi_hba *phba)
953{
954 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530955 struct hwi_controller *phwi_ctrlr;
956 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530957 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530958
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530959 phwi_ctrlr = phba->phwi_ctrlr;
960 phwi_context = phwi_ctrlr->phwi_ctxt;
961
962 if (phba->msix_enabled) {
963 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700964 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
965 GFP_KERNEL);
966 if (!phba->msi_name[i]) {
967 ret = -ENOMEM;
968 goto free_msix_irqs;
969 }
970
971 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
972 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530973 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700974 ret = request_irq(msix_vec, be_isr_msix, 0,
975 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530976 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530977 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530978 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
979 "BM_%d : beiscsi_init_irqs-Failed to"
980 "register msix for i = %d\n",
981 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700982 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530983 goto free_msix_irqs;
984 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530985 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700986 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
987 if (!phba->msi_name[i]) {
988 ret = -ENOMEM;
989 goto free_msix_irqs;
990 }
991 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
992 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530993 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700994 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530995 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530996 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530997 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
998 "BM_%d : beiscsi_init_irqs-"
999 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001000 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301001 goto free_msix_irqs;
1002 }
1003
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301004 } else {
1005 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
1006 "beiscsi", phba);
1007 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301008 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
1009 "BM_%d : beiscsi_init_irqs-"
1010 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301011 return ret;
1012 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301013 }
1014 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301015free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001016 for (j = i - 1; j >= 0; j--) {
1017 kfree(phba->msi_name[j]);
1018 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301019 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07001020 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +05301021 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301022}
1023
1024static void hwi_ring_cq_db(struct beiscsi_hba *phba,
1025 unsigned int id, unsigned int num_processed,
1026 unsigned char rearm, unsigned char event)
1027{
1028 u32 val = 0;
1029 val |= id & DB_CQ_RING_ID_MASK;
1030 if (rearm)
1031 val |= 1 << DB_CQ_REARM_SHIFT;
1032 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
1033 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
1034}
1035
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301036static unsigned int
1037beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
1038 struct beiscsi_hba *phba,
1039 unsigned short cid,
1040 struct pdu_base *ppdu,
1041 unsigned long pdu_len,
1042 void *pbuffer, unsigned long buf_len)
1043{
1044 struct iscsi_conn *conn = beiscsi_conn->conn;
1045 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301046 struct iscsi_task *task;
1047 struct beiscsi_io_task *io_task;
1048 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301049
1050 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1051 PDUBASE_OPCODE_MASK) {
1052 case ISCSI_OP_NOOP_IN:
1053 pbuffer = NULL;
1054 buf_len = 0;
1055 break;
1056 case ISCSI_OP_ASYNC_EVENT:
1057 break;
1058 case ISCSI_OP_REJECT:
1059 WARN_ON(!pbuffer);
1060 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301061 beiscsi_log(phba, KERN_ERR,
1062 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1063 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301064 break;
1065 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301066 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301067 task = conn->login_task;
1068 io_task = task->dd_data;
1069 login_hdr = (struct iscsi_hdr *)ppdu;
1070 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301071 break;
1072 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301073 beiscsi_log(phba, KERN_WARNING,
1074 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1075 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1076 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301077 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301078 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301079 return 1;
1080 }
1081
1082 spin_lock_bh(&session->lock);
1083 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1084 spin_unlock_bh(&session->lock);
1085 return 0;
1086}
1087
1088static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1089{
1090 struct sgl_handle *psgl_handle;
1091
1092 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301093 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1094 "BM_%d : In alloc_io_sgl_handle,"
1095 " io_sgl_alloc_index=%d\n",
1096 phba->io_sgl_alloc_index);
1097
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301098 psgl_handle = phba->io_sgl_hndl_base[phba->
1099 io_sgl_alloc_index];
1100 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1101 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301102 if (phba->io_sgl_alloc_index == (phba->params.
1103 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301104 phba->io_sgl_alloc_index = 0;
1105 else
1106 phba->io_sgl_alloc_index++;
1107 } else
1108 psgl_handle = NULL;
1109 return psgl_handle;
1110}
1111
1112static void
1113free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1114{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301115 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1116 "BM_%d : In free_,io_sgl_free_index=%d\n",
1117 phba->io_sgl_free_index);
1118
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301119 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1120 /*
1121 * this can happen if clean_task is called on a task that
1122 * failed in xmit_task or alloc_pdu.
1123 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301124 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1125 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1126 "value there=%p\n", phba->io_sgl_free_index,
1127 phba->io_sgl_hndl_base
1128 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129 return;
1130 }
1131 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1132 phba->io_sgl_hndl_avbl++;
1133 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1134 phba->io_sgl_free_index = 0;
1135 else
1136 phba->io_sgl_free_index++;
1137}
1138
1139/**
1140 * alloc_wrb_handle - To allocate a wrb handle
1141 * @phba: The hba pointer
1142 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301143 *
1144 * This happens under session_lock until submission to chip
1145 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301146struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301147{
1148 struct hwi_wrb_context *pwrb_context;
1149 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301150 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301151
1152 phwi_ctrlr = phba->phwi_ctrlr;
1153 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301154 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301155 pwrb_handle = pwrb_context->pwrb_handle_base[
1156 pwrb_context->alloc_index];
1157 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301158 if (pwrb_context->alloc_index ==
1159 (phba->params.wrbs_per_cxn - 1))
1160 pwrb_context->alloc_index = 0;
1161 else
1162 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301163 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1164 pwrb_context->alloc_index];
1165 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301166 } else
1167 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301168 return pwrb_handle;
1169}
1170
1171/**
1172 * free_wrb_handle - To free the wrb handle back to pool
1173 * @phba: The hba pointer
1174 * @pwrb_context: The context to free from
1175 * @pwrb_handle: The wrb_handle to free
1176 *
1177 * This happens under session_lock until submission to chip
1178 */
1179static void
1180free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1181 struct wrb_handle *pwrb_handle)
1182{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301183 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301184 pwrb_context->wrb_handles_available++;
1185 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1186 pwrb_context->free_index = 0;
1187 else
1188 pwrb_context->free_index++;
1189
John Soni Jose99bc5d52012-08-20 23:00:18 +05301190 beiscsi_log(phba, KERN_INFO,
1191 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1192 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1193 "wrb_handles_available=%d\n",
1194 pwrb_handle, pwrb_context->free_index,
1195 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301196}
1197
1198static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1199{
1200 struct sgl_handle *psgl_handle;
1201
1202 if (phba->eh_sgl_hndl_avbl) {
1203 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1204 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301205 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1206 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1207 phba->eh_sgl_alloc_index,
1208 phba->eh_sgl_alloc_index);
1209
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301210 phba->eh_sgl_hndl_avbl--;
1211 if (phba->eh_sgl_alloc_index ==
1212 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1213 1))
1214 phba->eh_sgl_alloc_index = 0;
1215 else
1216 phba->eh_sgl_alloc_index++;
1217 } else
1218 psgl_handle = NULL;
1219 return psgl_handle;
1220}
1221
1222void
1223free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1224{
1225
John Soni Jose99bc5d52012-08-20 23:00:18 +05301226 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1227 "BM_%d : In free_mgmt_sgl_handle,"
1228 "eh_sgl_free_index=%d\n",
1229 phba->eh_sgl_free_index);
1230
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301231 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1232 /*
1233 * this can happen if clean_task is called on a task that
1234 * failed in xmit_task or alloc_pdu.
1235 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301236 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1237 "BM_%d : Double Free in eh SGL ,"
1238 "eh_sgl_free_index=%d\n",
1239 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301240 return;
1241 }
1242 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1243 phba->eh_sgl_hndl_avbl++;
1244 if (phba->eh_sgl_free_index ==
1245 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1246 phba->eh_sgl_free_index = 0;
1247 else
1248 phba->eh_sgl_free_index++;
1249}
1250
1251static void
1252be_complete_io(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301253 struct iscsi_task *task,
1254 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301255{
1256 struct beiscsi_io_task *io_task = task->dd_data;
1257 struct be_status_bhs *sts_bhs =
1258 (struct be_status_bhs *)io_task->cmd_bhs;
1259 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301260 unsigned char *sense;
1261 u32 resid = 0, exp_cmdsn, max_cmdsn;
1262 u8 rsp, status, flags;
1263
John Soni Jose73133262012-10-20 04:44:49 +05301264 exp_cmdsn = csol_cqe->exp_cmdsn;
1265 max_cmdsn = (csol_cqe->exp_cmdsn +
1266 csol_cqe->cmd_wnd - 1);
1267 rsp = csol_cqe->i_resp;
1268 status = csol_cqe->i_sts;
1269 flags = csol_cqe->i_flags;
1270 resid = csol_cqe->res_cnt;
1271
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001272 if (!task->sc) {
1273 if (io_task->scsi_cmnd)
1274 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301275
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001276 return;
1277 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301278 task->sc->result = (DID_OK << 16) | status;
1279 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1280 task->sc->result = DID_ERROR << 16;
1281 goto unmap;
1282 }
1283
1284 /* bidi not initially supported */
1285 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301286 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1287 task->sc->result = DID_ERROR << 16;
1288
1289 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1290 scsi_set_resid(task->sc, resid);
1291 if (!status && (scsi_bufflen(task->sc) - resid <
1292 task->sc->underflow))
1293 task->sc->result = DID_ERROR << 16;
1294 }
1295 }
1296
1297 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001298 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301299 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001300
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301301 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001302 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301303 memcpy(task->sc->sense_buffer, sense,
1304 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1305 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301306
John Soni Jose73133262012-10-20 04:44:49 +05301307 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ)
1308 conn->rxdata_octets += resid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301309unmap:
1310 scsi_dma_unmap(io_task->scsi_cmnd);
1311 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1312}
1313
1314static void
1315be_complete_logout(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301316 struct iscsi_task *task,
1317 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301318{
1319 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301320 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301321 struct iscsi_conn *conn = beiscsi_conn->conn;
1322
1323 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301324 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301325 hdr->t2wait = 5;
1326 hdr->t2retain = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301327 hdr->flags = csol_cqe->i_flags;
1328 hdr->response = csol_cqe->i_resp;
1329 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1330 hdr->max_cmdsn = (csol_cqe->exp_cmdsn + csol_cqe->cmd_wnd - 1);
1331
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301332 hdr->dlength[0] = 0;
1333 hdr->dlength[1] = 0;
1334 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301335 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301336 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301337 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1338}
1339
1340static void
1341be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301342 struct iscsi_task *task,
1343 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301344{
1345 struct iscsi_tm_rsp *hdr;
1346 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301347 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301348
1349 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301350 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
John Soni Jose73133262012-10-20 04:44:49 +05301351 hdr->flags = csol_cqe->i_flags;
1352 hdr->response = csol_cqe->i_resp;
1353 hdr->exp_cmdsn = csol_cqe->exp_cmdsn;
1354 hdr->max_cmdsn = (csol_cqe->exp_cmdsn +
1355 csol_cqe->cmd_wnd - 1);
1356
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301357 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301358 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1359}
1360
1361static void
1362hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1363 struct beiscsi_hba *phba, struct sol_cqe *psol)
1364{
1365 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301366 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301367 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301368 struct iscsi_task *task;
1369 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301370 struct iscsi_conn *conn = beiscsi_conn->conn;
1371 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301372 uint16_t wrb_index, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301373
1374 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301375 if (chip_skh_r(phba->pcidev)) {
1376 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1377 wrb_idx, psol);
1378 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe_v2,
1379 cid, psol);
1380 } else {
1381 wrb_index = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1382 wrb_idx, psol);
1383 cid = AMAP_GET_BITS(struct amap_it_dmsg_cqe,
1384 cid, psol);
1385 }
1386
1387 pwrb_context = &phwi_ctrlr->wrb_context[
1388 cid - phba->fw_config.iscsi_cid_start];
1389 pwrb_handle = pwrb_context->pwrb_handle_basestd[wrb_index];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301390 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301391
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301392 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001393 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301394 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001395 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301396 spin_lock_bh(&session->lock);
1397 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1398 spin_unlock_bh(&session->lock);
1399}
1400
1401static void
1402be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
John Soni Jose73133262012-10-20 04:44:49 +05301403 struct iscsi_task *task,
1404 struct common_sol_cqe *csol_cqe)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301405{
1406 struct iscsi_nopin *hdr;
1407 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301408 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301409
1410 hdr = (struct iscsi_nopin *)task->hdr;
John Soni Jose73133262012-10-20 04:44:49 +05301411 hdr->flags = csol_cqe->i_flags;
1412 hdr->exp_cmdsn = cpu_to_be32(csol_cqe->exp_cmdsn);
1413 hdr->max_cmdsn = be32_to_cpu(hdr->exp_cmdsn +
1414 csol_cqe->cmd_wnd - 1);
1415
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301416 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301417 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301418 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1419}
1420
John Soni Jose73133262012-10-20 04:44:49 +05301421static void adapter_get_sol_cqe(struct beiscsi_hba *phba,
1422 struct sol_cqe *psol,
1423 struct common_sol_cqe *csol_cqe)
1424{
1425 if (chip_skh_r(phba->pcidev)) {
1426 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1427 i_exp_cmd_sn, psol);
1428 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1429 i_res_cnt, psol);
1430 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1431 wrb_index, psol);
1432 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1433 cid, psol);
1434 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1435 hw_sts, psol);
1436 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1437 i_cmd_wnd, psol);
1438 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1439 cmd_cmpl, psol))
1440 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1441 i_sts, psol);
1442 else
1443 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe_v2,
1444 i_sts, psol);
1445 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1446 u, psol))
1447 csol_cqe->i_flags = ISCSI_FLAG_CMD_UNDERFLOW;
1448
1449 if (AMAP_GET_BITS(struct amap_sol_cqe_v2,
1450 o, psol))
1451 csol_cqe->i_flags |= ISCSI_FLAG_CMD_OVERFLOW;
1452 } else {
1453 csol_cqe->exp_cmdsn = AMAP_GET_BITS(struct amap_sol_cqe,
1454 i_exp_cmd_sn, psol);
1455 csol_cqe->res_cnt = AMAP_GET_BITS(struct amap_sol_cqe,
1456 i_res_cnt, psol);
1457 csol_cqe->cmd_wnd = AMAP_GET_BITS(struct amap_sol_cqe,
1458 i_cmd_wnd, psol);
1459 csol_cqe->wrb_index = AMAP_GET_BITS(struct amap_sol_cqe,
1460 wrb_index, psol);
1461 csol_cqe->cid = AMAP_GET_BITS(struct amap_sol_cqe,
1462 cid, psol);
1463 csol_cqe->hw_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1464 hw_sts, psol);
1465 csol_cqe->i_resp = AMAP_GET_BITS(struct amap_sol_cqe,
1466 i_resp, psol);
1467 csol_cqe->i_sts = AMAP_GET_BITS(struct amap_sol_cqe,
1468 i_sts, psol);
1469 csol_cqe->i_flags = AMAP_GET_BITS(struct amap_sol_cqe,
1470 i_flags, psol);
1471 }
1472}
1473
1474
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301475static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1476 struct beiscsi_hba *phba, struct sol_cqe *psol)
1477{
1478 struct hwi_wrb_context *pwrb_context;
1479 struct wrb_handle *pwrb_handle;
1480 struct iscsi_wrb *pwrb = NULL;
1481 struct hwi_controller *phwi_ctrlr;
1482 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301483 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301484 struct iscsi_conn *conn = beiscsi_conn->conn;
1485 struct iscsi_session *session = conn->session;
John Soni Jose73133262012-10-20 04:44:49 +05301486 struct common_sol_cqe csol_cqe = {0};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301487
1488 phwi_ctrlr = phba->phwi_ctrlr;
John Soni Jose73133262012-10-20 04:44:49 +05301489
1490 /* Copy the elements to a common structure */
1491 adapter_get_sol_cqe(phba, psol, &csol_cqe);
1492
1493 pwrb_context = &phwi_ctrlr->wrb_context[
1494 csol_cqe.cid - phba->fw_config.iscsi_cid_start];
1495
1496 pwrb_handle = pwrb_context->pwrb_handle_basestd[
1497 csol_cqe.wrb_index];
1498
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301499 task = pwrb_handle->pio_handle;
1500 pwrb = pwrb_handle->pwrb;
John Soni Jose73133262012-10-20 04:44:49 +05301501 type = ((struct beiscsi_io_task *)task->dd_data)->wrb_type;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301502
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301503 spin_lock_bh(&session->lock);
1504 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301505 case HWH_TYPE_IO:
1506 case HWH_TYPE_IO_RD:
1507 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301508 ISCSI_OP_NOOP_OUT)
John Soni Jose73133262012-10-20 04:44:49 +05301509 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301510 else
John Soni Jose73133262012-10-20 04:44:49 +05301511 be_complete_io(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301512 break;
1513
1514 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301515 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
John Soni Jose73133262012-10-20 04:44:49 +05301516 be_complete_logout(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301517 else
John Soni Jose73133262012-10-20 04:44:49 +05301518 be_complete_tmf(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301519 break;
1520
1521 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301522 beiscsi_log(phba, KERN_ERR,
1523 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1524 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1525 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 break;
1527
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301528 case HWH_TYPE_NOP:
John Soni Jose73133262012-10-20 04:44:49 +05301529 be_complete_nopin_resp(beiscsi_conn, task, &csol_cqe);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301530 break;
1531
1532 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301533 beiscsi_log(phba, KERN_WARNING,
1534 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1535 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1536 "wrb_index 0x%x CID 0x%x\n", type,
John Soni Jose73133262012-10-20 04:44:49 +05301537 csol_cqe.wrb_index,
1538 csol_cqe.cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301539 break;
1540 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301541
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301542 spin_unlock_bh(&session->lock);
1543}
1544
1545static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1546 *pasync_ctx, unsigned int is_header,
1547 unsigned int host_write_ptr)
1548{
1549 if (is_header)
1550 return &pasync_ctx->async_entry[host_write_ptr].
1551 header_busy_list;
1552 else
1553 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1554}
1555
1556static struct async_pdu_handle *
1557hwi_get_async_handle(struct beiscsi_hba *phba,
1558 struct beiscsi_conn *beiscsi_conn,
1559 struct hwi_async_pdu_context *pasync_ctx,
1560 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1561{
1562 struct be_bus_address phys_addr;
1563 struct list_head *pbusy_list;
1564 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301565 unsigned char is_header = 0;
John Soni Jose73133262012-10-20 04:44:49 +05301566 unsigned int index, dpl;
1567
1568 if (chip_skh_r(phba->pcidev)) {
1569 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1570 dpl, pdpdu_cqe);
1571 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe_v2,
1572 index, pdpdu_cqe);
1573 } else {
1574 dpl = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1575 dpl, pdpdu_cqe);
1576 index = AMAP_GET_BITS(struct amap_i_t_dpdu_cqe,
1577 index, pdpdu_cqe);
1578 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301579
1580 phys_addr.u.a32.address_lo =
John Soni Jose73133262012-10-20 04:44:49 +05301581 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1582 db_addr_lo) / 32] - dpl);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301583 phys_addr.u.a32.address_hi =
John Soni Jose73133262012-10-20 04:44:49 +05301584 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1585 db_addr_hi) / 32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301586
1587 phys_addr.u.a64.address =
1588 *((unsigned long long *)(&phys_addr.u.a64.address));
1589
1590 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1591 & PDUCQE_CODE_MASK) {
1592 case UNSOL_HDR_NOTIFY:
1593 is_header = 1;
1594
John Soni Jose73133262012-10-20 04:44:49 +05301595 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1596 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301597 break;
1598 case UNSOL_DATA_NOTIFY:
John Soni Jose73133262012-10-20 04:44:49 +05301599 pbusy_list = hwi_get_async_busy_list(pasync_ctx,
1600 is_header, index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301601 break;
1602 default:
1603 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301604 beiscsi_log(phba, KERN_WARNING,
1605 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1606 "BM_%d : Unexpected code=%d\n",
1607 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1608 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301609 return NULL;
1610 }
1611
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301612 WARN_ON(list_empty(pbusy_list));
1613 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001614 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301615 break;
1616 }
1617
1618 WARN_ON(!pasync_handle);
1619
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301620 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1621 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301622 pasync_handle->is_header = is_header;
John Soni Jose73133262012-10-20 04:44:49 +05301623 pasync_handle->buffer_len = dpl;
1624 *pcq_index = index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301625
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301626 return pasync_handle;
1627}
1628
1629static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301630hwi_update_async_writables(struct beiscsi_hba *phba,
1631 struct hwi_async_pdu_context *pasync_ctx,
1632 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301633{
1634 struct list_head *pbusy_list;
1635 struct async_pdu_handle *pasync_handle;
1636 unsigned int num_entries, writables = 0;
1637 unsigned int *pep_read_ptr, *pwritables;
1638
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001639 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301640 if (is_header) {
1641 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1642 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301643 } else {
1644 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1645 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301646 }
1647
1648 while ((*pep_read_ptr) != cq_index) {
1649 (*pep_read_ptr)++;
1650 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1651
1652 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1653 *pep_read_ptr);
1654 if (writables == 0)
1655 WARN_ON(list_empty(pbusy_list));
1656
1657 if (!list_empty(pbusy_list)) {
1658 pasync_handle = list_entry(pbusy_list->next,
1659 struct async_pdu_handle,
1660 link);
1661 WARN_ON(!pasync_handle);
1662 pasync_handle->consumed = 1;
1663 }
1664
1665 writables++;
1666 }
1667
1668 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301669 beiscsi_log(phba, KERN_ERR,
1670 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1671 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1672 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301673 WARN_ON(1);
1674 }
1675
1676 *pwritables = *pwritables + writables;
1677 return 0;
1678}
1679
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001680static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301681 unsigned int cri)
1682{
1683 struct hwi_controller *phwi_ctrlr;
1684 struct hwi_async_pdu_context *pasync_ctx;
1685 struct async_pdu_handle *pasync_handle, *tmp_handle;
1686 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301687
1688 phwi_ctrlr = phba->phwi_ctrlr;
1689 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1690
1691 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1692
1693 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1694 list_del(&pasync_handle->link);
1695
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001696 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301697 list_add_tail(&pasync_handle->link,
1698 &pasync_ctx->async_header.free_list);
1699 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301700 } else {
1701 list_add_tail(&pasync_handle->link,
1702 &pasync_ctx->async_data.free_list);
1703 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301704 }
1705 }
1706
1707 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1708 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1709 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301710}
1711
1712static struct phys_addr *
1713hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1714 unsigned int is_header, unsigned int host_write_ptr)
1715{
1716 struct phys_addr *pasync_sge = NULL;
1717
1718 if (is_header)
1719 pasync_sge = pasync_ctx->async_header.ring_base;
1720 else
1721 pasync_sge = pasync_ctx->async_data.ring_base;
1722
1723 return pasync_sge + host_write_ptr;
1724}
1725
1726static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1727 unsigned int is_header)
1728{
1729 struct hwi_controller *phwi_ctrlr;
1730 struct hwi_async_pdu_context *pasync_ctx;
1731 struct async_pdu_handle *pasync_handle;
1732 struct list_head *pfree_link, *pbusy_list;
1733 struct phys_addr *pasync_sge;
1734 unsigned int ring_id, num_entries;
1735 unsigned int host_write_num;
1736 unsigned int writables;
1737 unsigned int i = 0;
1738 u32 doorbell = 0;
1739
1740 phwi_ctrlr = phba->phwi_ctrlr;
1741 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001742 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301743
1744 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301745 writables = min(pasync_ctx->async_header.writables,
1746 pasync_ctx->async_header.free_entries);
1747 pfree_link = pasync_ctx->async_header.free_list.next;
1748 host_write_num = pasync_ctx->async_header.host_write_ptr;
1749 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1750 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301751 writables = min(pasync_ctx->async_data.writables,
1752 pasync_ctx->async_data.free_entries);
1753 pfree_link = pasync_ctx->async_data.free_list.next;
1754 host_write_num = pasync_ctx->async_data.host_write_ptr;
1755 ring_id = phwi_ctrlr->default_pdu_data.id;
1756 }
1757
1758 writables = (writables / 8) * 8;
1759 if (writables) {
1760 for (i = 0; i < writables; i++) {
1761 pbusy_list =
1762 hwi_get_async_busy_list(pasync_ctx, is_header,
1763 host_write_num);
1764 pasync_handle =
1765 list_entry(pfree_link, struct async_pdu_handle,
1766 link);
1767 WARN_ON(!pasync_handle);
1768 pasync_handle->consumed = 0;
1769
1770 pfree_link = pfree_link->next;
1771
1772 pasync_sge = hwi_get_ring_address(pasync_ctx,
1773 is_header, host_write_num);
1774
1775 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1776 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1777
1778 list_move(&pasync_handle->link, pbusy_list);
1779
1780 host_write_num++;
1781 host_write_num = host_write_num % num_entries;
1782 }
1783
1784 if (is_header) {
1785 pasync_ctx->async_header.host_write_ptr =
1786 host_write_num;
1787 pasync_ctx->async_header.free_entries -= writables;
1788 pasync_ctx->async_header.writables -= writables;
1789 pasync_ctx->async_header.busy_entries += writables;
1790 } else {
1791 pasync_ctx->async_data.host_write_ptr = host_write_num;
1792 pasync_ctx->async_data.free_entries -= writables;
1793 pasync_ctx->async_data.writables -= writables;
1794 pasync_ctx->async_data.busy_entries += writables;
1795 }
1796
1797 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1798 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1799 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1800 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1801 << DB_DEF_PDU_CQPROC_SHIFT;
1802
1803 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1804 }
1805}
1806
1807static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1808 struct beiscsi_conn *beiscsi_conn,
1809 struct i_t_dpdu_cqe *pdpdu_cqe)
1810{
1811 struct hwi_controller *phwi_ctrlr;
1812 struct hwi_async_pdu_context *pasync_ctx;
1813 struct async_pdu_handle *pasync_handle = NULL;
1814 unsigned int cq_index = -1;
1815
1816 phwi_ctrlr = phba->phwi_ctrlr;
1817 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1818
1819 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1820 pdpdu_cqe, &cq_index);
1821 BUG_ON(pasync_handle->is_header != 0);
1822 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301823 hwi_update_async_writables(phba, pasync_ctx,
1824 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301825
1826 hwi_free_async_msg(phba, pasync_handle->cri);
1827 hwi_post_async_buffers(phba, pasync_handle->is_header);
1828}
1829
1830static unsigned int
1831hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1832 struct beiscsi_hba *phba,
1833 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1834{
1835 struct list_head *plist;
1836 struct async_pdu_handle *pasync_handle;
1837 void *phdr = NULL;
1838 unsigned int hdr_len = 0, buf_len = 0;
1839 unsigned int status, index = 0, offset = 0;
1840 void *pfirst_buffer = NULL;
1841 unsigned int num_buf = 0;
1842
1843 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1844
1845 list_for_each_entry(pasync_handle, plist, link) {
1846 if (index == 0) {
1847 phdr = pasync_handle->pbuffer;
1848 hdr_len = pasync_handle->buffer_len;
1849 } else {
1850 buf_len = pasync_handle->buffer_len;
1851 if (!num_buf) {
1852 pfirst_buffer = pasync_handle->pbuffer;
1853 num_buf++;
1854 }
1855 memcpy(pfirst_buffer + offset,
1856 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001857 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301858 }
1859 index++;
1860 }
1861
1862 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301863 (beiscsi_conn->beiscsi_conn_cid -
1864 phba->fw_config.iscsi_cid_start),
1865 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001866 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301867
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001868 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301869 return 0;
1870}
1871
1872static unsigned int
1873hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1874 struct beiscsi_hba *phba,
1875 struct async_pdu_handle *pasync_handle)
1876{
1877 struct hwi_async_pdu_context *pasync_ctx;
1878 struct hwi_controller *phwi_ctrlr;
1879 unsigned int bytes_needed = 0, status = 0;
1880 unsigned short cri = pasync_handle->cri;
1881 struct pdu_base *ppdu;
1882
1883 phwi_ctrlr = phba->phwi_ctrlr;
1884 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1885
1886 list_del(&pasync_handle->link);
1887 if (pasync_handle->is_header) {
1888 pasync_ctx->async_header.busy_entries--;
1889 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1890 hwi_free_async_msg(phba, cri);
1891 BUG();
1892 }
1893
1894 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1895 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1896 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1897 (unsigned short)pasync_handle->buffer_len;
1898 list_add_tail(&pasync_handle->link,
1899 &pasync_ctx->async_entry[cri].wait_queue.list);
1900
1901 ppdu = pasync_handle->pbuffer;
1902 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1903 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1904 0xFFFF0000) | ((be16_to_cpu((ppdu->
1905 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1906 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1907
1908 if (status == 0) {
1909 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1910 bytes_needed;
1911
1912 if (bytes_needed == 0)
1913 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1914 pasync_ctx, cri);
1915 }
1916 } else {
1917 pasync_ctx->async_data.busy_entries--;
1918 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1919 list_add_tail(&pasync_handle->link,
1920 &pasync_ctx->async_entry[cri].wait_queue.
1921 list);
1922 pasync_ctx->async_entry[cri].wait_queue.
1923 bytes_received +=
1924 (unsigned short)pasync_handle->buffer_len;
1925
1926 if (pasync_ctx->async_entry[cri].wait_queue.
1927 bytes_received >=
1928 pasync_ctx->async_entry[cri].wait_queue.
1929 bytes_needed)
1930 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1931 pasync_ctx, cri);
1932 }
1933 }
1934 return status;
1935}
1936
1937static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1938 struct beiscsi_hba *phba,
1939 struct i_t_dpdu_cqe *pdpdu_cqe)
1940{
1941 struct hwi_controller *phwi_ctrlr;
1942 struct hwi_async_pdu_context *pasync_ctx;
1943 struct async_pdu_handle *pasync_handle = NULL;
1944 unsigned int cq_index = -1;
1945
1946 phwi_ctrlr = phba->phwi_ctrlr;
1947 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1948 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1949 pdpdu_cqe, &cq_index);
1950
1951 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301952 hwi_update_async_writables(phba, pasync_ctx,
1953 pasync_handle->is_header, cq_index);
1954
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301955 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1956 hwi_post_async_buffers(phba, pasync_handle->is_header);
1957}
1958
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301959static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1960{
1961 struct be_queue_info *mcc_cq;
1962 struct be_mcc_compl *mcc_compl;
1963 unsigned int num_processed = 0;
1964
1965 mcc_cq = &phba->ctrl.mcc_obj.cq;
1966 mcc_compl = queue_tail_node(mcc_cq);
1967 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1968 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1969
1970 if (num_processed >= 32) {
1971 hwi_ring_cq_db(phba, mcc_cq->id,
1972 num_processed, 0, 0);
1973 num_processed = 0;
1974 }
1975 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1976 /* Interpret flags as an async trailer */
1977 if (is_link_state_evt(mcc_compl->flags))
1978 /* Interpret compl as a async link evt */
1979 beiscsi_async_link_state_process(phba,
1980 (struct be_async_event_link_state *) mcc_compl);
1981 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301982 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1983 "BM_%d : Unsupported Async Event, flags"
1984 " = 0x%08x\n",
1985 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301986 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1987 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1988 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1989 }
1990
1991 mcc_compl->flags = 0;
1992 queue_tail_inc(mcc_cq);
1993 mcc_compl = queue_tail_node(mcc_cq);
1994 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1995 num_processed++;
1996 }
1997
1998 if (num_processed > 0)
1999 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
2000
2001}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302002
John Soni Jose6763daa2012-10-20 04:41:45 +05302003/**
2004 * beiscsi_process_cq()- Process the Completion Queue
2005 * @pbe_eq: Event Q on which the Completion has come
2006 *
2007 * return
2008 * Number of Completion Entries processed.
2009 **/
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302010static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302011{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302012 struct be_queue_info *cq;
2013 struct sol_cqe *sol;
2014 struct dmsg_cqe *dmsg;
2015 unsigned int num_processed = 0;
2016 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05302017 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05302019 struct beiscsi_endpoint *beiscsi_ep;
2020 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302021 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302022
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302023 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302024 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302025 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302026
2027 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
2028 CQE_VALID_MASK) {
2029 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
2030
John Soni Jose73133262012-10-20 04:44:49 +05302031 code = (sol->dw[offsetof(struct amap_sol_cqe, code) /
2032 32] & CQE_CODE_MASK);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05302033
John Soni Jose73133262012-10-20 04:44:49 +05302034 /* Get the CID */
2035 if (chip_skh_r(phba->pcidev)) {
2036 if ((code == DRIVERMSG_NOTIFY) ||
2037 (code == UNSOL_HDR_NOTIFY) ||
2038 (code == UNSOL_DATA_NOTIFY))
2039 cid = AMAP_GET_BITS(
2040 struct amap_i_t_dpdu_cqe_v2,
2041 cid, sol);
2042 else
2043 cid = AMAP_GET_BITS(struct amap_sol_cqe_v2,
2044 cid, sol);
2045 } else
2046 cid = AMAP_GET_BITS(struct amap_sol_cqe, cid, sol);
2047
2048 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05302049 beiscsi_ep = ep->dd_data;
2050 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302051
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302052 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302053 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302054 num_processed, 0, 0);
2055 tot_nump += num_processed;
2056 num_processed = 0;
2057 }
2058
John Soni Jose0a513dd2012-08-20 23:00:55 +05302059 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302060 case SOL_CMD_COMPLETE:
2061 hwi_complete_cmd(beiscsi_conn, phba, sol);
2062 break;
2063 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302064 beiscsi_log(phba, KERN_INFO,
2065 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302066 "BM_%d : Received %s[%d] on CID : %d\n",
2067 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302068
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302069 dmsg = (struct dmsg_cqe *)sol;
2070 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
2071 break;
2072 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302073 beiscsi_log(phba, KERN_INFO,
2074 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302075 "BM_%d : Received %s[%d] on CID : %d\n",
2076 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302077
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302078 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2079 (struct i_t_dpdu_cqe *)sol);
2080 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302081 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302082 beiscsi_log(phba, KERN_INFO,
2083 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302084 "BM_%d : Received %s[%d] on CID : %d\n",
2085 cqe_desc[code], code, cid);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302086
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302087 hwi_process_default_pdu_ring(beiscsi_conn, phba,
2088 (struct i_t_dpdu_cqe *)sol);
2089 break;
2090 case CXN_INVALIDATE_INDEX_NOTIFY:
2091 case CMD_INVALIDATED_NOTIFY:
2092 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302093 beiscsi_log(phba, KERN_ERR,
2094 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302095 "BM_%d : Ignoring %s[%d] on CID : %d\n",
2096 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302097 break;
2098 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
2099 case CMD_KILLED_INVALID_STATSN_RCVD:
2100 case CMD_KILLED_INVALID_R2T_RCVD:
2101 case CMD_CXN_KILLED_LUN_INVALID:
2102 case CMD_CXN_KILLED_ICD_INVALID:
2103 case CMD_CXN_KILLED_ITT_INVALID:
2104 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
2105 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302106 beiscsi_log(phba, KERN_ERR,
2107 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
John Soni Jose6763daa2012-10-20 04:41:45 +05302108 "BM_%d : Cmd Notification %s[%d] on CID : %d\n",
2109 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302110 break;
2111 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302112 beiscsi_log(phba, KERN_ERR,
2113 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302114 "BM_%d : Dropping %s[%d] on DPDU ring on CID : %d\n",
2115 cqe_desc[code], code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302116 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2117 (struct i_t_dpdu_cqe *) sol);
2118 break;
2119 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2120 case CXN_KILLED_BURST_LEN_MISMATCH:
2121 case CXN_KILLED_AHS_RCVD:
2122 case CXN_KILLED_HDR_DIGEST_ERR:
2123 case CXN_KILLED_UNKNOWN_HDR:
2124 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2125 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2126 case CXN_KILLED_TIMED_OUT:
2127 case CXN_KILLED_FIN_RCVD:
John Soni Jose6763daa2012-10-20 04:41:45 +05302128 case CXN_KILLED_RST_SENT:
2129 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302130 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2131 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2132 case CXN_KILLED_OVER_RUN_RESIDUAL:
2133 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2134 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302135 beiscsi_log(phba, KERN_ERR,
2136 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302137 "BM_%d : Event %s[%d] received on CID : %d\n",
2138 cqe_desc[code], code, cid);
John Soni Jose0a513dd2012-08-20 23:00:55 +05302139 if (beiscsi_conn)
2140 iscsi_conn_failure(beiscsi_conn->conn,
2141 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302142 break;
2143 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302144 beiscsi_log(phba, KERN_ERR,
2145 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
John Soni Jose6763daa2012-10-20 04:41:45 +05302146 "BM_%d : Invalid CQE Event Received Code : %d"
2147 "CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302148 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302149 break;
2150 }
2151
2152 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2153 queue_tail_inc(cq);
2154 sol = queue_tail_node(cq);
2155 num_processed++;
2156 }
2157
2158 if (num_processed > 0) {
2159 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302160 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302161 }
2162 return tot_nump;
2163}
2164
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302165void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302166{
2167 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302168 struct hwi_controller *phwi_ctrlr;
2169 struct hwi_context_memory *phwi_context;
John Soni Jose72fb46a2012-10-20 04:42:49 +05302170 struct beiscsi_hba *phba;
2171 struct be_eq_obj *pbe_eq =
2172 container_of(work, struct be_eq_obj, work_cqs);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302173
John Soni Jose72fb46a2012-10-20 04:42:49 +05302174 phba = pbe_eq->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302175 phwi_ctrlr = phba->phwi_ctrlr;
2176 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302177
John Soni Jose72fb46a2012-10-20 04:42:49 +05302178 if (pbe_eq->todo_mcc_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302179 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302180 pbe_eq->todo_mcc_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302181 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302182 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302183 }
2184
John Soni Jose72fb46a2012-10-20 04:42:49 +05302185 if (pbe_eq->todo_cq) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302186 spin_lock_irqsave(&phba->isr_lock, flags);
John Soni Jose72fb46a2012-10-20 04:42:49 +05302187 pbe_eq->todo_cq = false;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302188 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302189 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302190 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05302191
2192 /* rearm EQ for further interrupts */
2193 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302194}
2195
2196static int be_iopoll(struct blk_iopoll *iop, int budget)
2197{
2198 static unsigned int ret;
2199 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302200 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302201
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302202 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2203 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302204 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302205 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302206 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302207 beiscsi_log(phba, KERN_INFO,
2208 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2209 "BM_%d : rearm pbe_eq->q.id =%d\n",
2210 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302211 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302212 }
2213 return ret;
2214}
2215
2216static void
John Soni Jose09a10932012-10-20 04:44:23 +05302217hwi_write_sgl_v2(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2218 unsigned int num_sg, struct beiscsi_io_task *io_task)
2219{
2220 struct iscsi_sge *psgl;
2221 unsigned int sg_len, index;
2222 unsigned int sge_len = 0;
2223 unsigned long long addr;
2224 struct scatterlist *l_sg;
2225 unsigned int offset;
2226
2227 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_lo, pwrb,
2228 io_task->bhs_pa.u.a32.address_lo);
2229 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, iscsi_bhs_addr_hi, pwrb,
2230 io_task->bhs_pa.u.a32.address_hi);
2231
2232 l_sg = sg;
2233 for (index = 0; (index < num_sg) && (index < 2); index++,
2234 sg = sg_next(sg)) {
2235 if (index == 0) {
2236 sg_len = sg_dma_len(sg);
2237 addr = (u64) sg_dma_address(sg);
2238 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2239 sge0_addr_lo, pwrb,
2240 lower_32_bits(addr));
2241 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2242 sge0_addr_hi, pwrb,
2243 upper_32_bits(addr));
2244 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2245 sge0_len, pwrb,
2246 sg_len);
2247 sge_len = sg_len;
2248 } else {
2249 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_r2t_offset,
2250 pwrb, sge_len);
2251 sg_len = sg_dma_len(sg);
2252 addr = (u64) sg_dma_address(sg);
2253 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2254 sge1_addr_lo, pwrb,
2255 lower_32_bits(addr));
2256 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2257 sge1_addr_hi, pwrb,
2258 upper_32_bits(addr));
2259 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
2260 sge1_len, pwrb,
2261 sg_len);
2262 }
2263 }
2264 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2265 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2266
2267 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2268
2269 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2270 io_task->bhs_pa.u.a32.address_hi);
2271 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2272 io_task->bhs_pa.u.a32.address_lo);
2273
2274 if (num_sg == 1) {
2275 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2276 1);
2277 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2278 0);
2279 } else if (num_sg == 2) {
2280 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2281 0);
2282 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2283 1);
2284 } else {
2285 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge0_last, pwrb,
2286 0);
2287 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sge1_last, pwrb,
2288 0);
2289 }
2290
2291 sg = l_sg;
2292 psgl++;
2293 psgl++;
2294 offset = 0;
2295 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
2296 sg_len = sg_dma_len(sg);
2297 addr = (u64) sg_dma_address(sg);
2298 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2299 lower_32_bits(addr));
2300 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2301 upper_32_bits(addr));
2302 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2303 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2304 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2305 offset += sg_len;
2306 }
2307 psgl--;
2308 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2309}
2310
2311static void
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302312hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2313 unsigned int num_sg, struct beiscsi_io_task *io_task)
2314{
2315 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302316 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302317 unsigned int sge_len = 0;
2318 unsigned long long addr;
2319 struct scatterlist *l_sg;
2320 unsigned int offset;
2321
2322 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2323 io_task->bhs_pa.u.a32.address_lo);
2324 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2325 io_task->bhs_pa.u.a32.address_hi);
2326
2327 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302328 for (index = 0; (index < num_sg) && (index < 2); index++,
2329 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302330 if (index == 0) {
2331 sg_len = sg_dma_len(sg);
2332 addr = (u64) sg_dma_address(sg);
2333 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302334 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302335 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302336 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302337 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2338 sg_len);
2339 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302340 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302341 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2342 pwrb, sge_len);
2343 sg_len = sg_dma_len(sg);
2344 addr = (u64) sg_dma_address(sg);
2345 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302346 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302347 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302348 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302349 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2350 sg_len);
2351 }
2352 }
2353 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2354 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2355
2356 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2357
2358 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2359 io_task->bhs_pa.u.a32.address_hi);
2360 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2361 io_task->bhs_pa.u.a32.address_lo);
2362
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302363 if (num_sg == 1) {
2364 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2365 1);
2366 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2367 0);
2368 } else if (num_sg == 2) {
2369 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2370 0);
2371 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2372 1);
2373 } else {
2374 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2375 0);
2376 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2377 0);
2378 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302379 sg = l_sg;
2380 psgl++;
2381 psgl++;
2382 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302383 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302384 sg_len = sg_dma_len(sg);
2385 addr = (u64) sg_dma_address(sg);
2386 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2387 (addr & 0xFFFFFFFF));
2388 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2389 (addr >> 32));
2390 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2391 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2392 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2393 offset += sg_len;
2394 }
2395 psgl--;
2396 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2397}
2398
John Soni Josed629c472012-10-20 04:42:00 +05302399/**
2400 * hwi_write_buffer()- Populate the WRB with task info
2401 * @pwrb: ptr to the WRB entry
2402 * @task: iscsi task which is to be executed
2403 **/
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302404static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2405{
2406 struct iscsi_sge *psgl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302407 struct beiscsi_io_task *io_task = task->dd_data;
2408 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2409 struct beiscsi_hba *phba = beiscsi_conn->phba;
John Soni Jose09a10932012-10-20 04:44:23 +05302410 uint8_t dsp_value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302411
2412 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2413 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2414 io_task->bhs_pa.u.a32.address_lo);
2415 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2416 io_task->bhs_pa.u.a32.address_hi);
2417
2418 if (task->data) {
John Soni Jose09a10932012-10-20 04:44:23 +05302419
2420 /* Check for the data_count */
2421 dsp_value = (task->data_count) ? 1 : 0;
2422
2423 if (chip_skh_r(phba->pcidev))
2424 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp,
2425 pwrb, dsp_value);
2426 else
2427 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp,
2428 pwrb, dsp_value);
2429
2430 /* Map addr only if there is data_count */
2431 if (dsp_value) {
John Soni Josed629c472012-10-20 04:42:00 +05302432 io_task->mtask_addr = pci_map_single(phba->pcidev,
2433 task->data,
2434 task->data_count,
2435 PCI_DMA_TODEVICE);
John Soni Josed629c472012-10-20 04:42:00 +05302436 io_task->mtask_data_count = task->data_count;
John Soni Jose09a10932012-10-20 04:44:23 +05302437 } else
John Soni Josed629c472012-10-20 04:42:00 +05302438 io_task->mtask_addr = 0;
John Soni Jose09a10932012-10-20 04:44:23 +05302439
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302440 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302441 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302442 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
John Soni Josed629c472012-10-20 04:42:00 +05302443 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302444 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2445 task->data_count);
2446
2447 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2448 } else {
2449 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
John Soni Josed629c472012-10-20 04:42:00 +05302450 io_task->mtask_addr = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302451 }
2452
2453 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2454
2455 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2456
2457 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2458 io_task->bhs_pa.u.a32.address_hi);
2459 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2460 io_task->bhs_pa.u.a32.address_lo);
2461 if (task->data) {
2462 psgl++;
2463 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2464 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2465 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2466 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2467 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2468 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2469
2470 psgl++;
2471 if (task->data) {
2472 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302473 lower_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302474 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
John Soni Josed629c472012-10-20 04:42:00 +05302475 upper_32_bits(io_task->mtask_addr));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302476 }
2477 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2478 }
2479 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2480}
2481
2482static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2483{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302484 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302485 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2486 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2487
2488 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2489 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302490 num_async_pdu_buf_pages =
2491 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2492 phba->params.defpdu_hdr_sz);
2493 num_async_pdu_buf_sgl_pages =
2494 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2495 sizeof(struct phys_addr));
2496 num_async_pdu_data_pages =
2497 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2498 phba->params.defpdu_data_sz);
2499 num_async_pdu_data_sgl_pages =
2500 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2501 sizeof(struct phys_addr));
2502
2503 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2504
2505 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2506 BE_ISCSI_PDU_HEADER_SIZE;
2507 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2508 sizeof(struct hwi_context_memory);
2509
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302510
2511 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2512 * (phba->params.wrbs_per_cxn)
2513 * phba->params.cxns_per_ctrl;
2514 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2515 (phba->params.wrbs_per_cxn);
2516 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2517 phba->params.cxns_per_ctrl);
2518
2519 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2520 phba->params.icds_per_ctrl;
2521 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2522 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2523
2524 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2525 num_async_pdu_buf_pages * PAGE_SIZE;
2526 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2527 num_async_pdu_data_pages * PAGE_SIZE;
2528 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2529 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2530 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2531 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2532 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2533 phba->params.asyncpdus_per_ctrl *
2534 sizeof(struct async_pdu_handle);
2535 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2536 phba->params.asyncpdus_per_ctrl *
2537 sizeof(struct async_pdu_handle);
2538 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2539 sizeof(struct hwi_async_pdu_context) +
2540 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2541}
2542
2543static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2544{
2545 struct be_mem_descriptor *mem_descr;
2546 dma_addr_t bus_add;
2547 struct mem_array *mem_arr, *mem_arr_orig;
2548 unsigned int i, j, alloc_size, curr_alloc_size;
2549
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002550 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302551 if (!phba->phwi_ctrlr)
2552 return -ENOMEM;
2553
2554 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2555 GFP_KERNEL);
2556 if (!phba->init_mem) {
2557 kfree(phba->phwi_ctrlr);
2558 return -ENOMEM;
2559 }
2560
2561 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2562 GFP_KERNEL);
2563 if (!mem_arr_orig) {
2564 kfree(phba->init_mem);
2565 kfree(phba->phwi_ctrlr);
2566 return -ENOMEM;
2567 }
2568
2569 mem_descr = phba->init_mem;
2570 for (i = 0; i < SE_MEM_MAX; i++) {
2571 j = 0;
2572 mem_arr = mem_arr_orig;
2573 alloc_size = phba->mem_req[i];
2574 memset(mem_arr, 0, sizeof(struct mem_array) *
2575 BEISCSI_MAX_FRAGS_INIT);
2576 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2577 do {
2578 mem_arr->virtual_address = pci_alloc_consistent(
2579 phba->pcidev,
2580 curr_alloc_size,
2581 &bus_add);
2582 if (!mem_arr->virtual_address) {
2583 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2584 goto free_mem;
2585 if (curr_alloc_size -
2586 rounddown_pow_of_two(curr_alloc_size))
2587 curr_alloc_size = rounddown_pow_of_two
2588 (curr_alloc_size);
2589 else
2590 curr_alloc_size = curr_alloc_size / 2;
2591 } else {
2592 mem_arr->bus_address.u.
2593 a64.address = (__u64) bus_add;
2594 mem_arr->size = curr_alloc_size;
2595 alloc_size -= curr_alloc_size;
2596 curr_alloc_size = min(be_max_phys_size *
2597 1024, alloc_size);
2598 j++;
2599 mem_arr++;
2600 }
2601 } while (alloc_size);
2602 mem_descr->num_elements = j;
2603 mem_descr->size_in_bytes = phba->mem_req[i];
2604 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2605 GFP_KERNEL);
2606 if (!mem_descr->mem_array)
2607 goto free_mem;
2608
2609 memcpy(mem_descr->mem_array, mem_arr_orig,
2610 sizeof(struct mem_array) * j);
2611 mem_descr++;
2612 }
2613 kfree(mem_arr_orig);
2614 return 0;
2615free_mem:
2616 mem_descr->num_elements = j;
2617 while ((i) || (j)) {
2618 for (j = mem_descr->num_elements; j > 0; j--) {
2619 pci_free_consistent(phba->pcidev,
2620 mem_descr->mem_array[j - 1].size,
2621 mem_descr->mem_array[j - 1].
2622 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302623 (unsigned long)mem_descr->
2624 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302625 bus_address.u.a64.address);
2626 }
2627 if (i) {
2628 i--;
2629 kfree(mem_descr->mem_array);
2630 mem_descr--;
2631 }
2632 }
2633 kfree(mem_arr_orig);
2634 kfree(phba->init_mem);
2635 kfree(phba->phwi_ctrlr);
2636 return -ENOMEM;
2637}
2638
2639static int beiscsi_get_memory(struct beiscsi_hba *phba)
2640{
2641 beiscsi_find_mem_req(phba);
2642 return beiscsi_alloc_mem(phba);
2643}
2644
2645static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2646{
2647 struct pdu_data_out *pdata_out;
2648 struct pdu_nop_out *pnop_out;
2649 struct be_mem_descriptor *mem_descr;
2650
2651 mem_descr = phba->init_mem;
2652 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2653 pdata_out =
2654 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2655 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2656
2657 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2658 IIOC_SCSI_DATA);
2659
2660 pnop_out =
2661 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2662 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2663
2664 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2665 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2666 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2667 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2668}
2669
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002670static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302671{
2672 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002673 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302674 struct hwi_controller *phwi_ctrlr;
2675 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002676 struct iscsi_wrb *pwrb = NULL;
2677 unsigned int num_cxn_wrbh = 0;
2678 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302679
2680 mem_descr_wrbh = phba->init_mem;
2681 mem_descr_wrbh += HWI_MEM_WRBH;
2682
2683 mem_descr_wrb = phba->init_mem;
2684 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302685 phwi_ctrlr = phba->phwi_ctrlr;
2686
2687 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2688 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302689 pwrb_context->pwrb_handle_base =
2690 kzalloc(sizeof(struct wrb_handle *) *
2691 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002692 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302693 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2694 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002695 goto init_wrb_hndl_failed;
2696 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302697 pwrb_context->pwrb_handle_basestd =
2698 kzalloc(sizeof(struct wrb_handle *) *
2699 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002700 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302701 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2702 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002703 goto init_wrb_hndl_failed;
2704 }
2705 if (!num_cxn_wrbh) {
2706 pwrb_handle =
2707 mem_descr_wrbh->mem_array[idx].virtual_address;
2708 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2709 ((sizeof(struct wrb_handle)) *
2710 phba->params.wrbs_per_cxn));
2711 idx++;
2712 }
2713 pwrb_context->alloc_index = 0;
2714 pwrb_context->wrb_handles_available = 0;
2715 pwrb_context->free_index = 0;
2716
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302717 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302718 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2719 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2720 pwrb_context->pwrb_handle_basestd[j] =
2721 pwrb_handle;
2722 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302723 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302724 pwrb_handle++;
2725 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302726 num_cxn_wrbh--;
2727 }
2728 }
2729 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302730 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302731 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002732 if (!num_cxn_wrb) {
2733 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2734 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2735 ((sizeof(struct iscsi_wrb) *
2736 phba->params.wrbs_per_cxn));
2737 idx++;
2738 }
2739
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302740 if (num_cxn_wrb) {
2741 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2742 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2743 pwrb_handle->pwrb = pwrb;
2744 pwrb++;
2745 }
2746 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302747 }
2748 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002749 return 0;
2750init_wrb_hndl_failed:
2751 for (j = index; j > 0; j--) {
2752 pwrb_context = &phwi_ctrlr->wrb_context[j];
2753 kfree(pwrb_context->pwrb_handle_base);
2754 kfree(pwrb_context->pwrb_handle_basestd);
2755 }
2756 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302757}
2758
2759static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2760{
2761 struct hwi_controller *phwi_ctrlr;
2762 struct hba_parameters *p = &phba->params;
2763 struct hwi_async_pdu_context *pasync_ctx;
2764 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002765 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302766 struct be_mem_descriptor *mem_descr;
2767
2768 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2769 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2770
2771 phwi_ctrlr = phba->phwi_ctrlr;
2772 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2773 mem_descr->mem_array[0].virtual_address;
2774 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2775 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2776
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002777 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2778 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302779
2780 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2781 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2782 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302783 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2784 "BM_%d : hwi_init_async_pdu_ctx"
2785 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2786 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302787 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302788 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2789 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302790
2791 pasync_ctx->async_header.va_base =
2792 mem_descr->mem_array[0].virtual_address;
2793
2794 pasync_ctx->async_header.pa_base.u.a64.address =
2795 mem_descr->mem_array[0].bus_address.u.a64.address;
2796
2797 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2798 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2799 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302800 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2801 "BM_%d : hwi_init_async_pdu_ctx"
2802 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2803 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302804 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302805 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2806 "BM_%d : No Virtual address\n");
2807
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302808 pasync_ctx->async_header.ring_base =
2809 mem_descr->mem_array[0].virtual_address;
2810
2811 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2812 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2813 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302814 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2815 "BM_%d : hwi_init_async_pdu_ctx"
2816 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2817 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302818 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302819 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2820 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302821
2822 pasync_ctx->async_header.handle_base =
2823 mem_descr->mem_array[0].virtual_address;
2824 pasync_ctx->async_header.writables = 0;
2825 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2826
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302827
2828 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2829 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2830 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302831 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2832 "BM_%d : hwi_init_async_pdu_ctx"
2833 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2834 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302835 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302836 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2837 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302838
2839 pasync_ctx->async_data.ring_base =
2840 mem_descr->mem_array[0].virtual_address;
2841
2842 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2843 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2844 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302845 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2846 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302847
2848 pasync_ctx->async_data.handle_base =
2849 mem_descr->mem_array[0].virtual_address;
2850 pasync_ctx->async_data.writables = 0;
2851 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2852
2853 pasync_header_h =
2854 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2855 pasync_data_h =
2856 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2857
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002858 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2859 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2860 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302861 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2862 "BM_%d : hwi_init_async_pdu_ctx"
2863 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2864 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002865 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302866 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2867 "BM_%d : No Virtual address\n");
2868
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002869 idx = 0;
2870 pasync_ctx->async_data.va_base =
2871 mem_descr->mem_array[idx].virtual_address;
2872 pasync_ctx->async_data.pa_base.u.a64.address =
2873 mem_descr->mem_array[idx].bus_address.u.a64.address;
2874
2875 num_async_data = ((mem_descr->mem_array[idx].size) /
2876 phba->params.defpdu_data_sz);
2877 num_per_mem = 0;
2878
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302879 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2880 pasync_header_h->cri = -1;
2881 pasync_header_h->index = (char)index;
2882 INIT_LIST_HEAD(&pasync_header_h->link);
2883 pasync_header_h->pbuffer =
2884 (void *)((unsigned long)
2885 (pasync_ctx->async_header.va_base) +
2886 (p->defpdu_hdr_sz * index));
2887
2888 pasync_header_h->pa.u.a64.address =
2889 pasync_ctx->async_header.pa_base.u.a64.address +
2890 (p->defpdu_hdr_sz * index);
2891
2892 list_add_tail(&pasync_header_h->link,
2893 &pasync_ctx->async_header.free_list);
2894 pasync_header_h++;
2895 pasync_ctx->async_header.free_entries++;
2896 pasync_ctx->async_header.writables++;
2897
2898 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2899 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2900 header_busy_list);
2901 pasync_data_h->cri = -1;
2902 pasync_data_h->index = (char)index;
2903 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002904
2905 if (!num_async_data) {
2906 num_per_mem = 0;
2907 idx++;
2908 pasync_ctx->async_data.va_base =
2909 mem_descr->mem_array[idx].virtual_address;
2910 pasync_ctx->async_data.pa_base.u.a64.address =
2911 mem_descr->mem_array[idx].
2912 bus_address.u.a64.address;
2913
2914 num_async_data = ((mem_descr->mem_array[idx].size) /
2915 phba->params.defpdu_data_sz);
2916 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302917 pasync_data_h->pbuffer =
2918 (void *)((unsigned long)
2919 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002920 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302921
2922 pasync_data_h->pa.u.a64.address =
2923 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002924 (p->defpdu_data_sz * num_per_mem);
2925 num_per_mem++;
2926 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302927
2928 list_add_tail(&pasync_data_h->link,
2929 &pasync_ctx->async_data.free_list);
2930 pasync_data_h++;
2931 pasync_ctx->async_data.free_entries++;
2932 pasync_ctx->async_data.writables++;
2933
2934 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2935 }
2936
2937 pasync_ctx->async_header.host_write_ptr = 0;
2938 pasync_ctx->async_header.ep_read_ptr = -1;
2939 pasync_ctx->async_data.host_write_ptr = 0;
2940 pasync_ctx->async_data.ep_read_ptr = -1;
2941}
2942
2943static int
2944be_sgl_create_contiguous(void *virtual_address,
2945 u64 physical_address, u32 length,
2946 struct be_dma_mem *sgl)
2947{
2948 WARN_ON(!virtual_address);
2949 WARN_ON(!physical_address);
2950 WARN_ON(!length > 0);
2951 WARN_ON(!sgl);
2952
2953 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302954 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302955 sgl->size = length;
2956
2957 return 0;
2958}
2959
2960static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2961{
2962 memset(sgl, 0, sizeof(*sgl));
2963}
2964
2965static void
2966hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2967 struct mem_array *pmem, struct be_dma_mem *sgl)
2968{
2969 if (sgl->va)
2970 be_sgl_destroy_contiguous(sgl);
2971
2972 be_sgl_create_contiguous(pmem->virtual_address,
2973 pmem->bus_address.u.a64.address,
2974 pmem->size, sgl);
2975}
2976
2977static void
2978hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2979 struct mem_array *pmem, struct be_dma_mem *sgl)
2980{
2981 if (sgl->va)
2982 be_sgl_destroy_contiguous(sgl);
2983
2984 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2985 pmem->bus_address.u.a64.address,
2986 pmem->size, sgl);
2987}
2988
2989static int be_fill_queue(struct be_queue_info *q,
2990 u16 len, u16 entry_size, void *vaddress)
2991{
2992 struct be_dma_mem *mem = &q->dma_mem;
2993
2994 memset(q, 0, sizeof(*q));
2995 q->len = len;
2996 q->entry_size = entry_size;
2997 mem->size = len * entry_size;
2998 mem->va = vaddress;
2999 if (!mem->va)
3000 return -ENOMEM;
3001 memset(mem->va, 0, mem->size);
3002 return 0;
3003}
3004
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303005static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303006 struct hwi_context_memory *phwi_context)
3007{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303008 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303009 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303010 struct be_queue_info *eq;
3011 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303012 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303013 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303014
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303015 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
3016 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303017
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303018 if (phba->msix_enabled)
3019 eq_for_mcc = 1;
3020 else
3021 eq_for_mcc = 0;
3022 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
3023 eq = &phwi_context->be_eq[i].q;
3024 mem = &eq->dma_mem;
3025 phwi_context->be_eq[i].phba = phba;
3026 eq_vaddress = pci_alloc_consistent(phba->pcidev,
3027 num_eq_pages * PAGE_SIZE,
3028 &paddr);
3029 if (!eq_vaddress)
3030 goto create_eq_error;
3031
3032 mem->va = eq_vaddress;
3033 ret = be_fill_queue(eq, phba->params.num_eq_entries,
3034 sizeof(struct be_eq_entry), eq_vaddress);
3035 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303036 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3037 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303038 goto create_eq_error;
3039 }
3040
3041 mem->dma = paddr;
3042 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
3043 phwi_context->cur_eqd);
3044 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303045 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3046 "BM_%d : beiscsi_cmd_eq_create"
3047 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303048 goto create_eq_error;
3049 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303050
3051 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3052 "BM_%d : eqid = %d\n",
3053 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303054 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303055 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303056create_eq_error:
John Soni Jose107dfcb2012-10-20 04:42:13 +05303057 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303058 eq = &phwi_context->be_eq[i].q;
3059 mem = &eq->dma_mem;
3060 if (mem->va)
3061 pci_free_consistent(phba->pcidev, num_eq_pages
3062 * PAGE_SIZE,
3063 mem->va, mem->dma);
3064 }
3065 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303066}
3067
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303068static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303069 struct hwi_context_memory *phwi_context)
3070{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303071 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303072 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303073 struct be_queue_info *cq, *eq;
3074 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303075 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303076 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303077 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303078
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303079 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
3080 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303081
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303082 for (i = 0; i < phba->num_cpus; i++) {
3083 cq = &phwi_context->be_cq[i];
3084 eq = &phwi_context->be_eq[i].q;
3085 pbe_eq = &phwi_context->be_eq[i];
3086 pbe_eq->cq = cq;
3087 pbe_eq->phba = phba;
3088 mem = &cq->dma_mem;
3089 cq_vaddress = pci_alloc_consistent(phba->pcidev,
3090 num_cq_pages * PAGE_SIZE,
3091 &paddr);
3092 if (!cq_vaddress)
3093 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303094 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303095 sizeof(struct sol_cqe), cq_vaddress);
3096 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303097 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3098 "BM_%d : be_fill_queue Failed "
3099 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303100 goto create_cq_error;
3101 }
3102
3103 mem->dma = paddr;
3104 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
3105 false, 0);
3106 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303107 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3108 "BM_%d : beiscsi_cmd_eq_create"
3109 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303110 goto create_cq_error;
3111 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303112 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3113 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
3114 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303115 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303116 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303117
3118create_cq_error:
3119 for (i = 0; i < phba->num_cpus; i++) {
3120 cq = &phwi_context->be_cq[i];
3121 mem = &cq->dma_mem;
3122 if (mem->va)
3123 pci_free_consistent(phba->pcidev, num_cq_pages
3124 * PAGE_SIZE,
3125 mem->va, mem->dma);
3126 }
3127 return ret;
3128
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303129}
3130
3131static int
3132beiscsi_create_def_hdr(struct beiscsi_hba *phba,
3133 struct hwi_context_memory *phwi_context,
3134 struct hwi_controller *phwi_ctrlr,
3135 unsigned int def_pdu_ring_sz)
3136{
3137 unsigned int idx;
3138 int ret;
3139 struct be_queue_info *dq, *cq;
3140 struct be_dma_mem *mem;
3141 struct be_mem_descriptor *mem_descr;
3142 void *dq_vaddress;
3143
3144 idx = 0;
3145 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303146 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303147 mem = &dq->dma_mem;
3148 mem_descr = phba->init_mem;
3149 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
3150 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3151 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
3152 sizeof(struct phys_addr),
3153 sizeof(struct phys_addr), dq_vaddress);
3154 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303155 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3156 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303157 return ret;
3158 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303159 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3160 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303161 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
3162 def_pdu_ring_sz,
3163 phba->params.defpdu_hdr_sz);
3164 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303165 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3166 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303167 return ret;
3168 }
3169 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303170 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3171 "BM_%d : iscsi def pdu id is %d\n",
3172 phwi_context->be_def_hdrq.id);
3173
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303174 hwi_post_async_buffers(phba, 1);
3175 return 0;
3176}
3177
3178static int
3179beiscsi_create_def_data(struct beiscsi_hba *phba,
3180 struct hwi_context_memory *phwi_context,
3181 struct hwi_controller *phwi_ctrlr,
3182 unsigned int def_pdu_ring_sz)
3183{
3184 unsigned int idx;
3185 int ret;
3186 struct be_queue_info *dataq, *cq;
3187 struct be_dma_mem *mem;
3188 struct be_mem_descriptor *mem_descr;
3189 void *dq_vaddress;
3190
3191 idx = 0;
3192 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303193 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303194 mem = &dataq->dma_mem;
3195 mem_descr = phba->init_mem;
3196 mem_descr += HWI_MEM_ASYNC_DATA_RING;
3197 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
3198 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
3199 sizeof(struct phys_addr),
3200 sizeof(struct phys_addr), dq_vaddress);
3201 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303202 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3203 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303204 return ret;
3205 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303206 mem->dma = (unsigned long)mem_descr->mem_array[idx].
3207 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303208 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
3209 def_pdu_ring_sz,
3210 phba->params.defpdu_data_sz);
3211 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303212 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3213 "BM_%d be_cmd_create_default_pdu_queue"
3214 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303215 return ret;
3216 }
3217 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303218 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3219 "BM_%d : iscsi def data id is %d\n",
3220 phwi_context->be_def_dataq.id);
3221
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303222 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303223 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3224 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3225
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303226 return 0;
3227}
3228
3229static int
3230beiscsi_post_pages(struct beiscsi_hba *phba)
3231{
3232 struct be_mem_descriptor *mem_descr;
3233 struct mem_array *pm_arr;
3234 unsigned int page_offset, i;
3235 struct be_dma_mem sgl;
3236 int status;
3237
3238 mem_descr = phba->init_mem;
3239 mem_descr += HWI_MEM_SGE;
3240 pm_arr = mem_descr->mem_array;
3241
3242 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3243 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3244 for (i = 0; i < mem_descr->num_elements; i++) {
3245 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3246 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3247 page_offset,
3248 (pm_arr->size / PAGE_SIZE));
3249 page_offset += pm_arr->size / PAGE_SIZE;
3250 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303251 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3252 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303253 return status;
3254 }
3255 pm_arr++;
3256 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303257 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3258 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303259 return 0;
3260}
3261
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303262static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3263{
3264 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003265 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303266 pci_free_consistent(phba->pcidev, mem->size,
3267 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003268 mem->va = NULL;
3269 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303270}
3271
3272static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3273 u16 len, u16 entry_size)
3274{
3275 struct be_dma_mem *mem = &q->dma_mem;
3276
3277 memset(q, 0, sizeof(*q));
3278 q->len = len;
3279 q->entry_size = entry_size;
3280 mem->size = len * entry_size;
3281 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3282 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303283 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303284 memset(mem->va, 0, mem->size);
3285 return 0;
3286}
3287
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303288static int
3289beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3290 struct hwi_context_memory *phwi_context,
3291 struct hwi_controller *phwi_ctrlr)
3292{
3293 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3294 u64 pa_addr_lo;
3295 unsigned int idx, num, i;
3296 struct mem_array *pwrb_arr;
3297 void *wrb_vaddr;
3298 struct be_dma_mem sgl;
3299 struct be_mem_descriptor *mem_descr;
3300 int status;
3301
3302 idx = 0;
3303 mem_descr = phba->init_mem;
3304 mem_descr += HWI_MEM_WRB;
3305 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3306 GFP_KERNEL);
3307 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303308 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3309 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303310 return -ENOMEM;
3311 }
3312 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3313 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3314 num_wrb_rings = mem_descr->mem_array[idx].size /
3315 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3316
3317 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3318 if (num_wrb_rings) {
3319 pwrb_arr[num].virtual_address = wrb_vaddr;
3320 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3321 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3322 sizeof(struct iscsi_wrb);
3323 wrb_vaddr += pwrb_arr[num].size;
3324 pa_addr_lo += pwrb_arr[num].size;
3325 num_wrb_rings--;
3326 } else {
3327 idx++;
3328 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3329 pa_addr_lo = mem_descr->mem_array[idx].\
3330 bus_address.u.a64.address;
3331 num_wrb_rings = mem_descr->mem_array[idx].size /
3332 (phba->params.wrbs_per_cxn *
3333 sizeof(struct iscsi_wrb));
3334 pwrb_arr[num].virtual_address = wrb_vaddr;
3335 pwrb_arr[num].bus_address.u.a64.address\
3336 = pa_addr_lo;
3337 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3338 sizeof(struct iscsi_wrb);
3339 wrb_vaddr += pwrb_arr[num].size;
3340 pa_addr_lo += pwrb_arr[num].size;
3341 num_wrb_rings--;
3342 }
3343 }
3344 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3345 wrb_mem_index = 0;
3346 offset = 0;
3347 size = 0;
3348
3349 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3350 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3351 &phwi_context->be_wrbq[i]);
3352 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303353 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3354 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003355 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303356 return status;
3357 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303358 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3359 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303360 }
3361 kfree(pwrb_arr);
3362 return 0;
3363}
3364
3365static void free_wrb_handles(struct beiscsi_hba *phba)
3366{
3367 unsigned int index;
3368 struct hwi_controller *phwi_ctrlr;
3369 struct hwi_wrb_context *pwrb_context;
3370
3371 phwi_ctrlr = phba->phwi_ctrlr;
3372 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3373 pwrb_context = &phwi_ctrlr->wrb_context[index];
3374 kfree(pwrb_context->pwrb_handle_base);
3375 kfree(pwrb_context->pwrb_handle_basestd);
3376 }
3377}
3378
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303379static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3380{
3381 struct be_queue_info *q;
3382 struct be_ctrl_info *ctrl = &phba->ctrl;
3383
3384 q = &phba->ctrl.mcc_obj.q;
3385 if (q->created)
3386 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3387 be_queue_free(phba, q);
3388
3389 q = &phba->ctrl.mcc_obj.cq;
3390 if (q->created)
3391 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3392 be_queue_free(phba, q);
3393}
3394
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303395static void hwi_cleanup(struct beiscsi_hba *phba)
3396{
3397 struct be_queue_info *q;
3398 struct be_ctrl_info *ctrl = &phba->ctrl;
3399 struct hwi_controller *phwi_ctrlr;
3400 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303401 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303402
3403 phwi_ctrlr = phba->phwi_ctrlr;
3404 phwi_context = phwi_ctrlr->phwi_ctxt;
3405 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3406 q = &phwi_context->be_wrbq[i];
3407 if (q->created)
3408 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3409 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303410 free_wrb_handles(phba);
3411
3412 q = &phwi_context->be_def_hdrq;
3413 if (q->created)
3414 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3415
3416 q = &phwi_context->be_def_dataq;
3417 if (q->created)
3418 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3419
3420 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3421
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303422 for (i = 0; i < (phba->num_cpus); i++) {
3423 q = &phwi_context->be_cq[i];
3424 if (q->created)
3425 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3426 }
3427 if (phba->msix_enabled)
3428 eq_num = 1;
3429 else
3430 eq_num = 0;
3431 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3432 q = &phwi_context->be_eq[i].q;
3433 if (q->created)
3434 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3435 }
3436 be_mcc_queues_destroy(phba);
3437}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303438
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303439static int be_mcc_queues_create(struct beiscsi_hba *phba,
3440 struct hwi_context_memory *phwi_context)
3441{
3442 struct be_queue_info *q, *cq;
3443 struct be_ctrl_info *ctrl = &phba->ctrl;
3444
3445 /* Alloc MCC compl queue */
3446 cq = &phba->ctrl.mcc_obj.cq;
3447 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3448 sizeof(struct be_mcc_compl)))
3449 goto err;
3450 /* Ask BE to create MCC compl queue; */
3451 if (phba->msix_enabled) {
3452 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3453 [phba->num_cpus].q, false, true, 0))
3454 goto mcc_cq_free;
3455 } else {
3456 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3457 false, true, 0))
3458 goto mcc_cq_free;
3459 }
3460
3461 /* Alloc MCC queue */
3462 q = &phba->ctrl.mcc_obj.q;
3463 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3464 goto mcc_cq_destroy;
3465
3466 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303467 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303468 goto mcc_q_free;
3469
3470 return 0;
3471
3472mcc_q_free:
3473 be_queue_free(phba, q);
3474mcc_cq_destroy:
3475 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3476mcc_cq_free:
3477 be_queue_free(phba, cq);
3478err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303479 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303480}
3481
John Soni Jose107dfcb2012-10-20 04:42:13 +05303482/**
3483 * find_num_cpus()- Get the CPU online count
3484 * @phba: ptr to priv structure
3485 *
3486 * CPU count is used for creating EQ.
3487 **/
3488static void find_num_cpus(struct beiscsi_hba *phba)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303489{
3490 int num_cpus = 0;
3491
3492 num_cpus = num_online_cpus();
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303493
John Soni Jose22abeef2012-10-20 04:43:32 +05303494 switch (phba->generation) {
3495 case BE_GEN2:
3496 case BE_GEN3:
3497 phba->num_cpus = (num_cpus > BEISCSI_MAX_NUM_CPUS) ?
3498 BEISCSI_MAX_NUM_CPUS : num_cpus;
3499 break;
3500 case BE_GEN4:
3501 phba->num_cpus = (num_cpus > OC_SKH_MAX_NUM_CPUS) ?
3502 OC_SKH_MAX_NUM_CPUS : num_cpus;
3503 break;
3504 default:
3505 phba->num_cpus = 1;
3506 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303507}
3508
3509static int hwi_init_port(struct beiscsi_hba *phba)
3510{
3511 struct hwi_controller *phwi_ctrlr;
3512 struct hwi_context_memory *phwi_context;
3513 unsigned int def_pdu_ring_sz;
3514 struct be_ctrl_info *ctrl = &phba->ctrl;
3515 int status;
3516
3517 def_pdu_ring_sz =
3518 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3519 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303520 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303521 phwi_context->max_eqd = 0;
3522 phwi_context->min_eqd = 0;
3523 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303524 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303525
3526 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303527 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303528 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3529 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303530 goto error;
3531 }
3532
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303533 status = be_mcc_queues_create(phba, phwi_context);
3534 if (status != 0)
3535 goto error;
3536
3537 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303538 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303539 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3540 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303541 goto error;
3542 }
3543
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303544 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303545 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303546 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3547 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303548 goto error;
3549 }
3550
3551 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3552 def_pdu_ring_sz);
3553 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303554 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3555 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303556 goto error;
3557 }
3558
3559 status = beiscsi_create_def_data(phba, phwi_context,
3560 phwi_ctrlr, def_pdu_ring_sz);
3561 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303562 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3563 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303564 goto error;
3565 }
3566
3567 status = beiscsi_post_pages(phba);
3568 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303569 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3570 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303571 goto error;
3572 }
3573
3574 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3575 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303576 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3577 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303578 goto error;
3579 }
3580
John Soni Jose99bc5d52012-08-20 23:00:18 +05303581 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3582 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303583 return 0;
3584
3585error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303586 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3587 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303588 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003589 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590}
3591
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303592static int hwi_init_controller(struct beiscsi_hba *phba)
3593{
3594 struct hwi_controller *phwi_ctrlr;
3595
3596 phwi_ctrlr = phba->phwi_ctrlr;
3597 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3598 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3599 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303600 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3601 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3602 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303603 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303604 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3605 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3606 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303607 return -ENOMEM;
3608 }
3609
3610 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003611 if (beiscsi_init_wrb_handle(phba))
3612 return -ENOMEM;
3613
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303614 hwi_init_async_pdu_ctx(phba);
3615 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303616 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3617 "BM_%d : hwi_init_controller failed\n");
3618
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303619 return -ENOMEM;
3620 }
3621 return 0;
3622}
3623
3624static void beiscsi_free_mem(struct beiscsi_hba *phba)
3625{
3626 struct be_mem_descriptor *mem_descr;
3627 int i, j;
3628
3629 mem_descr = phba->init_mem;
3630 i = 0;
3631 j = 0;
3632 for (i = 0; i < SE_MEM_MAX; i++) {
3633 for (j = mem_descr->num_elements; j > 0; j--) {
3634 pci_free_consistent(phba->pcidev,
3635 mem_descr->mem_array[j - 1].size,
3636 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303637 (unsigned long)mem_descr->mem_array[j - 1].
3638 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303639 }
3640 kfree(mem_descr->mem_array);
3641 mem_descr++;
3642 }
3643 kfree(phba->init_mem);
3644 kfree(phba->phwi_ctrlr);
3645}
3646
3647static int beiscsi_init_controller(struct beiscsi_hba *phba)
3648{
3649 int ret = -ENOMEM;
3650
3651 ret = beiscsi_get_memory(phba);
3652 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303653 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3654 "BM_%d : beiscsi_dev_probe -"
3655 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303656 return ret;
3657 }
3658
3659 ret = hwi_init_controller(phba);
3660 if (ret)
3661 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303662 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3663 "BM_%d : Return success from beiscsi_init_controller");
3664
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303665 return 0;
3666
3667free_init:
3668 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003669 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303670}
3671
3672static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3673{
3674 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3675 struct sgl_handle *psgl_handle;
3676 struct iscsi_sge *pfrag;
3677 unsigned int arr_index, i, idx;
3678
3679 phba->io_sgl_hndl_avbl = 0;
3680 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303681
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303682 mem_descr_sglh = phba->init_mem;
3683 mem_descr_sglh += HWI_MEM_SGLH;
3684 if (1 == mem_descr_sglh->num_elements) {
3685 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3686 phba->params.ios_per_ctrl,
3687 GFP_KERNEL);
3688 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303689 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3690 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303691 return -ENOMEM;
3692 }
3693 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3694 (phba->params.icds_per_ctrl -
3695 phba->params.ios_per_ctrl),
3696 GFP_KERNEL);
3697 if (!phba->eh_sgl_hndl_base) {
3698 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303699 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3700 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303701 return -ENOMEM;
3702 }
3703 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303704 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3705 "BM_%d : HWI_MEM_SGLH is more than one element."
3706 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303707 return -ENOMEM;
3708 }
3709
3710 arr_index = 0;
3711 idx = 0;
3712 while (idx < mem_descr_sglh->num_elements) {
3713 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3714
3715 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3716 sizeof(struct sgl_handle)); i++) {
3717 if (arr_index < phba->params.ios_per_ctrl) {
3718 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3719 phba->io_sgl_hndl_avbl++;
3720 arr_index++;
3721 } else {
3722 phba->eh_sgl_hndl_base[arr_index -
3723 phba->params.ios_per_ctrl] =
3724 psgl_handle;
3725 arr_index++;
3726 phba->eh_sgl_hndl_avbl++;
3727 }
3728 psgl_handle++;
3729 }
3730 idx++;
3731 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303732 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3733 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3734 "phba->eh_sgl_hndl_avbl=%d\n",
3735 phba->io_sgl_hndl_avbl,
3736 phba->eh_sgl_hndl_avbl);
3737
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303738 mem_descr_sg = phba->init_mem;
3739 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303740 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3741 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3742 mem_descr_sg->num_elements);
3743
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303744 arr_index = 0;
3745 idx = 0;
3746 while (idx < mem_descr_sg->num_elements) {
3747 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3748
3749 for (i = 0;
3750 i < (mem_descr_sg->mem_array[idx].size) /
3751 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3752 i++) {
3753 if (arr_index < phba->params.ios_per_ctrl)
3754 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3755 else
3756 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3757 phba->params.ios_per_ctrl];
3758 psgl_handle->pfrag = pfrag;
3759 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3760 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3761 pfrag += phba->params.num_sge_per_io;
3762 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303763 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303764 }
3765 idx++;
3766 }
3767 phba->io_sgl_free_index = 0;
3768 phba->io_sgl_alloc_index = 0;
3769 phba->eh_sgl_free_index = 0;
3770 phba->eh_sgl_alloc_index = 0;
3771 return 0;
3772}
3773
3774static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3775{
3776 int i, new_cid;
3777
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05303778 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303779 GFP_KERNEL);
3780 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303781 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3782 "BM_%d : Failed to allocate memory in "
3783 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303784 return -ENOMEM;
3785 }
Jayamohan Kallickalc24622882010-01-05 05:05:34 +05303786 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303787 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3788 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303789 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3790 "BM_%d : Failed to allocate memory in "
3791 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303792 kfree(phba->cid_array);
3793 return -ENOMEM;
3794 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303795 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303796 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3797 phba->cid_array[i] = new_cid;
3798 new_cid += 2;
3799 }
3800 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3801 return 0;
3802}
3803
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303804static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303805{
3806 struct be_ctrl_info *ctrl = &phba->ctrl;
3807 struct hwi_controller *phwi_ctrlr;
3808 struct hwi_context_memory *phwi_context;
3809 struct be_queue_info *eq;
3810 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303811 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303812 u32 enabled;
3813
3814 phwi_ctrlr = phba->phwi_ctrlr;
3815 phwi_context = phwi_ctrlr->phwi_ctxt;
3816
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303817 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3818 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3819 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303820
3821 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3822 if (!enabled) {
3823 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303824 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3825 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303826 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003827 }
3828
3829 if (!phba->msix_enabled) {
3830 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303831 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3832 "BM_%d : eq->id=%d\n", eq->id);
3833
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003834 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3835 } else {
3836 for (i = 0; i <= phba->num_cpus; i++) {
3837 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303838 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3839 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303840 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3841 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303842 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303843}
3844
3845static void hwi_disable_intr(struct beiscsi_hba *phba)
3846{
3847 struct be_ctrl_info *ctrl = &phba->ctrl;
3848
3849 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3850 u32 reg = ioread32(addr);
3851
3852 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3853 if (enabled) {
3854 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3855 iowrite32(reg, addr);
3856 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303857 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3858 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303859}
3860
John Soni Jose9aef4202012-08-20 23:00:08 +05303861/**
3862 * beiscsi_get_boot_info()- Get the boot session info
3863 * @phba: The device priv structure instance
3864 *
3865 * Get the boot target info and store in driver priv structure
3866 *
3867 * return values
3868 * Success: 0
3869 * Failure: Non-Zero Value
3870 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303871static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3872{
Mike Christie0e438952012-04-03 23:41:51 -05003873 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303874 struct be_mcc_wrb *wrb;
3875 struct be_dma_mem nonemb_cmd;
3876 unsigned int tag, wrb_num;
3877 unsigned short status, extd_status;
John Soni Jose9aef4202012-08-20 23:00:08 +05303878 unsigned int s_handle;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303879 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003880 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303881
John Soni Jose9aef4202012-08-20 23:00:08 +05303882 /* Get the session handle of the boot target */
3883 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3884 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303885 beiscsi_log(phba, KERN_ERR,
3886 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3887 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303888 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303889 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303890 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3891 sizeof(*session_resp),
3892 &nonemb_cmd.dma);
3893 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303894 beiscsi_log(phba, KERN_ERR,
3895 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3896 "BM_%d : Failed to allocate memory for"
3897 "beiscsi_get_session_info\n");
3898
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303899 return -ENOMEM;
3900 }
3901
3902 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303903 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003904 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303905 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303906 beiscsi_log(phba, KERN_ERR,
3907 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3908 "BM_%d : beiscsi_get_session_info"
3909 " Failed\n");
3910
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303911 goto boot_freemem;
3912 } else
3913 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3914 phba->ctrl.mcc_numtag[tag]);
3915
3916 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3917 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3918 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3919 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303920 beiscsi_log(phba, KERN_ERR,
3921 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3922 "BM_%d : beiscsi_get_session_info Failed"
3923 " status = %d extd_status = %d\n",
3924 status, extd_status);
3925
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303926 free_mcc_tag(&phba->ctrl, tag);
3927 goto boot_freemem;
3928 }
3929 wrb = queue_get_wrb(mccq, wrb_num);
3930 free_mcc_tag(&phba->ctrl, tag);
3931 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003932
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303933 memcpy(&phba->boot_sess, &session_resp->session_info,
3934 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003935 ret = 0;
3936
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303937boot_freemem:
3938 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3939 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003940 return ret;
3941}
3942
3943static void beiscsi_boot_release(void *data)
3944{
3945 struct beiscsi_hba *phba = data;
3946
3947 scsi_host_put(phba->shost);
3948}
3949
3950static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3951{
3952 struct iscsi_boot_kobj *boot_kobj;
3953
3954 /* get boot info using mgmt cmd */
3955 if (beiscsi_get_boot_info(phba))
3956 /* Try to see if we can carry on without this */
3957 return 0;
3958
3959 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3960 if (!phba->boot_kset)
3961 return -ENOMEM;
3962
3963 /* get a ref because the show function will ref the phba */
3964 if (!scsi_host_get(phba->shost))
3965 goto free_kset;
3966 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3967 beiscsi_show_boot_tgt_info,
3968 beiscsi_tgt_get_attr_visibility,
3969 beiscsi_boot_release);
3970 if (!boot_kobj)
3971 goto put_shost;
3972
3973 if (!scsi_host_get(phba->shost))
3974 goto free_kset;
3975 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3976 beiscsi_show_boot_ini_info,
3977 beiscsi_ini_get_attr_visibility,
3978 beiscsi_boot_release);
3979 if (!boot_kobj)
3980 goto put_shost;
3981
3982 if (!scsi_host_get(phba->shost))
3983 goto free_kset;
3984 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3985 beiscsi_show_boot_eth_info,
3986 beiscsi_eth_get_attr_visibility,
3987 beiscsi_boot_release);
3988 if (!boot_kobj)
3989 goto put_shost;
3990 return 0;
3991
3992put_shost:
3993 scsi_host_put(phba->shost);
3994free_kset:
3995 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303996 return -ENOMEM;
3997}
3998
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303999static int beiscsi_init_port(struct beiscsi_hba *phba)
4000{
4001 int ret;
4002
4003 ret = beiscsi_init_controller(phba);
4004 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304005 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4006 "BM_%d : beiscsi_dev_probe - Failed in"
4007 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304008 return ret;
4009 }
4010 ret = beiscsi_init_sgl_handle(phba);
4011 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304012 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4013 "BM_%d : beiscsi_dev_probe - Failed in"
4014 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304015 goto do_cleanup_ctrlr;
4016 }
4017
4018 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304019 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4020 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304021 kfree(phba->io_sgl_hndl_base);
4022 kfree(phba->eh_sgl_hndl_base);
4023 goto do_cleanup_ctrlr;
4024 }
4025
4026 return ret;
4027
4028do_cleanup_ctrlr:
4029 hwi_cleanup(phba);
4030 return ret;
4031}
4032
4033static void hwi_purge_eq(struct beiscsi_hba *phba)
4034{
4035 struct hwi_controller *phwi_ctrlr;
4036 struct hwi_context_memory *phwi_context;
4037 struct be_queue_info *eq;
4038 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304039 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304040 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304041
4042 phwi_ctrlr = phba->phwi_ctrlr;
4043 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304044 if (phba->msix_enabled)
4045 eq_msix = 1;
4046 else
4047 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304048
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304049 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
4050 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304051 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304052 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304053 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
4054 & EQE_VALID_MASK) {
4055 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
4056 queue_tail_inc(eq);
4057 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304058 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304059 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304060
4061 if (num_processed)
4062 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304063 }
4064}
4065
4066static void beiscsi_clean_port(struct beiscsi_hba *phba)
4067{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05304068 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304069
4070 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
4071 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05304072 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
4073 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304074
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304075 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304076 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304077 kfree(phba->io_sgl_hndl_base);
4078 kfree(phba->eh_sgl_hndl_base);
4079 kfree(phba->cid_array);
4080 kfree(phba->ep_array);
4081}
4082
John Soni Josed629c472012-10-20 04:42:00 +05304083/**
4084 * beiscsi_cleanup_task()- Free driver resources of the task
4085 * @task: ptr to the iscsi task
4086 *
4087 **/
Mike Christie1282ab72012-04-18 03:06:00 -05004088static void beiscsi_cleanup_task(struct iscsi_task *task)
4089{
4090 struct beiscsi_io_task *io_task = task->dd_data;
4091 struct iscsi_conn *conn = task->conn;
4092 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4093 struct beiscsi_hba *phba = beiscsi_conn->phba;
4094 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4095 struct hwi_wrb_context *pwrb_context;
4096 struct hwi_controller *phwi_ctrlr;
4097
4098 phwi_ctrlr = phba->phwi_ctrlr;
4099 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
4100 - phba->fw_config.iscsi_cid_start];
4101
4102 if (io_task->cmd_bhs) {
4103 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4104 io_task->bhs_pa.u.a64.address);
4105 io_task->cmd_bhs = NULL;
4106 }
4107
4108 if (task->sc) {
4109 if (io_task->pwrb_handle) {
4110 free_wrb_handle(phba, pwrb_context,
4111 io_task->pwrb_handle);
4112 io_task->pwrb_handle = NULL;
4113 }
4114
4115 if (io_task->psgl_handle) {
4116 spin_lock(&phba->io_sgl_lock);
4117 free_io_sgl_handle(phba, io_task->psgl_handle);
4118 spin_unlock(&phba->io_sgl_lock);
4119 io_task->psgl_handle = NULL;
4120 }
4121 } else {
4122 if (!beiscsi_conn->login_in_progress) {
4123 if (io_task->pwrb_handle) {
4124 free_wrb_handle(phba, pwrb_context,
4125 io_task->pwrb_handle);
4126 io_task->pwrb_handle = NULL;
4127 }
4128 if (io_task->psgl_handle) {
4129 spin_lock(&phba->mgmt_sgl_lock);
4130 free_mgmt_sgl_handle(phba,
4131 io_task->psgl_handle);
4132 spin_unlock(&phba->mgmt_sgl_lock);
4133 io_task->psgl_handle = NULL;
4134 }
John Soni Josed629c472012-10-20 04:42:00 +05304135 if (io_task->mtask_addr) {
4136 pci_unmap_single(phba->pcidev,
4137 io_task->mtask_addr,
4138 io_task->mtask_data_count,
4139 PCI_DMA_TODEVICE);
4140 io_task->mtask_addr = 0;
4141 }
Mike Christie1282ab72012-04-18 03:06:00 -05004142 }
4143 }
4144}
4145
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304146void
4147beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
4148 struct beiscsi_offload_params *params)
4149{
4150 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304151 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05004152 struct iscsi_task *task = beiscsi_conn->task;
4153 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304154 u32 doorbell = 0;
4155
4156 /*
4157 * We can always use 0 here because it is reserved by libiscsi for
4158 * login/startup related tasks.
4159 */
Mike Christie1282ab72012-04-18 03:06:00 -05004160 beiscsi_conn->login_in_progress = 0;
4161 spin_lock_bh(&session->lock);
4162 beiscsi_cleanup_task(task);
4163 spin_unlock_bh(&session->lock);
4164
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304165 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05304166 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304167
John Soni Joseacb96932012-10-20 04:44:35 +05304168 /* Check for the adapter family */
4169 if (chip_skh_r(phba->pcidev))
4170 beiscsi_offload_cxn_v2(params, pwrb_handle);
4171 else
4172 beiscsi_offload_cxn_v0(params, pwrb_handle,
4173 phba->init_mem);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304174
John Soni Joseacb96932012-10-20 04:44:35 +05304175 be_dws_le_to_cpu(pwrb_handle->pwrb,
4176 sizeof(struct iscsi_target_context_update_wrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304177
4178 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304179 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304180 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304181 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4182
4183 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4184}
4185
4186static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4187 int *index, int *age)
4188{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304189 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304190 if (age)
4191 *age = conn->session->age;
4192}
4193
4194/**
4195 * beiscsi_alloc_pdu - allocates pdu and related resources
4196 * @task: libiscsi task
4197 * @opcode: opcode of pdu for task
4198 *
4199 * This is called with the session lock held. It will allocate
4200 * the wrb and sgl if needed for the command. And it will prep
4201 * the pdu's itt. beiscsi_parse_pdu will later translate
4202 * the pdu itt to the libiscsi task itt.
4203 */
4204static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4205{
4206 struct beiscsi_io_task *io_task = task->dd_data;
4207 struct iscsi_conn *conn = task->conn;
4208 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4209 struct beiscsi_hba *phba = beiscsi_conn->phba;
4210 struct hwi_wrb_context *pwrb_context;
4211 struct hwi_controller *phwi_ctrlr;
4212 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304213 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4214 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304215
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304216 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004217 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304218 if (!io_task->cmd_bhs)
4219 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304220 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304221 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304222 io_task->conn = beiscsi_conn;
4223
4224 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4225 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304226 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004227 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304228
4229 if (task->sc) {
4230 spin_lock(&phba->io_sgl_lock);
4231 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4232 spin_unlock(&phba->io_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304233 if (!io_task->psgl_handle) {
4234 beiscsi_log(phba, KERN_ERR,
4235 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4236 "BM_%d : Alloc of IO_SGL_ICD Failed"
4237 "for the CID : %d\n",
4238 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304239 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304240 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304241 io_task->pwrb_handle = alloc_wrb_handle(phba,
4242 beiscsi_conn->beiscsi_conn_cid -
4243 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304244 if (!io_task->pwrb_handle) {
4245 beiscsi_log(phba, KERN_ERR,
4246 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4247 "BM_%d : Alloc of WRB_HANDLE Failed"
4248 "for the CID : %d\n",
4249 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304250 goto free_io_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304251 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304252 } else {
4253 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304254 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304255 if (!beiscsi_conn->login_in_progress) {
4256 spin_lock(&phba->mgmt_sgl_lock);
4257 io_task->psgl_handle = (struct sgl_handle *)
4258 alloc_mgmt_sgl_handle(phba);
4259 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304260 if (!io_task->psgl_handle) {
4261 beiscsi_log(phba, KERN_ERR,
4262 BEISCSI_LOG_IO |
4263 BEISCSI_LOG_CONFIG,
4264 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4265 "for the CID : %d\n",
4266 beiscsi_conn->
4267 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304268 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304269 }
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304270
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304271 beiscsi_conn->login_in_progress = 1;
4272 beiscsi_conn->plogin_sgl_handle =
4273 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304274 io_task->pwrb_handle =
4275 alloc_wrb_handle(phba,
4276 beiscsi_conn->beiscsi_conn_cid -
4277 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304278 if (!io_task->pwrb_handle) {
4279 beiscsi_log(phba, KERN_ERR,
4280 BEISCSI_LOG_IO |
4281 BEISCSI_LOG_CONFIG,
4282 "BM_%d : Alloc of WRB_HANDLE Failed"
4283 "for the CID : %d\n",
4284 beiscsi_conn->
4285 beiscsi_conn_cid);
4286 goto free_mgmt_hndls;
4287 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304288 beiscsi_conn->plogin_wrb_handle =
4289 io_task->pwrb_handle;
4290
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304291 } else {
4292 io_task->psgl_handle =
4293 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304294 io_task->pwrb_handle =
4295 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304296 }
Mike Christie1282ab72012-04-18 03:06:00 -05004297 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304298 } else {
4299 spin_lock(&phba->mgmt_sgl_lock);
4300 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4301 spin_unlock(&phba->mgmt_sgl_lock);
John Soni Jose8359c792012-10-20 04:43:03 +05304302 if (!io_task->psgl_handle) {
4303 beiscsi_log(phba, KERN_ERR,
4304 BEISCSI_LOG_IO |
4305 BEISCSI_LOG_CONFIG,
4306 "BM_%d : Alloc of MGMT_SGL_ICD Failed"
4307 "for the CID : %d\n",
4308 beiscsi_conn->
4309 beiscsi_conn_cid);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304310 goto free_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304311 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304312 io_task->pwrb_handle =
4313 alloc_wrb_handle(phba,
4314 beiscsi_conn->beiscsi_conn_cid -
4315 phba->fw_config.iscsi_cid_start);
John Soni Jose8359c792012-10-20 04:43:03 +05304316 if (!io_task->pwrb_handle) {
4317 beiscsi_log(phba, KERN_ERR,
4318 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4319 "BM_%d : Alloc of WRB_HANDLE Failed"
4320 "for the CID : %d\n",
4321 beiscsi_conn->beiscsi_conn_cid);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304322 goto free_mgmt_hndls;
John Soni Jose8359c792012-10-20 04:43:03 +05304323 }
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304324
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304325 }
4326 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304327 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4328 wrb_index << 16) | (unsigned int)
4329 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304330 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304331
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304332 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4333 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304334
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304335free_io_hndls:
4336 spin_lock(&phba->io_sgl_lock);
4337 free_io_sgl_handle(phba, io_task->psgl_handle);
4338 spin_unlock(&phba->io_sgl_lock);
4339 goto free_hndls;
4340free_mgmt_hndls:
4341 spin_lock(&phba->mgmt_sgl_lock);
4342 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4343 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304344free_hndls:
4345 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304346 pwrb_context = &phwi_ctrlr->wrb_context[
4347 beiscsi_conn->beiscsi_conn_cid -
4348 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304349 if (io_task->pwrb_handle)
4350 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304351 io_task->pwrb_handle = NULL;
4352 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4353 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004354 io_task->cmd_bhs = NULL;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304355 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304356}
John Soni Jose09a10932012-10-20 04:44:23 +05304357int beiscsi_iotask_v2(struct iscsi_task *task, struct scatterlist *sg,
4358 unsigned int num_sg, unsigned int xferlen,
4359 unsigned int writedir)
4360{
4361
4362 struct beiscsi_io_task *io_task = task->dd_data;
4363 struct iscsi_conn *conn = task->conn;
4364 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4365 struct beiscsi_hba *phba = beiscsi_conn->phba;
4366 struct iscsi_wrb *pwrb = NULL;
4367 unsigned int doorbell = 0;
4368
4369 pwrb = io_task->pwrb_handle->pwrb;
4370 memset(pwrb, 0, sizeof(*pwrb));
4371
4372 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4373 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4374
4375 if (writedir) {
4376 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4377 INI_WR_CMD);
4378 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 1);
4379 } else {
4380 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, type, pwrb,
4381 INI_RD_CMD);
4382 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, dsp, pwrb, 0);
4383 }
4384
4385 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb_v2,
4386 type, pwrb);
4387
4388 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, lun, pwrb,
4389 cpu_to_be16(*(unsigned short *)
4390 &io_task->cmd_bhs->iscsi_hdr.lun));
4391 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb, xferlen);
4392 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4393 io_task->pwrb_handle->wrb_index);
4394 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4395 be32_to_cpu(task->cmdsn));
4396 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4397 io_task->psgl_handle->sgl_index);
4398
4399 hwi_write_sgl_v2(pwrb, sg, num_sg, io_task);
4400 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4401 io_task->pwrb_handle->nxt_wrb_index);
4402
4403 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4404
4405 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
4406 doorbell |= (io_task->pwrb_handle->wrb_index &
4407 DB_DEF_PDU_WRB_INDEX_MASK) <<
4408 DB_DEF_PDU_WRB_INDEX_SHIFT;
4409 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4410 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4411 return 0;
4412}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304413
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304414static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4415 unsigned int num_sg, unsigned int xferlen,
4416 unsigned int writedir)
4417{
4418
4419 struct beiscsi_io_task *io_task = task->dd_data;
4420 struct iscsi_conn *conn = task->conn;
4421 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4422 struct beiscsi_hba *phba = beiscsi_conn->phba;
4423 struct iscsi_wrb *pwrb = NULL;
4424 unsigned int doorbell = 0;
4425
4426 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304427 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4428 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4429
4430 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304431 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4432 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304433 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304434 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304435 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4436 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304437 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4438 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304439
John Soni Jose09a10932012-10-20 04:44:23 +05304440 io_task->wrb_type = AMAP_GET_BITS(struct amap_iscsi_wrb,
4441 type, pwrb);
4442
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304443 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004444 cpu_to_be16(*(unsigned short *)
4445 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304446 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4447 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4448 io_task->pwrb_handle->wrb_index);
4449 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4450 be32_to_cpu(task->cmdsn));
4451 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4452 io_task->psgl_handle->sgl_index);
4453
4454 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4455
4456 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4457 io_task->pwrb_handle->nxt_wrb_index);
4458 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4459
4460 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304461 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304462 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4463 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4464
4465 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4466 return 0;
4467}
4468
4469static int beiscsi_mtask(struct iscsi_task *task)
4470{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304471 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304472 struct iscsi_conn *conn = task->conn;
4473 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4474 struct beiscsi_hba *phba = beiscsi_conn->phba;
4475 struct iscsi_wrb *pwrb = NULL;
4476 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304477 unsigned int cid;
John Soni Jose09a10932012-10-20 04:44:23 +05304478 unsigned int pwrb_typeoffset = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304479
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304480 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304481 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304482 memset(pwrb, 0, sizeof(*pwrb));
John Soni Jose09a10932012-10-20 04:44:23 +05304483
4484 if (chip_skh_r(phba->pcidev)) {
4485 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, cmdsn_itt, pwrb,
4486 be32_to_cpu(task->cmdsn));
4487 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, wrb_idx, pwrb,
4488 io_task->pwrb_handle->wrb_index);
4489 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, sgl_idx, pwrb,
4490 io_task->psgl_handle->sgl_index);
4491 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, r2t_exp_dtl, pwrb,
4492 task->data_count);
4493 AMAP_SET_BITS(struct amap_iscsi_wrb_v2, ptr2nextwrb, pwrb,
4494 io_task->pwrb_handle->nxt_wrb_index);
4495 pwrb_typeoffset = SKH_WRB_TYPE_OFFSET;
4496 } else {
4497 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4498 be32_to_cpu(task->cmdsn));
4499 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4500 io_task->pwrb_handle->wrb_index);
4501 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4502 io_task->psgl_handle->sgl_index);
4503 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
4504 task->data_count);
4505 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4506 io_task->pwrb_handle->nxt_wrb_index);
4507 pwrb_typeoffset = BE_WRB_TYPE_OFFSET;
4508 }
4509
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304510
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304511 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4512 case ISCSI_OP_LOGIN:
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304513 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
John Soni Jose09a10932012-10-20 04:44:23 +05304514 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304515 hwi_write_buffer(pwrb, task);
4516 break;
4517 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004518 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
John Soni Jose09a10932012-10-20 04:44:23 +05304519 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
4520 if (chip_skh_r(phba->pcidev))
4521 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4522 dmsg, pwrb, 1);
4523 else
4524 AMAP_SET_BITS(struct amap_iscsi_wrb,
4525 dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004526 } else {
John Soni Jose09a10932012-10-20 04:44:23 +05304527 ADAPTER_SET_WRB_TYPE(pwrb, INI_RD_CMD, pwrb_typeoffset);
4528 if (chip_skh_r(phba->pcidev))
4529 AMAP_SET_BITS(struct amap_iscsi_wrb_v2,
4530 dmsg, pwrb, 0);
4531 else
4532 AMAP_SET_BITS(struct amap_iscsi_wrb,
4533 dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004534 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304535 hwi_write_buffer(pwrb, task);
4536 break;
4537 case ISCSI_OP_TEXT:
John Soni Jose09a10932012-10-20 04:44:23 +05304538 ADAPTER_SET_WRB_TYPE(pwrb, TGT_DM_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304539 hwi_write_buffer(pwrb, task);
4540 break;
4541 case ISCSI_OP_SCSI_TMFUNC:
John Soni Jose09a10932012-10-20 04:44:23 +05304542 ADAPTER_SET_WRB_TYPE(pwrb, INI_TMF_CMD, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304543 hwi_write_buffer(pwrb, task);
4544 break;
4545 case ISCSI_OP_LOGOUT:
John Soni Jose09a10932012-10-20 04:44:23 +05304546 ADAPTER_SET_WRB_TYPE(pwrb, HWH_TYPE_LOGOUT, pwrb_typeoffset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304547 hwi_write_buffer(pwrb, task);
4548 break;
4549
4550 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304551 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4552 "BM_%d : opcode =%d Not supported\n",
4553 task->hdr->opcode & ISCSI_OPCODE_MASK);
4554
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304555 return -EINVAL;
4556 }
4557
John Soni Jose09a10932012-10-20 04:44:23 +05304558 /* Set the task type */
4559 io_task->wrb_type = (chip_skh_r(phba->pcidev)) ?
4560 AMAP_GET_BITS(struct amap_iscsi_wrb_v2, type, pwrb) :
4561 AMAP_GET_BITS(struct amap_iscsi_wrb, type, pwrb);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304562
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304563 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304564 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304565 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4566 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4567 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4568 return 0;
4569}
4570
4571static int beiscsi_task_xmit(struct iscsi_task *task)
4572{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304573 struct beiscsi_io_task *io_task = task->dd_data;
4574 struct scsi_cmnd *sc = task->sc;
John Soni Jose09a10932012-10-20 04:44:23 +05304575 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304576 struct scatterlist *sg;
4577 int num_sg;
4578 unsigned int writedir = 0, xferlen = 0;
4579
John Soni Jose09a10932012-10-20 04:44:23 +05304580 phba = ((struct beiscsi_conn *)task->conn->dd_data)->phba;
4581
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304582 if (!sc)
4583 return beiscsi_mtask(task);
4584
4585 io_task->scsi_cmnd = sc;
4586 num_sg = scsi_dma_map(sc);
4587 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304588 struct iscsi_conn *conn = task->conn;
4589 struct beiscsi_hba *phba = NULL;
4590
4591 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4592 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4593 "BM_%d : scsi_dma_map Failed\n");
4594
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304595 return num_sg;
4596 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304597 xferlen = scsi_bufflen(sc);
4598 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304599 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304600 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304601 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304602 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304603
John Soni Jose09a10932012-10-20 04:44:23 +05304604 return phba->iotask_fn(task, sg, num_sg, xferlen, writedir);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304605}
4606
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004607/**
4608 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4609 * @job: job to handle
4610 */
4611static int beiscsi_bsg_request(struct bsg_job *job)
4612{
4613 struct Scsi_Host *shost;
4614 struct beiscsi_hba *phba;
4615 struct iscsi_bsg_request *bsg_req = job->request;
4616 int rc = -EINVAL;
4617 unsigned int tag;
4618 struct be_dma_mem nonemb_cmd;
4619 struct be_cmd_resp_hdr *resp;
4620 struct iscsi_bsg_reply *bsg_reply = job->reply;
4621 unsigned short status, extd_status;
4622
4623 shost = iscsi_job_to_shost(job);
4624 phba = iscsi_host_priv(shost);
4625
4626 switch (bsg_req->msgcode) {
4627 case ISCSI_BSG_HST_VENDOR:
4628 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4629 job->request_payload.payload_len,
4630 &nonemb_cmd.dma);
4631 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304632 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4633 "BM_%d : Failed to allocate memory for "
4634 "beiscsi_bsg_request\n");
John Soni Jose8359c792012-10-20 04:43:03 +05304635 return -ENOMEM;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004636 }
4637 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4638 &nonemb_cmd);
4639 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304640 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304641 "BM_%d : MBX Tag Allocation Failed\n");
John Soni Jose99bc5d52012-08-20 23:00:18 +05304642
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004643 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4644 nonemb_cmd.va, nonemb_cmd.dma);
4645 return -EAGAIN;
4646 } else
4647 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
4648 phba->ctrl.mcc_numtag[tag]);
4649 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4650 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4651 free_mcc_tag(&phba->ctrl, tag);
4652 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4653 sg_copy_from_buffer(job->reply_payload.sg_list,
4654 job->reply_payload.sg_cnt,
4655 nonemb_cmd.va, (resp->response_length
4656 + sizeof(*resp)));
4657 bsg_reply->reply_payload_rcv_len = resp->response_length;
4658 bsg_reply->result = status;
4659 bsg_job_done(job, bsg_reply->result,
4660 bsg_reply->reply_payload_rcv_len);
4661 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4662 nonemb_cmd.va, nonemb_cmd.dma);
4663 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304664 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
John Soni Jose8359c792012-10-20 04:43:03 +05304665 "BM_%d : MBX Cmd Failed"
John Soni Jose99bc5d52012-08-20 23:00:18 +05304666 " status = %d extd_status = %d\n",
4667 status, extd_status);
4668
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004669 return -EIO;
John Soni Jose8359c792012-10-20 04:43:03 +05304670 } else {
4671 rc = 0;
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004672 }
4673 break;
4674
4675 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304676 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4677 "BM_%d : Unsupported bsg command: 0x%x\n",
4678 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004679 break;
4680 }
4681
4682 return rc;
4683}
4684
John Soni Jose99bc5d52012-08-20 23:00:18 +05304685void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4686{
4687 /* Set the logging parameter */
4688 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4689}
4690
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304691/*
4692 * beiscsi_quiesce()- Cleanup Driver resources
4693 * @phba: Instance Priv structure
4694 *
4695 * Free the OS and HW resources held by the driver
4696 **/
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004697static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304698{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304699 struct hwi_controller *phwi_ctrlr;
4700 struct hwi_context_memory *phwi_context;
4701 struct be_eq_obj *pbe_eq;
4702 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304703
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304704 phwi_ctrlr = phba->phwi_ctrlr;
4705 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304706 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304707 if (phba->msix_enabled) {
4708 for (i = 0; i <= phba->num_cpus; i++) {
4709 msix_vec = phba->msix_entries[i].vector;
4710 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004711 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304712 }
4713 } else
4714 if (phba->pcidev->irq)
4715 free_irq(phba->pcidev->irq, phba);
4716 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304717 destroy_workqueue(phba->wq);
4718 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304719 for (i = 0; i < phba->num_cpus; i++) {
4720 pbe_eq = &phwi_context->be_eq[i];
4721 blk_iopoll_disable(&pbe_eq->iopoll);
4722 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304723
4724 beiscsi_clean_port(phba);
4725 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304726
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304727 beiscsi_unmap_pci_function(phba);
4728 pci_free_consistent(phba->pcidev,
4729 phba->ctrl.mbox_mem_alloced.size,
4730 phba->ctrl.mbox_mem_alloced.va,
4731 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004732}
4733
4734static void beiscsi_remove(struct pci_dev *pcidev)
4735{
4736
4737 struct beiscsi_hba *phba = NULL;
4738
4739 phba = pci_get_drvdata(pcidev);
4740 if (!phba) {
4741 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4742 return;
4743 }
4744
Mike Christie0e438952012-04-03 23:41:51 -05004745 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004746 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004747 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304748 iscsi_host_remove(phba->shost);
4749 pci_dev_put(phba->pcidev);
4750 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004751 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304752}
4753
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004754static void beiscsi_shutdown(struct pci_dev *pcidev)
4755{
4756
4757 struct beiscsi_hba *phba = NULL;
4758
4759 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4760 if (!phba) {
4761 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4762 return;
4763 }
4764
4765 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004766 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004767}
4768
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304769static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4770{
4771 int i, status;
4772
4773 for (i = 0; i <= phba->num_cpus; i++)
4774 phba->msix_entries[i].entry = i;
4775
4776 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4777 (phba->num_cpus + 1));
4778 if (!status)
4779 phba->msix_enabled = true;
4780
4781 return;
4782}
4783
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304784static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4785 const struct pci_device_id *id)
4786{
4787 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304788 struct hwi_controller *phwi_ctrlr;
4789 struct hwi_context_memory *phwi_context;
4790 struct be_eq_obj *pbe_eq;
John Soni Jose107dfcb2012-10-20 04:42:13 +05304791 int ret, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304792
4793 ret = beiscsi_enable_pci(pcidev);
4794 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304795 dev_err(&pcidev->dev,
4796 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304797 return ret;
4798 }
4799
4800 phba = beiscsi_hba_alloc(pcidev);
4801 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304802 dev_err(&pcidev->dev,
4803 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304804 goto disable_pci;
4805 }
4806
John Soni Jose99bc5d52012-08-20 23:00:18 +05304807 /* Initialize Driver configuration Paramters */
4808 beiscsi_hba_attrs_init(phba);
4809
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304810 switch (pcidev->device) {
4811 case BE_DEVICE_ID1:
4812 case OC_DEVICE_ID1:
4813 case OC_DEVICE_ID2:
4814 phba->generation = BE_GEN2;
John Soni Jose09a10932012-10-20 04:44:23 +05304815 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304816 break;
4817 case BE_DEVICE_ID2:
4818 case OC_DEVICE_ID3:
4819 phba->generation = BE_GEN3;
John Soni Jose09a10932012-10-20 04:44:23 +05304820 phba->iotask_fn = beiscsi_iotask;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304821 break;
John Soni Jose139a1b12012-10-20 04:43:20 +05304822 case OC_SKH_ID1:
4823 phba->generation = BE_GEN4;
John Soni Jose09a10932012-10-20 04:44:23 +05304824 phba->iotask_fn = beiscsi_iotask_v2;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304825 default:
4826 phba->generation = 0;
4827 }
4828
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304829 if (enable_msix)
John Soni Jose107dfcb2012-10-20 04:42:13 +05304830 find_num_cpus(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304831 else
John Soni Jose107dfcb2012-10-20 04:42:13 +05304832 phba->num_cpus = 1;
4833
John Soni Jose99bc5d52012-08-20 23:00:18 +05304834 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4835 "BM_%d : num_cpus = %d\n",
4836 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304837
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004838 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304839 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004840 if (!phba->msix_enabled)
4841 phba->num_cpus = 1;
4842 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304843 ret = be_ctrl_init(phba, pcidev);
4844 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304845 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4846 "BM_%d : beiscsi_dev_probe-"
4847 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304848 goto hba_free;
4849 }
4850
John Soni Jose4d4d1ef2012-10-20 04:42:37 +05304851 ret = beiscsi_cmd_reset_function(phba);
4852 if (ret) {
4853 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4854 "BM_%d : Reset Failed. Aborting Crashdump\n");
4855 goto hba_free;
4856 }
4857 ret = be_chk_reset_complete(phba);
4858 if (ret) {
4859 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4860 "BM_%d : Failed to get out of reset."
4861 "Aborting Crashdump\n");
4862 goto hba_free;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304863 }
4864
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304865 spin_lock_init(&phba->io_sgl_lock);
4866 spin_lock_init(&phba->mgmt_sgl_lock);
4867 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304868 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4869 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304870 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4871 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304872 goto free_port;
4873 }
4874 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304875 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304876 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304877 ret = beiscsi_init_port(phba);
4878 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304879 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4880 "BM_%d : beiscsi_dev_probe-"
4881 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304882 goto free_port;
4883 }
4884
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304885 for (i = 0; i < MAX_MCC_CMD ; i++) {
4886 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4887 phba->ctrl.mcc_tag[i] = i + 1;
4888 phba->ctrl.mcc_numtag[i + 1] = 0;
4889 phba->ctrl.mcc_tag_available++;
4890 }
4891
4892 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4893
John Soni Jose72fb46a2012-10-20 04:42:49 +05304894 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_%02x_wq",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304895 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004896 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304897 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304898 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4899 "BM_%d : beiscsi_dev_probe-"
4900 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304901 goto free_twq;
4902 }
4903
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304904
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304905 phwi_ctrlr = phba->phwi_ctrlr;
4906 phwi_context = phwi_ctrlr->phwi_ctxt;
John Soni Jose72fb46a2012-10-20 04:42:49 +05304907
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304908 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304909 for (i = 0; i < phba->num_cpus; i++) {
4910 pbe_eq = &phwi_context->be_eq[i];
4911 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4912 be_iopoll);
4913 blk_iopoll_enable(&pbe_eq->iopoll);
4914 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304915
4916 i = (phba->msix_enabled) ? i : 0;
4917 /* Work item for MCC handling */
4918 pbe_eq = &phwi_context->be_eq[i];
4919 INIT_WORK(&pbe_eq->work_cqs, beiscsi_process_all_cqs);
4920 } else {
4921 if (phba->msix_enabled) {
4922 for (i = 0; i <= phba->num_cpus; i++) {
4923 pbe_eq = &phwi_context->be_eq[i];
4924 INIT_WORK(&pbe_eq->work_cqs,
4925 beiscsi_process_all_cqs);
4926 }
4927 } else {
4928 pbe_eq = &phwi_context->be_eq[0];
4929 INIT_WORK(&pbe_eq->work_cqs,
4930 beiscsi_process_all_cqs);
4931 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304932 }
John Soni Jose72fb46a2012-10-20 04:42:49 +05304933
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304934 ret = beiscsi_init_irqs(phba);
4935 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304936 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4937 "BM_%d : beiscsi_dev_probe-"
4938 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304939 goto free_blkenbld;
4940 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304941 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004942
4943 if (beiscsi_setup_boot_info(phba))
4944 /*
4945 * log error but continue, because we may not be using
4946 * iscsi boot.
4947 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304948 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4949 "BM_%d : Could not set up "
4950 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004951
Mike Christie0e438952012-04-03 23:41:51 -05004952 beiscsi_create_def_ifaces(phba);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304953 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4954 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304955 return 0;
4956
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304957free_blkenbld:
4958 destroy_workqueue(phba->wq);
4959 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304960 for (i = 0; i < phba->num_cpus; i++) {
4961 pbe_eq = &phwi_context->be_eq[i];
4962 blk_iopoll_disable(&pbe_eq->iopoll);
4963 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304964free_twq:
4965 beiscsi_clean_port(phba);
4966 beiscsi_free_mem(phba);
4967free_port:
4968 pci_free_consistent(phba->pcidev,
4969 phba->ctrl.mbox_mem_alloced.size,
4970 phba->ctrl.mbox_mem_alloced.va,
4971 phba->ctrl.mbox_mem_alloced.dma);
4972 beiscsi_unmap_pci_function(phba);
4973hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304974 if (phba->msix_enabled)
4975 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304976 iscsi_host_remove(phba->shost);
4977 pci_dev_put(phba->pcidev);
4978 iscsi_host_free(phba->shost);
4979disable_pci:
4980 pci_disable_device(pcidev);
4981 return ret;
4982}
4983
4984struct iscsi_transport beiscsi_iscsi_transport = {
4985 .owner = THIS_MODULE,
4986 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304987 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304988 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304989 .create_session = beiscsi_session_create,
4990 .destroy_session = beiscsi_session_destroy,
4991 .create_conn = beiscsi_conn_create,
4992 .bind_conn = beiscsi_conn_bind,
4993 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004994 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05004995 .set_iface_param = be2iscsi_iface_set_param,
4996 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304997 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004998 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304999 .get_session_param = iscsi_session_get_param,
5000 .get_host_param = beiscsi_get_host_param,
5001 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05005002 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305003 .send_pdu = iscsi_conn_send_pdu,
5004 .xmit_task = beiscsi_task_xmit,
5005 .cleanup_task = beiscsi_cleanup_task,
5006 .alloc_pdu = beiscsi_alloc_pdu,
5007 .parse_pdu_itt = beiscsi_parse_pdu,
5008 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06005009 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305010 .ep_connect = beiscsi_ep_connect,
5011 .ep_poll = beiscsi_ep_poll,
5012 .ep_disconnect = beiscsi_ep_disconnect,
5013 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05005014 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305015};
5016
5017static struct pci_driver beiscsi_pci_driver = {
5018 .name = DRV_NAME,
5019 .probe = beiscsi_dev_probe,
5020 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07005021 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305022 .id_table = beiscsi_pci_id_table
5023};
5024
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05305025
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305026static int __init beiscsi_module_init(void)
5027{
5028 int ret;
5029
5030 beiscsi_scsi_transport =
5031 iscsi_register_transport(&beiscsi_iscsi_transport);
5032 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305033 printk(KERN_ERR
5034 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05305035 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305036 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05305037 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
5038 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305039
5040 ret = pci_register_driver(&beiscsi_pci_driver);
5041 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05305042 printk(KERN_ERR
5043 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305044 goto unregister_iscsi_transport;
5045 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05305046 return 0;
5047
5048unregister_iscsi_transport:
5049 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5050 return ret;
5051}
5052
5053static void __exit beiscsi_module_exit(void)
5054{
5055 pci_unregister_driver(&beiscsi_pci_driver);
5056 iscsi_unregister_transport(&beiscsi_iscsi_transport);
5057}
5058
5059module_init(beiscsi_module_init);
5060module_exit(beiscsi_module_exit);