blob: ff73f9500b01c43c3bdbbd210052fd651a8f3487 [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 Kallickale9b91192010-07-22 04:24:53 +053050static unsigned int gcrashmode = 0;
51static unsigned int num_hba = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053052
53MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
54MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
Jayamohan Kallickal76d15db2012-04-03 23:41:46 -050055MODULE_VERSION(BUILD_STR);
Jayamohan Kallickal2f635882012-04-03 23:41:45 -050056MODULE_AUTHOR("Emulex Corporation");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053057MODULE_LICENSE("GPL");
58module_param(be_iopoll_budget, int, 0);
59module_param(enable_msix, int, 0);
60module_param(be_max_phys_size, uint, S_IRUGO);
John Soni Jose99bc5d52012-08-20 23:00:18 +053061MODULE_PARM_DESC(be_max_phys_size,
62 "Maximum Size (In Kilobytes) of physically contiguous "
63 "memory that can be allocated. Range is 16 - 128");
64
65#define beiscsi_disp_param(_name)\
66ssize_t \
67beiscsi_##_name##_disp(struct device *dev,\
68 struct device_attribute *attrib, char *buf) \
69{ \
70 struct Scsi_Host *shost = class_to_shost(dev);\
71 struct beiscsi_hba *phba = iscsi_host_priv(shost); \
72 uint32_t param_val = 0; \
73 param_val = phba->attr_##_name;\
74 return snprintf(buf, PAGE_SIZE, "%d\n",\
75 phba->attr_##_name);\
76}
77
78#define beiscsi_change_param(_name, _minval, _maxval, _defaval)\
79int \
80beiscsi_##_name##_change(struct beiscsi_hba *phba, uint32_t val)\
81{\
82 if (val >= _minval && val <= _maxval) {\
83 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
84 "BA_%d : beiscsi_"#_name" updated "\
85 "from 0x%x ==> 0x%x\n",\
86 phba->attr_##_name, val); \
87 phba->attr_##_name = val;\
88 return 0;\
89 } \
90 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT, \
91 "BA_%d beiscsi_"#_name" attribute "\
92 "cannot be updated to 0x%x, "\
93 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
94 return -EINVAL;\
95}
96
97#define beiscsi_store_param(_name) \
98ssize_t \
99beiscsi_##_name##_store(struct device *dev,\
100 struct device_attribute *attr, const char *buf,\
101 size_t count) \
102{ \
103 struct Scsi_Host *shost = class_to_shost(dev);\
104 struct beiscsi_hba *phba = iscsi_host_priv(shost);\
105 uint32_t param_val = 0;\
106 if (!isdigit(buf[0]))\
107 return -EINVAL;\
108 if (sscanf(buf, "%i", &param_val) != 1)\
109 return -EINVAL;\
110 if (beiscsi_##_name##_change(phba, param_val) == 0) \
111 return strlen(buf);\
112 else \
113 return -EINVAL;\
114}
115
116#define beiscsi_init_param(_name, _minval, _maxval, _defval) \
117int \
118beiscsi_##_name##_init(struct beiscsi_hba *phba, uint32_t val) \
119{ \
120 if (val >= _minval && val <= _maxval) {\
121 phba->attr_##_name = val;\
122 return 0;\
123 } \
124 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,\
125 "BA_%d beiscsi_"#_name" attribute " \
126 "cannot be updated to 0x%x, "\
127 "range allowed is ["#_minval" - "#_maxval"]\n", val);\
128 phba->attr_##_name = _defval;\
129 return -EINVAL;\
130}
131
132#define BEISCSI_RW_ATTR(_name, _minval, _maxval, _defval, _descp) \
133static uint beiscsi_##_name = _defval;\
134module_param(beiscsi_##_name, uint, S_IRUGO);\
135MODULE_PARM_DESC(beiscsi_##_name, _descp);\
136beiscsi_disp_param(_name)\
137beiscsi_change_param(_name, _minval, _maxval, _defval)\
138beiscsi_store_param(_name)\
139beiscsi_init_param(_name, _minval, _maxval, _defval)\
140DEVICE_ATTR(beiscsi_##_name, S_IRUGO | S_IWUSR,\
141 beiscsi_##_name##_disp, beiscsi_##_name##_store)
142
143/*
144 * When new log level added update the
145 * the MAX allowed value for log_enable
146 */
147BEISCSI_RW_ATTR(log_enable, 0x00,
148 0xFF, 0x00, "Enable logging Bit Mask\n"
149 "\t\t\t\tInitialization Events : 0x01\n"
150 "\t\t\t\tMailbox Events : 0x02\n"
151 "\t\t\t\tMiscellaneous Events : 0x04\n"
152 "\t\t\t\tError Handling : 0x08\n"
153 "\t\t\t\tIO Path Events : 0x10\n"
154 "\t\t\t\tConfiguration Path : 0x20\n");
155
156struct device_attribute *beiscsi_attrs[] = {
157 &dev_attr_beiscsi_log_enable,
158 NULL,
159};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530160
161static int beiscsi_slave_configure(struct scsi_device *sdev)
162{
163 blk_queue_max_segment_size(sdev->request_queue, 65536);
164 return 0;
165}
166
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530167static int beiscsi_eh_abort(struct scsi_cmnd *sc)
168{
169 struct iscsi_cls_session *cls_session;
170 struct iscsi_task *aborted_task = (struct iscsi_task *)sc->SCp.ptr;
171 struct beiscsi_io_task *aborted_io_task;
172 struct iscsi_conn *conn;
173 struct beiscsi_conn *beiscsi_conn;
174 struct beiscsi_hba *phba;
175 struct iscsi_session *session;
176 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530177 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530178 unsigned int cid, tag, num_invalidate;
179
180 cls_session = starget_to_session(scsi_target(sc->device));
181 session = cls_session->dd_data;
182
183 spin_lock_bh(&session->lock);
184 if (!aborted_task || !aborted_task->sc) {
185 /* we raced */
186 spin_unlock_bh(&session->lock);
187 return SUCCESS;
188 }
189
190 aborted_io_task = aborted_task->dd_data;
191 if (!aborted_io_task->scsi_cmnd) {
192 /* raced or invalid command */
193 spin_unlock_bh(&session->lock);
194 return SUCCESS;
195 }
196 spin_unlock_bh(&session->lock);
197 conn = aborted_task->conn;
198 beiscsi_conn = conn->dd_data;
199 phba = beiscsi_conn->phba;
200
201 /* invalidate iocb */
202 cid = beiscsi_conn->beiscsi_conn_cid;
203 inv_tbl = phba->inv_tbl;
204 memset(inv_tbl, 0x0, sizeof(*inv_tbl));
205 inv_tbl->cid = cid;
206 inv_tbl->icd = aborted_io_task->psgl_handle->sgl_index;
207 num_invalidate = 1;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530208 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
209 sizeof(struct invalidate_commands_params_in),
210 &nonemb_cmd.dma);
211 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530212 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
213 "BM_%d : Failed to allocate memory for"
214 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530215 return FAILED;
216 }
217 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
218
219 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
220 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530221 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530222 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
223 "BM_%d : mgmt_invalidate_icds could not be"
224 "submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530225 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
226 nonemb_cmd.va, nonemb_cmd.dma);
227
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530228 return FAILED;
229 } else {
230 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
231 phba->ctrl.mcc_numtag[tag]);
232 free_mcc_tag(&phba->ctrl, tag);
233 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530234 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
235 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530236 return iscsi_eh_abort(sc);
237}
238
239static int beiscsi_eh_device_reset(struct scsi_cmnd *sc)
240{
241 struct iscsi_task *abrt_task;
242 struct beiscsi_io_task *abrt_io_task;
243 struct iscsi_conn *conn;
244 struct beiscsi_conn *beiscsi_conn;
245 struct beiscsi_hba *phba;
246 struct iscsi_session *session;
247 struct iscsi_cls_session *cls_session;
248 struct invalidate_command_table *inv_tbl;
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530249 struct be_dma_mem nonemb_cmd;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530250 unsigned int cid, tag, i, num_invalidate;
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530251
252 /* invalidate iocbs */
253 cls_session = starget_to_session(scsi_target(sc->device));
254 session = cls_session->dd_data;
255 spin_lock_bh(&session->lock);
Jayamohan Kallickaldb7f7702012-04-03 23:41:43 -0500256 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) {
257 spin_unlock_bh(&session->lock);
258 return FAILED;
259 }
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530260 conn = session->leadconn;
261 beiscsi_conn = conn->dd_data;
262 phba = beiscsi_conn->phba;
263 cid = beiscsi_conn->beiscsi_conn_cid;
264 inv_tbl = phba->inv_tbl;
265 memset(inv_tbl, 0x0, sizeof(*inv_tbl) * BE2_CMDS_PER_CXN);
266 num_invalidate = 0;
267 for (i = 0; i < conn->session->cmds_max; i++) {
268 abrt_task = conn->session->cmds[i];
269 abrt_io_task = abrt_task->dd_data;
270 if (!abrt_task->sc || abrt_task->state == ISCSI_TASK_FREE)
271 continue;
272
273 if (abrt_task->sc->device->lun != abrt_task->sc->device->lun)
274 continue;
275
276 inv_tbl->cid = cid;
277 inv_tbl->icd = abrt_io_task->psgl_handle->sgl_index;
278 num_invalidate++;
279 inv_tbl++;
280 }
281 spin_unlock_bh(&session->lock);
282 inv_tbl = phba->inv_tbl;
283
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530284 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
285 sizeof(struct invalidate_commands_params_in),
286 &nonemb_cmd.dma);
287 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530288 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_EH,
289 "BM_%d : Failed to allocate memory for"
290 "mgmt_invalidate_icds\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530291 return FAILED;
292 }
293 nonemb_cmd.size = sizeof(struct invalidate_commands_params_in);
294 memset(nonemb_cmd.va, 0, nonemb_cmd.size);
295 tag = mgmt_invalidate_icds(phba, inv_tbl, num_invalidate,
296 cid, &nonemb_cmd);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530297 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530298 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_EH,
299 "BM_%d : mgmt_invalidate_icds could not be"
300 " submitted\n");
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530301 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
302 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530303 return FAILED;
304 } else {
305 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
306 phba->ctrl.mcc_numtag[tag]);
307 free_mcc_tag(&phba->ctrl, tag);
308 }
Jayamohan Kallickal3cbb7a72010-07-22 04:27:47 +0530309 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
310 nonemb_cmd.va, nonemb_cmd.dma);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530311 return iscsi_eh_device_reset(sc);
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530312}
313
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530314static ssize_t beiscsi_show_boot_tgt_info(void *data, int type, char *buf)
315{
316 struct beiscsi_hba *phba = data;
Mike Christief457a462011-06-24 15:11:53 -0500317 struct mgmt_session_info *boot_sess = &phba->boot_sess;
318 struct mgmt_conn_info *boot_conn = &boot_sess->conn_list[0];
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530319 char *str = buf;
320 int rc;
321
322 switch (type) {
323 case ISCSI_BOOT_TGT_NAME:
324 rc = sprintf(buf, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500325 (int)strlen(boot_sess->target_name),
326 (char *)&boot_sess->target_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530327 break;
328 case ISCSI_BOOT_TGT_IP_ADDR:
Mike Christief457a462011-06-24 15:11:53 -0500329 if (boot_conn->dest_ipaddr.ip_type == 0x1)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530330 rc = sprintf(buf, "%pI4\n",
Mike Christie0e438952012-04-03 23:41:51 -0500331 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530332 else
333 rc = sprintf(str, "%pI6\n",
Mike Christie0e438952012-04-03 23:41:51 -0500334 (char *)&boot_conn->dest_ipaddr.addr);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530335 break;
336 case ISCSI_BOOT_TGT_PORT:
Mike Christief457a462011-06-24 15:11:53 -0500337 rc = sprintf(str, "%d\n", boot_conn->dest_port);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530338 break;
339
340 case ISCSI_BOOT_TGT_CHAP_NAME:
341 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500342 boot_conn->negotiated_login_options.auth_data.chap.
343 target_chap_name_length,
344 (char *)&boot_conn->negotiated_login_options.
345 auth_data.chap.target_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530346 break;
347 case ISCSI_BOOT_TGT_CHAP_SECRET:
348 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500349 boot_conn->negotiated_login_options.auth_data.chap.
350 target_secret_length,
351 (char *)&boot_conn->negotiated_login_options.
352 auth_data.chap.target_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530353 break;
354 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
355 rc = sprintf(str, "%.*s\n",
Mike Christief457a462011-06-24 15:11:53 -0500356 boot_conn->negotiated_login_options.auth_data.chap.
357 intr_chap_name_length,
358 (char *)&boot_conn->negotiated_login_options.
359 auth_data.chap.intr_chap_name);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530360 break;
361 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
Mike Christief457a462011-06-24 15:11:53 -0500362 rc = sprintf(str, "%.*s\n",
363 boot_conn->negotiated_login_options.auth_data.chap.
364 intr_secret_length,
365 (char *)&boot_conn->negotiated_login_options.
366 auth_data.chap.intr_secret);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530367 break;
368 case ISCSI_BOOT_TGT_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500369 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530370 break;
371 case ISCSI_BOOT_TGT_NIC_ASSOC:
Mike Christief457a462011-06-24 15:11:53 -0500372 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530373 break;
374 default:
375 rc = -ENOSYS;
376 break;
377 }
378 return rc;
379}
380
381static ssize_t beiscsi_show_boot_ini_info(void *data, int type, char *buf)
382{
383 struct beiscsi_hba *phba = data;
384 char *str = buf;
385 int rc;
386
387 switch (type) {
388 case ISCSI_BOOT_INI_INITIATOR_NAME:
389 rc = sprintf(str, "%s\n", phba->boot_sess.initiator_iscsiname);
390 break;
391 default:
392 rc = -ENOSYS;
393 break;
394 }
395 return rc;
396}
397
398static ssize_t beiscsi_show_boot_eth_info(void *data, int type, char *buf)
399{
400 struct beiscsi_hba *phba = data;
401 char *str = buf;
402 int rc;
403
404 switch (type) {
405 case ISCSI_BOOT_ETH_FLAGS:
Mike Christief457a462011-06-24 15:11:53 -0500406 rc = sprintf(str, "2\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530407 break;
408 case ISCSI_BOOT_ETH_INDEX:
Mike Christief457a462011-06-24 15:11:53 -0500409 rc = sprintf(str, "0\n");
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530410 break;
411 case ISCSI_BOOT_ETH_MAC:
Mike Christie0e438952012-04-03 23:41:51 -0500412 rc = beiscsi_get_macaddr(str, phba);
413 break;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530414 default:
415 rc = -ENOSYS;
416 break;
417 }
418 return rc;
419}
420
421
Al Viro587a1f12011-07-23 23:11:19 -0400422static umode_t beiscsi_tgt_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530423{
Al Viro587a1f12011-07-23 23:11:19 -0400424 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530425
426 switch (type) {
427 case ISCSI_BOOT_TGT_NAME:
428 case ISCSI_BOOT_TGT_IP_ADDR:
429 case ISCSI_BOOT_TGT_PORT:
430 case ISCSI_BOOT_TGT_CHAP_NAME:
431 case ISCSI_BOOT_TGT_CHAP_SECRET:
432 case ISCSI_BOOT_TGT_REV_CHAP_NAME:
433 case ISCSI_BOOT_TGT_REV_CHAP_SECRET:
434 case ISCSI_BOOT_TGT_NIC_ASSOC:
435 case ISCSI_BOOT_TGT_FLAGS:
436 rc = S_IRUGO;
437 break;
438 default:
439 rc = 0;
440 break;
441 }
442 return rc;
443}
444
Al Viro587a1f12011-07-23 23:11:19 -0400445static umode_t beiscsi_ini_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530446{
Al Viro587a1f12011-07-23 23:11:19 -0400447 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530448
449 switch (type) {
450 case ISCSI_BOOT_INI_INITIATOR_NAME:
451 rc = S_IRUGO;
452 break;
453 default:
454 rc = 0;
455 break;
456 }
457 return rc;
458}
459
460
Al Viro587a1f12011-07-23 23:11:19 -0400461static umode_t beiscsi_eth_get_attr_visibility(void *data, int type)
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530462{
Al Viro587a1f12011-07-23 23:11:19 -0400463 umode_t rc;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530464
465 switch (type) {
466 case ISCSI_BOOT_ETH_FLAGS:
467 case ISCSI_BOOT_ETH_MAC:
468 case ISCSI_BOOT_ETH_INDEX:
469 rc = S_IRUGO;
470 break;
471 default:
472 rc = 0;
473 break;
474 }
475 return rc;
476}
477
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530478/*------------------- PCI Driver operations and data ----------------- */
479static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
480 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530481 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID2) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530482 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
483 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
484 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530485 { 0 }
486};
487MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
488
John Soni Jose99bc5d52012-08-20 23:00:18 +0530489
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530490static struct scsi_host_template beiscsi_sht = {
491 .module = THIS_MODULE,
Jayamohan Kallickal2f635882012-04-03 23:41:45 -0500492 .name = "Emulex 10Gbe open-iscsi Initiator Driver",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530493 .proc_name = DRV_NAME,
494 .queuecommand = iscsi_queuecommand,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530495 .change_queue_depth = iscsi_change_queue_depth,
496 .slave_configure = beiscsi_slave_configure,
497 .target_alloc = iscsi_target_alloc,
Jayamohan Kallickal41831222010-02-20 08:02:39 +0530498 .eh_abort_handler = beiscsi_eh_abort,
499 .eh_device_reset_handler = beiscsi_eh_device_reset,
Jayamohan Kallickal309ce152010-02-20 08:02:10 +0530500 .eh_target_reset_handler = iscsi_eh_session_reset,
John Soni Jose99bc5d52012-08-20 23:00:18 +0530501 .shost_attrs = beiscsi_attrs,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530502 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
503 .can_queue = BE2_IO_DEPTH,
504 .this_id = -1,
505 .max_sectors = BEISCSI_MAX_SECTORS,
506 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
507 .use_clustering = ENABLE_CLUSTERING,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -0500508 .vendor_id = SCSI_NL_VID_TYPE_PCI | BE_VENDOR_ID,
509
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530510};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530511
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530512static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530513
514static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
515{
516 struct beiscsi_hba *phba;
517 struct Scsi_Host *shost;
518
519 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
520 if (!shost) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530521 dev_err(&pcidev->dev,
522 "beiscsi_hba_alloc - iscsi_host_alloc failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530523 return NULL;
524 }
525 shost->dma_boundary = pcidev->dma_mask;
526 shost->max_id = BE2_MAX_SESSIONS;
527 shost->max_channel = 0;
528 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
529 shost->max_lun = BEISCSI_NUM_MAX_LUN;
530 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530531 phba = iscsi_host_priv(shost);
532 memset(phba, 0, sizeof(*phba));
533 phba->shost = shost;
534 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530535 pci_set_drvdata(pcidev, phba);
Mike Christie0e438952012-04-03 23:41:51 -0500536 phba->interface_handle = 0xFFFFFFFF;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530537
538 if (iscsi_host_add(shost, &phba->pcidev->dev))
539 goto free_devices;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +0530540
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530541 return phba;
542
543free_devices:
544 pci_dev_put(phba->pcidev);
545 iscsi_host_free(phba->shost);
546 return NULL;
547}
548
549static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
550{
551 if (phba->csr_va) {
552 iounmap(phba->csr_va);
553 phba->csr_va = NULL;
554 }
555 if (phba->db_va) {
556 iounmap(phba->db_va);
557 phba->db_va = NULL;
558 }
559 if (phba->pci_va) {
560 iounmap(phba->pci_va);
561 phba->pci_va = NULL;
562 }
563}
564
565static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
566 struct pci_dev *pcidev)
567{
568 u8 __iomem *addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530569 int pcicfg_reg;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530570
571 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
572 pci_resource_len(pcidev, 2));
573 if (addr == NULL)
574 return -ENOMEM;
575 phba->ctrl.csr = addr;
576 phba->csr_va = addr;
577 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
578
579 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
580 if (addr == NULL)
581 goto pci_map_err;
582 phba->ctrl.db = addr;
583 phba->db_va = addr;
584 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
585
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530586 if (phba->generation == BE_GEN2)
587 pcicfg_reg = 1;
588 else
589 pcicfg_reg = 0;
590
591 addr = ioremap_nocache(pci_resource_start(pcidev, pcicfg_reg),
592 pci_resource_len(pcidev, pcicfg_reg));
593
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530594 if (addr == NULL)
595 goto pci_map_err;
596 phba->ctrl.pcicfg = addr;
597 phba->pci_va = addr;
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +0530598 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, pcicfg_reg);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530599 return 0;
600
601pci_map_err:
602 beiscsi_unmap_pci_function(phba);
603 return -ENOMEM;
604}
605
606static int beiscsi_enable_pci(struct pci_dev *pcidev)
607{
608 int ret;
609
610 ret = pci_enable_device(pcidev);
611 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530612 dev_err(&pcidev->dev,
613 "beiscsi_enable_pci - enable device failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530614 return ret;
615 }
616
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530617 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530618 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
619 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
620 if (ret) {
621 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
622 pci_disable_device(pcidev);
623 return ret;
624 }
625 }
626 return 0;
627}
628
629static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
630{
631 struct be_ctrl_info *ctrl = &phba->ctrl;
632 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
633 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
634 int status = 0;
635
636 ctrl->pdev = pdev;
637 status = beiscsi_map_pci_bars(phba, pdev);
638 if (status)
639 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530640 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
641 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
642 mbox_mem_alloc->size,
643 &mbox_mem_alloc->dma);
644 if (!mbox_mem_alloc->va) {
645 beiscsi_unmap_pci_function(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -0500646 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530647 }
648
649 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
650 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
651 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
652 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
653 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530654 spin_lock_init(&phba->ctrl.mcc_lock);
655 spin_lock_init(&phba->ctrl.mcc_cq_lock);
656
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530657 return status;
658}
659
660static void beiscsi_get_params(struct beiscsi_hba *phba)
661{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530662 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
663 - (phba->fw_config.iscsi_cid_count
664 + BE2_TMFS
665 + BE2_NOPOUT_REQ));
666 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +0530667 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count * 2;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700668 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530669 phba->params.num_sge_per_io = BE2_SGE;
670 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
671 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
672 phba->params.eq_timer = 64;
673 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530674 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
675 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530676 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
677 ? 1024 : phba->params.num_eq_entries;
John Soni Jose99bc5d52012-08-20 23:00:18 +0530678 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
679 "BM_%d : phba->params.num_eq_entries=%d\n",
680 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530681 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530682 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
683 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530684 phba->params.wrbs_per_cxn = 256;
685}
686
687static void hwi_ring_eq_db(struct beiscsi_hba *phba,
688 unsigned int id, unsigned int clr_interrupt,
689 unsigned int num_processed,
690 unsigned char rearm, unsigned char event)
691{
692 u32 val = 0;
693 val |= id & DB_EQ_RING_ID_MASK;
694 if (rearm)
695 val |= 1 << DB_EQ_REARM_SHIFT;
696 if (clr_interrupt)
697 val |= 1 << DB_EQ_CLR_SHIFT;
698 if (event)
699 val |= 1 << DB_EQ_EVNT_SHIFT;
700 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
701 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
702}
703
704/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530705 * be_isr_mcc - The isr routine of the driver.
706 * @irq: Not used
707 * @dev_id: Pointer to host adapter structure
708 */
709static irqreturn_t be_isr_mcc(int irq, void *dev_id)
710{
711 struct beiscsi_hba *phba;
712 struct be_eq_entry *eqe = NULL;
713 struct be_queue_info *eq;
714 struct be_queue_info *mcc;
715 unsigned int num_eq_processed;
716 struct be_eq_obj *pbe_eq;
717 unsigned long flags;
718
719 pbe_eq = dev_id;
720 eq = &pbe_eq->q;
721 phba = pbe_eq->phba;
722 mcc = &phba->ctrl.mcc_obj.cq;
723 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530724
725 num_eq_processed = 0;
726
727 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
728 & EQE_VALID_MASK) {
729 if (((eqe->dw[offsetof(struct amap_eq_entry,
730 resource_id) / 32] &
731 EQE_RESID_MASK) >> 16) == mcc->id) {
732 spin_lock_irqsave(&phba->isr_lock, flags);
733 phba->todo_mcc_cq = 1;
734 spin_unlock_irqrestore(&phba->isr_lock, flags);
735 }
736 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
737 queue_tail_inc(eq);
738 eqe = queue_tail_node(eq);
739 num_eq_processed++;
740 }
741 if (phba->todo_mcc_cq)
742 queue_work(phba->wq, &phba->work_cqs);
743 if (num_eq_processed)
744 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
745
746 return IRQ_HANDLED;
747}
748
749/**
750 * be_isr_msix - The isr routine of the driver.
751 * @irq: Not used
752 * @dev_id: Pointer to host adapter structure
753 */
754static irqreturn_t be_isr_msix(int irq, void *dev_id)
755{
756 struct beiscsi_hba *phba;
757 struct be_eq_entry *eqe = NULL;
758 struct be_queue_info *eq;
759 struct be_queue_info *cq;
760 unsigned int num_eq_processed;
761 struct be_eq_obj *pbe_eq;
762 unsigned long flags;
763
764 pbe_eq = dev_id;
765 eq = &pbe_eq->q;
766 cq = pbe_eq->cq;
767 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530768
769 phba = pbe_eq->phba;
770 num_eq_processed = 0;
771 if (blk_iopoll_enabled) {
772 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
773 & EQE_VALID_MASK) {
774 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
775 blk_iopoll_sched(&pbe_eq->iopoll);
776
777 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
778 queue_tail_inc(eq);
779 eqe = queue_tail_node(eq);
780 num_eq_processed++;
781 }
782 if (num_eq_processed)
783 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
784
785 return IRQ_HANDLED;
786 } else {
787 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
788 & EQE_VALID_MASK) {
789 spin_lock_irqsave(&phba->isr_lock, flags);
790 phba->todo_cq = 1;
791 spin_unlock_irqrestore(&phba->isr_lock, flags);
792 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
793 queue_tail_inc(eq);
794 eqe = queue_tail_node(eq);
795 num_eq_processed++;
796 }
797 if (phba->todo_cq)
798 queue_work(phba->wq, &phba->work_cqs);
799
800 if (num_eq_processed)
801 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
802
803 return IRQ_HANDLED;
804 }
805}
806
807/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530808 * be_isr - The isr routine of the driver.
809 * @irq: Not used
810 * @dev_id: Pointer to host adapter structure
811 */
812static irqreturn_t be_isr(int irq, void *dev_id)
813{
814 struct beiscsi_hba *phba;
815 struct hwi_controller *phwi_ctrlr;
816 struct hwi_context_memory *phwi_context;
817 struct be_eq_entry *eqe = NULL;
818 struct be_queue_info *eq;
819 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530820 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530821 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530822 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530823 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530824 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530825 int isr;
826
827 phba = dev_id;
Justin P. Mattock6eab04a2011-04-08 19:49:08 -0700828 ctrl = &phba->ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530829 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
830 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
831 if (!isr)
832 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530833
834 phwi_ctrlr = phba->phwi_ctrlr;
835 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530836 pbe_eq = &phwi_context->be_eq[0];
837
838 eq = &phwi_context->be_eq[0].q;
839 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530840 index = 0;
841 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530842
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530843 num_ioeq_processed = 0;
844 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530845 if (blk_iopoll_enabled) {
846 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
847 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530848 if (((eqe->dw[offsetof(struct amap_eq_entry,
849 resource_id) / 32] &
850 EQE_RESID_MASK) >> 16) == mcc->id) {
851 spin_lock_irqsave(&phba->isr_lock, flags);
852 phba->todo_mcc_cq = 1;
853 spin_unlock_irqrestore(&phba->isr_lock, flags);
854 num_mcceq_processed++;
855 } else {
856 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
857 blk_iopoll_sched(&pbe_eq->iopoll);
858 num_ioeq_processed++;
859 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530860 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
861 queue_tail_inc(eq);
862 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530863 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530864 if (num_ioeq_processed || num_mcceq_processed) {
865 if (phba->todo_mcc_cq)
866 queue_work(phba->wq, &phba->work_cqs);
867
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530868 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530869 hwi_ring_eq_db(phba, eq->id, 0,
870 (num_ioeq_processed +
871 num_mcceq_processed) , 1, 1);
872 else
873 hwi_ring_eq_db(phba, eq->id, 0,
874 (num_ioeq_processed +
875 num_mcceq_processed), 0, 1);
876
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530877 return IRQ_HANDLED;
878 } else
879 return IRQ_NONE;
880 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530881 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530882 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
883 & EQE_VALID_MASK) {
884
885 if (((eqe->dw[offsetof(struct amap_eq_entry,
886 resource_id) / 32] &
887 EQE_RESID_MASK) >> 16) != cq->id) {
888 spin_lock_irqsave(&phba->isr_lock, flags);
889 phba->todo_mcc_cq = 1;
890 spin_unlock_irqrestore(&phba->isr_lock, flags);
891 } else {
892 spin_lock_irqsave(&phba->isr_lock, flags);
893 phba->todo_cq = 1;
894 spin_unlock_irqrestore(&phba->isr_lock, flags);
895 }
896 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
897 queue_tail_inc(eq);
898 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530899 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530900 }
901 if (phba->todo_cq || phba->todo_mcc_cq)
902 queue_work(phba->wq, &phba->work_cqs);
903
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530904 if (num_ioeq_processed) {
905 hwi_ring_eq_db(phba, eq->id, 0,
906 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530907 return IRQ_HANDLED;
908 } else
909 return IRQ_NONE;
910 }
911}
912
913static int beiscsi_init_irqs(struct beiscsi_hba *phba)
914{
915 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530916 struct hwi_controller *phwi_ctrlr;
917 struct hwi_context_memory *phwi_context;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530918 int ret, msix_vec, i, j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530919
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530920 phwi_ctrlr = phba->phwi_ctrlr;
921 phwi_context = phwi_ctrlr->phwi_ctxt;
922
923 if (phba->msix_enabled) {
924 for (i = 0; i < phba->num_cpus; i++) {
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700925 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME,
926 GFP_KERNEL);
927 if (!phba->msi_name[i]) {
928 ret = -ENOMEM;
929 goto free_msix_irqs;
930 }
931
932 sprintf(phba->msi_name[i], "beiscsi_%02x_%02x",
933 phba->shost->host_no, i);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530934 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700935 ret = request_irq(msix_vec, be_isr_msix, 0,
936 phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530937 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530938 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530939 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
940 "BM_%d : beiscsi_init_irqs-Failed to"
941 "register msix for i = %d\n",
942 i);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700943 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530944 goto free_msix_irqs;
945 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530946 }
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700947 phba->msi_name[i] = kzalloc(BEISCSI_MSI_NAME, GFP_KERNEL);
948 if (!phba->msi_name[i]) {
949 ret = -ENOMEM;
950 goto free_msix_irqs;
951 }
952 sprintf(phba->msi_name[i], "beiscsi_mcc_%02x",
953 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530954 msix_vec = phba->msix_entries[i].vector;
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700955 ret = request_irq(msix_vec, be_isr_mcc, 0, phba->msi_name[i],
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530956 &phwi_context->be_eq[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530957 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530958 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT ,
959 "BM_%d : beiscsi_init_irqs-"
960 "Failed to register beiscsi_msix_mcc\n");
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700961 kfree(phba->msi_name[i]);
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530962 goto free_msix_irqs;
963 }
964
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530965 } else {
966 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
967 "beiscsi", phba);
968 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +0530969 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
970 "BM_%d : beiscsi_init_irqs-"
971 "Failed to register irq\\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530972 return ret;
973 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530974 }
975 return 0;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530976free_msix_irqs:
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700977 for (j = i - 1; j >= 0; j--) {
978 kfree(phba->msi_name[j]);
979 msix_vec = phba->msix_entries[j].vector;
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530980 free_irq(msix_vec, &phwi_context->be_eq[j]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -0700981 }
Jayamohan Kallickal4f5af072010-07-22 04:23:55 +0530982 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530983}
984
985static void hwi_ring_cq_db(struct beiscsi_hba *phba,
986 unsigned int id, unsigned int num_processed,
987 unsigned char rearm, unsigned char event)
988{
989 u32 val = 0;
990 val |= id & DB_CQ_RING_ID_MASK;
991 if (rearm)
992 val |= 1 << DB_CQ_REARM_SHIFT;
993 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
994 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
995}
996
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530997static unsigned int
998beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
999 struct beiscsi_hba *phba,
1000 unsigned short cid,
1001 struct pdu_base *ppdu,
1002 unsigned long pdu_len,
1003 void *pbuffer, unsigned long buf_len)
1004{
1005 struct iscsi_conn *conn = beiscsi_conn->conn;
1006 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301007 struct iscsi_task *task;
1008 struct beiscsi_io_task *io_task;
1009 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301010
1011 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
1012 PDUBASE_OPCODE_MASK) {
1013 case ISCSI_OP_NOOP_IN:
1014 pbuffer = NULL;
1015 buf_len = 0;
1016 break;
1017 case ISCSI_OP_ASYNC_EVENT:
1018 break;
1019 case ISCSI_OP_REJECT:
1020 WARN_ON(!pbuffer);
1021 WARN_ON(!(buf_len == 48));
John Soni Jose99bc5d52012-08-20 23:00:18 +05301022 beiscsi_log(phba, KERN_ERR,
1023 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1024 "BM_%d : In ISCSI_OP_REJECT\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301025 break;
1026 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301027 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301028 task = conn->login_task;
1029 io_task = task->dd_data;
1030 login_hdr = (struct iscsi_hdr *)ppdu;
1031 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301032 break;
1033 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301034 beiscsi_log(phba, KERN_WARNING,
1035 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1036 "BM_%d : Unrecognized opcode 0x%x in async msg\n",
1037 (ppdu->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301038 dw[offsetof(struct amap_pdu_base, opcode) / 32]
John Soni Jose99bc5d52012-08-20 23:00:18 +05301039 & PDUBASE_OPCODE_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301040 return 1;
1041 }
1042
1043 spin_lock_bh(&session->lock);
1044 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
1045 spin_unlock_bh(&session->lock);
1046 return 0;
1047}
1048
1049static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
1050{
1051 struct sgl_handle *psgl_handle;
1052
1053 if (phba->io_sgl_hndl_avbl) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301054 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1055 "BM_%d : In alloc_io_sgl_handle,"
1056 " io_sgl_alloc_index=%d\n",
1057 phba->io_sgl_alloc_index);
1058
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301059 psgl_handle = phba->io_sgl_hndl_base[phba->
1060 io_sgl_alloc_index];
1061 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
1062 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301063 if (phba->io_sgl_alloc_index == (phba->params.
1064 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301065 phba->io_sgl_alloc_index = 0;
1066 else
1067 phba->io_sgl_alloc_index++;
1068 } else
1069 psgl_handle = NULL;
1070 return psgl_handle;
1071}
1072
1073static void
1074free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1075{
John Soni Jose99bc5d52012-08-20 23:00:18 +05301076 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1077 "BM_%d : In free_,io_sgl_free_index=%d\n",
1078 phba->io_sgl_free_index);
1079
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301080 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
1081 /*
1082 * this can happen if clean_task is called on a task that
1083 * failed in xmit_task or alloc_pdu.
1084 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301085 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_IO,
1086 "BM_%d : Double Free in IO SGL io_sgl_free_index=%d,"
1087 "value there=%p\n", phba->io_sgl_free_index,
1088 phba->io_sgl_hndl_base
1089 [phba->io_sgl_free_index]);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301090 return;
1091 }
1092 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
1093 phba->io_sgl_hndl_avbl++;
1094 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
1095 phba->io_sgl_free_index = 0;
1096 else
1097 phba->io_sgl_free_index++;
1098}
1099
1100/**
1101 * alloc_wrb_handle - To allocate a wrb handle
1102 * @phba: The hba pointer
1103 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301104 *
1105 * This happens under session_lock until submission to chip
1106 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301107struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301108{
1109 struct hwi_wrb_context *pwrb_context;
1110 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301111 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301112
1113 phwi_ctrlr = phba->phwi_ctrlr;
1114 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301115 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301116 pwrb_handle = pwrb_context->pwrb_handle_base[
1117 pwrb_context->alloc_index];
1118 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301119 if (pwrb_context->alloc_index ==
1120 (phba->params.wrbs_per_cxn - 1))
1121 pwrb_context->alloc_index = 0;
1122 else
1123 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +05301124 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
1125 pwrb_context->alloc_index];
1126 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301127 } else
1128 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301129 return pwrb_handle;
1130}
1131
1132/**
1133 * free_wrb_handle - To free the wrb handle back to pool
1134 * @phba: The hba pointer
1135 * @pwrb_context: The context to free from
1136 * @pwrb_handle: The wrb_handle to free
1137 *
1138 * This happens under session_lock until submission to chip
1139 */
1140static void
1141free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
1142 struct wrb_handle *pwrb_handle)
1143{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301144 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301145 pwrb_context->wrb_handles_available++;
1146 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
1147 pwrb_context->free_index = 0;
1148 else
1149 pwrb_context->free_index++;
1150
John Soni Jose99bc5d52012-08-20 23:00:18 +05301151 beiscsi_log(phba, KERN_INFO,
1152 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1153 "BM_%d : FREE WRB: pwrb_handle=%p free_index=0x%x"
1154 "wrb_handles_available=%d\n",
1155 pwrb_handle, pwrb_context->free_index,
1156 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301157}
1158
1159static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
1160{
1161 struct sgl_handle *psgl_handle;
1162
1163 if (phba->eh_sgl_hndl_avbl) {
1164 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
1165 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301166 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1167 "BM_%d : mgmt_sgl_alloc_index=%d=0x%x\n",
1168 phba->eh_sgl_alloc_index,
1169 phba->eh_sgl_alloc_index);
1170
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301171 phba->eh_sgl_hndl_avbl--;
1172 if (phba->eh_sgl_alloc_index ==
1173 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
1174 1))
1175 phba->eh_sgl_alloc_index = 0;
1176 else
1177 phba->eh_sgl_alloc_index++;
1178 } else
1179 psgl_handle = NULL;
1180 return psgl_handle;
1181}
1182
1183void
1184free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
1185{
1186
John Soni Jose99bc5d52012-08-20 23:00:18 +05301187 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_CONFIG,
1188 "BM_%d : In free_mgmt_sgl_handle,"
1189 "eh_sgl_free_index=%d\n",
1190 phba->eh_sgl_free_index);
1191
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301192 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
1193 /*
1194 * this can happen if clean_task is called on a task that
1195 * failed in xmit_task or alloc_pdu.
1196 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05301197 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_CONFIG,
1198 "BM_%d : Double Free in eh SGL ,"
1199 "eh_sgl_free_index=%d\n",
1200 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301201 return;
1202 }
1203 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
1204 phba->eh_sgl_hndl_avbl++;
1205 if (phba->eh_sgl_free_index ==
1206 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
1207 phba->eh_sgl_free_index = 0;
1208 else
1209 phba->eh_sgl_free_index++;
1210}
1211
1212static void
1213be_complete_io(struct beiscsi_conn *beiscsi_conn,
1214 struct iscsi_task *task, struct sol_cqe *psol)
1215{
1216 struct beiscsi_io_task *io_task = task->dd_data;
1217 struct be_status_bhs *sts_bhs =
1218 (struct be_status_bhs *)io_task->cmd_bhs;
1219 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301220 unsigned char *sense;
1221 u32 resid = 0, exp_cmdsn, max_cmdsn;
1222 u8 rsp, status, flags;
1223
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301224 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301225 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1226 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301227 max_cmdsn = ((psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301228 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1229 & SOL_EXP_CMD_SN_MASK) +
1230 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1231 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1232 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
1233 & SOL_RESP_MASK) >> 16);
1234 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
1235 & SOL_STS_MASK) >> 8);
1236 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1237 & SOL_FLAGS_MASK) >> 24) | 0x80;
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001238 if (!task->sc) {
1239 if (io_task->scsi_cmnd)
1240 scsi_dma_unmap(io_task->scsi_cmnd);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301241
Jayamohan Kallickalbd535452011-10-07 19:31:10 -05001242 return;
1243 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301244 task->sc->result = (DID_OK << 16) | status;
1245 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
1246 task->sc->result = DID_ERROR << 16;
1247 goto unmap;
1248 }
1249
1250 /* bidi not initially supported */
1251 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
1252 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
1253 32] & SOL_RES_CNT_MASK);
1254
1255 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
1256 task->sc->result = DID_ERROR << 16;
1257
1258 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
1259 scsi_set_resid(task->sc, resid);
1260 if (!status && (scsi_bufflen(task->sc) - resid <
1261 task->sc->underflow))
1262 task->sc->result = DID_ERROR << 16;
1263 }
1264 }
1265
1266 if (status == SAM_STAT_CHECK_CONDITION) {
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001267 u16 sense_len;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301268 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001269
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301270 sense = sts_bhs->sense_info + sizeof(unsigned short);
Dan Carpenter4053a4b2011-09-26 09:23:37 +03001271 sense_len = be16_to_cpu(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301272 memcpy(task->sc->sense_buffer, sense,
1273 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
1274 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301275
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301276 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
1277 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1278 & SOL_RES_CNT_MASK)
1279 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301280 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
1281 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301282 }
1283unmap:
1284 scsi_dma_unmap(io_task->scsi_cmnd);
1285 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
1286}
1287
1288static void
1289be_complete_logout(struct beiscsi_conn *beiscsi_conn,
1290 struct iscsi_task *task, struct sol_cqe *psol)
1291{
1292 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301293 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301294 struct iscsi_conn *conn = beiscsi_conn->conn;
1295
1296 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301297 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301298 hdr->t2wait = 5;
1299 hdr->t2retain = 0;
1300 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1301 & SOL_FLAGS_MASK) >> 24) | 0x80;
1302 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1303 32] & SOL_RESP_MASK);
1304 hdr->exp_cmdsn = cpu_to_be32(psol->
1305 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1306 & SOL_EXP_CMD_SN_MASK);
1307 hdr->max_cmdsn = be32_to_cpu((psol->
1308 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
1309 & SOL_EXP_CMD_SN_MASK) +
1310 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1311 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301312 hdr->dlength[0] = 0;
1313 hdr->dlength[1] = 0;
1314 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301315 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301316 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301317 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1318}
1319
1320static void
1321be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
1322 struct iscsi_task *task, struct sol_cqe *psol)
1323{
1324 struct iscsi_tm_rsp *hdr;
1325 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301326 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301327
1328 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +05301329 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301330 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1331 & SOL_FLAGS_MASK) >> 24) | 0x80;
1332 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
1333 32] & SOL_RESP_MASK);
1334 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301335 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301336 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1337 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1338 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1339 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301340 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301341 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1342}
1343
1344static void
1345hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
1346 struct beiscsi_hba *phba, struct sol_cqe *psol)
1347{
1348 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301349 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301350 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301351 struct iscsi_task *task;
1352 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301353 struct iscsi_conn *conn = beiscsi_conn->conn;
1354 struct iscsi_session *session = conn->session;
1355
1356 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301357 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301358 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301359 SOL_CID_MASK) >> 6) -
1360 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301361 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301362 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1363 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301364 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301365
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301366 io_task = task->dd_data;
Mike Christie1282ab72012-04-18 03:06:00 -05001367 spin_lock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301368 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
Mike Christie1282ab72012-04-18 03:06:00 -05001369 spin_unlock_bh(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301370 spin_lock_bh(&session->lock);
1371 free_wrb_handle(phba, pwrb_context, pwrb_handle);
1372 spin_unlock_bh(&session->lock);
1373}
1374
1375static void
1376be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
1377 struct iscsi_task *task, struct sol_cqe *psol)
1378{
1379 struct iscsi_nopin *hdr;
1380 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301381 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301382
1383 hdr = (struct iscsi_nopin *)task->hdr;
1384 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
1385 & SOL_FLAGS_MASK) >> 24) | 0x80;
1386 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
1387 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
1388 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
1389 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
1390 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
1391 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
1392 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301393 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301394 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
1395}
1396
1397static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
1398 struct beiscsi_hba *phba, struct sol_cqe *psol)
1399{
1400 struct hwi_wrb_context *pwrb_context;
1401 struct wrb_handle *pwrb_handle;
1402 struct iscsi_wrb *pwrb = NULL;
1403 struct hwi_controller *phwi_ctrlr;
1404 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301405 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301406 struct iscsi_conn *conn = beiscsi_conn->conn;
1407 struct iscsi_session *session = conn->session;
1408
1409 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301410 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301411 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301412 & SOL_CID_MASK) >> 6) -
1413 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301414 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301415 dw[offsetof(struct amap_sol_cqe, wrb_index) /
1416 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301417 task = pwrb_handle->pio_handle;
1418 pwrb = pwrb_handle->pwrb;
1419 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
1420 WRB_TYPE_MASK) >> 28;
1421
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301422 spin_lock_bh(&session->lock);
1423 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301424 case HWH_TYPE_IO:
1425 case HWH_TYPE_IO_RD:
1426 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301427 ISCSI_OP_NOOP_OUT)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301428 be_complete_nopin_resp(beiscsi_conn, task, psol);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301429 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301430 be_complete_io(beiscsi_conn, task, psol);
1431 break;
1432
1433 case HWH_TYPE_LOGOUT:
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05301434 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
1435 be_complete_logout(beiscsi_conn, task, psol);
1436 else
1437 be_complete_tmf(beiscsi_conn, task, psol);
1438
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301439 break;
1440
1441 case HWH_TYPE_LOGIN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301442 beiscsi_log(phba, KERN_ERR,
1443 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1444 "BM_%d :\t\t No HWH_TYPE_LOGIN Expected in"
1445 " hwi_complete_cmd- Solicited path\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301446 break;
1447
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301448 case HWH_TYPE_NOP:
1449 be_complete_nopin_resp(beiscsi_conn, task, psol);
1450 break;
1451
1452 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301453 beiscsi_log(phba, KERN_WARNING,
1454 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1455 "BM_%d : In hwi_complete_cmd, unknown type = %d"
1456 "wrb_index 0x%x CID 0x%x\n", type,
1457 ((psol->dw[offsetof(struct amap_iscsi_wrb,
1458 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
1459 ((psol->dw[offsetof(struct amap_sol_cqe,
1460 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301461 break;
1462 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05301463
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301464 spin_unlock_bh(&session->lock);
1465}
1466
1467static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
1468 *pasync_ctx, unsigned int is_header,
1469 unsigned int host_write_ptr)
1470{
1471 if (is_header)
1472 return &pasync_ctx->async_entry[host_write_ptr].
1473 header_busy_list;
1474 else
1475 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
1476}
1477
1478static struct async_pdu_handle *
1479hwi_get_async_handle(struct beiscsi_hba *phba,
1480 struct beiscsi_conn *beiscsi_conn,
1481 struct hwi_async_pdu_context *pasync_ctx,
1482 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
1483{
1484 struct be_bus_address phys_addr;
1485 struct list_head *pbusy_list;
1486 struct async_pdu_handle *pasync_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301487 unsigned char is_header = 0;
1488
1489 phys_addr.u.a32.address_lo =
1490 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1491 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1492 & PDUCQE_DPL_MASK) >> 16);
1493 phys_addr.u.a32.address_hi =
1494 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1495
1496 phys_addr.u.a64.address =
1497 *((unsigned long long *)(&phys_addr.u.a64.address));
1498
1499 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1500 & PDUCQE_CODE_MASK) {
1501 case UNSOL_HDR_NOTIFY:
1502 is_header = 1;
1503
1504 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1505 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1506 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301507 break;
1508 case UNSOL_DATA_NOTIFY:
1509 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1510 dw[offsetof(struct amap_i_t_dpdu_cqe,
1511 index) / 32] & PDUCQE_INDEX_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301512 break;
1513 default:
1514 pbusy_list = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05301515 beiscsi_log(phba, KERN_WARNING,
1516 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1517 "BM_%d : Unexpected code=%d\n",
1518 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1519 code) / 32] & PDUCQE_CODE_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301520 return NULL;
1521 }
1522
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301523 WARN_ON(list_empty(pbusy_list));
1524 list_for_each_entry(pasync_handle, pbusy_list, link) {
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001525 if (pasync_handle->pa.u.a64.address == phys_addr.u.a64.address)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301526 break;
1527 }
1528
1529 WARN_ON(!pasync_handle);
1530
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301531 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1532 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301533 pasync_handle->is_header = is_header;
1534 pasync_handle->buffer_len = ((pdpdu_cqe->
1535 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1536 & PDUCQE_DPL_MASK) >> 16);
1537
1538 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1539 index) / 32] & PDUCQE_INDEX_MASK);
1540 return pasync_handle;
1541}
1542
1543static unsigned int
John Soni Jose99bc5d52012-08-20 23:00:18 +05301544hwi_update_async_writables(struct beiscsi_hba *phba,
1545 struct hwi_async_pdu_context *pasync_ctx,
1546 unsigned int is_header, unsigned int cq_index)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301547{
1548 struct list_head *pbusy_list;
1549 struct async_pdu_handle *pasync_handle;
1550 unsigned int num_entries, writables = 0;
1551 unsigned int *pep_read_ptr, *pwritables;
1552
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001553 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301554 if (is_header) {
1555 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1556 pwritables = &pasync_ctx->async_header.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301557 } else {
1558 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1559 pwritables = &pasync_ctx->async_data.writables;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301560 }
1561
1562 while ((*pep_read_ptr) != cq_index) {
1563 (*pep_read_ptr)++;
1564 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1565
1566 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1567 *pep_read_ptr);
1568 if (writables == 0)
1569 WARN_ON(list_empty(pbusy_list));
1570
1571 if (!list_empty(pbusy_list)) {
1572 pasync_handle = list_entry(pbusy_list->next,
1573 struct async_pdu_handle,
1574 link);
1575 WARN_ON(!pasync_handle);
1576 pasync_handle->consumed = 1;
1577 }
1578
1579 writables++;
1580 }
1581
1582 if (!writables) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05301583 beiscsi_log(phba, KERN_ERR,
1584 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1585 "BM_%d : Duplicate notification received - index 0x%x!!\n",
1586 cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301587 WARN_ON(1);
1588 }
1589
1590 *pwritables = *pwritables + writables;
1591 return 0;
1592}
1593
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001594static void hwi_free_async_msg(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301595 unsigned int cri)
1596{
1597 struct hwi_controller *phwi_ctrlr;
1598 struct hwi_async_pdu_context *pasync_ctx;
1599 struct async_pdu_handle *pasync_handle, *tmp_handle;
1600 struct list_head *plist;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301601
1602 phwi_ctrlr = phba->phwi_ctrlr;
1603 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1604
1605 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1606
1607 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1608 list_del(&pasync_handle->link);
1609
Jayamohan Kallickal9728d8d2012-04-03 23:41:47 -05001610 if (pasync_handle->is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301611 list_add_tail(&pasync_handle->link,
1612 &pasync_ctx->async_header.free_list);
1613 pasync_ctx->async_header.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301614 } else {
1615 list_add_tail(&pasync_handle->link,
1616 &pasync_ctx->async_data.free_list);
1617 pasync_ctx->async_data.free_entries++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301618 }
1619 }
1620
1621 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1622 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1623 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301624}
1625
1626static struct phys_addr *
1627hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1628 unsigned int is_header, unsigned int host_write_ptr)
1629{
1630 struct phys_addr *pasync_sge = NULL;
1631
1632 if (is_header)
1633 pasync_sge = pasync_ctx->async_header.ring_base;
1634 else
1635 pasync_sge = pasync_ctx->async_data.ring_base;
1636
1637 return pasync_sge + host_write_ptr;
1638}
1639
1640static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1641 unsigned int is_header)
1642{
1643 struct hwi_controller *phwi_ctrlr;
1644 struct hwi_async_pdu_context *pasync_ctx;
1645 struct async_pdu_handle *pasync_handle;
1646 struct list_head *pfree_link, *pbusy_list;
1647 struct phys_addr *pasync_sge;
1648 unsigned int ring_id, num_entries;
1649 unsigned int host_write_num;
1650 unsigned int writables;
1651 unsigned int i = 0;
1652 u32 doorbell = 0;
1653
1654 phwi_ctrlr = phba->phwi_ctrlr;
1655 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05001656 num_entries = pasync_ctx->num_entries;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301657
1658 if (is_header) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301659 writables = min(pasync_ctx->async_header.writables,
1660 pasync_ctx->async_header.free_entries);
1661 pfree_link = pasync_ctx->async_header.free_list.next;
1662 host_write_num = pasync_ctx->async_header.host_write_ptr;
1663 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1664 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301665 writables = min(pasync_ctx->async_data.writables,
1666 pasync_ctx->async_data.free_entries);
1667 pfree_link = pasync_ctx->async_data.free_list.next;
1668 host_write_num = pasync_ctx->async_data.host_write_ptr;
1669 ring_id = phwi_ctrlr->default_pdu_data.id;
1670 }
1671
1672 writables = (writables / 8) * 8;
1673 if (writables) {
1674 for (i = 0; i < writables; i++) {
1675 pbusy_list =
1676 hwi_get_async_busy_list(pasync_ctx, is_header,
1677 host_write_num);
1678 pasync_handle =
1679 list_entry(pfree_link, struct async_pdu_handle,
1680 link);
1681 WARN_ON(!pasync_handle);
1682 pasync_handle->consumed = 0;
1683
1684 pfree_link = pfree_link->next;
1685
1686 pasync_sge = hwi_get_ring_address(pasync_ctx,
1687 is_header, host_write_num);
1688
1689 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1690 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1691
1692 list_move(&pasync_handle->link, pbusy_list);
1693
1694 host_write_num++;
1695 host_write_num = host_write_num % num_entries;
1696 }
1697
1698 if (is_header) {
1699 pasync_ctx->async_header.host_write_ptr =
1700 host_write_num;
1701 pasync_ctx->async_header.free_entries -= writables;
1702 pasync_ctx->async_header.writables -= writables;
1703 pasync_ctx->async_header.busy_entries += writables;
1704 } else {
1705 pasync_ctx->async_data.host_write_ptr = host_write_num;
1706 pasync_ctx->async_data.free_entries -= writables;
1707 pasync_ctx->async_data.writables -= writables;
1708 pasync_ctx->async_data.busy_entries += writables;
1709 }
1710
1711 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1712 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1713 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1714 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1715 << DB_DEF_PDU_CQPROC_SHIFT;
1716
1717 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1718 }
1719}
1720
1721static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1722 struct beiscsi_conn *beiscsi_conn,
1723 struct i_t_dpdu_cqe *pdpdu_cqe)
1724{
1725 struct hwi_controller *phwi_ctrlr;
1726 struct hwi_async_pdu_context *pasync_ctx;
1727 struct async_pdu_handle *pasync_handle = NULL;
1728 unsigned int cq_index = -1;
1729
1730 phwi_ctrlr = phba->phwi_ctrlr;
1731 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1732
1733 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1734 pdpdu_cqe, &cq_index);
1735 BUG_ON(pasync_handle->is_header != 0);
1736 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301737 hwi_update_async_writables(phba, pasync_ctx,
1738 pasync_handle->is_header, cq_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301739
1740 hwi_free_async_msg(phba, pasync_handle->cri);
1741 hwi_post_async_buffers(phba, pasync_handle->is_header);
1742}
1743
1744static unsigned int
1745hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1746 struct beiscsi_hba *phba,
1747 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1748{
1749 struct list_head *plist;
1750 struct async_pdu_handle *pasync_handle;
1751 void *phdr = NULL;
1752 unsigned int hdr_len = 0, buf_len = 0;
1753 unsigned int status, index = 0, offset = 0;
1754 void *pfirst_buffer = NULL;
1755 unsigned int num_buf = 0;
1756
1757 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1758
1759 list_for_each_entry(pasync_handle, plist, link) {
1760 if (index == 0) {
1761 phdr = pasync_handle->pbuffer;
1762 hdr_len = pasync_handle->buffer_len;
1763 } else {
1764 buf_len = pasync_handle->buffer_len;
1765 if (!num_buf) {
1766 pfirst_buffer = pasync_handle->pbuffer;
1767 num_buf++;
1768 }
1769 memcpy(pfirst_buffer + offset,
1770 pasync_handle->pbuffer, buf_len);
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001771 offset += buf_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301772 }
1773 index++;
1774 }
1775
1776 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301777 (beiscsi_conn->beiscsi_conn_cid -
1778 phba->fw_config.iscsi_cid_start),
1779 phdr, hdr_len, pfirst_buffer,
Jayamohan Kallickalf2ba02b2012-04-03 23:41:37 -05001780 offset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301781
Jayamohan Kallickal605c6cd2012-04-03 23:41:48 -05001782 hwi_free_async_msg(phba, cri);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301783 return 0;
1784}
1785
1786static unsigned int
1787hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1788 struct beiscsi_hba *phba,
1789 struct async_pdu_handle *pasync_handle)
1790{
1791 struct hwi_async_pdu_context *pasync_ctx;
1792 struct hwi_controller *phwi_ctrlr;
1793 unsigned int bytes_needed = 0, status = 0;
1794 unsigned short cri = pasync_handle->cri;
1795 struct pdu_base *ppdu;
1796
1797 phwi_ctrlr = phba->phwi_ctrlr;
1798 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1799
1800 list_del(&pasync_handle->link);
1801 if (pasync_handle->is_header) {
1802 pasync_ctx->async_header.busy_entries--;
1803 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1804 hwi_free_async_msg(phba, cri);
1805 BUG();
1806 }
1807
1808 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1809 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1810 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1811 (unsigned short)pasync_handle->buffer_len;
1812 list_add_tail(&pasync_handle->link,
1813 &pasync_ctx->async_entry[cri].wait_queue.list);
1814
1815 ppdu = pasync_handle->pbuffer;
1816 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1817 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1818 0xFFFF0000) | ((be16_to_cpu((ppdu->
1819 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1820 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1821
1822 if (status == 0) {
1823 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1824 bytes_needed;
1825
1826 if (bytes_needed == 0)
1827 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1828 pasync_ctx, cri);
1829 }
1830 } else {
1831 pasync_ctx->async_data.busy_entries--;
1832 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1833 list_add_tail(&pasync_handle->link,
1834 &pasync_ctx->async_entry[cri].wait_queue.
1835 list);
1836 pasync_ctx->async_entry[cri].wait_queue.
1837 bytes_received +=
1838 (unsigned short)pasync_handle->buffer_len;
1839
1840 if (pasync_ctx->async_entry[cri].wait_queue.
1841 bytes_received >=
1842 pasync_ctx->async_entry[cri].wait_queue.
1843 bytes_needed)
1844 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1845 pasync_ctx, cri);
1846 }
1847 }
1848 return status;
1849}
1850
1851static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1852 struct beiscsi_hba *phba,
1853 struct i_t_dpdu_cqe *pdpdu_cqe)
1854{
1855 struct hwi_controller *phwi_ctrlr;
1856 struct hwi_async_pdu_context *pasync_ctx;
1857 struct async_pdu_handle *pasync_handle = NULL;
1858 unsigned int cq_index = -1;
1859
1860 phwi_ctrlr = phba->phwi_ctrlr;
1861 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1862 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1863 pdpdu_cqe, &cq_index);
1864
1865 if (pasync_handle->consumed == 0)
John Soni Jose99bc5d52012-08-20 23:00:18 +05301866 hwi_update_async_writables(phba, pasync_ctx,
1867 pasync_handle->is_header, cq_index);
1868
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301869 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1870 hwi_post_async_buffers(phba, pasync_handle->is_header);
1871}
1872
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301873static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1874{
1875 struct be_queue_info *mcc_cq;
1876 struct be_mcc_compl *mcc_compl;
1877 unsigned int num_processed = 0;
1878
1879 mcc_cq = &phba->ctrl.mcc_obj.cq;
1880 mcc_compl = queue_tail_node(mcc_cq);
1881 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1882 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1883
1884 if (num_processed >= 32) {
1885 hwi_ring_cq_db(phba, mcc_cq->id,
1886 num_processed, 0, 0);
1887 num_processed = 0;
1888 }
1889 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1890 /* Interpret flags as an async trailer */
1891 if (is_link_state_evt(mcc_compl->flags))
1892 /* Interpret compl as a async link evt */
1893 beiscsi_async_link_state_process(phba,
1894 (struct be_async_event_link_state *) mcc_compl);
1895 else
John Soni Jose99bc5d52012-08-20 23:00:18 +05301896 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_MBOX,
1897 "BM_%d : Unsupported Async Event, flags"
1898 " = 0x%08x\n",
1899 mcc_compl->flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301900 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1901 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1902 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1903 }
1904
1905 mcc_compl->flags = 0;
1906 queue_tail_inc(mcc_cq);
1907 mcc_compl = queue_tail_node(mcc_cq);
1908 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1909 num_processed++;
1910 }
1911
1912 if (num_processed > 0)
1913 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1914
1915}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301916
1917static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301918{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301919 struct be_queue_info *cq;
1920 struct sol_cqe *sol;
1921 struct dmsg_cqe *dmsg;
1922 unsigned int num_processed = 0;
1923 unsigned int tot_nump = 0;
John Soni Jose0a513dd2012-08-20 23:00:55 +05301924 unsigned short code = 0, cid = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301925 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301926 struct beiscsi_endpoint *beiscsi_ep;
1927 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301928 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301929
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301930 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301931 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301932 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301933
1934 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1935 CQE_VALID_MASK) {
1936 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1937
John Soni Jose0a513dd2012-08-20 23:00:55 +05301938 cid = ((sol->dw[offsetof(struct amap_sol_cqe, cid)/32] &
1939 CQE_CID_MASK) >> 6);
1940 code = (sol->dw[offsetof(struct amap_sol_cqe, code)/32] &
1941 CQE_CODE_MASK);
1942 ep = phba->ep_array[cid - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301943
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301944 beiscsi_ep = ep->dd_data;
1945 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301946
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301947 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301948 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301949 num_processed, 0, 0);
1950 tot_nump += num_processed;
1951 num_processed = 0;
1952 }
1953
John Soni Jose0a513dd2012-08-20 23:00:55 +05301954 switch (code) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301955 case SOL_CMD_COMPLETE:
1956 hwi_complete_cmd(beiscsi_conn, phba, sol);
1957 break;
1958 case DRIVERMSG_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301959 beiscsi_log(phba, KERN_INFO,
1960 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1961 "BM_%d : Received DRIVERMSG_NOTIFY\n");
1962
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301963 dmsg = (struct dmsg_cqe *)sol;
1964 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1965 break;
1966 case UNSOL_HDR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301967 beiscsi_log(phba, KERN_INFO,
1968 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1969 "BM_%d : Received UNSOL_HDR_ NOTIFY\n");
1970
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301971 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1972 (struct i_t_dpdu_cqe *)sol);
1973 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301974 case UNSOL_DATA_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301975 beiscsi_log(phba, KERN_INFO,
1976 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
1977 "BM_%d : Received UNSOL_DATA_NOTIFY\n");
1978
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301979 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1980 (struct i_t_dpdu_cqe *)sol);
1981 break;
1982 case CXN_INVALIDATE_INDEX_NOTIFY:
1983 case CMD_INVALIDATED_NOTIFY:
1984 case CXN_INVALIDATE_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301985 beiscsi_log(phba, KERN_ERR,
1986 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
1987 "BM_%d : Ignoring CQ Error notification for"
1988 " cmd/cxn invalidate\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301989 break;
1990 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1991 case CMD_KILLED_INVALID_STATSN_RCVD:
1992 case CMD_KILLED_INVALID_R2T_RCVD:
1993 case CMD_CXN_KILLED_LUN_INVALID:
1994 case CMD_CXN_KILLED_ICD_INVALID:
1995 case CMD_CXN_KILLED_ITT_INVALID:
1996 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1997 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05301998 beiscsi_log(phba, KERN_ERR,
1999 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2000 "BM_%d : CQ Error notification for cmd.. "
John Soni Jose0a513dd2012-08-20 23:00:55 +05302001 "code %d cid 0x%x\n", code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302002 break;
2003 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302004 beiscsi_log(phba, KERN_ERR,
2005 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2006 "BM_%d : Digest error on def pdu ring,"
2007 " dropping..\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302008 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
2009 (struct i_t_dpdu_cqe *) sol);
2010 break;
2011 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
2012 case CXN_KILLED_BURST_LEN_MISMATCH:
2013 case CXN_KILLED_AHS_RCVD:
2014 case CXN_KILLED_HDR_DIGEST_ERR:
2015 case CXN_KILLED_UNKNOWN_HDR:
2016 case CXN_KILLED_STALE_ITT_TTT_RCVD:
2017 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
2018 case CXN_KILLED_TIMED_OUT:
2019 case CXN_KILLED_FIN_RCVD:
2020 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
2021 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
2022 case CXN_KILLED_OVER_RUN_RESIDUAL:
2023 case CXN_KILLED_UNDER_RUN_RESIDUAL:
2024 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302025 beiscsi_log(phba, KERN_ERR,
2026 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2027 "BM_%d : CQ Error %d, reset CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302028 code, cid);
2029 if (beiscsi_conn)
2030 iscsi_conn_failure(beiscsi_conn->conn,
2031 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302032 break;
2033 case CXN_KILLED_RST_SENT:
2034 case CXN_KILLED_RST_RCVD:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302035 beiscsi_log(phba, KERN_ERR,
2036 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2037 "BM_%d : CQ Error %d, reset"
2038 "received/sent on CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302039 code, cid);
2040 if (beiscsi_conn)
2041 iscsi_conn_failure(beiscsi_conn->conn,
2042 ISCSI_ERR_CONN_FAILED);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302043 break;
2044 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05302045 beiscsi_log(phba, KERN_ERR,
2046 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
2047 "BM_%d : CQ Error Invalid code= %d "
2048 "received on CID 0x%x...\n",
John Soni Jose0a513dd2012-08-20 23:00:55 +05302049 code, cid);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302050 break;
2051 }
2052
2053 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
2054 queue_tail_inc(cq);
2055 sol = queue_tail_node(cq);
2056 num_processed++;
2057 }
2058
2059 if (num_processed > 0) {
2060 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302061 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302062 }
2063 return tot_nump;
2064}
2065
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302066void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302067{
2068 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302069 struct hwi_controller *phwi_ctrlr;
2070 struct hwi_context_memory *phwi_context;
2071 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302072 struct beiscsi_hba *phba =
2073 container_of(work, struct beiscsi_hba, work_cqs);
2074
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302075 phwi_ctrlr = phba->phwi_ctrlr;
2076 phwi_context = phwi_ctrlr->phwi_ctxt;
2077 if (phba->msix_enabled)
2078 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
2079 else
2080 pbe_eq = &phwi_context->be_eq[0];
2081
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302082 if (phba->todo_mcc_cq) {
2083 spin_lock_irqsave(&phba->isr_lock, flags);
2084 phba->todo_mcc_cq = 0;
2085 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05302086 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302087 }
2088
2089 if (phba->todo_cq) {
2090 spin_lock_irqsave(&phba->isr_lock, flags);
2091 phba->todo_cq = 0;
2092 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302093 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302094 }
2095}
2096
2097static int be_iopoll(struct blk_iopoll *iop, int budget)
2098{
2099 static unsigned int ret;
2100 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302101 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302102
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302103 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
2104 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302105 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302106 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302107 blk_iopoll_complete(iop);
John Soni Jose99bc5d52012-08-20 23:00:18 +05302108 beiscsi_log(phba, KERN_INFO,
2109 BEISCSI_LOG_CONFIG | BEISCSI_LOG_IO,
2110 "BM_%d : rearm pbe_eq->q.id =%d\n",
2111 pbe_eq->q.id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302112 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302113 }
2114 return ret;
2115}
2116
2117static void
2118hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
2119 unsigned int num_sg, struct beiscsi_io_task *io_task)
2120{
2121 struct iscsi_sge *psgl;
Jayamohan Kallickal58ff4bd2010-10-06 23:46:47 +05302122 unsigned int sg_len, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302123 unsigned int sge_len = 0;
2124 unsigned long long addr;
2125 struct scatterlist *l_sg;
2126 unsigned int offset;
2127
2128 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2129 io_task->bhs_pa.u.a32.address_lo);
2130 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2131 io_task->bhs_pa.u.a32.address_hi);
2132
2133 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302134 for (index = 0; (index < num_sg) && (index < 2); index++,
2135 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302136 if (index == 0) {
2137 sg_len = sg_dma_len(sg);
2138 addr = (u64) sg_dma_address(sg);
2139 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302140 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302141 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302142 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302143 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2144 sg_len);
2145 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302146 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302147 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
2148 pwrb, sge_len);
2149 sg_len = sg_dma_len(sg);
2150 addr = (u64) sg_dma_address(sg);
2151 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302152 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302153 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302154 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302155 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
2156 sg_len);
2157 }
2158 }
2159 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2160 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
2161
2162 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
2163
2164 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2165 io_task->bhs_pa.u.a32.address_hi);
2166 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2167 io_task->bhs_pa.u.a32.address_lo);
2168
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05302169 if (num_sg == 1) {
2170 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2171 1);
2172 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2173 0);
2174 } else if (num_sg == 2) {
2175 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2176 0);
2177 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2178 1);
2179 } else {
2180 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
2181 0);
2182 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
2183 0);
2184 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302185 sg = l_sg;
2186 psgl++;
2187 psgl++;
2188 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05302189 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302190 sg_len = sg_dma_len(sg);
2191 addr = (u64) sg_dma_address(sg);
2192 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2193 (addr & 0xFFFFFFFF));
2194 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2195 (addr >> 32));
2196 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
2197 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
2198 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2199 offset += sg_len;
2200 }
2201 psgl--;
2202 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2203}
2204
2205static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
2206{
2207 struct iscsi_sge *psgl;
2208 unsigned long long addr;
2209 struct beiscsi_io_task *io_task = task->dd_data;
2210 struct beiscsi_conn *beiscsi_conn = io_task->conn;
2211 struct beiscsi_hba *phba = beiscsi_conn->phba;
2212
2213 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
2214 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
2215 io_task->bhs_pa.u.a32.address_lo);
2216 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
2217 io_task->bhs_pa.u.a32.address_hi);
2218
2219 if (task->data) {
2220 if (task->data_count) {
2221 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
2222 addr = (u64) pci_map_single(phba->pcidev,
2223 task->data,
2224 task->data_count, 1);
2225 } else {
2226 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2227 addr = 0;
2228 }
2229 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302230 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302231 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302232 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302233 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
2234 task->data_count);
2235
2236 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
2237 } else {
2238 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
2239 addr = 0;
2240 }
2241
2242 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
2243
2244 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
2245
2246 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
2247 io_task->bhs_pa.u.a32.address_hi);
2248 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
2249 io_task->bhs_pa.u.a32.address_lo);
2250 if (task->data) {
2251 psgl++;
2252 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
2253 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
2254 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
2255 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
2256 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
2257 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
2258
2259 psgl++;
2260 if (task->data) {
2261 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302262 ((u32)(addr & 0xFFFFFFFF)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302263 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302264 ((u32)(addr >> 32)));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302265 }
2266 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
2267 }
2268 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
2269}
2270
2271static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
2272{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302273 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302274 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
2275 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
2276
2277 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2278 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302279 num_async_pdu_buf_pages =
2280 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2281 phba->params.defpdu_hdr_sz);
2282 num_async_pdu_buf_sgl_pages =
2283 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2284 sizeof(struct phys_addr));
2285 num_async_pdu_data_pages =
2286 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2287 phba->params.defpdu_data_sz);
2288 num_async_pdu_data_sgl_pages =
2289 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
2290 sizeof(struct phys_addr));
2291
2292 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
2293
2294 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
2295 BE_ISCSI_PDU_HEADER_SIZE;
2296 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
2297 sizeof(struct hwi_context_memory);
2298
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302299
2300 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
2301 * (phba->params.wrbs_per_cxn)
2302 * phba->params.cxns_per_ctrl;
2303 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
2304 (phba->params.wrbs_per_cxn);
2305 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
2306 phba->params.cxns_per_ctrl);
2307
2308 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
2309 phba->params.icds_per_ctrl;
2310 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
2311 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
2312
2313 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
2314 num_async_pdu_buf_pages * PAGE_SIZE;
2315 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
2316 num_async_pdu_data_pages * PAGE_SIZE;
2317 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
2318 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
2319 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
2320 num_async_pdu_data_sgl_pages * PAGE_SIZE;
2321 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
2322 phba->params.asyncpdus_per_ctrl *
2323 sizeof(struct async_pdu_handle);
2324 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
2325 phba->params.asyncpdus_per_ctrl *
2326 sizeof(struct async_pdu_handle);
2327 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
2328 sizeof(struct hwi_async_pdu_context) +
2329 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
2330}
2331
2332static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
2333{
2334 struct be_mem_descriptor *mem_descr;
2335 dma_addr_t bus_add;
2336 struct mem_array *mem_arr, *mem_arr_orig;
2337 unsigned int i, j, alloc_size, curr_alloc_size;
2338
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002339 phba->phwi_ctrlr = kzalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302340 if (!phba->phwi_ctrlr)
2341 return -ENOMEM;
2342
2343 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
2344 GFP_KERNEL);
2345 if (!phba->init_mem) {
2346 kfree(phba->phwi_ctrlr);
2347 return -ENOMEM;
2348 }
2349
2350 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
2351 GFP_KERNEL);
2352 if (!mem_arr_orig) {
2353 kfree(phba->init_mem);
2354 kfree(phba->phwi_ctrlr);
2355 return -ENOMEM;
2356 }
2357
2358 mem_descr = phba->init_mem;
2359 for (i = 0; i < SE_MEM_MAX; i++) {
2360 j = 0;
2361 mem_arr = mem_arr_orig;
2362 alloc_size = phba->mem_req[i];
2363 memset(mem_arr, 0, sizeof(struct mem_array) *
2364 BEISCSI_MAX_FRAGS_INIT);
2365 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
2366 do {
2367 mem_arr->virtual_address = pci_alloc_consistent(
2368 phba->pcidev,
2369 curr_alloc_size,
2370 &bus_add);
2371 if (!mem_arr->virtual_address) {
2372 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
2373 goto free_mem;
2374 if (curr_alloc_size -
2375 rounddown_pow_of_two(curr_alloc_size))
2376 curr_alloc_size = rounddown_pow_of_two
2377 (curr_alloc_size);
2378 else
2379 curr_alloc_size = curr_alloc_size / 2;
2380 } else {
2381 mem_arr->bus_address.u.
2382 a64.address = (__u64) bus_add;
2383 mem_arr->size = curr_alloc_size;
2384 alloc_size -= curr_alloc_size;
2385 curr_alloc_size = min(be_max_phys_size *
2386 1024, alloc_size);
2387 j++;
2388 mem_arr++;
2389 }
2390 } while (alloc_size);
2391 mem_descr->num_elements = j;
2392 mem_descr->size_in_bytes = phba->mem_req[i];
2393 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
2394 GFP_KERNEL);
2395 if (!mem_descr->mem_array)
2396 goto free_mem;
2397
2398 memcpy(mem_descr->mem_array, mem_arr_orig,
2399 sizeof(struct mem_array) * j);
2400 mem_descr++;
2401 }
2402 kfree(mem_arr_orig);
2403 return 0;
2404free_mem:
2405 mem_descr->num_elements = j;
2406 while ((i) || (j)) {
2407 for (j = mem_descr->num_elements; j > 0; j--) {
2408 pci_free_consistent(phba->pcidev,
2409 mem_descr->mem_array[j - 1].size,
2410 mem_descr->mem_array[j - 1].
2411 virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302412 (unsigned long)mem_descr->
2413 mem_array[j - 1].
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302414 bus_address.u.a64.address);
2415 }
2416 if (i) {
2417 i--;
2418 kfree(mem_descr->mem_array);
2419 mem_descr--;
2420 }
2421 }
2422 kfree(mem_arr_orig);
2423 kfree(phba->init_mem);
2424 kfree(phba->phwi_ctrlr);
2425 return -ENOMEM;
2426}
2427
2428static int beiscsi_get_memory(struct beiscsi_hba *phba)
2429{
2430 beiscsi_find_mem_req(phba);
2431 return beiscsi_alloc_mem(phba);
2432}
2433
2434static void iscsi_init_global_templates(struct beiscsi_hba *phba)
2435{
2436 struct pdu_data_out *pdata_out;
2437 struct pdu_nop_out *pnop_out;
2438 struct be_mem_descriptor *mem_descr;
2439
2440 mem_descr = phba->init_mem;
2441 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
2442 pdata_out =
2443 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
2444 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2445
2446 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
2447 IIOC_SCSI_DATA);
2448
2449 pnop_out =
2450 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
2451 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
2452
2453 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
2454 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
2455 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
2456 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
2457}
2458
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002459static int beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302460{
2461 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002462 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302463 struct hwi_controller *phwi_ctrlr;
2464 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002465 struct iscsi_wrb *pwrb = NULL;
2466 unsigned int num_cxn_wrbh = 0;
2467 unsigned int num_cxn_wrb = 0, j, idx = 0, index;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302468
2469 mem_descr_wrbh = phba->init_mem;
2470 mem_descr_wrbh += HWI_MEM_WRBH;
2471
2472 mem_descr_wrb = phba->init_mem;
2473 mem_descr_wrb += HWI_MEM_WRB;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302474 phwi_ctrlr = phba->phwi_ctrlr;
2475
2476 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2477 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302478 pwrb_context->pwrb_handle_base =
2479 kzalloc(sizeof(struct wrb_handle *) *
2480 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002481 if (!pwrb_context->pwrb_handle_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302482 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2483 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002484 goto init_wrb_hndl_failed;
2485 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302486 pwrb_context->pwrb_handle_basestd =
2487 kzalloc(sizeof(struct wrb_handle *) *
2488 phba->params.wrbs_per_cxn, GFP_KERNEL);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002489 if (!pwrb_context->pwrb_handle_basestd) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302490 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2491 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002492 goto init_wrb_hndl_failed;
2493 }
2494 if (!num_cxn_wrbh) {
2495 pwrb_handle =
2496 mem_descr_wrbh->mem_array[idx].virtual_address;
2497 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
2498 ((sizeof(struct wrb_handle)) *
2499 phba->params.wrbs_per_cxn));
2500 idx++;
2501 }
2502 pwrb_context->alloc_index = 0;
2503 pwrb_context->wrb_handles_available = 0;
2504 pwrb_context->free_index = 0;
2505
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302506 if (num_cxn_wrbh) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302507 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2508 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2509 pwrb_context->pwrb_handle_basestd[j] =
2510 pwrb_handle;
2511 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302512 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302513 pwrb_handle++;
2514 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302515 num_cxn_wrbh--;
2516 }
2517 }
2518 idx = 0;
Jayamohan Kallickaled58ea22010-02-20 08:05:07 +05302519 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302520 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002521 if (!num_cxn_wrb) {
2522 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2523 num_cxn_wrb = (mem_descr_wrb->mem_array[idx].size) /
2524 ((sizeof(struct iscsi_wrb) *
2525 phba->params.wrbs_per_cxn));
2526 idx++;
2527 }
2528
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302529 if (num_cxn_wrb) {
2530 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2531 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2532 pwrb_handle->pwrb = pwrb;
2533 pwrb++;
2534 }
2535 num_cxn_wrb--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302536 }
2537 }
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05002538 return 0;
2539init_wrb_hndl_failed:
2540 for (j = index; j > 0; j--) {
2541 pwrb_context = &phwi_ctrlr->wrb_context[j];
2542 kfree(pwrb_context->pwrb_handle_base);
2543 kfree(pwrb_context->pwrb_handle_basestd);
2544 }
2545 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302546}
2547
2548static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2549{
2550 struct hwi_controller *phwi_ctrlr;
2551 struct hba_parameters *p = &phba->params;
2552 struct hwi_async_pdu_context *pasync_ctx;
2553 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002554 unsigned int index, idx, num_per_mem, num_async_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302555 struct be_mem_descriptor *mem_descr;
2556
2557 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2558 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2559
2560 phwi_ctrlr = phba->phwi_ctrlr;
2561 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2562 mem_descr->mem_array[0].virtual_address;
2563 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2564 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2565
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002566 pasync_ctx->num_entries = p->asyncpdus_per_ctrl;
2567 pasync_ctx->buffer_size = p->defpdu_hdr_sz;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302568
2569 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2570 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2571 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302572 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2573 "BM_%d : hwi_init_async_pdu_ctx"
2574 " HWI_MEM_ASYNC_HEADER_BUF va=%p\n",
2575 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302576 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302577 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2578 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302579
2580 pasync_ctx->async_header.va_base =
2581 mem_descr->mem_array[0].virtual_address;
2582
2583 pasync_ctx->async_header.pa_base.u.a64.address =
2584 mem_descr->mem_array[0].bus_address.u.a64.address;
2585
2586 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2587 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2588 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302589 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2590 "BM_%d : hwi_init_async_pdu_ctx"
2591 " HWI_MEM_ASYNC_HEADER_RING va=%p\n",
2592 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302593 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302594 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2595 "BM_%d : No Virtual address\n");
2596
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302597 pasync_ctx->async_header.ring_base =
2598 mem_descr->mem_array[0].virtual_address;
2599
2600 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2601 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2602 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302603 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2604 "BM_%d : hwi_init_async_pdu_ctx"
2605 " HWI_MEM_ASYNC_HEADER_HANDLE va=%p\n",
2606 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302607 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302608 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2609 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302610
2611 pasync_ctx->async_header.handle_base =
2612 mem_descr->mem_array[0].virtual_address;
2613 pasync_ctx->async_header.writables = 0;
2614 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2615
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302616
2617 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2618 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2619 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302620 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2621 "BM_%d : hwi_init_async_pdu_ctx"
2622 " HWI_MEM_ASYNC_DATA_RING va=%p\n",
2623 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302624 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302625 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2626 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302627
2628 pasync_ctx->async_data.ring_base =
2629 mem_descr->mem_array[0].virtual_address;
2630
2631 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2632 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2633 if (!mem_descr->mem_array[0].virtual_address)
John Soni Jose99bc5d52012-08-20 23:00:18 +05302634 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2635 "BM_%d : No Virtual address\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302636
2637 pasync_ctx->async_data.handle_base =
2638 mem_descr->mem_array[0].virtual_address;
2639 pasync_ctx->async_data.writables = 0;
2640 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2641
2642 pasync_header_h =
2643 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2644 pasync_data_h =
2645 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2646
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002647 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2648 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2649 if (mem_descr->mem_array[0].virtual_address) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302650 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2651 "BM_%d : hwi_init_async_pdu_ctx"
2652 " HWI_MEM_ASYNC_DATA_BUF va=%p\n",
2653 mem_descr->mem_array[0].virtual_address);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002654 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05302655 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
2656 "BM_%d : No Virtual address\n");
2657
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002658 idx = 0;
2659 pasync_ctx->async_data.va_base =
2660 mem_descr->mem_array[idx].virtual_address;
2661 pasync_ctx->async_data.pa_base.u.a64.address =
2662 mem_descr->mem_array[idx].bus_address.u.a64.address;
2663
2664 num_async_data = ((mem_descr->mem_array[idx].size) /
2665 phba->params.defpdu_data_sz);
2666 num_per_mem = 0;
2667
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302668 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2669 pasync_header_h->cri = -1;
2670 pasync_header_h->index = (char)index;
2671 INIT_LIST_HEAD(&pasync_header_h->link);
2672 pasync_header_h->pbuffer =
2673 (void *)((unsigned long)
2674 (pasync_ctx->async_header.va_base) +
2675 (p->defpdu_hdr_sz * index));
2676
2677 pasync_header_h->pa.u.a64.address =
2678 pasync_ctx->async_header.pa_base.u.a64.address +
2679 (p->defpdu_hdr_sz * index);
2680
2681 list_add_tail(&pasync_header_h->link,
2682 &pasync_ctx->async_header.free_list);
2683 pasync_header_h++;
2684 pasync_ctx->async_header.free_entries++;
2685 pasync_ctx->async_header.writables++;
2686
2687 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2688 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2689 header_busy_list);
2690 pasync_data_h->cri = -1;
2691 pasync_data_h->index = (char)index;
2692 INIT_LIST_HEAD(&pasync_data_h->link);
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002693
2694 if (!num_async_data) {
2695 num_per_mem = 0;
2696 idx++;
2697 pasync_ctx->async_data.va_base =
2698 mem_descr->mem_array[idx].virtual_address;
2699 pasync_ctx->async_data.pa_base.u.a64.address =
2700 mem_descr->mem_array[idx].
2701 bus_address.u.a64.address;
2702
2703 num_async_data = ((mem_descr->mem_array[idx].size) /
2704 phba->params.defpdu_data_sz);
2705 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302706 pasync_data_h->pbuffer =
2707 (void *)((unsigned long)
2708 (pasync_ctx->async_data.va_base) +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002709 (p->defpdu_data_sz * num_per_mem));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302710
2711 pasync_data_h->pa.u.a64.address =
2712 pasync_ctx->async_data.pa_base.u.a64.address +
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05002713 (p->defpdu_data_sz * num_per_mem);
2714 num_per_mem++;
2715 num_async_data--;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302716
2717 list_add_tail(&pasync_data_h->link,
2718 &pasync_ctx->async_data.free_list);
2719 pasync_data_h++;
2720 pasync_ctx->async_data.free_entries++;
2721 pasync_ctx->async_data.writables++;
2722
2723 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2724 }
2725
2726 pasync_ctx->async_header.host_write_ptr = 0;
2727 pasync_ctx->async_header.ep_read_ptr = -1;
2728 pasync_ctx->async_data.host_write_ptr = 0;
2729 pasync_ctx->async_data.ep_read_ptr = -1;
2730}
2731
2732static int
2733be_sgl_create_contiguous(void *virtual_address,
2734 u64 physical_address, u32 length,
2735 struct be_dma_mem *sgl)
2736{
2737 WARN_ON(!virtual_address);
2738 WARN_ON(!physical_address);
2739 WARN_ON(!length > 0);
2740 WARN_ON(!sgl);
2741
2742 sgl->va = virtual_address;
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302743 sgl->dma = (unsigned long)physical_address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302744 sgl->size = length;
2745
2746 return 0;
2747}
2748
2749static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2750{
2751 memset(sgl, 0, sizeof(*sgl));
2752}
2753
2754static void
2755hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2756 struct mem_array *pmem, struct be_dma_mem *sgl)
2757{
2758 if (sgl->va)
2759 be_sgl_destroy_contiguous(sgl);
2760
2761 be_sgl_create_contiguous(pmem->virtual_address,
2762 pmem->bus_address.u.a64.address,
2763 pmem->size, sgl);
2764}
2765
2766static void
2767hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2768 struct mem_array *pmem, struct be_dma_mem *sgl)
2769{
2770 if (sgl->va)
2771 be_sgl_destroy_contiguous(sgl);
2772
2773 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2774 pmem->bus_address.u.a64.address,
2775 pmem->size, sgl);
2776}
2777
2778static int be_fill_queue(struct be_queue_info *q,
2779 u16 len, u16 entry_size, void *vaddress)
2780{
2781 struct be_dma_mem *mem = &q->dma_mem;
2782
2783 memset(q, 0, sizeof(*q));
2784 q->len = len;
2785 q->entry_size = entry_size;
2786 mem->size = len * entry_size;
2787 mem->va = vaddress;
2788 if (!mem->va)
2789 return -ENOMEM;
2790 memset(mem->va, 0, mem->size);
2791 return 0;
2792}
2793
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302794static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302795 struct hwi_context_memory *phwi_context)
2796{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302797 unsigned int i, num_eq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302798 int ret = 0, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302799 struct be_queue_info *eq;
2800 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302801 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302802 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302803
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302804 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2805 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302806
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302807 if (phba->msix_enabled)
2808 eq_for_mcc = 1;
2809 else
2810 eq_for_mcc = 0;
2811 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2812 eq = &phwi_context->be_eq[i].q;
2813 mem = &eq->dma_mem;
2814 phwi_context->be_eq[i].phba = phba;
2815 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2816 num_eq_pages * PAGE_SIZE,
2817 &paddr);
2818 if (!eq_vaddress)
2819 goto create_eq_error;
2820
2821 mem->va = eq_vaddress;
2822 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2823 sizeof(struct be_eq_entry), eq_vaddress);
2824 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302825 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2826 "BM_%d : be_fill_queue Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302827 goto create_eq_error;
2828 }
2829
2830 mem->dma = paddr;
2831 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2832 phwi_context->cur_eqd);
2833 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302834 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2835 "BM_%d : beiscsi_cmd_eq_create"
2836 "Failed for EQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302837 goto create_eq_error;
2838 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05302839
2840 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2841 "BM_%d : eqid = %d\n",
2842 phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302843 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302844 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302845create_eq_error:
2846 for (i = 0; i < (phba->num_cpus + 1); i++) {
2847 eq = &phwi_context->be_eq[i].q;
2848 mem = &eq->dma_mem;
2849 if (mem->va)
2850 pci_free_consistent(phba->pcidev, num_eq_pages
2851 * PAGE_SIZE,
2852 mem->va, mem->dma);
2853 }
2854 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302855}
2856
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302857static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302858 struct hwi_context_memory *phwi_context)
2859{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302860 unsigned int i, num_cq_pages;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302861 int ret = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302862 struct be_queue_info *cq, *eq;
2863 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302864 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302865 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302866 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302867
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302868 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2869 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302870
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302871 for (i = 0; i < phba->num_cpus; i++) {
2872 cq = &phwi_context->be_cq[i];
2873 eq = &phwi_context->be_eq[i].q;
2874 pbe_eq = &phwi_context->be_eq[i];
2875 pbe_eq->cq = cq;
2876 pbe_eq->phba = phba;
2877 mem = &cq->dma_mem;
2878 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2879 num_cq_pages * PAGE_SIZE,
2880 &paddr);
2881 if (!cq_vaddress)
2882 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302883 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302884 sizeof(struct sol_cqe), cq_vaddress);
2885 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302886 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2887 "BM_%d : be_fill_queue Failed "
2888 "for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302889 goto create_cq_error;
2890 }
2891
2892 mem->dma = paddr;
2893 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2894 false, 0);
2895 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302896 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2897 "BM_%d : beiscsi_cmd_eq_create"
2898 "Failed for ISCSI CQ\n");
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302899 goto create_cq_error;
2900 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05302901 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2902 "BM_%d : iscsi cq_id is %d for eq_id %d\n"
2903 "iSCSI CQ CREATED\n", cq->id, eq->id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302904 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302905 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302906
2907create_cq_error:
2908 for (i = 0; i < phba->num_cpus; i++) {
2909 cq = &phwi_context->be_cq[i];
2910 mem = &cq->dma_mem;
2911 if (mem->va)
2912 pci_free_consistent(phba->pcidev, num_cq_pages
2913 * PAGE_SIZE,
2914 mem->va, mem->dma);
2915 }
2916 return ret;
2917
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302918}
2919
2920static int
2921beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2922 struct hwi_context_memory *phwi_context,
2923 struct hwi_controller *phwi_ctrlr,
2924 unsigned int def_pdu_ring_sz)
2925{
2926 unsigned int idx;
2927 int ret;
2928 struct be_queue_info *dq, *cq;
2929 struct be_dma_mem *mem;
2930 struct be_mem_descriptor *mem_descr;
2931 void *dq_vaddress;
2932
2933 idx = 0;
2934 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302935 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302936 mem = &dq->dma_mem;
2937 mem_descr = phba->init_mem;
2938 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2939 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2940 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2941 sizeof(struct phys_addr),
2942 sizeof(struct phys_addr), dq_vaddress);
2943 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302944 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2945 "BM_%d : be_fill_queue Failed for DEF PDU HDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302946 return ret;
2947 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302948 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2949 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302950 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2951 def_pdu_ring_sz,
2952 phba->params.defpdu_hdr_sz);
2953 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302954 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2955 "BM_%d : be_cmd_create_default_pdu_queue Failed DEFHDR\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302956 return ret;
2957 }
2958 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05302959 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
2960 "BM_%d : iscsi def pdu id is %d\n",
2961 phwi_context->be_def_hdrq.id);
2962
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302963 hwi_post_async_buffers(phba, 1);
2964 return 0;
2965}
2966
2967static int
2968beiscsi_create_def_data(struct beiscsi_hba *phba,
2969 struct hwi_context_memory *phwi_context,
2970 struct hwi_controller *phwi_ctrlr,
2971 unsigned int def_pdu_ring_sz)
2972{
2973 unsigned int idx;
2974 int ret;
2975 struct be_queue_info *dataq, *cq;
2976 struct be_dma_mem *mem;
2977 struct be_mem_descriptor *mem_descr;
2978 void *dq_vaddress;
2979
2980 idx = 0;
2981 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302982 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302983 mem = &dataq->dma_mem;
2984 mem_descr = phba->init_mem;
2985 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2986 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2987 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2988 sizeof(struct phys_addr),
2989 sizeof(struct phys_addr), dq_vaddress);
2990 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05302991 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
2992 "BM_%d : be_fill_queue Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302993 return ret;
2994 }
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05302995 mem->dma = (unsigned long)mem_descr->mem_array[idx].
2996 bus_address.u.a64.address;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302997 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2998 def_pdu_ring_sz,
2999 phba->params.defpdu_data_sz);
3000 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303001 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3002 "BM_%d be_cmd_create_default_pdu_queue"
3003 " Failed for DEF PDU DATA\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303004 return ret;
3005 }
3006 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303007 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3008 "BM_%d : iscsi def data id is %d\n",
3009 phwi_context->be_def_dataq.id);
3010
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303011 hwi_post_async_buffers(phba, 0);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303012 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3013 "BM_%d : DEFAULT PDU DATA RING CREATED\n");
3014
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303015 return 0;
3016}
3017
3018static int
3019beiscsi_post_pages(struct beiscsi_hba *phba)
3020{
3021 struct be_mem_descriptor *mem_descr;
3022 struct mem_array *pm_arr;
3023 unsigned int page_offset, i;
3024 struct be_dma_mem sgl;
3025 int status;
3026
3027 mem_descr = phba->init_mem;
3028 mem_descr += HWI_MEM_SGE;
3029 pm_arr = mem_descr->mem_array;
3030
3031 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
3032 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
3033 for (i = 0; i < mem_descr->num_elements; i++) {
3034 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
3035 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
3036 page_offset,
3037 (pm_arr->size / PAGE_SIZE));
3038 page_offset += pm_arr->size / PAGE_SIZE;
3039 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303040 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3041 "BM_%d : post sgl failed.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303042 return status;
3043 }
3044 pm_arr++;
3045 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303046 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3047 "BM_%d : POSTED PAGES\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303048 return 0;
3049}
3050
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303051static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
3052{
3053 struct be_dma_mem *mem = &q->dma_mem;
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003054 if (mem->va) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303055 pci_free_consistent(phba->pcidev, mem->size,
3056 mem->va, mem->dma);
Jayamohan Kallickalc8b25592012-04-03 23:41:42 -05003057 mem->va = NULL;
3058 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303059}
3060
3061static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
3062 u16 len, u16 entry_size)
3063{
3064 struct be_dma_mem *mem = &q->dma_mem;
3065
3066 memset(q, 0, sizeof(*q));
3067 q->len = len;
3068 q->entry_size = entry_size;
3069 mem->size = len * entry_size;
3070 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
3071 if (!mem->va)
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303072 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303073 memset(mem->va, 0, mem->size);
3074 return 0;
3075}
3076
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303077static int
3078beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
3079 struct hwi_context_memory *phwi_context,
3080 struct hwi_controller *phwi_ctrlr)
3081{
3082 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
3083 u64 pa_addr_lo;
3084 unsigned int idx, num, i;
3085 struct mem_array *pwrb_arr;
3086 void *wrb_vaddr;
3087 struct be_dma_mem sgl;
3088 struct be_mem_descriptor *mem_descr;
3089 int status;
3090
3091 idx = 0;
3092 mem_descr = phba->init_mem;
3093 mem_descr += HWI_MEM_WRB;
3094 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
3095 GFP_KERNEL);
3096 if (!pwrb_arr) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303097 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3098 "BM_%d : Memory alloc failed in create wrb ring.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303099 return -ENOMEM;
3100 }
3101 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3102 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
3103 num_wrb_rings = mem_descr->mem_array[idx].size /
3104 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
3105
3106 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
3107 if (num_wrb_rings) {
3108 pwrb_arr[num].virtual_address = wrb_vaddr;
3109 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
3110 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3111 sizeof(struct iscsi_wrb);
3112 wrb_vaddr += pwrb_arr[num].size;
3113 pa_addr_lo += pwrb_arr[num].size;
3114 num_wrb_rings--;
3115 } else {
3116 idx++;
3117 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
3118 pa_addr_lo = mem_descr->mem_array[idx].\
3119 bus_address.u.a64.address;
3120 num_wrb_rings = mem_descr->mem_array[idx].size /
3121 (phba->params.wrbs_per_cxn *
3122 sizeof(struct iscsi_wrb));
3123 pwrb_arr[num].virtual_address = wrb_vaddr;
3124 pwrb_arr[num].bus_address.u.a64.address\
3125 = pa_addr_lo;
3126 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
3127 sizeof(struct iscsi_wrb);
3128 wrb_vaddr += pwrb_arr[num].size;
3129 pa_addr_lo += pwrb_arr[num].size;
3130 num_wrb_rings--;
3131 }
3132 }
3133 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3134 wrb_mem_index = 0;
3135 offset = 0;
3136 size = 0;
3137
3138 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
3139 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
3140 &phwi_context->be_wrbq[i]);
3141 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303142 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3143 "BM_%d : wrbq create failed.");
Dan Carpenter1462b8f2010-06-10 09:52:21 +02003144 kfree(pwrb_arr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303145 return status;
3146 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303147 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
3148 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303149 }
3150 kfree(pwrb_arr);
3151 return 0;
3152}
3153
3154static void free_wrb_handles(struct beiscsi_hba *phba)
3155{
3156 unsigned int index;
3157 struct hwi_controller *phwi_ctrlr;
3158 struct hwi_wrb_context *pwrb_context;
3159
3160 phwi_ctrlr = phba->phwi_ctrlr;
3161 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
3162 pwrb_context = &phwi_ctrlr->wrb_context[index];
3163 kfree(pwrb_context->pwrb_handle_base);
3164 kfree(pwrb_context->pwrb_handle_basestd);
3165 }
3166}
3167
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303168static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
3169{
3170 struct be_queue_info *q;
3171 struct be_ctrl_info *ctrl = &phba->ctrl;
3172
3173 q = &phba->ctrl.mcc_obj.q;
3174 if (q->created)
3175 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
3176 be_queue_free(phba, q);
3177
3178 q = &phba->ctrl.mcc_obj.cq;
3179 if (q->created)
3180 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3181 be_queue_free(phba, q);
3182}
3183
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303184static void hwi_cleanup(struct beiscsi_hba *phba)
3185{
3186 struct be_queue_info *q;
3187 struct be_ctrl_info *ctrl = &phba->ctrl;
3188 struct hwi_controller *phwi_ctrlr;
3189 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303190 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303191
3192 phwi_ctrlr = phba->phwi_ctrlr;
3193 phwi_context = phwi_ctrlr->phwi_ctxt;
3194 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3195 q = &phwi_context->be_wrbq[i];
3196 if (q->created)
3197 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
3198 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303199 free_wrb_handles(phba);
3200
3201 q = &phwi_context->be_def_hdrq;
3202 if (q->created)
3203 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3204
3205 q = &phwi_context->be_def_dataq;
3206 if (q->created)
3207 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
3208
3209 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
3210
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303211 for (i = 0; i < (phba->num_cpus); i++) {
3212 q = &phwi_context->be_cq[i];
3213 if (q->created)
3214 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
3215 }
3216 if (phba->msix_enabled)
3217 eq_num = 1;
3218 else
3219 eq_num = 0;
3220 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
3221 q = &phwi_context->be_eq[i].q;
3222 if (q->created)
3223 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
3224 }
3225 be_mcc_queues_destroy(phba);
3226}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303227
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303228static int be_mcc_queues_create(struct beiscsi_hba *phba,
3229 struct hwi_context_memory *phwi_context)
3230{
3231 struct be_queue_info *q, *cq;
3232 struct be_ctrl_info *ctrl = &phba->ctrl;
3233
3234 /* Alloc MCC compl queue */
3235 cq = &phba->ctrl.mcc_obj.cq;
3236 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
3237 sizeof(struct be_mcc_compl)))
3238 goto err;
3239 /* Ask BE to create MCC compl queue; */
3240 if (phba->msix_enabled) {
3241 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
3242 [phba->num_cpus].q, false, true, 0))
3243 goto mcc_cq_free;
3244 } else {
3245 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
3246 false, true, 0))
3247 goto mcc_cq_free;
3248 }
3249
3250 /* Alloc MCC queue */
3251 q = &phba->ctrl.mcc_obj.q;
3252 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
3253 goto mcc_cq_destroy;
3254
3255 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05303256 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303257 goto mcc_q_free;
3258
3259 return 0;
3260
3261mcc_q_free:
3262 be_queue_free(phba, q);
3263mcc_cq_destroy:
3264 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
3265mcc_cq_free:
3266 be_queue_free(phba, cq);
3267err:
Jayamohan Kallickald3ad2bb2010-07-22 04:16:38 +05303268 return -ENOMEM;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303269}
3270
3271static int find_num_cpus(void)
3272{
3273 int num_cpus = 0;
3274
3275 num_cpus = num_online_cpus();
3276 if (num_cpus >= MAX_CPUS)
3277 num_cpus = MAX_CPUS - 1;
3278
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303279 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303280}
3281
3282static int hwi_init_port(struct beiscsi_hba *phba)
3283{
3284 struct hwi_controller *phwi_ctrlr;
3285 struct hwi_context_memory *phwi_context;
3286 unsigned int def_pdu_ring_sz;
3287 struct be_ctrl_info *ctrl = &phba->ctrl;
3288 int status;
3289
3290 def_pdu_ring_sz =
3291 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
3292 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303293 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303294 phwi_context->max_eqd = 0;
3295 phwi_context->min_eqd = 0;
3296 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303297 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303298
3299 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303300 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303301 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3302 "BM_%d : EQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303303 goto error;
3304 }
3305
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303306 status = be_mcc_queues_create(phba, phwi_context);
3307 if (status != 0)
3308 goto error;
3309
3310 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303311 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303312 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3313 "BM_%d : Unsupported fw version\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303314 goto error;
3315 }
3316
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303317 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303318 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303319 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3320 "BM_%d : CQ not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303321 goto error;
3322 }
3323
3324 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
3325 def_pdu_ring_sz);
3326 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303327 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3328 "BM_%d : Default Header not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303329 goto error;
3330 }
3331
3332 status = beiscsi_create_def_data(phba, phwi_context,
3333 phwi_ctrlr, def_pdu_ring_sz);
3334 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303335 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3336 "BM_%d : Default Data not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303337 goto error;
3338 }
3339
3340 status = beiscsi_post_pages(phba);
3341 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303342 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3343 "BM_%d : Post SGL Pages Failed\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303344 goto error;
3345 }
3346
3347 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
3348 if (status != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303349 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3350 "BM_%d : WRB Rings not created\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303351 goto error;
3352 }
3353
John Soni Jose99bc5d52012-08-20 23:00:18 +05303354 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3355 "BM_%d : hwi_init_port success\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303356 return 0;
3357
3358error:
John Soni Jose99bc5d52012-08-20 23:00:18 +05303359 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3360 "BM_%d : hwi_init_port failed");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303361 hwi_cleanup(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003362 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303363}
3364
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303365static int hwi_init_controller(struct beiscsi_hba *phba)
3366{
3367 struct hwi_controller *phwi_ctrlr;
3368
3369 phwi_ctrlr = phba->phwi_ctrlr;
3370 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
3371 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
3372 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303373 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3374 "BM_%d : phwi_ctrlr->phwi_ctxt=%p\n",
3375 phwi_ctrlr->phwi_ctxt);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303376 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303377 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3378 "BM_%d : HWI_MEM_ADDN_CONTEXT is more "
3379 "than one element.Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303380 return -ENOMEM;
3381 }
3382
3383 iscsi_init_global_templates(phba);
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05003384 if (beiscsi_init_wrb_handle(phba))
3385 return -ENOMEM;
3386
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303387 hwi_init_async_pdu_ctx(phba);
3388 if (hwi_init_port(phba) != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303389 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3390 "BM_%d : hwi_init_controller failed\n");
3391
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303392 return -ENOMEM;
3393 }
3394 return 0;
3395}
3396
3397static void beiscsi_free_mem(struct beiscsi_hba *phba)
3398{
3399 struct be_mem_descriptor *mem_descr;
3400 int i, j;
3401
3402 mem_descr = phba->init_mem;
3403 i = 0;
3404 j = 0;
3405 for (i = 0; i < SE_MEM_MAX; i++) {
3406 for (j = mem_descr->num_elements; j > 0; j--) {
3407 pci_free_consistent(phba->pcidev,
3408 mem_descr->mem_array[j - 1].size,
3409 mem_descr->mem_array[j - 1].virtual_address,
Jayamohan Kallickal457ff3b2010-07-22 04:16:00 +05303410 (unsigned long)mem_descr->mem_array[j - 1].
3411 bus_address.u.a64.address);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303412 }
3413 kfree(mem_descr->mem_array);
3414 mem_descr++;
3415 }
3416 kfree(phba->init_mem);
3417 kfree(phba->phwi_ctrlr);
3418}
3419
3420static int beiscsi_init_controller(struct beiscsi_hba *phba)
3421{
3422 int ret = -ENOMEM;
3423
3424 ret = beiscsi_get_memory(phba);
3425 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303426 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3427 "BM_%d : beiscsi_dev_probe -"
3428 "Failed in beiscsi_alloc_memory\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303429 return ret;
3430 }
3431
3432 ret = hwi_init_controller(phba);
3433 if (ret)
3434 goto free_init;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303435 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3436 "BM_%d : Return success from beiscsi_init_controller");
3437
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303438 return 0;
3439
3440free_init:
3441 beiscsi_free_mem(phba);
Jayamohan Kallickala49e06d2012-04-03 23:41:44 -05003442 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303443}
3444
3445static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
3446{
3447 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
3448 struct sgl_handle *psgl_handle;
3449 struct iscsi_sge *pfrag;
3450 unsigned int arr_index, i, idx;
3451
3452 phba->io_sgl_hndl_avbl = 0;
3453 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303454
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303455 mem_descr_sglh = phba->init_mem;
3456 mem_descr_sglh += HWI_MEM_SGLH;
3457 if (1 == mem_descr_sglh->num_elements) {
3458 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3459 phba->params.ios_per_ctrl,
3460 GFP_KERNEL);
3461 if (!phba->io_sgl_hndl_base) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303462 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3463 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303464 return -ENOMEM;
3465 }
3466 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
3467 (phba->params.icds_per_ctrl -
3468 phba->params.ios_per_ctrl),
3469 GFP_KERNEL);
3470 if (!phba->eh_sgl_hndl_base) {
3471 kfree(phba->io_sgl_hndl_base);
John Soni Jose99bc5d52012-08-20 23:00:18 +05303472 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3473 "BM_%d : Mem Alloc Failed. Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303474 return -ENOMEM;
3475 }
3476 } else {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303477 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3478 "BM_%d : HWI_MEM_SGLH is more than one element."
3479 "Failing to load\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303480 return -ENOMEM;
3481 }
3482
3483 arr_index = 0;
3484 idx = 0;
3485 while (idx < mem_descr_sglh->num_elements) {
3486 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
3487
3488 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
3489 sizeof(struct sgl_handle)); i++) {
3490 if (arr_index < phba->params.ios_per_ctrl) {
3491 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
3492 phba->io_sgl_hndl_avbl++;
3493 arr_index++;
3494 } else {
3495 phba->eh_sgl_hndl_base[arr_index -
3496 phba->params.ios_per_ctrl] =
3497 psgl_handle;
3498 arr_index++;
3499 phba->eh_sgl_hndl_avbl++;
3500 }
3501 psgl_handle++;
3502 }
3503 idx++;
3504 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05303505 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3506 "BM_%d : phba->io_sgl_hndl_avbl=%d"
3507 "phba->eh_sgl_hndl_avbl=%d\n",
3508 phba->io_sgl_hndl_avbl,
3509 phba->eh_sgl_hndl_avbl);
3510
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303511 mem_descr_sg = phba->init_mem;
3512 mem_descr_sg += HWI_MEM_SGE;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303513 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3514 "\n BM_%d : mem_descr_sg->num_elements=%d\n",
3515 mem_descr_sg->num_elements);
3516
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303517 arr_index = 0;
3518 idx = 0;
3519 while (idx < mem_descr_sg->num_elements) {
3520 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
3521
3522 for (i = 0;
3523 i < (mem_descr_sg->mem_array[idx].size) /
3524 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
3525 i++) {
3526 if (arr_index < phba->params.ios_per_ctrl)
3527 psgl_handle = phba->io_sgl_hndl_base[arr_index];
3528 else
3529 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3530 phba->params.ios_per_ctrl];
3531 psgl_handle->pfrag = pfrag;
3532 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3533 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3534 pfrag += phba->params.num_sge_per_io;
3535 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303536 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303537 }
3538 idx++;
3539 }
3540 phba->io_sgl_free_index = 0;
3541 phba->io_sgl_alloc_index = 0;
3542 phba->eh_sgl_free_index = 0;
3543 phba->eh_sgl_alloc_index = 0;
3544 return 0;
3545}
3546
3547static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3548{
3549 int i, new_cid;
3550
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303551 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303552 GFP_KERNEL);
3553 if (!phba->cid_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303554 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3555 "BM_%d : Failed to allocate memory in "
3556 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303557 return -ENOMEM;
3558 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303559 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303560 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3561 if (!phba->ep_array) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303562 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3563 "BM_%d : Failed to allocate memory in "
3564 "hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303565 kfree(phba->cid_array);
3566 return -ENOMEM;
3567 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303568 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303569 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3570 phba->cid_array[i] = new_cid;
3571 new_cid += 2;
3572 }
3573 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3574 return 0;
3575}
3576
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05303577static void hwi_enable_intr(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303578{
3579 struct be_ctrl_info *ctrl = &phba->ctrl;
3580 struct hwi_controller *phwi_ctrlr;
3581 struct hwi_context_memory *phwi_context;
3582 struct be_queue_info *eq;
3583 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303584 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303585 u32 enabled;
3586
3587 phwi_ctrlr = phba->phwi_ctrlr;
3588 phwi_context = phwi_ctrlr->phwi_ctxt;
3589
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303590 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3591 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3592 reg = ioread32(addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303593
3594 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3595 if (!enabled) {
3596 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303597 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3598 "BM_%d : reg =x%08x addr=%p\n", reg, addr);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303599 iowrite32(reg, addr);
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003600 }
3601
3602 if (!phba->msix_enabled) {
3603 eq = &phwi_context->be_eq[0].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303604 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3605 "BM_%d : eq->id=%d\n", eq->id);
3606
Jayamohan Kallickal665d6d92011-04-29 14:30:06 -05003607 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3608 } else {
3609 for (i = 0; i <= phba->num_cpus; i++) {
3610 eq = &phwi_context->be_eq[i].q;
John Soni Jose99bc5d52012-08-20 23:00:18 +05303611 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
3612 "BM_%d : eq->id=%d\n", eq->id);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303613 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3614 }
Jayamohan Kallickalc03af1a2010-02-20 08:05:43 +05303615 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303616}
3617
3618static void hwi_disable_intr(struct beiscsi_hba *phba)
3619{
3620 struct be_ctrl_info *ctrl = &phba->ctrl;
3621
3622 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3623 u32 reg = ioread32(addr);
3624
3625 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3626 if (enabled) {
3627 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3628 iowrite32(reg, addr);
3629 } else
John Soni Jose99bc5d52012-08-20 23:00:18 +05303630 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3631 "BM_%d : In hwi_disable_intr, Already Disabled\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303632}
3633
John Soni Jose9aef4202012-08-20 23:00:08 +05303634/**
3635 * beiscsi_get_boot_info()- Get the boot session info
3636 * @phba: The device priv structure instance
3637 *
3638 * Get the boot target info and store in driver priv structure
3639 *
3640 * return values
3641 * Success: 0
3642 * Failure: Non-Zero Value
3643 **/
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303644static int beiscsi_get_boot_info(struct beiscsi_hba *phba)
3645{
Mike Christie0e438952012-04-03 23:41:51 -05003646 struct be_cmd_get_session_resp *session_resp;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303647 struct be_mcc_wrb *wrb;
3648 struct be_dma_mem nonemb_cmd;
3649 unsigned int tag, wrb_num;
3650 unsigned short status, extd_status;
John Soni Jose9aef4202012-08-20 23:00:08 +05303651 unsigned int s_handle;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303652 struct be_queue_info *mccq = &phba->ctrl.mcc_obj.q;
Mike Christief457a462011-06-24 15:11:53 -05003653 int ret = -ENOMEM;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303654
John Soni Jose9aef4202012-08-20 23:00:08 +05303655 /* Get the session handle of the boot target */
3656 ret = be_mgmt_get_boot_shandle(phba, &s_handle);
3657 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303658 beiscsi_log(phba, KERN_ERR,
3659 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3660 "BM_%d : No boot session\n");
John Soni Jose9aef4202012-08-20 23:00:08 +05303661 return ret;
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303662 }
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303663 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
3664 sizeof(*session_resp),
3665 &nonemb_cmd.dma);
3666 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303667 beiscsi_log(phba, KERN_ERR,
3668 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3669 "BM_%d : Failed to allocate memory for"
3670 "beiscsi_get_session_info\n");
3671
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303672 return -ENOMEM;
3673 }
3674
3675 memset(nonemb_cmd.va, 0, sizeof(*session_resp));
John Soni Jose9aef4202012-08-20 23:00:08 +05303676 tag = mgmt_get_session_info(phba, s_handle,
Mike Christie0e438952012-04-03 23:41:51 -05003677 &nonemb_cmd);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303678 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303679 beiscsi_log(phba, KERN_ERR,
3680 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3681 "BM_%d : beiscsi_get_session_info"
3682 " Failed\n");
3683
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303684 goto boot_freemem;
3685 } else
3686 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3687 phba->ctrl.mcc_numtag[tag]);
3688
3689 wrb_num = (phba->ctrl.mcc_numtag[tag] & 0x00FF0000) >> 16;
3690 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
3691 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
3692 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303693 beiscsi_log(phba, KERN_ERR,
3694 BEISCSI_LOG_INIT | BEISCSI_LOG_CONFIG,
3695 "BM_%d : beiscsi_get_session_info Failed"
3696 " status = %d extd_status = %d\n",
3697 status, extd_status);
3698
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303699 free_mcc_tag(&phba->ctrl, tag);
3700 goto boot_freemem;
3701 }
3702 wrb = queue_get_wrb(mccq, wrb_num);
3703 free_mcc_tag(&phba->ctrl, tag);
3704 session_resp = nonemb_cmd.va ;
Mike Christief457a462011-06-24 15:11:53 -05003705
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303706 memcpy(&phba->boot_sess, &session_resp->session_info,
3707 sizeof(struct mgmt_session_info));
Mike Christief457a462011-06-24 15:11:53 -05003708 ret = 0;
3709
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303710boot_freemem:
3711 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
3712 nonemb_cmd.va, nonemb_cmd.dma);
Mike Christief457a462011-06-24 15:11:53 -05003713 return ret;
3714}
3715
3716static void beiscsi_boot_release(void *data)
3717{
3718 struct beiscsi_hba *phba = data;
3719
3720 scsi_host_put(phba->shost);
3721}
3722
3723static int beiscsi_setup_boot_info(struct beiscsi_hba *phba)
3724{
3725 struct iscsi_boot_kobj *boot_kobj;
3726
3727 /* get boot info using mgmt cmd */
3728 if (beiscsi_get_boot_info(phba))
3729 /* Try to see if we can carry on without this */
3730 return 0;
3731
3732 phba->boot_kset = iscsi_boot_create_host_kset(phba->shost->host_no);
3733 if (!phba->boot_kset)
3734 return -ENOMEM;
3735
3736 /* get a ref because the show function will ref the phba */
3737 if (!scsi_host_get(phba->shost))
3738 goto free_kset;
3739 boot_kobj = iscsi_boot_create_target(phba->boot_kset, 0, phba,
3740 beiscsi_show_boot_tgt_info,
3741 beiscsi_tgt_get_attr_visibility,
3742 beiscsi_boot_release);
3743 if (!boot_kobj)
3744 goto put_shost;
3745
3746 if (!scsi_host_get(phba->shost))
3747 goto free_kset;
3748 boot_kobj = iscsi_boot_create_initiator(phba->boot_kset, 0, phba,
3749 beiscsi_show_boot_ini_info,
3750 beiscsi_ini_get_attr_visibility,
3751 beiscsi_boot_release);
3752 if (!boot_kobj)
3753 goto put_shost;
3754
3755 if (!scsi_host_get(phba->shost))
3756 goto free_kset;
3757 boot_kobj = iscsi_boot_create_ethernet(phba->boot_kset, 0, phba,
3758 beiscsi_show_boot_eth_info,
3759 beiscsi_eth_get_attr_visibility,
3760 beiscsi_boot_release);
3761 if (!boot_kobj)
3762 goto put_shost;
3763 return 0;
3764
3765put_shost:
3766 scsi_host_put(phba->shost);
3767free_kset:
3768 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickalc7acc5b2010-07-22 04:29:18 +05303769 return -ENOMEM;
3770}
3771
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303772static int beiscsi_init_port(struct beiscsi_hba *phba)
3773{
3774 int ret;
3775
3776 ret = beiscsi_init_controller(phba);
3777 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303778 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3779 "BM_%d : beiscsi_dev_probe - Failed in"
3780 "beiscsi_init_controller\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303781 return ret;
3782 }
3783 ret = beiscsi_init_sgl_handle(phba);
3784 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303785 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3786 "BM_%d : beiscsi_dev_probe - Failed in"
3787 "beiscsi_init_sgl_handle\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303788 goto do_cleanup_ctrlr;
3789 }
3790
3791 if (hba_setup_cid_tbls(phba)) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05303792 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
3793 "BM_%d : Failed in hba_setup_cid_tbls\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303794 kfree(phba->io_sgl_hndl_base);
3795 kfree(phba->eh_sgl_hndl_base);
3796 goto do_cleanup_ctrlr;
3797 }
3798
3799 return ret;
3800
3801do_cleanup_ctrlr:
3802 hwi_cleanup(phba);
3803 return ret;
3804}
3805
3806static void hwi_purge_eq(struct beiscsi_hba *phba)
3807{
3808 struct hwi_controller *phwi_ctrlr;
3809 struct hwi_context_memory *phwi_context;
3810 struct be_queue_info *eq;
3811 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303812 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303813 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303814
3815 phwi_ctrlr = phba->phwi_ctrlr;
3816 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303817 if (phba->msix_enabled)
3818 eq_msix = 1;
3819 else
3820 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303821
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303822 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3823 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303824 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303825 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303826 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3827 & EQE_VALID_MASK) {
3828 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3829 queue_tail_inc(eq);
3830 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303831 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303832 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303833
3834 if (num_processed)
3835 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303836 }
3837}
3838
3839static void beiscsi_clean_port(struct beiscsi_hba *phba)
3840{
Jayamohan Kallickal03a12312010-07-22 04:17:16 +05303841 int mgmt_status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303842
3843 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3844 if (mgmt_status)
John Soni Jose99bc5d52012-08-20 23:00:18 +05303845 beiscsi_log(phba, KERN_WARNING, BEISCSI_LOG_INIT,
3846 "BM_%d : mgmt_epfw_cleanup FAILED\n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303847
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303848 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303849 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303850 kfree(phba->io_sgl_hndl_base);
3851 kfree(phba->eh_sgl_hndl_base);
3852 kfree(phba->cid_array);
3853 kfree(phba->ep_array);
3854}
3855
Mike Christie1282ab72012-04-18 03:06:00 -05003856static void beiscsi_cleanup_task(struct iscsi_task *task)
3857{
3858 struct beiscsi_io_task *io_task = task->dd_data;
3859 struct iscsi_conn *conn = task->conn;
3860 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3861 struct beiscsi_hba *phba = beiscsi_conn->phba;
3862 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3863 struct hwi_wrb_context *pwrb_context;
3864 struct hwi_controller *phwi_ctrlr;
3865
3866 phwi_ctrlr = phba->phwi_ctrlr;
3867 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3868 - phba->fw_config.iscsi_cid_start];
3869
3870 if (io_task->cmd_bhs) {
3871 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3872 io_task->bhs_pa.u.a64.address);
3873 io_task->cmd_bhs = NULL;
3874 }
3875
3876 if (task->sc) {
3877 if (io_task->pwrb_handle) {
3878 free_wrb_handle(phba, pwrb_context,
3879 io_task->pwrb_handle);
3880 io_task->pwrb_handle = NULL;
3881 }
3882
3883 if (io_task->psgl_handle) {
3884 spin_lock(&phba->io_sgl_lock);
3885 free_io_sgl_handle(phba, io_task->psgl_handle);
3886 spin_unlock(&phba->io_sgl_lock);
3887 io_task->psgl_handle = NULL;
3888 }
3889 } else {
3890 if (!beiscsi_conn->login_in_progress) {
3891 if (io_task->pwrb_handle) {
3892 free_wrb_handle(phba, pwrb_context,
3893 io_task->pwrb_handle);
3894 io_task->pwrb_handle = NULL;
3895 }
3896 if (io_task->psgl_handle) {
3897 spin_lock(&phba->mgmt_sgl_lock);
3898 free_mgmt_sgl_handle(phba,
3899 io_task->psgl_handle);
3900 spin_unlock(&phba->mgmt_sgl_lock);
3901 io_task->psgl_handle = NULL;
3902 }
3903 }
3904 }
3905}
3906
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303907void
3908beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3909 struct beiscsi_offload_params *params)
3910{
3911 struct wrb_handle *pwrb_handle;
3912 struct iscsi_target_context_update_wrb *pwrb = NULL;
3913 struct be_mem_descriptor *mem_descr;
3914 struct beiscsi_hba *phba = beiscsi_conn->phba;
Mike Christie1282ab72012-04-18 03:06:00 -05003915 struct iscsi_task *task = beiscsi_conn->task;
3916 struct iscsi_session *session = task->conn->session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303917 u32 doorbell = 0;
3918
3919 /*
3920 * We can always use 0 here because it is reserved by libiscsi for
3921 * login/startup related tasks.
3922 */
Mike Christie1282ab72012-04-18 03:06:00 -05003923 beiscsi_conn->login_in_progress = 0;
3924 spin_lock_bh(&session->lock);
3925 beiscsi_cleanup_task(task);
3926 spin_unlock_bh(&session->lock);
3927
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303928 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303929 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303930 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3931 memset(pwrb, 0, sizeof(*pwrb));
3932 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3933 max_burst_length, pwrb, params->dw[offsetof
3934 (struct amap_beiscsi_offload_params,
3935 max_burst_length) / 32]);
3936 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3937 max_send_data_segment_length, pwrb,
3938 params->dw[offsetof(struct amap_beiscsi_offload_params,
3939 max_send_data_segment_length) / 32]);
3940 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3941 first_burst_length,
3942 pwrb,
3943 params->dw[offsetof(struct amap_beiscsi_offload_params,
3944 first_burst_length) / 32]);
3945
3946 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3947 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3948 erl) / 32] & OFFLD_PARAMS_ERL));
3949 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3950 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3951 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3952 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3953 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3954 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3955 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3956 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3957 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3958 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3959 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3960 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3961 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3962 pwrb,
3963 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3964 exp_statsn) / 32] + 1));
3965 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3966 0x7);
3967 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3968 pwrb, pwrb_handle->wrb_index);
3969 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3970 pwrb, pwrb_handle->nxt_wrb_index);
3971 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3972 session_state, pwrb, 0);
3973 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3974 pwrb, 1);
3975 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3976 pwrb, 0);
3977 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3978 0);
3979
3980 mem_descr = phba->init_mem;
3981 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3982
3983 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3984 pad_buffer_addr_hi, pwrb,
3985 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3986 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3987 pad_buffer_addr_lo, pwrb,
3988 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3989
3990 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3991
3992 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303993 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303994 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303995 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3996
3997 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3998}
3999
4000static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
4001 int *index, int *age)
4002{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304003 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304004 if (age)
4005 *age = conn->session->age;
4006}
4007
4008/**
4009 * beiscsi_alloc_pdu - allocates pdu and related resources
4010 * @task: libiscsi task
4011 * @opcode: opcode of pdu for task
4012 *
4013 * This is called with the session lock held. It will allocate
4014 * the wrb and sgl if needed for the command. And it will prep
4015 * the pdu's itt. beiscsi_parse_pdu will later translate
4016 * the pdu itt to the libiscsi task itt.
4017 */
4018static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
4019{
4020 struct beiscsi_io_task *io_task = task->dd_data;
4021 struct iscsi_conn *conn = task->conn;
4022 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4023 struct beiscsi_hba *phba = beiscsi_conn->phba;
4024 struct hwi_wrb_context *pwrb_context;
4025 struct hwi_controller *phwi_ctrlr;
4026 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304027 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
4028 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304029
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304030 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
Mike Christiebc7acce2010-12-31 02:22:19 -06004031 GFP_ATOMIC, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304032 if (!io_task->cmd_bhs)
4033 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304034 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304035 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304036 io_task->conn = beiscsi_conn;
4037
4038 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
4039 task->hdr_max = sizeof(struct be_cmd_bhs);
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304040 io_task->psgl_handle = NULL;
Jayamohan Kallickal3ec78272012-04-03 23:41:38 -05004041 io_task->pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304042
4043 if (task->sc) {
4044 spin_lock(&phba->io_sgl_lock);
4045 io_task->psgl_handle = alloc_io_sgl_handle(phba);
4046 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304047 if (!io_task->psgl_handle)
4048 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304049 io_task->pwrb_handle = alloc_wrb_handle(phba,
4050 beiscsi_conn->beiscsi_conn_cid -
4051 phba->fw_config.iscsi_cid_start);
4052 if (!io_task->pwrb_handle)
4053 goto free_io_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304054 } else {
4055 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05304056 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304057 if (!beiscsi_conn->login_in_progress) {
4058 spin_lock(&phba->mgmt_sgl_lock);
4059 io_task->psgl_handle = (struct sgl_handle *)
4060 alloc_mgmt_sgl_handle(phba);
4061 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304062 if (!io_task->psgl_handle)
4063 goto free_hndls;
4064
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304065 beiscsi_conn->login_in_progress = 1;
4066 beiscsi_conn->plogin_sgl_handle =
4067 io_task->psgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304068 io_task->pwrb_handle =
4069 alloc_wrb_handle(phba,
4070 beiscsi_conn->beiscsi_conn_cid -
4071 phba->fw_config.iscsi_cid_start);
4072 if (!io_task->pwrb_handle)
4073 goto free_io_hndls;
4074 beiscsi_conn->plogin_wrb_handle =
4075 io_task->pwrb_handle;
4076
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304077 } else {
4078 io_task->psgl_handle =
4079 beiscsi_conn->plogin_sgl_handle;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304080 io_task->pwrb_handle =
4081 beiscsi_conn->plogin_wrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304082 }
Mike Christie1282ab72012-04-18 03:06:00 -05004083 beiscsi_conn->task = task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304084 } else {
4085 spin_lock(&phba->mgmt_sgl_lock);
4086 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
4087 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304088 if (!io_task->psgl_handle)
4089 goto free_hndls;
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304090 io_task->pwrb_handle =
4091 alloc_wrb_handle(phba,
4092 beiscsi_conn->beiscsi_conn_cid -
4093 phba->fw_config.iscsi_cid_start);
4094 if (!io_task->pwrb_handle)
4095 goto free_mgmt_hndls;
4096
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304097 }
4098 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304099 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
4100 wrb_index << 16) | (unsigned int)
4101 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304102 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304103
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304104 io_task->cmd_bhs->iscsi_hdr.itt = itt;
4105 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304106
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304107free_io_hndls:
4108 spin_lock(&phba->io_sgl_lock);
4109 free_io_sgl_handle(phba, io_task->psgl_handle);
4110 spin_unlock(&phba->io_sgl_lock);
4111 goto free_hndls;
4112free_mgmt_hndls:
4113 spin_lock(&phba->mgmt_sgl_lock);
4114 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
4115 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304116free_hndls:
4117 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304118 pwrb_context = &phwi_ctrlr->wrb_context[
4119 beiscsi_conn->beiscsi_conn_cid -
4120 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickald2cecf02010-07-22 04:25:40 +05304121 if (io_task->pwrb_handle)
4122 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304123 io_task->pwrb_handle = NULL;
4124 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
4125 io_task->bhs_pa.u.a64.address);
Mike Christie1282ab72012-04-18 03:06:00 -05004126 io_task->cmd_bhs = NULL;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304127 beiscsi_log(phba, KERN_ERR,
4128 BEISCSI_LOG_IO | BEISCSI_LOG_CONFIG,
4129 "BM_%d : Alloc of SGL_ICD Failed\n");
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05304130 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304131}
4132
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304133static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
4134 unsigned int num_sg, unsigned int xferlen,
4135 unsigned int writedir)
4136{
4137
4138 struct beiscsi_io_task *io_task = task->dd_data;
4139 struct iscsi_conn *conn = task->conn;
4140 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4141 struct beiscsi_hba *phba = beiscsi_conn->phba;
4142 struct iscsi_wrb *pwrb = NULL;
4143 unsigned int doorbell = 0;
4144
4145 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304146 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
4147 io_task->bhs_len = sizeof(struct be_cmd_bhs);
4148
4149 if (writedir) {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304150 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4151 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304152 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304153 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304154 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4155 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304156 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
4157 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304158
4159 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
Jayamohan Kallickaldc63aac2012-04-03 23:41:36 -05004160 cpu_to_be16(*(unsigned short *)
4161 &io_task->cmd_bhs->iscsi_hdr.lun));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304162 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
4163 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4164 io_task->pwrb_handle->wrb_index);
4165 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4166 be32_to_cpu(task->cmdsn));
4167 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4168 io_task->psgl_handle->sgl_index);
4169
4170 hwi_write_sgl(pwrb, sg, num_sg, io_task);
4171
4172 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4173 io_task->pwrb_handle->nxt_wrb_index);
4174 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4175
4176 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304177 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304178 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4179 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4180
4181 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4182 return 0;
4183}
4184
4185static int beiscsi_mtask(struct iscsi_task *task)
4186{
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304187 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304188 struct iscsi_conn *conn = task->conn;
4189 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
4190 struct beiscsi_hba *phba = beiscsi_conn->phba;
4191 struct iscsi_wrb *pwrb = NULL;
4192 unsigned int doorbell = 0;
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304193 unsigned int cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304194
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304195 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304196 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05304197 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304198 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
4199 be32_to_cpu(task->cmdsn));
4200 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
4201 io_task->pwrb_handle->wrb_index);
4202 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
4203 io_task->psgl_handle->sgl_index);
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304204
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304205 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
4206 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304207 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4208 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304209 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4210 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
4211 hwi_write_buffer(pwrb, task);
4212 break;
4213 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004214 if (task->hdr->ttt != ISCSI_RESERVED_TAG) {
4215 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4216 TGT_DM_CMD);
4217 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt,
4218 pwrb, 0);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004219 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004220 } else {
4221 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4222 INI_RD_CMD);
Jayamohan Kallickal685e16f2011-10-07 19:31:09 -05004223 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal1390b012011-03-25 14:24:01 -07004224 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304225 hwi_write_buffer(pwrb, task);
4226 break;
4227 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304228 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickalb30c6da2010-01-23 05:38:56 +05304229 TGT_DM_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05304230 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304231 hwi_write_buffer(pwrb, task);
4232 break;
4233 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304234 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
4235 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304236 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4237 hwi_write_buffer(pwrb, task);
4238 break;
4239 case ISCSI_OP_LOGOUT:
4240 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
4241 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
Jayamohan Kallickaldafab8e2010-02-20 08:03:56 +05304242 HWH_TYPE_LOGOUT);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304243 hwi_write_buffer(pwrb, task);
4244 break;
4245
4246 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304247 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4248 "BM_%d : opcode =%d Not supported\n",
4249 task->hdr->opcode & ISCSI_OPCODE_MASK);
4250
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304251 return -EINVAL;
4252 }
4253
4254 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05304255 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304256 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
4257 io_task->pwrb_handle->nxt_wrb_index);
4258 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
4259
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304260 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05304261 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304262 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
4263 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
4264 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
4265 return 0;
4266}
4267
4268static int beiscsi_task_xmit(struct iscsi_task *task)
4269{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304270 struct beiscsi_io_task *io_task = task->dd_data;
4271 struct scsi_cmnd *sc = task->sc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304272 struct scatterlist *sg;
4273 int num_sg;
4274 unsigned int writedir = 0, xferlen = 0;
4275
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304276 if (!sc)
4277 return beiscsi_mtask(task);
4278
4279 io_task->scsi_cmnd = sc;
4280 num_sg = scsi_dma_map(sc);
4281 if (num_sg < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304282 struct iscsi_conn *conn = task->conn;
4283 struct beiscsi_hba *phba = NULL;
4284
4285 phba = ((struct beiscsi_conn *)conn->dd_data)->phba;
4286 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_IO,
4287 "BM_%d : scsi_dma_map Failed\n");
4288
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304289 return num_sg;
4290 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304291 xferlen = scsi_bufflen(sc);
4292 sg = scsi_sglist(sc);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304293 if (sc->sc_data_direction == DMA_TO_DEVICE)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304294 writedir = 1;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304295 else
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304296 writedir = 0;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304297
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304298 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
4299}
4300
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004301/**
4302 * beiscsi_bsg_request - handle bsg request from ISCSI transport
4303 * @job: job to handle
4304 */
4305static int beiscsi_bsg_request(struct bsg_job *job)
4306{
4307 struct Scsi_Host *shost;
4308 struct beiscsi_hba *phba;
4309 struct iscsi_bsg_request *bsg_req = job->request;
4310 int rc = -EINVAL;
4311 unsigned int tag;
4312 struct be_dma_mem nonemb_cmd;
4313 struct be_cmd_resp_hdr *resp;
4314 struct iscsi_bsg_reply *bsg_reply = job->reply;
4315 unsigned short status, extd_status;
4316
4317 shost = iscsi_job_to_shost(job);
4318 phba = iscsi_host_priv(shost);
4319
4320 switch (bsg_req->msgcode) {
4321 case ISCSI_BSG_HST_VENDOR:
4322 nonemb_cmd.va = pci_alloc_consistent(phba->ctrl.pdev,
4323 job->request_payload.payload_len,
4324 &nonemb_cmd.dma);
4325 if (nonemb_cmd.va == NULL) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304326 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4327 "BM_%d : Failed to allocate memory for "
4328 "beiscsi_bsg_request\n");
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004329 return -EIO;
4330 }
4331 tag = mgmt_vendor_specific_fw_cmd(&phba->ctrl, phba, job,
4332 &nonemb_cmd);
4333 if (!tag) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304334 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4335 "BM_%d : be_cmd_get_mac_addr Failed\n");
4336
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004337 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4338 nonemb_cmd.va, nonemb_cmd.dma);
4339 return -EAGAIN;
4340 } else
4341 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
4342 phba->ctrl.mcc_numtag[tag]);
4343 extd_status = (phba->ctrl.mcc_numtag[tag] & 0x0000FF00) >> 8;
4344 status = phba->ctrl.mcc_numtag[tag] & 0x000000FF;
4345 free_mcc_tag(&phba->ctrl, tag);
4346 resp = (struct be_cmd_resp_hdr *)nonemb_cmd.va;
4347 sg_copy_from_buffer(job->reply_payload.sg_list,
4348 job->reply_payload.sg_cnt,
4349 nonemb_cmd.va, (resp->response_length
4350 + sizeof(*resp)));
4351 bsg_reply->reply_payload_rcv_len = resp->response_length;
4352 bsg_reply->result = status;
4353 bsg_job_done(job, bsg_reply->result,
4354 bsg_reply->reply_payload_rcv_len);
4355 pci_free_consistent(phba->ctrl.pdev, nonemb_cmd.size,
4356 nonemb_cmd.va, nonemb_cmd.dma);
4357 if (status || extd_status) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304358 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4359 "BM_%d : be_cmd_get_mac_addr Failed"
4360 " status = %d extd_status = %d\n",
4361 status, extd_status);
4362
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004363 return -EIO;
4364 }
4365 break;
4366
4367 default:
John Soni Jose99bc5d52012-08-20 23:00:18 +05304368 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_CONFIG,
4369 "BM_%d : Unsupported bsg command: 0x%x\n",
4370 bsg_req->msgcode);
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004371 break;
4372 }
4373
4374 return rc;
4375}
4376
John Soni Jose99bc5d52012-08-20 23:00:18 +05304377void beiscsi_hba_attrs_init(struct beiscsi_hba *phba)
4378{
4379 /* Set the logging parameter */
4380 beiscsi_log_enable_init(phba, beiscsi_log_enable);
4381}
4382
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004383static void beiscsi_quiesce(struct beiscsi_hba *phba)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304384{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304385 struct hwi_controller *phwi_ctrlr;
4386 struct hwi_context_memory *phwi_context;
4387 struct be_eq_obj *pbe_eq;
4388 unsigned int i, msix_vec;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304389 u8 *real_offset = 0;
4390 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304391
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304392 phwi_ctrlr = phba->phwi_ctrlr;
4393 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304394 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304395 if (phba->msix_enabled) {
4396 for (i = 0; i <= phba->num_cpus; i++) {
4397 msix_vec = phba->msix_entries[i].vector;
4398 free_irq(msix_vec, &phwi_context->be_eq[i]);
Jayamohan Kallickal8fcfb212011-08-24 16:05:30 -07004399 kfree(phba->msi_name[i]);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304400 }
4401 } else
4402 if (phba->pcidev->irq)
4403 free_irq(phba->pcidev->irq, phba);
4404 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304405 destroy_workqueue(phba->wq);
4406 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304407 for (i = 0; i < phba->num_cpus; i++) {
4408 pbe_eq = &phwi_context->be_eq[i];
4409 blk_iopoll_disable(&pbe_eq->iopoll);
4410 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304411
4412 beiscsi_clean_port(phba);
4413 beiscsi_free_mem(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304414 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4415
4416 value = readl((void *)real_offset);
4417
4418 if (value & 0x00010000) {
4419 value &= 0xfffeffff;
4420 writel(value, (void *)real_offset);
4421 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304422 beiscsi_unmap_pci_function(phba);
4423 pci_free_consistent(phba->pcidev,
4424 phba->ctrl.mbox_mem_alloced.size,
4425 phba->ctrl.mbox_mem_alloced.va,
4426 phba->ctrl.mbox_mem_alloced.dma);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004427}
4428
4429static void beiscsi_remove(struct pci_dev *pcidev)
4430{
4431
4432 struct beiscsi_hba *phba = NULL;
4433
4434 phba = pci_get_drvdata(pcidev);
4435 if (!phba) {
4436 dev_err(&pcidev->dev, "beiscsi_remove called with no phba\n");
4437 return;
4438 }
4439
Mike Christie0e438952012-04-03 23:41:51 -05004440 beiscsi_destroy_def_ifaces(phba);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004441 beiscsi_quiesce(phba);
Mike Christie9d045162011-06-24 15:11:52 -05004442 iscsi_boot_destroy_kset(phba->boot_kset);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304443 iscsi_host_remove(phba->shost);
4444 pci_dev_put(phba->pcidev);
4445 iscsi_host_free(phba->shost);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004446 pci_disable_device(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304447}
4448
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004449static void beiscsi_shutdown(struct pci_dev *pcidev)
4450{
4451
4452 struct beiscsi_hba *phba = NULL;
4453
4454 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
4455 if (!phba) {
4456 dev_err(&pcidev->dev, "beiscsi_shutdown called with no phba\n");
4457 return;
4458 }
4459
4460 beiscsi_quiesce(phba);
Jayamohan Kallickal8dce69f2011-08-22 10:08:29 -07004461 pci_disable_device(pcidev);
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004462}
4463
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304464static void beiscsi_msix_enable(struct beiscsi_hba *phba)
4465{
4466 int i, status;
4467
4468 for (i = 0; i <= phba->num_cpus; i++)
4469 phba->msix_entries[i].entry = i;
4470
4471 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
4472 (phba->num_cpus + 1));
4473 if (!status)
4474 phba->msix_enabled = true;
4475
4476 return;
4477}
4478
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304479static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
4480 const struct pci_device_id *id)
4481{
4482 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304483 struct hwi_controller *phwi_ctrlr;
4484 struct hwi_context_memory *phwi_context;
4485 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304486 int ret, num_cpus, i;
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304487 u8 *real_offset = 0;
4488 u32 value = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304489
4490 ret = beiscsi_enable_pci(pcidev);
4491 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304492 dev_err(&pcidev->dev,
4493 "beiscsi_dev_probe - Failed to enable pci device\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304494 return ret;
4495 }
4496
4497 phba = beiscsi_hba_alloc(pcidev);
4498 if (!phba) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304499 dev_err(&pcidev->dev,
4500 "beiscsi_dev_probe - Failed in beiscsi_hba_alloc\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304501 goto disable_pci;
4502 }
4503
John Soni Jose99bc5d52012-08-20 23:00:18 +05304504 /* Initialize Driver configuration Paramters */
4505 beiscsi_hba_attrs_init(phba);
4506
Jayamohan Kallickalf98c96b2010-02-11 05:11:15 +05304507 switch (pcidev->device) {
4508 case BE_DEVICE_ID1:
4509 case OC_DEVICE_ID1:
4510 case OC_DEVICE_ID2:
4511 phba->generation = BE_GEN2;
4512 break;
4513 case BE_DEVICE_ID2:
4514 case OC_DEVICE_ID3:
4515 phba->generation = BE_GEN3;
4516 break;
4517 default:
4518 phba->generation = 0;
4519 }
4520
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304521 if (enable_msix)
4522 num_cpus = find_num_cpus();
4523 else
4524 num_cpus = 1;
4525 phba->num_cpus = num_cpus;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304526 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4527 "BM_%d : num_cpus = %d\n",
4528 phba->num_cpus);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304529
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004530 if (enable_msix) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304531 beiscsi_msix_enable(phba);
Jayamohan Kallickalb547f2d2012-04-03 23:41:41 -05004532 if (!phba->msix_enabled)
4533 phba->num_cpus = 1;
4534 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304535 ret = be_ctrl_init(phba, pcidev);
4536 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304537 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4538 "BM_%d : beiscsi_dev_probe-"
4539 "Failed in be_ctrl_init\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304540 goto hba_free;
4541 }
4542
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304543 if (!num_hba) {
4544 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4545 value = readl((void *)real_offset);
4546 if (value & 0x00010000) {
4547 gcrashmode++;
John Soni Jose99bc5d52012-08-20 23:00:18 +05304548 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4549 "BM_%d : Loading Driver in crashdump mode\n");
Jayamohan Kallickale5285862011-10-07 19:31:08 -05004550 ret = beiscsi_cmd_reset_function(phba);
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304551 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304552 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4553 "BM_%d : Reset Failed. Aborting Crashdump\n");
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304554 goto hba_free;
4555 }
4556 ret = be_chk_reset_complete(phba);
4557 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304558 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4559 "BM_%d : Failed to get out of reset."
4560 "Aborting Crashdump\n");
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304561 goto hba_free;
4562 }
4563 } else {
4564 value |= 0x00010000;
4565 writel(value, (void *)real_offset);
4566 num_hba++;
4567 }
4568 }
4569
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304570 spin_lock_init(&phba->io_sgl_lock);
4571 spin_lock_init(&phba->mgmt_sgl_lock);
4572 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304573 ret = mgmt_get_fw_config(&phba->ctrl, phba);
4574 if (ret != 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304575 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4576 "BM_%d : Error getting fw config\n");
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05304577 goto free_port;
4578 }
4579 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304580 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05304581 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304582 ret = beiscsi_init_port(phba);
4583 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304584 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4585 "BM_%d : beiscsi_dev_probe-"
4586 "Failed in beiscsi_init_port\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304587 goto free_port;
4588 }
4589
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05304590 for (i = 0; i < MAX_MCC_CMD ; i++) {
4591 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
4592 phba->ctrl.mcc_tag[i] = i + 1;
4593 phba->ctrl.mcc_numtag[i + 1] = 0;
4594 phba->ctrl.mcc_tag_available++;
4595 }
4596
4597 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
4598
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304599 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
4600 phba->shost->host_no);
Tejun Heo278274d2011-02-01 11:42:42 +01004601 phba->wq = alloc_workqueue(phba->wq_name, WQ_MEM_RECLAIM, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304602 if (!phba->wq) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304603 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4604 "BM_%d : beiscsi_dev_probe-"
4605 "Failed to allocate work queue\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304606 goto free_twq;
4607 }
4608
4609 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
4610
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304611 phwi_ctrlr = phba->phwi_ctrlr;
4612 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304613 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304614 for (i = 0; i < phba->num_cpus; i++) {
4615 pbe_eq = &phwi_context->be_eq[i];
4616 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
4617 be_iopoll);
4618 blk_iopoll_enable(&pbe_eq->iopoll);
4619 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304620 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304621 ret = beiscsi_init_irqs(phba);
4622 if (ret < 0) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304623 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4624 "BM_%d : beiscsi_dev_probe-"
4625 "Failed to beiscsi_init_irqs\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304626 goto free_blkenbld;
4627 }
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304628 hwi_enable_intr(phba);
Mike Christief457a462011-06-24 15:11:53 -05004629
4630 if (beiscsi_setup_boot_info(phba))
4631 /*
4632 * log error but continue, because we may not be using
4633 * iscsi boot.
4634 */
John Soni Jose99bc5d52012-08-20 23:00:18 +05304635 beiscsi_log(phba, KERN_ERR, BEISCSI_LOG_INIT,
4636 "BM_%d : Could not set up "
4637 "iSCSI boot info.\n");
Mike Christief457a462011-06-24 15:11:53 -05004638
Mike Christie0e438952012-04-03 23:41:51 -05004639 beiscsi_create_def_ifaces(phba);
John Soni Jose99bc5d52012-08-20 23:00:18 +05304640 beiscsi_log(phba, KERN_INFO, BEISCSI_LOG_INIT,
4641 "\n\n\n BM_%d : SUCCESS - DRIVER LOADED\n\n\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304642 return 0;
4643
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304644free_blkenbld:
4645 destroy_workqueue(phba->wq);
4646 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304647 for (i = 0; i < phba->num_cpus; i++) {
4648 pbe_eq = &phwi_context->be_eq[i];
4649 blk_iopoll_disable(&pbe_eq->iopoll);
4650 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304651free_twq:
4652 beiscsi_clean_port(phba);
4653 beiscsi_free_mem(phba);
4654free_port:
Jayamohan Kallickale9b91192010-07-22 04:24:53 +05304655 real_offset = (u8 *)phba->csr_va + MPU_EP_SEMAPHORE;
4656
4657 value = readl((void *)real_offset);
4658
4659 if (value & 0x00010000) {
4660 value &= 0xfffeffff;
4661 writel(value, (void *)real_offset);
4662 }
4663
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304664 pci_free_consistent(phba->pcidev,
4665 phba->ctrl.mbox_mem_alloced.size,
4666 phba->ctrl.mbox_mem_alloced.va,
4667 phba->ctrl.mbox_mem_alloced.dma);
4668 beiscsi_unmap_pci_function(phba);
4669hba_free:
Jayamohan Kallickal238f6b72010-07-22 04:23:22 +05304670 if (phba->msix_enabled)
4671 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304672 iscsi_host_remove(phba->shost);
4673 pci_dev_put(phba->pcidev);
4674 iscsi_host_free(phba->shost);
4675disable_pci:
4676 pci_disable_device(pcidev);
4677 return ret;
4678}
4679
4680struct iscsi_transport beiscsi_iscsi_transport = {
4681 .owner = THIS_MODULE,
4682 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05304683 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304684 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304685 .create_session = beiscsi_session_create,
4686 .destroy_session = beiscsi_session_destroy,
4687 .create_conn = beiscsi_conn_create,
4688 .bind_conn = beiscsi_conn_bind,
4689 .destroy_conn = iscsi_conn_teardown,
Mike Christie3128c6c2011-07-25 13:48:42 -05004690 .attr_is_visible = be2iscsi_attr_is_visible,
Mike Christie0e438952012-04-03 23:41:51 -05004691 .set_iface_param = be2iscsi_iface_set_param,
4692 .get_iface_param = be2iscsi_iface_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304693 .set_param = beiscsi_set_param,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004694 .get_conn_param = iscsi_conn_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304695 .get_session_param = iscsi_session_get_param,
4696 .get_host_param = beiscsi_get_host_param,
4697 .start_conn = beiscsi_conn_start,
Mike Christiefa95d202010-06-09 03:30:08 -05004698 .stop_conn = iscsi_conn_stop,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304699 .send_pdu = iscsi_conn_send_pdu,
4700 .xmit_task = beiscsi_task_xmit,
4701 .cleanup_task = beiscsi_cleanup_task,
4702 .alloc_pdu = beiscsi_alloc_pdu,
4703 .parse_pdu_itt = beiscsi_parse_pdu,
4704 .get_stats = beiscsi_conn_get_stats,
Mike Christiec7f7fd52011-02-16 15:04:41 -06004705 .get_ep_param = beiscsi_ep_get_param,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304706 .ep_connect = beiscsi_ep_connect,
4707 .ep_poll = beiscsi_ep_poll,
4708 .ep_disconnect = beiscsi_ep_disconnect,
4709 .session_recovery_timedout = iscsi_session_recovery_timedout,
Jayamohan Kallickalffce3e22012-04-03 23:41:50 -05004710 .bsg_request = beiscsi_bsg_request,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304711};
4712
4713static struct pci_driver beiscsi_pci_driver = {
4714 .name = DRV_NAME,
4715 .probe = beiscsi_dev_probe,
4716 .remove = beiscsi_remove,
Jayamohan Kallickal25602c92011-08-22 10:08:28 -07004717 .shutdown = beiscsi_shutdown,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304718 .id_table = beiscsi_pci_id_table
4719};
4720
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05304721
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304722static int __init beiscsi_module_init(void)
4723{
4724 int ret;
4725
4726 beiscsi_scsi_transport =
4727 iscsi_register_transport(&beiscsi_iscsi_transport);
4728 if (!beiscsi_scsi_transport) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304729 printk(KERN_ERR
4730 "beiscsi_module_init - Unable to register beiscsi transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05304731 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304732 }
John Soni Jose99bc5d52012-08-20 23:00:18 +05304733 printk(KERN_INFO "In beiscsi_module_init, tt=%p\n",
4734 &beiscsi_iscsi_transport);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304735
4736 ret = pci_register_driver(&beiscsi_pci_driver);
4737 if (ret) {
John Soni Jose99bc5d52012-08-20 23:00:18 +05304738 printk(KERN_ERR
4739 "beiscsi_module_init - Unable to register beiscsi pci driver.\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304740 goto unregister_iscsi_transport;
4741 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05304742 return 0;
4743
4744unregister_iscsi_transport:
4745 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4746 return ret;
4747}
4748
4749static void __exit beiscsi_module_exit(void)
4750{
4751 pci_unregister_driver(&beiscsi_pci_driver);
4752 iscsi_unregister_transport(&beiscsi_iscsi_transport);
4753}
4754
4755module_init(beiscsi_module_init);
4756module_exit(beiscsi_module_exit);