blob: 85c0e80628a8dea4f3bf34c3c91077c1abb1d6ba [file] [log] [blame]
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301/**
Jayamohan Kallickald2eeb1a2010-01-23 05:35:15 +05302 * Copyright (C) 2005 - 2010 ServerEngines
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 *
10 * Written by: Jayamohan Kallickal (jayamohank@serverengines.com)
11 *
12 * Contact Information:
13 * linux-drivers@serverengines.com
14 *
15 * ServerEngines
16 * 209 N. Fair Oaks Ave
17 * Sunnyvale, CA 94085
18 *
19 */
20#include <linux/reboot.h>
21#include <linux/delay.h>
22#include <linux/interrupt.h>
23#include <linux/blkdev.h>
24#include <linux/pci.h>
25#include <linux/string.h>
26#include <linux/kernel.h>
27#include <linux/semaphore.h>
28
29#include <scsi/libiscsi.h>
30#include <scsi/scsi_transport_iscsi.h>
31#include <scsi/scsi_transport.h>
32#include <scsi/scsi_cmnd.h>
33#include <scsi/scsi_device.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include "be_main.h"
37#include "be_iscsi.h"
38#include "be_mgmt.h"
39
40static unsigned int be_iopoll_budget = 10;
41static unsigned int be_max_phys_size = 64;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053042static unsigned int enable_msix = 1;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053043
44MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
45MODULE_DESCRIPTION(DRV_DESC " " BUILD_STR);
46MODULE_AUTHOR("ServerEngines Corporation");
47MODULE_LICENSE("GPL");
48module_param(be_iopoll_budget, int, 0);
49module_param(enable_msix, int, 0);
50module_param(be_max_phys_size, uint, S_IRUGO);
51MODULE_PARM_DESC(be_max_phys_size, "Maximum Size (In Kilobytes) of physically"
52 "contiguous memory that can be allocated."
53 "Range is 16 - 128");
54
55static int beiscsi_slave_configure(struct scsi_device *sdev)
56{
57 blk_queue_max_segment_size(sdev->request_queue, 65536);
58 return 0;
59}
60
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053061/*------------------- PCI Driver operations and data ----------------- */
62static DEFINE_PCI_DEVICE_TABLE(beiscsi_pci_id_table) = {
63 { PCI_DEVICE(BE_VENDOR_ID, BE_DEVICE_ID1) },
64 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID1) },
65 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID2) },
66 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID3) },
67 { PCI_DEVICE(BE_VENDOR_ID, OC_DEVICE_ID4) },
68 { 0 }
69};
70MODULE_DEVICE_TABLE(pci, beiscsi_pci_id_table);
71
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053072static struct scsi_host_template beiscsi_sht = {
73 .module = THIS_MODULE,
74 .name = "ServerEngines 10Gbe open-iscsi Initiator Driver",
75 .proc_name = DRV_NAME,
76 .queuecommand = iscsi_queuecommand,
77 .eh_abort_handler = iscsi_eh_abort,
78 .change_queue_depth = iscsi_change_queue_depth,
79 .slave_configure = beiscsi_slave_configure,
80 .target_alloc = iscsi_target_alloc,
81 .eh_device_reset_handler = iscsi_eh_device_reset,
82 .eh_target_reset_handler = iscsi_eh_target_reset,
83 .sg_tablesize = BEISCSI_SGLIST_ELEMENTS,
84 .can_queue = BE2_IO_DEPTH,
85 .this_id = -1,
86 .max_sectors = BEISCSI_MAX_SECTORS,
87 .cmd_per_lun = BEISCSI_CMD_PER_LUN,
88 .use_clustering = ENABLE_CLUSTERING,
89};
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053090
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +053091static struct scsi_transport_template *beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +053092
93static struct beiscsi_hba *beiscsi_hba_alloc(struct pci_dev *pcidev)
94{
95 struct beiscsi_hba *phba;
96 struct Scsi_Host *shost;
97
98 shost = iscsi_host_alloc(&beiscsi_sht, sizeof(*phba), 0);
99 if (!shost) {
100 dev_err(&pcidev->dev, "beiscsi_hba_alloc -"
101 "iscsi_host_alloc failed \n");
102 return NULL;
103 }
104 shost->dma_boundary = pcidev->dma_mask;
105 shost->max_id = BE2_MAX_SESSIONS;
106 shost->max_channel = 0;
107 shost->max_cmd_len = BEISCSI_MAX_CMD_LEN;
108 shost->max_lun = BEISCSI_NUM_MAX_LUN;
109 shost->transportt = beiscsi_scsi_transport;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530110 phba = iscsi_host_priv(shost);
111 memset(phba, 0, sizeof(*phba));
112 phba->shost = shost;
113 phba->pcidev = pci_dev_get(pcidev);
Jayamohan Kallickal2807afb2010-01-05 05:07:49 +0530114 pci_set_drvdata(pcidev, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530115
116 if (iscsi_host_add(shost, &phba->pcidev->dev))
117 goto free_devices;
118 return phba;
119
120free_devices:
121 pci_dev_put(phba->pcidev);
122 iscsi_host_free(phba->shost);
123 return NULL;
124}
125
126static void beiscsi_unmap_pci_function(struct beiscsi_hba *phba)
127{
128 if (phba->csr_va) {
129 iounmap(phba->csr_va);
130 phba->csr_va = NULL;
131 }
132 if (phba->db_va) {
133 iounmap(phba->db_va);
134 phba->db_va = NULL;
135 }
136 if (phba->pci_va) {
137 iounmap(phba->pci_va);
138 phba->pci_va = NULL;
139 }
140}
141
142static int beiscsi_map_pci_bars(struct beiscsi_hba *phba,
143 struct pci_dev *pcidev)
144{
145 u8 __iomem *addr;
146
147 addr = ioremap_nocache(pci_resource_start(pcidev, 2),
148 pci_resource_len(pcidev, 2));
149 if (addr == NULL)
150 return -ENOMEM;
151 phba->ctrl.csr = addr;
152 phba->csr_va = addr;
153 phba->csr_pa.u.a64.address = pci_resource_start(pcidev, 2);
154
155 addr = ioremap_nocache(pci_resource_start(pcidev, 4), 128 * 1024);
156 if (addr == NULL)
157 goto pci_map_err;
158 phba->ctrl.db = addr;
159 phba->db_va = addr;
160 phba->db_pa.u.a64.address = pci_resource_start(pcidev, 4);
161
162 addr = ioremap_nocache(pci_resource_start(pcidev, 1),
163 pci_resource_len(pcidev, 1));
164 if (addr == NULL)
165 goto pci_map_err;
166 phba->ctrl.pcicfg = addr;
167 phba->pci_va = addr;
168 phba->pci_pa.u.a64.address = pci_resource_start(pcidev, 1);
169 return 0;
170
171pci_map_err:
172 beiscsi_unmap_pci_function(phba);
173 return -ENOMEM;
174}
175
176static int beiscsi_enable_pci(struct pci_dev *pcidev)
177{
178 int ret;
179
180 ret = pci_enable_device(pcidev);
181 if (ret) {
182 dev_err(&pcidev->dev, "beiscsi_enable_pci - enable device "
183 "failed. Returning -ENODEV\n");
184 return ret;
185 }
186
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530187 pci_set_master(pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530188 if (pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(64))) {
189 ret = pci_set_consistent_dma_mask(pcidev, DMA_BIT_MASK(32));
190 if (ret) {
191 dev_err(&pcidev->dev, "Could not set PCI DMA Mask\n");
192 pci_disable_device(pcidev);
193 return ret;
194 }
195 }
196 return 0;
197}
198
199static int be_ctrl_init(struct beiscsi_hba *phba, struct pci_dev *pdev)
200{
201 struct be_ctrl_info *ctrl = &phba->ctrl;
202 struct be_dma_mem *mbox_mem_alloc = &ctrl->mbox_mem_alloced;
203 struct be_dma_mem *mbox_mem_align = &ctrl->mbox_mem;
204 int status = 0;
205
206 ctrl->pdev = pdev;
207 status = beiscsi_map_pci_bars(phba, pdev);
208 if (status)
209 return status;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530210 mbox_mem_alloc->size = sizeof(struct be_mcc_mailbox) + 16;
211 mbox_mem_alloc->va = pci_alloc_consistent(pdev,
212 mbox_mem_alloc->size,
213 &mbox_mem_alloc->dma);
214 if (!mbox_mem_alloc->va) {
215 beiscsi_unmap_pci_function(phba);
216 status = -ENOMEM;
217 return status;
218 }
219
220 mbox_mem_align->size = sizeof(struct be_mcc_mailbox);
221 mbox_mem_align->va = PTR_ALIGN(mbox_mem_alloc->va, 16);
222 mbox_mem_align->dma = PTR_ALIGN(mbox_mem_alloc->dma, 16);
223 memset(mbox_mem_align->va, 0, sizeof(struct be_mcc_mailbox));
224 spin_lock_init(&ctrl->mbox_lock);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530225 spin_lock_init(&phba->ctrl.mcc_lock);
226 spin_lock_init(&phba->ctrl.mcc_cq_lock);
227
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530228 return status;
229}
230
231static void beiscsi_get_params(struct beiscsi_hba *phba)
232{
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530233 phba->params.ios_per_ctrl = (phba->fw_config.iscsi_icd_count
234 - (phba->fw_config.iscsi_cid_count
235 + BE2_TMFS
236 + BE2_NOPOUT_REQ));
237 phba->params.cxns_per_ctrl = phba->fw_config.iscsi_cid_count;
238 phba->params.asyncpdus_per_ctrl = phba->fw_config.iscsi_cid_count;;
239 phba->params.icds_per_ctrl = phba->fw_config.iscsi_icd_count;;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530240 phba->params.num_sge_per_io = BE2_SGE;
241 phba->params.defpdu_hdr_sz = BE2_DEFPDU_HDR_SZ;
242 phba->params.defpdu_data_sz = BE2_DEFPDU_DATA_SZ;
243 phba->params.eq_timer = 64;
244 phba->params.num_eq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530245 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
246 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530247 phba->params.num_eq_entries = (phba->params.num_eq_entries < 1024)
248 ? 1024 : phba->params.num_eq_entries;
249 SE_DEBUG(DBG_LVL_8, "phba->params.num_eq_entries=%d \n",
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530250 phba->params.num_eq_entries);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530251 phba->params.num_cq_entries =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530252 (((BE2_CMDS_PER_CXN * 2 + phba->fw_config.iscsi_cid_count * 2
253 + BE2_TMFS) / 512) + 1) * 512;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530254 phba->params.wrbs_per_cxn = 256;
255}
256
257static void hwi_ring_eq_db(struct beiscsi_hba *phba,
258 unsigned int id, unsigned int clr_interrupt,
259 unsigned int num_processed,
260 unsigned char rearm, unsigned char event)
261{
262 u32 val = 0;
263 val |= id & DB_EQ_RING_ID_MASK;
264 if (rearm)
265 val |= 1 << DB_EQ_REARM_SHIFT;
266 if (clr_interrupt)
267 val |= 1 << DB_EQ_CLR_SHIFT;
268 if (event)
269 val |= 1 << DB_EQ_EVNT_SHIFT;
270 val |= num_processed << DB_EQ_NUM_POPPED_SHIFT;
271 iowrite32(val, phba->db_va + DB_EQ_OFFSET);
272}
273
274/**
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530275 * be_isr_mcc - The isr routine of the driver.
276 * @irq: Not used
277 * @dev_id: Pointer to host adapter structure
278 */
279static irqreturn_t be_isr_mcc(int irq, void *dev_id)
280{
281 struct beiscsi_hba *phba;
282 struct be_eq_entry *eqe = NULL;
283 struct be_queue_info *eq;
284 struct be_queue_info *mcc;
285 unsigned int num_eq_processed;
286 struct be_eq_obj *pbe_eq;
287 unsigned long flags;
288
289 pbe_eq = dev_id;
290 eq = &pbe_eq->q;
291 phba = pbe_eq->phba;
292 mcc = &phba->ctrl.mcc_obj.cq;
293 eqe = queue_tail_node(eq);
294 if (!eqe)
295 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
296
297 num_eq_processed = 0;
298
299 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
300 & EQE_VALID_MASK) {
301 if (((eqe->dw[offsetof(struct amap_eq_entry,
302 resource_id) / 32] &
303 EQE_RESID_MASK) >> 16) == mcc->id) {
304 spin_lock_irqsave(&phba->isr_lock, flags);
305 phba->todo_mcc_cq = 1;
306 spin_unlock_irqrestore(&phba->isr_lock, flags);
307 }
308 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
309 queue_tail_inc(eq);
310 eqe = queue_tail_node(eq);
311 num_eq_processed++;
312 }
313 if (phba->todo_mcc_cq)
314 queue_work(phba->wq, &phba->work_cqs);
315 if (num_eq_processed)
316 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
317
318 return IRQ_HANDLED;
319}
320
321/**
322 * be_isr_msix - The isr routine of the driver.
323 * @irq: Not used
324 * @dev_id: Pointer to host adapter structure
325 */
326static irqreturn_t be_isr_msix(int irq, void *dev_id)
327{
328 struct beiscsi_hba *phba;
329 struct be_eq_entry *eqe = NULL;
330 struct be_queue_info *eq;
331 struct be_queue_info *cq;
332 unsigned int num_eq_processed;
333 struct be_eq_obj *pbe_eq;
334 unsigned long flags;
335
336 pbe_eq = dev_id;
337 eq = &pbe_eq->q;
338 cq = pbe_eq->cq;
339 eqe = queue_tail_node(eq);
340 if (!eqe)
341 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
342
343 phba = pbe_eq->phba;
344 num_eq_processed = 0;
345 if (blk_iopoll_enabled) {
346 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
347 & EQE_VALID_MASK) {
348 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
349 blk_iopoll_sched(&pbe_eq->iopoll);
350
351 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
352 queue_tail_inc(eq);
353 eqe = queue_tail_node(eq);
354 num_eq_processed++;
355 }
356 if (num_eq_processed)
357 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 0, 1);
358
359 return IRQ_HANDLED;
360 } else {
361 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
362 & EQE_VALID_MASK) {
363 spin_lock_irqsave(&phba->isr_lock, flags);
364 phba->todo_cq = 1;
365 spin_unlock_irqrestore(&phba->isr_lock, flags);
366 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
367 queue_tail_inc(eq);
368 eqe = queue_tail_node(eq);
369 num_eq_processed++;
370 }
371 if (phba->todo_cq)
372 queue_work(phba->wq, &phba->work_cqs);
373
374 if (num_eq_processed)
375 hwi_ring_eq_db(phba, eq->id, 1, num_eq_processed, 1, 1);
376
377 return IRQ_HANDLED;
378 }
379}
380
381/**
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530382 * be_isr - The isr routine of the driver.
383 * @irq: Not used
384 * @dev_id: Pointer to host adapter structure
385 */
386static irqreturn_t be_isr(int irq, void *dev_id)
387{
388 struct beiscsi_hba *phba;
389 struct hwi_controller *phwi_ctrlr;
390 struct hwi_context_memory *phwi_context;
391 struct be_eq_entry *eqe = NULL;
392 struct be_queue_info *eq;
393 struct be_queue_info *cq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530394 struct be_queue_info *mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530395 unsigned long flags, index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530396 unsigned int num_mcceq_processed, num_ioeq_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530397 struct be_ctrl_info *ctrl;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530398 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530399 int isr;
400
401 phba = dev_id;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530402 ctrl = &phba->ctrl;;
403 isr = ioread32(ctrl->csr + CEV_ISR0_OFFSET +
404 (PCI_FUNC(ctrl->pdev->devfn) * CEV_ISR_SIZE));
405 if (!isr)
406 return IRQ_NONE;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530407
408 phwi_ctrlr = phba->phwi_ctrlr;
409 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530410 pbe_eq = &phwi_context->be_eq[0];
411
412 eq = &phwi_context->be_eq[0].q;
413 mcc = &phba->ctrl.mcc_obj.cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530414 index = 0;
415 eqe = queue_tail_node(eq);
416 if (!eqe)
417 SE_DEBUG(DBG_LVL_1, "eqe is NULL\n");
418
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530419 num_ioeq_processed = 0;
420 num_mcceq_processed = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530421 if (blk_iopoll_enabled) {
422 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
423 & EQE_VALID_MASK) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530424 if (((eqe->dw[offsetof(struct amap_eq_entry,
425 resource_id) / 32] &
426 EQE_RESID_MASK) >> 16) == mcc->id) {
427 spin_lock_irqsave(&phba->isr_lock, flags);
428 phba->todo_mcc_cq = 1;
429 spin_unlock_irqrestore(&phba->isr_lock, flags);
430 num_mcceq_processed++;
431 } else {
432 if (!blk_iopoll_sched_prep(&pbe_eq->iopoll))
433 blk_iopoll_sched(&pbe_eq->iopoll);
434 num_ioeq_processed++;
435 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530436 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
437 queue_tail_inc(eq);
438 eqe = queue_tail_node(eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530439 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530440 if (num_ioeq_processed || num_mcceq_processed) {
441 if (phba->todo_mcc_cq)
442 queue_work(phba->wq, &phba->work_cqs);
443
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530444 if ((num_mcceq_processed) && (!num_ioeq_processed))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530445 hwi_ring_eq_db(phba, eq->id, 0,
446 (num_ioeq_processed +
447 num_mcceq_processed) , 1, 1);
448 else
449 hwi_ring_eq_db(phba, eq->id, 0,
450 (num_ioeq_processed +
451 num_mcceq_processed), 0, 1);
452
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530453 return IRQ_HANDLED;
454 } else
455 return IRQ_NONE;
456 } else {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530457 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530458 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
459 & EQE_VALID_MASK) {
460
461 if (((eqe->dw[offsetof(struct amap_eq_entry,
462 resource_id) / 32] &
463 EQE_RESID_MASK) >> 16) != cq->id) {
464 spin_lock_irqsave(&phba->isr_lock, flags);
465 phba->todo_mcc_cq = 1;
466 spin_unlock_irqrestore(&phba->isr_lock, flags);
467 } else {
468 spin_lock_irqsave(&phba->isr_lock, flags);
469 phba->todo_cq = 1;
470 spin_unlock_irqrestore(&phba->isr_lock, flags);
471 }
472 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
473 queue_tail_inc(eq);
474 eqe = queue_tail_node(eq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530475 num_ioeq_processed++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530476 }
477 if (phba->todo_cq || phba->todo_mcc_cq)
478 queue_work(phba->wq, &phba->work_cqs);
479
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530480 if (num_ioeq_processed) {
481 hwi_ring_eq_db(phba, eq->id, 0,
482 num_ioeq_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530483 return IRQ_HANDLED;
484 } else
485 return IRQ_NONE;
486 }
487}
488
489static int beiscsi_init_irqs(struct beiscsi_hba *phba)
490{
491 struct pci_dev *pcidev = phba->pcidev;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530492 struct hwi_controller *phwi_ctrlr;
493 struct hwi_context_memory *phwi_context;
494 int ret, msix_vec, i = 0;
495 char desc[32];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530496
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530497 phwi_ctrlr = phba->phwi_ctrlr;
498 phwi_context = phwi_ctrlr->phwi_ctxt;
499
500 if (phba->msix_enabled) {
501 for (i = 0; i < phba->num_cpus; i++) {
502 sprintf(desc, "beiscsi_msix_%04x", i);
503 msix_vec = phba->msix_entries[i].vector;
504 ret = request_irq(msix_vec, be_isr_msix, 0, desc,
505 &phwi_context->be_eq[i]);
506 }
507 msix_vec = phba->msix_entries[i].vector;
508 ret = request_irq(msix_vec, be_isr_mcc, 0, "beiscsi_msix_mcc",
509 &phwi_context->be_eq[i]);
510 } else {
511 ret = request_irq(pcidev->irq, be_isr, IRQF_SHARED,
512 "beiscsi", phba);
513 if (ret) {
514 shost_printk(KERN_ERR, phba->shost, "beiscsi_init_irqs-"
515 "Failed to register irq\\n");
516 return ret;
517 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530518 }
519 return 0;
520}
521
522static void hwi_ring_cq_db(struct beiscsi_hba *phba,
523 unsigned int id, unsigned int num_processed,
524 unsigned char rearm, unsigned char event)
525{
526 u32 val = 0;
527 val |= id & DB_CQ_RING_ID_MASK;
528 if (rearm)
529 val |= 1 << DB_CQ_REARM_SHIFT;
530 val |= num_processed << DB_CQ_NUM_POPPED_SHIFT;
531 iowrite32(val, phba->db_va + DB_CQ_OFFSET);
532}
533
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530534static unsigned int
535beiscsi_process_async_pdu(struct beiscsi_conn *beiscsi_conn,
536 struct beiscsi_hba *phba,
537 unsigned short cid,
538 struct pdu_base *ppdu,
539 unsigned long pdu_len,
540 void *pbuffer, unsigned long buf_len)
541{
542 struct iscsi_conn *conn = beiscsi_conn->conn;
543 struct iscsi_session *session = conn->session;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530544 struct iscsi_task *task;
545 struct beiscsi_io_task *io_task;
546 struct iscsi_hdr *login_hdr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530547
548 switch (ppdu->dw[offsetof(struct amap_pdu_base, opcode) / 32] &
549 PDUBASE_OPCODE_MASK) {
550 case ISCSI_OP_NOOP_IN:
551 pbuffer = NULL;
552 buf_len = 0;
553 break;
554 case ISCSI_OP_ASYNC_EVENT:
555 break;
556 case ISCSI_OP_REJECT:
557 WARN_ON(!pbuffer);
558 WARN_ON(!(buf_len == 48));
559 SE_DEBUG(DBG_LVL_1, "In ISCSI_OP_REJECT\n");
560 break;
561 case ISCSI_OP_LOGIN_RSP:
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530562 case ISCSI_OP_TEXT_RSP:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530563 task = conn->login_task;
564 io_task = task->dd_data;
565 login_hdr = (struct iscsi_hdr *)ppdu;
566 login_hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530567 break;
568 default:
569 shost_printk(KERN_WARNING, phba->shost,
570 "Unrecognized opcode 0x%x in async msg \n",
571 (ppdu->
572 dw[offsetof(struct amap_pdu_base, opcode) / 32]
573 & PDUBASE_OPCODE_MASK));
574 return 1;
575 }
576
577 spin_lock_bh(&session->lock);
578 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)ppdu, pbuffer, buf_len);
579 spin_unlock_bh(&session->lock);
580 return 0;
581}
582
583static struct sgl_handle *alloc_io_sgl_handle(struct beiscsi_hba *phba)
584{
585 struct sgl_handle *psgl_handle;
586
587 if (phba->io_sgl_hndl_avbl) {
588 SE_DEBUG(DBG_LVL_8,
589 "In alloc_io_sgl_handle,io_sgl_alloc_index=%d \n",
590 phba->io_sgl_alloc_index);
591 psgl_handle = phba->io_sgl_hndl_base[phba->
592 io_sgl_alloc_index];
593 phba->io_sgl_hndl_base[phba->io_sgl_alloc_index] = NULL;
594 phba->io_sgl_hndl_avbl--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530595 if (phba->io_sgl_alloc_index == (phba->params.
596 ios_per_ctrl - 1))
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530597 phba->io_sgl_alloc_index = 0;
598 else
599 phba->io_sgl_alloc_index++;
600 } else
601 psgl_handle = NULL;
602 return psgl_handle;
603}
604
605static void
606free_io_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
607{
608 SE_DEBUG(DBG_LVL_8, "In free_,io_sgl_free_index=%d \n",
609 phba->io_sgl_free_index);
610 if (phba->io_sgl_hndl_base[phba->io_sgl_free_index]) {
611 /*
612 * this can happen if clean_task is called on a task that
613 * failed in xmit_task or alloc_pdu.
614 */
615 SE_DEBUG(DBG_LVL_8,
616 "Double Free in IO SGL io_sgl_free_index=%d,"
617 "value there=%p \n", phba->io_sgl_free_index,
618 phba->io_sgl_hndl_base[phba->io_sgl_free_index]);
619 return;
620 }
621 phba->io_sgl_hndl_base[phba->io_sgl_free_index] = psgl_handle;
622 phba->io_sgl_hndl_avbl++;
623 if (phba->io_sgl_free_index == (phba->params.ios_per_ctrl - 1))
624 phba->io_sgl_free_index = 0;
625 else
626 phba->io_sgl_free_index++;
627}
628
629/**
630 * alloc_wrb_handle - To allocate a wrb handle
631 * @phba: The hba pointer
632 * @cid: The cid to use for allocation
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530633 *
634 * This happens under session_lock until submission to chip
635 */
Jayamohan Kallickald5431482010-01-05 05:06:21 +0530636struct wrb_handle *alloc_wrb_handle(struct beiscsi_hba *phba, unsigned int cid)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530637{
638 struct hwi_wrb_context *pwrb_context;
639 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickald5431482010-01-05 05:06:21 +0530640 struct wrb_handle *pwrb_handle, *pwrb_handle_tmp;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530641
642 phwi_ctrlr = phba->phwi_ctrlr;
643 pwrb_context = &phwi_ctrlr->wrb_context[cid];
Jayamohan Kallickald5431482010-01-05 05:06:21 +0530644 if (pwrb_context->wrb_handles_available >= 2) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530645 pwrb_handle = pwrb_context->pwrb_handle_base[
646 pwrb_context->alloc_index];
647 pwrb_context->wrb_handles_available--;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530648 if (pwrb_context->alloc_index ==
649 (phba->params.wrbs_per_cxn - 1))
650 pwrb_context->alloc_index = 0;
651 else
652 pwrb_context->alloc_index++;
Jayamohan Kallickald5431482010-01-05 05:06:21 +0530653 pwrb_handle_tmp = pwrb_context->pwrb_handle_base[
654 pwrb_context->alloc_index];
655 pwrb_handle->nxt_wrb_index = pwrb_handle_tmp->wrb_index;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530656 } else
657 pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530658 return pwrb_handle;
659}
660
661/**
662 * free_wrb_handle - To free the wrb handle back to pool
663 * @phba: The hba pointer
664 * @pwrb_context: The context to free from
665 * @pwrb_handle: The wrb_handle to free
666 *
667 * This happens under session_lock until submission to chip
668 */
669static void
670free_wrb_handle(struct beiscsi_hba *phba, struct hwi_wrb_context *pwrb_context,
671 struct wrb_handle *pwrb_handle)
672{
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530673 pwrb_context->pwrb_handle_base[pwrb_context->free_index] = pwrb_handle;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530674 pwrb_context->wrb_handles_available++;
675 if (pwrb_context->free_index == (phba->params.wrbs_per_cxn - 1))
676 pwrb_context->free_index = 0;
677 else
678 pwrb_context->free_index++;
679
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530680 SE_DEBUG(DBG_LVL_8,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530681 "FREE WRB: pwrb_handle=%p free_index=0x%x"
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530682 "wrb_handles_available=%d \n",
683 pwrb_handle, pwrb_context->free_index,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530684 pwrb_context->wrb_handles_available);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530685}
686
687static struct sgl_handle *alloc_mgmt_sgl_handle(struct beiscsi_hba *phba)
688{
689 struct sgl_handle *psgl_handle;
690
691 if (phba->eh_sgl_hndl_avbl) {
692 psgl_handle = phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index];
693 phba->eh_sgl_hndl_base[phba->eh_sgl_alloc_index] = NULL;
694 SE_DEBUG(DBG_LVL_8, "mgmt_sgl_alloc_index=%d=0x%x \n",
695 phba->eh_sgl_alloc_index, phba->eh_sgl_alloc_index);
696 phba->eh_sgl_hndl_avbl--;
697 if (phba->eh_sgl_alloc_index ==
698 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl -
699 1))
700 phba->eh_sgl_alloc_index = 0;
701 else
702 phba->eh_sgl_alloc_index++;
703 } else
704 psgl_handle = NULL;
705 return psgl_handle;
706}
707
708void
709free_mgmt_sgl_handle(struct beiscsi_hba *phba, struct sgl_handle *psgl_handle)
710{
711
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530712 SE_DEBUG(DBG_LVL_8, "In free_mgmt_sgl_handle,eh_sgl_free_index=%d \n",
713 phba->eh_sgl_free_index);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530714 if (phba->eh_sgl_hndl_base[phba->eh_sgl_free_index]) {
715 /*
716 * this can happen if clean_task is called on a task that
717 * failed in xmit_task or alloc_pdu.
718 */
719 SE_DEBUG(DBG_LVL_8,
720 "Double Free in eh SGL ,eh_sgl_free_index=%d \n",
721 phba->eh_sgl_free_index);
722 return;
723 }
724 phba->eh_sgl_hndl_base[phba->eh_sgl_free_index] = psgl_handle;
725 phba->eh_sgl_hndl_avbl++;
726 if (phba->eh_sgl_free_index ==
727 (phba->params.icds_per_ctrl - phba->params.ios_per_ctrl - 1))
728 phba->eh_sgl_free_index = 0;
729 else
730 phba->eh_sgl_free_index++;
731}
732
733static void
734be_complete_io(struct beiscsi_conn *beiscsi_conn,
735 struct iscsi_task *task, struct sol_cqe *psol)
736{
737 struct beiscsi_io_task *io_task = task->dd_data;
738 struct be_status_bhs *sts_bhs =
739 (struct be_status_bhs *)io_task->cmd_bhs;
740 struct iscsi_conn *conn = beiscsi_conn->conn;
741 unsigned int sense_len;
742 unsigned char *sense;
743 u32 resid = 0, exp_cmdsn, max_cmdsn;
744 u8 rsp, status, flags;
745
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530746 exp_cmdsn = (psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530747 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
748 & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530749 max_cmdsn = ((psol->
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530750 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
751 & SOL_EXP_CMD_SN_MASK) +
752 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
753 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
754 rsp = ((psol->dw[offsetof(struct amap_sol_cqe, i_resp) / 32]
755 & SOL_RESP_MASK) >> 16);
756 status = ((psol->dw[offsetof(struct amap_sol_cqe, i_sts) / 32]
757 & SOL_STS_MASK) >> 8);
758 flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
759 & SOL_FLAGS_MASK) >> 24) | 0x80;
760
761 task->sc->result = (DID_OK << 16) | status;
762 if (rsp != ISCSI_STATUS_CMD_COMPLETED) {
763 task->sc->result = DID_ERROR << 16;
764 goto unmap;
765 }
766
767 /* bidi not initially supported */
768 if (flags & (ISCSI_FLAG_CMD_UNDERFLOW | ISCSI_FLAG_CMD_OVERFLOW)) {
769 resid = (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) /
770 32] & SOL_RES_CNT_MASK);
771
772 if (!status && (flags & ISCSI_FLAG_CMD_OVERFLOW))
773 task->sc->result = DID_ERROR << 16;
774
775 if (flags & ISCSI_FLAG_CMD_UNDERFLOW) {
776 scsi_set_resid(task->sc, resid);
777 if (!status && (scsi_bufflen(task->sc) - resid <
778 task->sc->underflow))
779 task->sc->result = DID_ERROR << 16;
780 }
781 }
782
783 if (status == SAM_STAT_CHECK_CONDITION) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530784 unsigned short *slen = (unsigned short *)sts_bhs->sense_info;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530785 sense = sts_bhs->sense_info + sizeof(unsigned short);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530786 sense_len = cpu_to_be16(*slen);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530787 memcpy(task->sc->sense_buffer, sense,
788 min_t(u16, sense_len, SCSI_SENSE_BUFFERSIZE));
789 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +0530790
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530791 if (io_task->cmd_bhs->iscsi_hdr.flags & ISCSI_FLAG_CMD_READ) {
792 if (psol->dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
793 & SOL_RES_CNT_MASK)
794 conn->rxdata_octets += (psol->
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530795 dw[offsetof(struct amap_sol_cqe, i_res_cnt) / 32]
796 & SOL_RES_CNT_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530797 }
798unmap:
799 scsi_dma_unmap(io_task->scsi_cmnd);
800 iscsi_complete_scsi_task(task, exp_cmdsn, max_cmdsn);
801}
802
803static void
804be_complete_logout(struct beiscsi_conn *beiscsi_conn,
805 struct iscsi_task *task, struct sol_cqe *psol)
806{
807 struct iscsi_logout_rsp *hdr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530808 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530809 struct iscsi_conn *conn = beiscsi_conn->conn;
810
811 hdr = (struct iscsi_logout_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530812 hdr->opcode = ISCSI_OP_LOGOUT_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530813 hdr->t2wait = 5;
814 hdr->t2retain = 0;
815 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
816 & SOL_FLAGS_MASK) >> 24) | 0x80;
817 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
818 32] & SOL_RESP_MASK);
819 hdr->exp_cmdsn = cpu_to_be32(psol->
820 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
821 & SOL_EXP_CMD_SN_MASK);
822 hdr->max_cmdsn = be32_to_cpu((psol->
823 dw[offsetof(struct amap_sol_cqe, i_exp_cmd_sn) / 32]
824 & SOL_EXP_CMD_SN_MASK) +
825 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
826 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530827 hdr->dlength[0] = 0;
828 hdr->dlength[1] = 0;
829 hdr->dlength[2] = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530830 hdr->hlength = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530831 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530832 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
833}
834
835static void
836be_complete_tmf(struct beiscsi_conn *beiscsi_conn,
837 struct iscsi_task *task, struct sol_cqe *psol)
838{
839 struct iscsi_tm_rsp *hdr;
840 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530841 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530842
843 hdr = (struct iscsi_tm_rsp *)task->hdr;
Jayamohan Kallickal7bd6e252010-01-05 05:07:02 +0530844 hdr->opcode = ISCSI_OP_SCSI_TMFUNC_RSP;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530845 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
846 & SOL_FLAGS_MASK) >> 24) | 0x80;
847 hdr->response = (psol->dw[offsetof(struct amap_sol_cqe, i_resp) /
848 32] & SOL_RESP_MASK);
849 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530850 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530851 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
852 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
853 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
854 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530855 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530856 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
857}
858
859static void
860hwi_complete_drvr_msgs(struct beiscsi_conn *beiscsi_conn,
861 struct beiscsi_hba *phba, struct sol_cqe *psol)
862{
863 struct hwi_wrb_context *pwrb_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530864 struct wrb_handle *pwrb_handle = NULL;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530865 struct hwi_controller *phwi_ctrlr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530866 struct iscsi_task *task;
867 struct beiscsi_io_task *io_task;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530868 struct iscsi_conn *conn = beiscsi_conn->conn;
869 struct iscsi_session *session = conn->session;
870
871 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530872 pwrb_context = &phwi_ctrlr->wrb_context[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530873 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530874 SOL_CID_MASK) >> 6) -
875 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530876 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530877 dw[offsetof(struct amap_sol_cqe, wrb_index) /
878 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530879 task = pwrb_handle->pio_handle;
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530880
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530881 io_task = task->dd_data;
882 spin_lock(&phba->mgmt_sgl_lock);
883 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
884 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530885 spin_lock_bh(&session->lock);
886 free_wrb_handle(phba, pwrb_context, pwrb_handle);
887 spin_unlock_bh(&session->lock);
888}
889
890static void
891be_complete_nopin_resp(struct beiscsi_conn *beiscsi_conn,
892 struct iscsi_task *task, struct sol_cqe *psol)
893{
894 struct iscsi_nopin *hdr;
895 struct iscsi_conn *conn = beiscsi_conn->conn;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530896 struct beiscsi_io_task *io_task = task->dd_data;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530897
898 hdr = (struct iscsi_nopin *)task->hdr;
899 hdr->flags = ((psol->dw[offsetof(struct amap_sol_cqe, i_flags) / 32]
900 & SOL_FLAGS_MASK) >> 24) | 0x80;
901 hdr->exp_cmdsn = cpu_to_be32(psol->dw[offsetof(struct amap_sol_cqe,
902 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK);
903 hdr->max_cmdsn = be32_to_cpu((psol->dw[offsetof(struct amap_sol_cqe,
904 i_exp_cmd_sn) / 32] & SOL_EXP_CMD_SN_MASK) +
905 ((psol->dw[offsetof(struct amap_sol_cqe, i_cmd_wnd)
906 / 32] & SOL_CMD_WND_MASK) >> 24) - 1);
907 hdr->opcode = ISCSI_OP_NOOP_IN;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530908 hdr->itt = io_task->libiscsi_itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530909 __iscsi_complete_pdu(conn, (struct iscsi_hdr *)hdr, NULL, 0);
910}
911
912static void hwi_complete_cmd(struct beiscsi_conn *beiscsi_conn,
913 struct beiscsi_hba *phba, struct sol_cqe *psol)
914{
915 struct hwi_wrb_context *pwrb_context;
916 struct wrb_handle *pwrb_handle;
917 struct iscsi_wrb *pwrb = NULL;
918 struct hwi_controller *phwi_ctrlr;
919 struct iscsi_task *task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530920 unsigned int type;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530921 struct iscsi_conn *conn = beiscsi_conn->conn;
922 struct iscsi_session *session = conn->session;
923
924 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530925 pwrb_context = &phwi_ctrlr->wrb_context[((psol->dw[offsetof
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530926 (struct amap_sol_cqe, cid) / 32]
Jayamohan Kallickal7da50872010-01-05 05:04:12 +0530927 & SOL_CID_MASK) >> 6) -
928 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530929 pwrb_handle = pwrb_context->pwrb_handle_basestd[((psol->
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530930 dw[offsetof(struct amap_sol_cqe, wrb_index) /
931 32] & SOL_WRB_INDEX_MASK) >> 16)];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530932 task = pwrb_handle->pio_handle;
933 pwrb = pwrb_handle->pwrb;
934 type = (pwrb->dw[offsetof(struct amap_iscsi_wrb, type) / 32] &
935 WRB_TYPE_MASK) >> 28;
936
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +0530937 spin_lock_bh(&session->lock);
938 switch (type) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530939 case HWH_TYPE_IO:
940 case HWH_TYPE_IO_RD:
941 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) ==
942 ISCSI_OP_NOOP_OUT) {
943 be_complete_nopin_resp(beiscsi_conn, task, psol);
944 } else
945 be_complete_io(beiscsi_conn, task, psol);
946 break;
947
948 case HWH_TYPE_LOGOUT:
949 be_complete_logout(beiscsi_conn, task, psol);
950 break;
951
952 case HWH_TYPE_LOGIN:
953 SE_DEBUG(DBG_LVL_1,
954 "\t\t No HWH_TYPE_LOGIN Expected in hwi_complete_cmd"
955 "- Solicited path \n");
956 break;
957
958 case HWH_TYPE_TMF:
959 be_complete_tmf(beiscsi_conn, task, psol);
960 break;
961
962 case HWH_TYPE_NOP:
963 be_complete_nopin_resp(beiscsi_conn, task, psol);
964 break;
965
966 default:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +0530967 shost_printk(KERN_WARNING, phba->shost,
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530968 "In hwi_complete_cmd, unknown type = %d"
969 "wrb_index 0x%x CID 0x%x\n", type,
970 ((psol->dw[offsetof(struct amap_iscsi_wrb,
971 type) / 32] & SOL_WRB_INDEX_MASK) >> 16),
972 ((psol->dw[offsetof(struct amap_sol_cqe,
973 cid) / 32] & SOL_CID_MASK) >> 6));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530974 break;
975 }
Jayamohan Kallickal35e66012009-10-23 11:53:49 +0530976
Jayamohan Kallickal6733b392009-09-05 07:36:35 +0530977 spin_unlock_bh(&session->lock);
978}
979
980static struct list_head *hwi_get_async_busy_list(struct hwi_async_pdu_context
981 *pasync_ctx, unsigned int is_header,
982 unsigned int host_write_ptr)
983{
984 if (is_header)
985 return &pasync_ctx->async_entry[host_write_ptr].
986 header_busy_list;
987 else
988 return &pasync_ctx->async_entry[host_write_ptr].data_busy_list;
989}
990
991static struct async_pdu_handle *
992hwi_get_async_handle(struct beiscsi_hba *phba,
993 struct beiscsi_conn *beiscsi_conn,
994 struct hwi_async_pdu_context *pasync_ctx,
995 struct i_t_dpdu_cqe *pdpdu_cqe, unsigned int *pcq_index)
996{
997 struct be_bus_address phys_addr;
998 struct list_head *pbusy_list;
999 struct async_pdu_handle *pasync_handle = NULL;
1000 int buffer_len = 0;
1001 unsigned char buffer_index = -1;
1002 unsigned char is_header = 0;
1003
1004 phys_addr.u.a32.address_lo =
1005 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_lo) / 32] -
1006 ((pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1007 & PDUCQE_DPL_MASK) >> 16);
1008 phys_addr.u.a32.address_hi =
1009 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, db_addr_hi) / 32];
1010
1011 phys_addr.u.a64.address =
1012 *((unsigned long long *)(&phys_addr.u.a64.address));
1013
1014 switch (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe, code) / 32]
1015 & PDUCQE_CODE_MASK) {
1016 case UNSOL_HDR_NOTIFY:
1017 is_header = 1;
1018
1019 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 1,
1020 (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1021 index) / 32] & PDUCQE_INDEX_MASK));
1022
1023 buffer_len = (unsigned int)(phys_addr.u.a64.address -
1024 pasync_ctx->async_header.pa_base.u.a64.address);
1025
1026 buffer_index = buffer_len /
1027 pasync_ctx->async_header.buffer_size;
1028
1029 break;
1030 case UNSOL_DATA_NOTIFY:
1031 pbusy_list = hwi_get_async_busy_list(pasync_ctx, 0, (pdpdu_cqe->
1032 dw[offsetof(struct amap_i_t_dpdu_cqe,
1033 index) / 32] & PDUCQE_INDEX_MASK));
1034 buffer_len = (unsigned long)(phys_addr.u.a64.address -
1035 pasync_ctx->async_data.pa_base.u.
1036 a64.address);
1037 buffer_index = buffer_len / pasync_ctx->async_data.buffer_size;
1038 break;
1039 default:
1040 pbusy_list = NULL;
1041 shost_printk(KERN_WARNING, phba->shost,
1042 "Unexpected code=%d \n",
1043 pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1044 code) / 32] & PDUCQE_CODE_MASK);
1045 return NULL;
1046 }
1047
1048 WARN_ON(!(buffer_index <= pasync_ctx->async_data.num_entries));
1049 WARN_ON(list_empty(pbusy_list));
1050 list_for_each_entry(pasync_handle, pbusy_list, link) {
1051 WARN_ON(pasync_handle->consumed);
1052 if (pasync_handle->index == buffer_index)
1053 break;
1054 }
1055
1056 WARN_ON(!pasync_handle);
1057
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301058 pasync_handle->cri = (unsigned short)beiscsi_conn->beiscsi_conn_cid -
1059 phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301060 pasync_handle->is_header = is_header;
1061 pasync_handle->buffer_len = ((pdpdu_cqe->
1062 dw[offsetof(struct amap_i_t_dpdu_cqe, dpl) / 32]
1063 & PDUCQE_DPL_MASK) >> 16);
1064
1065 *pcq_index = (pdpdu_cqe->dw[offsetof(struct amap_i_t_dpdu_cqe,
1066 index) / 32] & PDUCQE_INDEX_MASK);
1067 return pasync_handle;
1068}
1069
1070static unsigned int
1071hwi_update_async_writables(struct hwi_async_pdu_context *pasync_ctx,
1072 unsigned int is_header, unsigned int cq_index)
1073{
1074 struct list_head *pbusy_list;
1075 struct async_pdu_handle *pasync_handle;
1076 unsigned int num_entries, writables = 0;
1077 unsigned int *pep_read_ptr, *pwritables;
1078
1079
1080 if (is_header) {
1081 pep_read_ptr = &pasync_ctx->async_header.ep_read_ptr;
1082 pwritables = &pasync_ctx->async_header.writables;
1083 num_entries = pasync_ctx->async_header.num_entries;
1084 } else {
1085 pep_read_ptr = &pasync_ctx->async_data.ep_read_ptr;
1086 pwritables = &pasync_ctx->async_data.writables;
1087 num_entries = pasync_ctx->async_data.num_entries;
1088 }
1089
1090 while ((*pep_read_ptr) != cq_index) {
1091 (*pep_read_ptr)++;
1092 *pep_read_ptr = (*pep_read_ptr) % num_entries;
1093
1094 pbusy_list = hwi_get_async_busy_list(pasync_ctx, is_header,
1095 *pep_read_ptr);
1096 if (writables == 0)
1097 WARN_ON(list_empty(pbusy_list));
1098
1099 if (!list_empty(pbusy_list)) {
1100 pasync_handle = list_entry(pbusy_list->next,
1101 struct async_pdu_handle,
1102 link);
1103 WARN_ON(!pasync_handle);
1104 pasync_handle->consumed = 1;
1105 }
1106
1107 writables++;
1108 }
1109
1110 if (!writables) {
1111 SE_DEBUG(DBG_LVL_1,
1112 "Duplicate notification received - index 0x%x!!\n",
1113 cq_index);
1114 WARN_ON(1);
1115 }
1116
1117 *pwritables = *pwritables + writables;
1118 return 0;
1119}
1120
1121static unsigned int hwi_free_async_msg(struct beiscsi_hba *phba,
1122 unsigned int cri)
1123{
1124 struct hwi_controller *phwi_ctrlr;
1125 struct hwi_async_pdu_context *pasync_ctx;
1126 struct async_pdu_handle *pasync_handle, *tmp_handle;
1127 struct list_head *plist;
1128 unsigned int i = 0;
1129
1130 phwi_ctrlr = phba->phwi_ctrlr;
1131 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1132
1133 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1134
1135 list_for_each_entry_safe(pasync_handle, tmp_handle, plist, link) {
1136 list_del(&pasync_handle->link);
1137
1138 if (i == 0) {
1139 list_add_tail(&pasync_handle->link,
1140 &pasync_ctx->async_header.free_list);
1141 pasync_ctx->async_header.free_entries++;
1142 i++;
1143 } else {
1144 list_add_tail(&pasync_handle->link,
1145 &pasync_ctx->async_data.free_list);
1146 pasync_ctx->async_data.free_entries++;
1147 i++;
1148 }
1149 }
1150
1151 INIT_LIST_HEAD(&pasync_ctx->async_entry[cri].wait_queue.list);
1152 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 0;
1153 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1154 return 0;
1155}
1156
1157static struct phys_addr *
1158hwi_get_ring_address(struct hwi_async_pdu_context *pasync_ctx,
1159 unsigned int is_header, unsigned int host_write_ptr)
1160{
1161 struct phys_addr *pasync_sge = NULL;
1162
1163 if (is_header)
1164 pasync_sge = pasync_ctx->async_header.ring_base;
1165 else
1166 pasync_sge = pasync_ctx->async_data.ring_base;
1167
1168 return pasync_sge + host_write_ptr;
1169}
1170
1171static void hwi_post_async_buffers(struct beiscsi_hba *phba,
1172 unsigned int is_header)
1173{
1174 struct hwi_controller *phwi_ctrlr;
1175 struct hwi_async_pdu_context *pasync_ctx;
1176 struct async_pdu_handle *pasync_handle;
1177 struct list_head *pfree_link, *pbusy_list;
1178 struct phys_addr *pasync_sge;
1179 unsigned int ring_id, num_entries;
1180 unsigned int host_write_num;
1181 unsigned int writables;
1182 unsigned int i = 0;
1183 u32 doorbell = 0;
1184
1185 phwi_ctrlr = phba->phwi_ctrlr;
1186 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1187
1188 if (is_header) {
1189 num_entries = pasync_ctx->async_header.num_entries;
1190 writables = min(pasync_ctx->async_header.writables,
1191 pasync_ctx->async_header.free_entries);
1192 pfree_link = pasync_ctx->async_header.free_list.next;
1193 host_write_num = pasync_ctx->async_header.host_write_ptr;
1194 ring_id = phwi_ctrlr->default_pdu_hdr.id;
1195 } else {
1196 num_entries = pasync_ctx->async_data.num_entries;
1197 writables = min(pasync_ctx->async_data.writables,
1198 pasync_ctx->async_data.free_entries);
1199 pfree_link = pasync_ctx->async_data.free_list.next;
1200 host_write_num = pasync_ctx->async_data.host_write_ptr;
1201 ring_id = phwi_ctrlr->default_pdu_data.id;
1202 }
1203
1204 writables = (writables / 8) * 8;
1205 if (writables) {
1206 for (i = 0; i < writables; i++) {
1207 pbusy_list =
1208 hwi_get_async_busy_list(pasync_ctx, is_header,
1209 host_write_num);
1210 pasync_handle =
1211 list_entry(pfree_link, struct async_pdu_handle,
1212 link);
1213 WARN_ON(!pasync_handle);
1214 pasync_handle->consumed = 0;
1215
1216 pfree_link = pfree_link->next;
1217
1218 pasync_sge = hwi_get_ring_address(pasync_ctx,
1219 is_header, host_write_num);
1220
1221 pasync_sge->hi = pasync_handle->pa.u.a32.address_lo;
1222 pasync_sge->lo = pasync_handle->pa.u.a32.address_hi;
1223
1224 list_move(&pasync_handle->link, pbusy_list);
1225
1226 host_write_num++;
1227 host_write_num = host_write_num % num_entries;
1228 }
1229
1230 if (is_header) {
1231 pasync_ctx->async_header.host_write_ptr =
1232 host_write_num;
1233 pasync_ctx->async_header.free_entries -= writables;
1234 pasync_ctx->async_header.writables -= writables;
1235 pasync_ctx->async_header.busy_entries += writables;
1236 } else {
1237 pasync_ctx->async_data.host_write_ptr = host_write_num;
1238 pasync_ctx->async_data.free_entries -= writables;
1239 pasync_ctx->async_data.writables -= writables;
1240 pasync_ctx->async_data.busy_entries += writables;
1241 }
1242
1243 doorbell |= ring_id & DB_DEF_PDU_RING_ID_MASK;
1244 doorbell |= 1 << DB_DEF_PDU_REARM_SHIFT;
1245 doorbell |= 0 << DB_DEF_PDU_EVENT_SHIFT;
1246 doorbell |= (writables & DB_DEF_PDU_CQPROC_MASK)
1247 << DB_DEF_PDU_CQPROC_SHIFT;
1248
1249 iowrite32(doorbell, phba->db_va + DB_RXULP0_OFFSET);
1250 }
1251}
1252
1253static void hwi_flush_default_pdu_buffer(struct beiscsi_hba *phba,
1254 struct beiscsi_conn *beiscsi_conn,
1255 struct i_t_dpdu_cqe *pdpdu_cqe)
1256{
1257 struct hwi_controller *phwi_ctrlr;
1258 struct hwi_async_pdu_context *pasync_ctx;
1259 struct async_pdu_handle *pasync_handle = NULL;
1260 unsigned int cq_index = -1;
1261
1262 phwi_ctrlr = phba->phwi_ctrlr;
1263 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1264
1265 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1266 pdpdu_cqe, &cq_index);
1267 BUG_ON(pasync_handle->is_header != 0);
1268 if (pasync_handle->consumed == 0)
1269 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1270 cq_index);
1271
1272 hwi_free_async_msg(phba, pasync_handle->cri);
1273 hwi_post_async_buffers(phba, pasync_handle->is_header);
1274}
1275
1276static unsigned int
1277hwi_fwd_async_msg(struct beiscsi_conn *beiscsi_conn,
1278 struct beiscsi_hba *phba,
1279 struct hwi_async_pdu_context *pasync_ctx, unsigned short cri)
1280{
1281 struct list_head *plist;
1282 struct async_pdu_handle *pasync_handle;
1283 void *phdr = NULL;
1284 unsigned int hdr_len = 0, buf_len = 0;
1285 unsigned int status, index = 0, offset = 0;
1286 void *pfirst_buffer = NULL;
1287 unsigned int num_buf = 0;
1288
1289 plist = &pasync_ctx->async_entry[cri].wait_queue.list;
1290
1291 list_for_each_entry(pasync_handle, plist, link) {
1292 if (index == 0) {
1293 phdr = pasync_handle->pbuffer;
1294 hdr_len = pasync_handle->buffer_len;
1295 } else {
1296 buf_len = pasync_handle->buffer_len;
1297 if (!num_buf) {
1298 pfirst_buffer = pasync_handle->pbuffer;
1299 num_buf++;
1300 }
1301 memcpy(pfirst_buffer + offset,
1302 pasync_handle->pbuffer, buf_len);
1303 offset = buf_len;
1304 }
1305 index++;
1306 }
1307
1308 status = beiscsi_process_async_pdu(beiscsi_conn, phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301309 (beiscsi_conn->beiscsi_conn_cid -
1310 phba->fw_config.iscsi_cid_start),
1311 phdr, hdr_len, pfirst_buffer,
1312 buf_len);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301313
1314 if (status == 0)
1315 hwi_free_async_msg(phba, cri);
1316 return 0;
1317}
1318
1319static unsigned int
1320hwi_gather_async_pdu(struct beiscsi_conn *beiscsi_conn,
1321 struct beiscsi_hba *phba,
1322 struct async_pdu_handle *pasync_handle)
1323{
1324 struct hwi_async_pdu_context *pasync_ctx;
1325 struct hwi_controller *phwi_ctrlr;
1326 unsigned int bytes_needed = 0, status = 0;
1327 unsigned short cri = pasync_handle->cri;
1328 struct pdu_base *ppdu;
1329
1330 phwi_ctrlr = phba->phwi_ctrlr;
1331 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1332
1333 list_del(&pasync_handle->link);
1334 if (pasync_handle->is_header) {
1335 pasync_ctx->async_header.busy_entries--;
1336 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1337 hwi_free_async_msg(phba, cri);
1338 BUG();
1339 }
1340
1341 pasync_ctx->async_entry[cri].wait_queue.bytes_received = 0;
1342 pasync_ctx->async_entry[cri].wait_queue.hdr_received = 1;
1343 pasync_ctx->async_entry[cri].wait_queue.hdr_len =
1344 (unsigned short)pasync_handle->buffer_len;
1345 list_add_tail(&pasync_handle->link,
1346 &pasync_ctx->async_entry[cri].wait_queue.list);
1347
1348 ppdu = pasync_handle->pbuffer;
1349 bytes_needed = ((((ppdu->dw[offsetof(struct amap_pdu_base,
1350 data_len_hi) / 32] & PDUBASE_DATALENHI_MASK) << 8) &
1351 0xFFFF0000) | ((be16_to_cpu((ppdu->
1352 dw[offsetof(struct amap_pdu_base, data_len_lo) / 32]
1353 & PDUBASE_DATALENLO_MASK) >> 16)) & 0x0000FFFF));
1354
1355 if (status == 0) {
1356 pasync_ctx->async_entry[cri].wait_queue.bytes_needed =
1357 bytes_needed;
1358
1359 if (bytes_needed == 0)
1360 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1361 pasync_ctx, cri);
1362 }
1363 } else {
1364 pasync_ctx->async_data.busy_entries--;
1365 if (pasync_ctx->async_entry[cri].wait_queue.hdr_received) {
1366 list_add_tail(&pasync_handle->link,
1367 &pasync_ctx->async_entry[cri].wait_queue.
1368 list);
1369 pasync_ctx->async_entry[cri].wait_queue.
1370 bytes_received +=
1371 (unsigned short)pasync_handle->buffer_len;
1372
1373 if (pasync_ctx->async_entry[cri].wait_queue.
1374 bytes_received >=
1375 pasync_ctx->async_entry[cri].wait_queue.
1376 bytes_needed)
1377 status = hwi_fwd_async_msg(beiscsi_conn, phba,
1378 pasync_ctx, cri);
1379 }
1380 }
1381 return status;
1382}
1383
1384static void hwi_process_default_pdu_ring(struct beiscsi_conn *beiscsi_conn,
1385 struct beiscsi_hba *phba,
1386 struct i_t_dpdu_cqe *pdpdu_cqe)
1387{
1388 struct hwi_controller *phwi_ctrlr;
1389 struct hwi_async_pdu_context *pasync_ctx;
1390 struct async_pdu_handle *pasync_handle = NULL;
1391 unsigned int cq_index = -1;
1392
1393 phwi_ctrlr = phba->phwi_ctrlr;
1394 pasync_ctx = HWI_GET_ASYNC_PDU_CTX(phwi_ctrlr);
1395 pasync_handle = hwi_get_async_handle(phba, beiscsi_conn, pasync_ctx,
1396 pdpdu_cqe, &cq_index);
1397
1398 if (pasync_handle->consumed == 0)
1399 hwi_update_async_writables(pasync_ctx, pasync_handle->is_header,
1400 cq_index);
1401 hwi_gather_async_pdu(beiscsi_conn, phba, pasync_handle);
1402 hwi_post_async_buffers(phba, pasync_handle->is_header);
1403}
1404
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301405static void beiscsi_process_mcc_isr(struct beiscsi_hba *phba)
1406{
1407 struct be_queue_info *mcc_cq;
1408 struct be_mcc_compl *mcc_compl;
1409 unsigned int num_processed = 0;
1410
1411 mcc_cq = &phba->ctrl.mcc_obj.cq;
1412 mcc_compl = queue_tail_node(mcc_cq);
1413 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1414 while (mcc_compl->flags & CQE_FLAGS_VALID_MASK) {
1415
1416 if (num_processed >= 32) {
1417 hwi_ring_cq_db(phba, mcc_cq->id,
1418 num_processed, 0, 0);
1419 num_processed = 0;
1420 }
1421 if (mcc_compl->flags & CQE_FLAGS_ASYNC_MASK) {
1422 /* Interpret flags as an async trailer */
1423 if (is_link_state_evt(mcc_compl->flags))
1424 /* Interpret compl as a async link evt */
1425 beiscsi_async_link_state_process(phba,
1426 (struct be_async_event_link_state *) mcc_compl);
1427 else
1428 SE_DEBUG(DBG_LVL_1,
1429 " Unsupported Async Event, flags"
1430 " = 0x%08x \n", mcc_compl->flags);
1431 } else if (mcc_compl->flags & CQE_FLAGS_COMPLETED_MASK) {
1432 be_mcc_compl_process_isr(&phba->ctrl, mcc_compl);
1433 atomic_dec(&phba->ctrl.mcc_obj.q.used);
1434 }
1435
1436 mcc_compl->flags = 0;
1437 queue_tail_inc(mcc_cq);
1438 mcc_compl = queue_tail_node(mcc_cq);
1439 mcc_compl->flags = le32_to_cpu(mcc_compl->flags);
1440 num_processed++;
1441 }
1442
1443 if (num_processed > 0)
1444 hwi_ring_cq_db(phba, mcc_cq->id, num_processed, 1, 0);
1445
1446}
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301447
1448static unsigned int beiscsi_process_cq(struct be_eq_obj *pbe_eq)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301449{
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301450 struct be_queue_info *cq;
1451 struct sol_cqe *sol;
1452 struct dmsg_cqe *dmsg;
1453 unsigned int num_processed = 0;
1454 unsigned int tot_nump = 0;
1455 struct beiscsi_conn *beiscsi_conn;
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301456 struct beiscsi_endpoint *beiscsi_ep;
1457 struct iscsi_endpoint *ep;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301458 struct beiscsi_hba *phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301459
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301460 cq = pbe_eq->cq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301461 sol = queue_tail_node(cq);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301462 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301463
1464 while (sol->dw[offsetof(struct amap_sol_cqe, valid) / 32] &
1465 CQE_VALID_MASK) {
1466 be_dws_le_to_cpu(sol, sizeof(struct sol_cqe));
1467
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301468 ep = phba->ep_array[(u32) ((sol->
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301469 dw[offsetof(struct amap_sol_cqe, cid) / 32] &
1470 SOL_CID_MASK) >> 6) -
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301471 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301472
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05301473 beiscsi_ep = ep->dd_data;
1474 beiscsi_conn = beiscsi_ep->conn;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301475
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301476 if (num_processed >= 32) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301477 hwi_ring_cq_db(phba, cq->id,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301478 num_processed, 0, 0);
1479 tot_nump += num_processed;
1480 num_processed = 0;
1481 }
1482
1483 switch ((u32) sol->dw[offsetof(struct amap_sol_cqe, code) /
1484 32] & CQE_CODE_MASK) {
1485 case SOL_CMD_COMPLETE:
1486 hwi_complete_cmd(beiscsi_conn, phba, sol);
1487 break;
1488 case DRIVERMSG_NOTIFY:
1489 SE_DEBUG(DBG_LVL_8, "Received DRIVERMSG_NOTIFY \n");
1490 dmsg = (struct dmsg_cqe *)sol;
1491 hwi_complete_drvr_msgs(beiscsi_conn, phba, sol);
1492 break;
1493 case UNSOL_HDR_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301494 SE_DEBUG(DBG_LVL_8, "Received UNSOL_HDR_ NOTIFY\n");
1495 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1496 (struct i_t_dpdu_cqe *)sol);
1497 break;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301498 case UNSOL_DATA_NOTIFY:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301499 SE_DEBUG(DBG_LVL_8, "Received UNSOL_DATA_NOTIFY\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301500 hwi_process_default_pdu_ring(beiscsi_conn, phba,
1501 (struct i_t_dpdu_cqe *)sol);
1502 break;
1503 case CXN_INVALIDATE_INDEX_NOTIFY:
1504 case CMD_INVALIDATED_NOTIFY:
1505 case CXN_INVALIDATE_NOTIFY:
1506 SE_DEBUG(DBG_LVL_1,
1507 "Ignoring CQ Error notification for cmd/cxn"
1508 "invalidate\n");
1509 break;
1510 case SOL_CMD_KILLED_DATA_DIGEST_ERR:
1511 case CMD_KILLED_INVALID_STATSN_RCVD:
1512 case CMD_KILLED_INVALID_R2T_RCVD:
1513 case CMD_CXN_KILLED_LUN_INVALID:
1514 case CMD_CXN_KILLED_ICD_INVALID:
1515 case CMD_CXN_KILLED_ITT_INVALID:
1516 case CMD_CXN_KILLED_SEQ_OUTOFORDER:
1517 case CMD_CXN_KILLED_INVALID_DATASN_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301518 SE_DEBUG(DBG_LVL_1,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301519 "CQ Error notification for cmd.. "
1520 "code %d cid 0x%x\n",
1521 sol->dw[offsetof(struct amap_sol_cqe, code) /
1522 32] & CQE_CODE_MASK,
1523 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1524 32] & SOL_CID_MASK));
1525 break;
1526 case UNSOL_DATA_DIGEST_ERROR_NOTIFY:
1527 SE_DEBUG(DBG_LVL_1,
1528 "Digest error on def pdu ring, dropping..\n");
1529 hwi_flush_default_pdu_buffer(phba, beiscsi_conn,
1530 (struct i_t_dpdu_cqe *) sol);
1531 break;
1532 case CXN_KILLED_PDU_SIZE_EXCEEDS_DSL:
1533 case CXN_KILLED_BURST_LEN_MISMATCH:
1534 case CXN_KILLED_AHS_RCVD:
1535 case CXN_KILLED_HDR_DIGEST_ERR:
1536 case CXN_KILLED_UNKNOWN_HDR:
1537 case CXN_KILLED_STALE_ITT_TTT_RCVD:
1538 case CXN_KILLED_INVALID_ITT_TTT_RCVD:
1539 case CXN_KILLED_TIMED_OUT:
1540 case CXN_KILLED_FIN_RCVD:
1541 case CXN_KILLED_BAD_UNSOL_PDU_RCVD:
1542 case CXN_KILLED_BAD_WRB_INDEX_ERROR:
1543 case CXN_KILLED_OVER_RUN_RESIDUAL:
1544 case CXN_KILLED_UNDER_RUN_RESIDUAL:
1545 case CXN_KILLED_CMND_DATA_NOT_ON_SAME_CONN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301546 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset CID "
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301547 "0x%x...\n",
1548 sol->dw[offsetof(struct amap_sol_cqe, code) /
1549 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301550 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1551 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301552 iscsi_conn_failure(beiscsi_conn->conn,
1553 ISCSI_ERR_CONN_FAILED);
1554 break;
1555 case CXN_KILLED_RST_SENT:
1556 case CXN_KILLED_RST_RCVD:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05301557 SE_DEBUG(DBG_LVL_1, "CQ Error %d, reset"
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301558 "received/sent on CID 0x%x...\n",
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301559 sol->dw[offsetof(struct amap_sol_cqe, code) /
1560 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301561 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1562 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301563 iscsi_conn_failure(beiscsi_conn->conn,
1564 ISCSI_ERR_CONN_FAILED);
1565 break;
1566 default:
1567 SE_DEBUG(DBG_LVL_1, "CQ Error Invalid code= %d "
1568 "received on CID 0x%x...\n",
1569 sol->dw[offsetof(struct amap_sol_cqe, code) /
1570 32] & CQE_CODE_MASK,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05301571 (sol->dw[offsetof(struct amap_sol_cqe, cid) /
1572 32] & CQE_CID_MASK));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301573 break;
1574 }
1575
1576 AMAP_SET_BITS(struct amap_sol_cqe, valid, sol, 0);
1577 queue_tail_inc(cq);
1578 sol = queue_tail_node(cq);
1579 num_processed++;
1580 }
1581
1582 if (num_processed > 0) {
1583 tot_nump += num_processed;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301584 hwi_ring_cq_db(phba, cq->id, num_processed, 1, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301585 }
1586 return tot_nump;
1587}
1588
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301589void beiscsi_process_all_cqs(struct work_struct *work)
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301590{
1591 unsigned long flags;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301592 struct hwi_controller *phwi_ctrlr;
1593 struct hwi_context_memory *phwi_context;
1594 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301595 struct beiscsi_hba *phba =
1596 container_of(work, struct beiscsi_hba, work_cqs);
1597
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301598 phwi_ctrlr = phba->phwi_ctrlr;
1599 phwi_context = phwi_ctrlr->phwi_ctxt;
1600 if (phba->msix_enabled)
1601 pbe_eq = &phwi_context->be_eq[phba->num_cpus];
1602 else
1603 pbe_eq = &phwi_context->be_eq[0];
1604
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301605 if (phba->todo_mcc_cq) {
1606 spin_lock_irqsave(&phba->isr_lock, flags);
1607 phba->todo_mcc_cq = 0;
1608 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05301609 beiscsi_process_mcc_isr(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301610 }
1611
1612 if (phba->todo_cq) {
1613 spin_lock_irqsave(&phba->isr_lock, flags);
1614 phba->todo_cq = 0;
1615 spin_unlock_irqrestore(&phba->isr_lock, flags);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301616 beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301617 }
1618}
1619
1620static int be_iopoll(struct blk_iopoll *iop, int budget)
1621{
1622 static unsigned int ret;
1623 struct beiscsi_hba *phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301624 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301625
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301626 pbe_eq = container_of(iop, struct be_eq_obj, iopoll);
1627 ret = beiscsi_process_cq(pbe_eq);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301628 if (ret < budget) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301629 phba = pbe_eq->phba;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301630 blk_iopoll_complete(iop);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301631 SE_DEBUG(DBG_LVL_8, "rearm pbe_eq->q.id =%d\n", pbe_eq->q.id);
1632 hwi_ring_eq_db(phba, pbe_eq->q.id, 0, 0, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301633 }
1634 return ret;
1635}
1636
1637static void
1638hwi_write_sgl(struct iscsi_wrb *pwrb, struct scatterlist *sg,
1639 unsigned int num_sg, struct beiscsi_io_task *io_task)
1640{
1641 struct iscsi_sge *psgl;
1642 unsigned short sg_len, index;
1643 unsigned int sge_len = 0;
1644 unsigned long long addr;
1645 struct scatterlist *l_sg;
1646 unsigned int offset;
1647
1648 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
1649 io_task->bhs_pa.u.a32.address_lo);
1650 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
1651 io_task->bhs_pa.u.a32.address_hi);
1652
1653 l_sg = sg;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05301654 for (index = 0; (index < num_sg) && (index < 2); index++,
1655 sg = sg_next(sg)) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301656 if (index == 0) {
1657 sg_len = sg_dma_len(sg);
1658 addr = (u64) sg_dma_address(sg);
1659 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
1660 (addr & 0xFFFFFFFF));
1661 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
1662 (addr >> 32));
1663 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
1664 sg_len);
1665 sge_len = sg_len;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301666 } else {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301667 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_r2t_offset,
1668 pwrb, sge_len);
1669 sg_len = sg_dma_len(sg);
1670 addr = (u64) sg_dma_address(sg);
1671 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_lo, pwrb,
1672 (addr & 0xFFFFFFFF));
1673 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_addr_hi, pwrb,
1674 (addr >> 32));
1675 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_len, pwrb,
1676 sg_len);
1677 }
1678 }
1679 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
1680 memset(psgl, 0, sizeof(*psgl) * BE2_SGE);
1681
1682 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len - 2);
1683
1684 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
1685 io_task->bhs_pa.u.a32.address_hi);
1686 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
1687 io_task->bhs_pa.u.a32.address_lo);
1688
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05301689 if (num_sg == 1) {
1690 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
1691 1);
1692 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
1693 0);
1694 } else if (num_sg == 2) {
1695 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
1696 0);
1697 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
1698 1);
1699 } else {
1700 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb,
1701 0);
1702 AMAP_SET_BITS(struct amap_iscsi_wrb, sge1_last, pwrb,
1703 0);
1704 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301705 sg = l_sg;
1706 psgl++;
1707 psgl++;
1708 offset = 0;
Jayamohan Kallickal48bd86c2010-01-07 01:50:19 +05301709 for (index = 0; index < num_sg; index++, sg = sg_next(sg), psgl++) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301710 sg_len = sg_dma_len(sg);
1711 addr = (u64) sg_dma_address(sg);
1712 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
1713 (addr & 0xFFFFFFFF));
1714 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
1715 (addr >> 32));
1716 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, sg_len);
1717 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, offset);
1718 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
1719 offset += sg_len;
1720 }
1721 psgl--;
1722 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
1723}
1724
1725static void hwi_write_buffer(struct iscsi_wrb *pwrb, struct iscsi_task *task)
1726{
1727 struct iscsi_sge *psgl;
1728 unsigned long long addr;
1729 struct beiscsi_io_task *io_task = task->dd_data;
1730 struct beiscsi_conn *beiscsi_conn = io_task->conn;
1731 struct beiscsi_hba *phba = beiscsi_conn->phba;
1732
1733 io_task->bhs_len = sizeof(struct be_nonio_bhs) - 2;
1734 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_lo, pwrb,
1735 io_task->bhs_pa.u.a32.address_lo);
1736 AMAP_SET_BITS(struct amap_iscsi_wrb, iscsi_bhs_addr_hi, pwrb,
1737 io_task->bhs_pa.u.a32.address_hi);
1738
1739 if (task->data) {
1740 if (task->data_count) {
1741 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
1742 addr = (u64) pci_map_single(phba->pcidev,
1743 task->data,
1744 task->data_count, 1);
1745 } else {
1746 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
1747 addr = 0;
1748 }
1749 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_lo, pwrb,
1750 (addr & 0xFFFFFFFF));
1751 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_addr_hi, pwrb,
1752 (addr >> 32));
1753 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_len, pwrb,
1754 task->data_count);
1755
1756 AMAP_SET_BITS(struct amap_iscsi_wrb, sge0_last, pwrb, 1);
1757 } else {
1758 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
1759 addr = 0;
1760 }
1761
1762 psgl = (struct iscsi_sge *)io_task->psgl_handle->pfrag;
1763
1764 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, io_task->bhs_len);
1765
1766 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
1767 io_task->bhs_pa.u.a32.address_hi);
1768 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
1769 io_task->bhs_pa.u.a32.address_lo);
1770 if (task->data) {
1771 psgl++;
1772 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl, 0);
1773 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl, 0);
1774 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0);
1775 AMAP_SET_BITS(struct amap_iscsi_sge, sge_offset, psgl, 0);
1776 AMAP_SET_BITS(struct amap_iscsi_sge, rsvd0, psgl, 0);
1777 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 0);
1778
1779 psgl++;
1780 if (task->data) {
1781 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, psgl,
1782 (addr & 0xFFFFFFFF));
1783 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, psgl,
1784 (addr >> 32));
1785 }
1786 AMAP_SET_BITS(struct amap_iscsi_sge, len, psgl, 0x106);
1787 }
1788 AMAP_SET_BITS(struct amap_iscsi_sge, last_sge, psgl, 1);
1789}
1790
1791static void beiscsi_find_mem_req(struct beiscsi_hba *phba)
1792{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05301793 unsigned int num_cq_pages, num_async_pdu_buf_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301794 unsigned int num_async_pdu_data_pages, wrb_sz_per_cxn;
1795 unsigned int num_async_pdu_buf_sgl_pages, num_async_pdu_data_sgl_pages;
1796
1797 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
1798 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301799 num_async_pdu_buf_pages =
1800 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
1801 phba->params.defpdu_hdr_sz);
1802 num_async_pdu_buf_sgl_pages =
1803 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
1804 sizeof(struct phys_addr));
1805 num_async_pdu_data_pages =
1806 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
1807 phba->params.defpdu_data_sz);
1808 num_async_pdu_data_sgl_pages =
1809 PAGES_REQUIRED(phba->params.asyncpdus_per_ctrl * \
1810 sizeof(struct phys_addr));
1811
1812 phba->params.hwi_ws_sz = sizeof(struct hwi_controller);
1813
1814 phba->mem_req[ISCSI_MEM_GLOBAL_HEADER] = 2 *
1815 BE_ISCSI_PDU_HEADER_SIZE;
1816 phba->mem_req[HWI_MEM_ADDN_CONTEXT] =
1817 sizeof(struct hwi_context_memory);
1818
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05301819
1820 phba->mem_req[HWI_MEM_WRB] = sizeof(struct iscsi_wrb)
1821 * (phba->params.wrbs_per_cxn)
1822 * phba->params.cxns_per_ctrl;
1823 wrb_sz_per_cxn = sizeof(struct wrb_handle) *
1824 (phba->params.wrbs_per_cxn);
1825 phba->mem_req[HWI_MEM_WRBH] = roundup_pow_of_two((wrb_sz_per_cxn) *
1826 phba->params.cxns_per_ctrl);
1827
1828 phba->mem_req[HWI_MEM_SGLH] = sizeof(struct sgl_handle) *
1829 phba->params.icds_per_ctrl;
1830 phba->mem_req[HWI_MEM_SGE] = sizeof(struct iscsi_sge) *
1831 phba->params.num_sge_per_io * phba->params.icds_per_ctrl;
1832
1833 phba->mem_req[HWI_MEM_ASYNC_HEADER_BUF] =
1834 num_async_pdu_buf_pages * PAGE_SIZE;
1835 phba->mem_req[HWI_MEM_ASYNC_DATA_BUF] =
1836 num_async_pdu_data_pages * PAGE_SIZE;
1837 phba->mem_req[HWI_MEM_ASYNC_HEADER_RING] =
1838 num_async_pdu_buf_sgl_pages * PAGE_SIZE;
1839 phba->mem_req[HWI_MEM_ASYNC_DATA_RING] =
1840 num_async_pdu_data_sgl_pages * PAGE_SIZE;
1841 phba->mem_req[HWI_MEM_ASYNC_HEADER_HANDLE] =
1842 phba->params.asyncpdus_per_ctrl *
1843 sizeof(struct async_pdu_handle);
1844 phba->mem_req[HWI_MEM_ASYNC_DATA_HANDLE] =
1845 phba->params.asyncpdus_per_ctrl *
1846 sizeof(struct async_pdu_handle);
1847 phba->mem_req[HWI_MEM_ASYNC_PDU_CONTEXT] =
1848 sizeof(struct hwi_async_pdu_context) +
1849 (phba->params.cxns_per_ctrl * sizeof(struct hwi_async_entry));
1850}
1851
1852static int beiscsi_alloc_mem(struct beiscsi_hba *phba)
1853{
1854 struct be_mem_descriptor *mem_descr;
1855 dma_addr_t bus_add;
1856 struct mem_array *mem_arr, *mem_arr_orig;
1857 unsigned int i, j, alloc_size, curr_alloc_size;
1858
1859 phba->phwi_ctrlr = kmalloc(phba->params.hwi_ws_sz, GFP_KERNEL);
1860 if (!phba->phwi_ctrlr)
1861 return -ENOMEM;
1862
1863 phba->init_mem = kcalloc(SE_MEM_MAX, sizeof(*mem_descr),
1864 GFP_KERNEL);
1865 if (!phba->init_mem) {
1866 kfree(phba->phwi_ctrlr);
1867 return -ENOMEM;
1868 }
1869
1870 mem_arr_orig = kmalloc(sizeof(*mem_arr_orig) * BEISCSI_MAX_FRAGS_INIT,
1871 GFP_KERNEL);
1872 if (!mem_arr_orig) {
1873 kfree(phba->init_mem);
1874 kfree(phba->phwi_ctrlr);
1875 return -ENOMEM;
1876 }
1877
1878 mem_descr = phba->init_mem;
1879 for (i = 0; i < SE_MEM_MAX; i++) {
1880 j = 0;
1881 mem_arr = mem_arr_orig;
1882 alloc_size = phba->mem_req[i];
1883 memset(mem_arr, 0, sizeof(struct mem_array) *
1884 BEISCSI_MAX_FRAGS_INIT);
1885 curr_alloc_size = min(be_max_phys_size * 1024, alloc_size);
1886 do {
1887 mem_arr->virtual_address = pci_alloc_consistent(
1888 phba->pcidev,
1889 curr_alloc_size,
1890 &bus_add);
1891 if (!mem_arr->virtual_address) {
1892 if (curr_alloc_size <= BE_MIN_MEM_SIZE)
1893 goto free_mem;
1894 if (curr_alloc_size -
1895 rounddown_pow_of_two(curr_alloc_size))
1896 curr_alloc_size = rounddown_pow_of_two
1897 (curr_alloc_size);
1898 else
1899 curr_alloc_size = curr_alloc_size / 2;
1900 } else {
1901 mem_arr->bus_address.u.
1902 a64.address = (__u64) bus_add;
1903 mem_arr->size = curr_alloc_size;
1904 alloc_size -= curr_alloc_size;
1905 curr_alloc_size = min(be_max_phys_size *
1906 1024, alloc_size);
1907 j++;
1908 mem_arr++;
1909 }
1910 } while (alloc_size);
1911 mem_descr->num_elements = j;
1912 mem_descr->size_in_bytes = phba->mem_req[i];
1913 mem_descr->mem_array = kmalloc(sizeof(*mem_arr) * j,
1914 GFP_KERNEL);
1915 if (!mem_descr->mem_array)
1916 goto free_mem;
1917
1918 memcpy(mem_descr->mem_array, mem_arr_orig,
1919 sizeof(struct mem_array) * j);
1920 mem_descr++;
1921 }
1922 kfree(mem_arr_orig);
1923 return 0;
1924free_mem:
1925 mem_descr->num_elements = j;
1926 while ((i) || (j)) {
1927 for (j = mem_descr->num_elements; j > 0; j--) {
1928 pci_free_consistent(phba->pcidev,
1929 mem_descr->mem_array[j - 1].size,
1930 mem_descr->mem_array[j - 1].
1931 virtual_address,
1932 mem_descr->mem_array[j - 1].
1933 bus_address.u.a64.address);
1934 }
1935 if (i) {
1936 i--;
1937 kfree(mem_descr->mem_array);
1938 mem_descr--;
1939 }
1940 }
1941 kfree(mem_arr_orig);
1942 kfree(phba->init_mem);
1943 kfree(phba->phwi_ctrlr);
1944 return -ENOMEM;
1945}
1946
1947static int beiscsi_get_memory(struct beiscsi_hba *phba)
1948{
1949 beiscsi_find_mem_req(phba);
1950 return beiscsi_alloc_mem(phba);
1951}
1952
1953static void iscsi_init_global_templates(struct beiscsi_hba *phba)
1954{
1955 struct pdu_data_out *pdata_out;
1956 struct pdu_nop_out *pnop_out;
1957 struct be_mem_descriptor *mem_descr;
1958
1959 mem_descr = phba->init_mem;
1960 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
1961 pdata_out =
1962 (struct pdu_data_out *)mem_descr->mem_array[0].virtual_address;
1963 memset(pdata_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
1964
1965 AMAP_SET_BITS(struct amap_pdu_data_out, opcode, pdata_out,
1966 IIOC_SCSI_DATA);
1967
1968 pnop_out =
1969 (struct pdu_nop_out *)((unsigned char *)mem_descr->mem_array[0].
1970 virtual_address + BE_ISCSI_PDU_HEADER_SIZE);
1971
1972 memset(pnop_out, 0, BE_ISCSI_PDU_HEADER_SIZE);
1973 AMAP_SET_BITS(struct amap_pdu_nop_out, ttt, pnop_out, 0xFFFFFFFF);
1974 AMAP_SET_BITS(struct amap_pdu_nop_out, f_bit, pnop_out, 1);
1975 AMAP_SET_BITS(struct amap_pdu_nop_out, i_bit, pnop_out, 0);
1976}
1977
1978static void beiscsi_init_wrb_handle(struct beiscsi_hba *phba)
1979{
1980 struct be_mem_descriptor *mem_descr_wrbh, *mem_descr_wrb;
1981 struct wrb_handle *pwrb_handle;
1982 struct hwi_controller *phwi_ctrlr;
1983 struct hwi_wrb_context *pwrb_context;
1984 struct iscsi_wrb *pwrb;
1985 unsigned int num_cxn_wrbh;
1986 unsigned int num_cxn_wrb, j, idx, index;
1987
1988 mem_descr_wrbh = phba->init_mem;
1989 mem_descr_wrbh += HWI_MEM_WRBH;
1990
1991 mem_descr_wrb = phba->init_mem;
1992 mem_descr_wrb += HWI_MEM_WRB;
1993
1994 idx = 0;
1995 pwrb_handle = mem_descr_wrbh->mem_array[idx].virtual_address;
1996 num_cxn_wrbh = ((mem_descr_wrbh->mem_array[idx].size) /
1997 ((sizeof(struct wrb_handle)) *
1998 phba->params.wrbs_per_cxn));
1999 phwi_ctrlr = phba->phwi_ctrlr;
2000
2001 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2002 pwrb_context = &phwi_ctrlr->wrb_context[index];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302003 pwrb_context->pwrb_handle_base =
2004 kzalloc(sizeof(struct wrb_handle *) *
2005 phba->params.wrbs_per_cxn, GFP_KERNEL);
2006 pwrb_context->pwrb_handle_basestd =
2007 kzalloc(sizeof(struct wrb_handle *) *
2008 phba->params.wrbs_per_cxn, GFP_KERNEL);
2009 if (num_cxn_wrbh) {
2010 pwrb_context->alloc_index = 0;
2011 pwrb_context->wrb_handles_available = 0;
2012 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2013 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2014 pwrb_context->pwrb_handle_basestd[j] =
2015 pwrb_handle;
2016 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302017 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302018 pwrb_handle++;
2019 }
2020 pwrb_context->free_index = 0;
2021 num_cxn_wrbh--;
2022 } else {
2023 idx++;
2024 pwrb_handle =
2025 mem_descr_wrbh->mem_array[idx].virtual_address;
2026 num_cxn_wrbh =
2027 ((mem_descr_wrbh->mem_array[idx].size) /
2028 ((sizeof(struct wrb_handle)) *
2029 phba->params.wrbs_per_cxn));
2030 pwrb_context->alloc_index = 0;
2031 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2032 pwrb_context->pwrb_handle_base[j] = pwrb_handle;
2033 pwrb_context->pwrb_handle_basestd[j] =
2034 pwrb_handle;
2035 pwrb_context->wrb_handles_available++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302036 pwrb_handle->wrb_index = j;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302037 pwrb_handle++;
2038 }
2039 pwrb_context->free_index = 0;
2040 num_cxn_wrbh--;
2041 }
2042 }
2043 idx = 0;
2044 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2045 num_cxn_wrb =
2046 ((mem_descr_wrb->mem_array[idx].size) / (sizeof(struct iscsi_wrb)) *
2047 phba->params.wrbs_per_cxn);
2048
2049 for (index = 0; index < phba->params.cxns_per_ctrl; index += 2) {
2050 pwrb_context = &phwi_ctrlr->wrb_context[index];
2051 if (num_cxn_wrb) {
2052 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2053 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2054 pwrb_handle->pwrb = pwrb;
2055 pwrb++;
2056 }
2057 num_cxn_wrb--;
2058 } else {
2059 idx++;
2060 pwrb = mem_descr_wrb->mem_array[idx].virtual_address;
2061 num_cxn_wrb = ((mem_descr_wrb->mem_array[idx].size) /
2062 (sizeof(struct iscsi_wrb)) *
2063 phba->params.wrbs_per_cxn);
2064 for (j = 0; j < phba->params.wrbs_per_cxn; j++) {
2065 pwrb_handle = pwrb_context->pwrb_handle_base[j];
2066 pwrb_handle->pwrb = pwrb;
2067 pwrb++;
2068 }
2069 num_cxn_wrb--;
2070 }
2071 }
2072}
2073
2074static void hwi_init_async_pdu_ctx(struct beiscsi_hba *phba)
2075{
2076 struct hwi_controller *phwi_ctrlr;
2077 struct hba_parameters *p = &phba->params;
2078 struct hwi_async_pdu_context *pasync_ctx;
2079 struct async_pdu_handle *pasync_header_h, *pasync_data_h;
2080 unsigned int index;
2081 struct be_mem_descriptor *mem_descr;
2082
2083 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2084 mem_descr += HWI_MEM_ASYNC_PDU_CONTEXT;
2085
2086 phwi_ctrlr = phba->phwi_ctrlr;
2087 phwi_ctrlr->phwi_ctxt->pasync_ctx = (struct hwi_async_pdu_context *)
2088 mem_descr->mem_array[0].virtual_address;
2089 pasync_ctx = phwi_ctrlr->phwi_ctxt->pasync_ctx;
2090 memset(pasync_ctx, 0, sizeof(*pasync_ctx));
2091
2092 pasync_ctx->async_header.num_entries = p->asyncpdus_per_ctrl;
2093 pasync_ctx->async_header.buffer_size = p->defpdu_hdr_sz;
2094 pasync_ctx->async_data.buffer_size = p->defpdu_data_sz;
2095 pasync_ctx->async_data.num_entries = p->asyncpdus_per_ctrl;
2096
2097 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2098 mem_descr += HWI_MEM_ASYNC_HEADER_BUF;
2099 if (mem_descr->mem_array[0].virtual_address) {
2100 SE_DEBUG(DBG_LVL_8,
2101 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_BUF"
2102 "va=%p \n", mem_descr->mem_array[0].virtual_address);
2103 } else
2104 shost_printk(KERN_WARNING, phba->shost,
2105 "No Virtual address \n");
2106
2107 pasync_ctx->async_header.va_base =
2108 mem_descr->mem_array[0].virtual_address;
2109
2110 pasync_ctx->async_header.pa_base.u.a64.address =
2111 mem_descr->mem_array[0].bus_address.u.a64.address;
2112
2113 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2114 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2115 if (mem_descr->mem_array[0].virtual_address) {
2116 SE_DEBUG(DBG_LVL_8,
2117 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_RING"
2118 "va=%p \n", mem_descr->mem_array[0].virtual_address);
2119 } else
2120 shost_printk(KERN_WARNING, phba->shost,
2121 "No Virtual address \n");
2122 pasync_ctx->async_header.ring_base =
2123 mem_descr->mem_array[0].virtual_address;
2124
2125 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2126 mem_descr += HWI_MEM_ASYNC_HEADER_HANDLE;
2127 if (mem_descr->mem_array[0].virtual_address) {
2128 SE_DEBUG(DBG_LVL_8,
2129 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_HEADER_HANDLE"
2130 "va=%p \n", mem_descr->mem_array[0].virtual_address);
2131 } else
2132 shost_printk(KERN_WARNING, phba->shost,
2133 "No Virtual address \n");
2134
2135 pasync_ctx->async_header.handle_base =
2136 mem_descr->mem_array[0].virtual_address;
2137 pasync_ctx->async_header.writables = 0;
2138 INIT_LIST_HEAD(&pasync_ctx->async_header.free_list);
2139
2140 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2141 mem_descr += HWI_MEM_ASYNC_DATA_BUF;
2142 if (mem_descr->mem_array[0].virtual_address) {
2143 SE_DEBUG(DBG_LVL_8,
2144 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_BUF"
2145 "va=%p \n", mem_descr->mem_array[0].virtual_address);
2146 } else
2147 shost_printk(KERN_WARNING, phba->shost,
2148 "No Virtual address \n");
2149 pasync_ctx->async_data.va_base =
2150 mem_descr->mem_array[0].virtual_address;
2151 pasync_ctx->async_data.pa_base.u.a64.address =
2152 mem_descr->mem_array[0].bus_address.u.a64.address;
2153
2154 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2155 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2156 if (mem_descr->mem_array[0].virtual_address) {
2157 SE_DEBUG(DBG_LVL_8,
2158 "hwi_init_async_pdu_ctx HWI_MEM_ASYNC_DATA_RING"
2159 "va=%p \n", mem_descr->mem_array[0].virtual_address);
2160 } else
2161 shost_printk(KERN_WARNING, phba->shost,
2162 "No Virtual address \n");
2163
2164 pasync_ctx->async_data.ring_base =
2165 mem_descr->mem_array[0].virtual_address;
2166
2167 mem_descr = (struct be_mem_descriptor *)phba->init_mem;
2168 mem_descr += HWI_MEM_ASYNC_DATA_HANDLE;
2169 if (!mem_descr->mem_array[0].virtual_address)
2170 shost_printk(KERN_WARNING, phba->shost,
2171 "No Virtual address \n");
2172
2173 pasync_ctx->async_data.handle_base =
2174 mem_descr->mem_array[0].virtual_address;
2175 pasync_ctx->async_data.writables = 0;
2176 INIT_LIST_HEAD(&pasync_ctx->async_data.free_list);
2177
2178 pasync_header_h =
2179 (struct async_pdu_handle *)pasync_ctx->async_header.handle_base;
2180 pasync_data_h =
2181 (struct async_pdu_handle *)pasync_ctx->async_data.handle_base;
2182
2183 for (index = 0; index < p->asyncpdus_per_ctrl; index++) {
2184 pasync_header_h->cri = -1;
2185 pasync_header_h->index = (char)index;
2186 INIT_LIST_HEAD(&pasync_header_h->link);
2187 pasync_header_h->pbuffer =
2188 (void *)((unsigned long)
2189 (pasync_ctx->async_header.va_base) +
2190 (p->defpdu_hdr_sz * index));
2191
2192 pasync_header_h->pa.u.a64.address =
2193 pasync_ctx->async_header.pa_base.u.a64.address +
2194 (p->defpdu_hdr_sz * index);
2195
2196 list_add_tail(&pasync_header_h->link,
2197 &pasync_ctx->async_header.free_list);
2198 pasync_header_h++;
2199 pasync_ctx->async_header.free_entries++;
2200 pasync_ctx->async_header.writables++;
2201
2202 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].wait_queue.list);
2203 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].
2204 header_busy_list);
2205 pasync_data_h->cri = -1;
2206 pasync_data_h->index = (char)index;
2207 INIT_LIST_HEAD(&pasync_data_h->link);
2208 pasync_data_h->pbuffer =
2209 (void *)((unsigned long)
2210 (pasync_ctx->async_data.va_base) +
2211 (p->defpdu_data_sz * index));
2212
2213 pasync_data_h->pa.u.a64.address =
2214 pasync_ctx->async_data.pa_base.u.a64.address +
2215 (p->defpdu_data_sz * index);
2216
2217 list_add_tail(&pasync_data_h->link,
2218 &pasync_ctx->async_data.free_list);
2219 pasync_data_h++;
2220 pasync_ctx->async_data.free_entries++;
2221 pasync_ctx->async_data.writables++;
2222
2223 INIT_LIST_HEAD(&pasync_ctx->async_entry[index].data_busy_list);
2224 }
2225
2226 pasync_ctx->async_header.host_write_ptr = 0;
2227 pasync_ctx->async_header.ep_read_ptr = -1;
2228 pasync_ctx->async_data.host_write_ptr = 0;
2229 pasync_ctx->async_data.ep_read_ptr = -1;
2230}
2231
2232static int
2233be_sgl_create_contiguous(void *virtual_address,
2234 u64 physical_address, u32 length,
2235 struct be_dma_mem *sgl)
2236{
2237 WARN_ON(!virtual_address);
2238 WARN_ON(!physical_address);
2239 WARN_ON(!length > 0);
2240 WARN_ON(!sgl);
2241
2242 sgl->va = virtual_address;
2243 sgl->dma = physical_address;
2244 sgl->size = length;
2245
2246 return 0;
2247}
2248
2249static void be_sgl_destroy_contiguous(struct be_dma_mem *sgl)
2250{
2251 memset(sgl, 0, sizeof(*sgl));
2252}
2253
2254static void
2255hwi_build_be_sgl_arr(struct beiscsi_hba *phba,
2256 struct mem_array *pmem, struct be_dma_mem *sgl)
2257{
2258 if (sgl->va)
2259 be_sgl_destroy_contiguous(sgl);
2260
2261 be_sgl_create_contiguous(pmem->virtual_address,
2262 pmem->bus_address.u.a64.address,
2263 pmem->size, sgl);
2264}
2265
2266static void
2267hwi_build_be_sgl_by_offset(struct beiscsi_hba *phba,
2268 struct mem_array *pmem, struct be_dma_mem *sgl)
2269{
2270 if (sgl->va)
2271 be_sgl_destroy_contiguous(sgl);
2272
2273 be_sgl_create_contiguous((unsigned char *)pmem->virtual_address,
2274 pmem->bus_address.u.a64.address,
2275 pmem->size, sgl);
2276}
2277
2278static int be_fill_queue(struct be_queue_info *q,
2279 u16 len, u16 entry_size, void *vaddress)
2280{
2281 struct be_dma_mem *mem = &q->dma_mem;
2282
2283 memset(q, 0, sizeof(*q));
2284 q->len = len;
2285 q->entry_size = entry_size;
2286 mem->size = len * entry_size;
2287 mem->va = vaddress;
2288 if (!mem->va)
2289 return -ENOMEM;
2290 memset(mem->va, 0, mem->size);
2291 return 0;
2292}
2293
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302294static int beiscsi_create_eqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302295 struct hwi_context_memory *phwi_context)
2296{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302297 unsigned int i, num_eq_pages;
2298 int ret, eq_for_mcc;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302299 struct be_queue_info *eq;
2300 struct be_dma_mem *mem;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302301 void *eq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302302 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302303
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302304 num_eq_pages = PAGES_REQUIRED(phba->params.num_eq_entries * \
2305 sizeof(struct be_eq_entry));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302306
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302307 if (phba->msix_enabled)
2308 eq_for_mcc = 1;
2309 else
2310 eq_for_mcc = 0;
2311 for (i = 0; i < (phba->num_cpus + eq_for_mcc); i++) {
2312 eq = &phwi_context->be_eq[i].q;
2313 mem = &eq->dma_mem;
2314 phwi_context->be_eq[i].phba = phba;
2315 eq_vaddress = pci_alloc_consistent(phba->pcidev,
2316 num_eq_pages * PAGE_SIZE,
2317 &paddr);
2318 if (!eq_vaddress)
2319 goto create_eq_error;
2320
2321 mem->va = eq_vaddress;
2322 ret = be_fill_queue(eq, phba->params.num_eq_entries,
2323 sizeof(struct be_eq_entry), eq_vaddress);
2324 if (ret) {
2325 shost_printk(KERN_ERR, phba->shost,
2326 "be_fill_queue Failed for EQ \n");
2327 goto create_eq_error;
2328 }
2329
2330 mem->dma = paddr;
2331 ret = beiscsi_cmd_eq_create(&phba->ctrl, eq,
2332 phwi_context->cur_eqd);
2333 if (ret) {
2334 shost_printk(KERN_ERR, phba->shost,
2335 "beiscsi_cmd_eq_create"
2336 "Failedfor EQ \n");
2337 goto create_eq_error;
2338 }
2339 SE_DEBUG(DBG_LVL_8, "eqid = %d\n", phwi_context->be_eq[i].q.id);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302340 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302341 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302342create_eq_error:
2343 for (i = 0; i < (phba->num_cpus + 1); i++) {
2344 eq = &phwi_context->be_eq[i].q;
2345 mem = &eq->dma_mem;
2346 if (mem->va)
2347 pci_free_consistent(phba->pcidev, num_eq_pages
2348 * PAGE_SIZE,
2349 mem->va, mem->dma);
2350 }
2351 return ret;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302352}
2353
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302354static int beiscsi_create_cqs(struct beiscsi_hba *phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302355 struct hwi_context_memory *phwi_context)
2356{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302357 unsigned int i, num_cq_pages;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302358 int ret;
2359 struct be_queue_info *cq, *eq;
2360 struct be_dma_mem *mem;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302361 struct be_eq_obj *pbe_eq;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302362 void *cq_vaddress;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302363 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302364
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302365 num_cq_pages = PAGES_REQUIRED(phba->params.num_cq_entries * \
2366 sizeof(struct sol_cqe));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302367
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302368 for (i = 0; i < phba->num_cpus; i++) {
2369 cq = &phwi_context->be_cq[i];
2370 eq = &phwi_context->be_eq[i].q;
2371 pbe_eq = &phwi_context->be_eq[i];
2372 pbe_eq->cq = cq;
2373 pbe_eq->phba = phba;
2374 mem = &cq->dma_mem;
2375 cq_vaddress = pci_alloc_consistent(phba->pcidev,
2376 num_cq_pages * PAGE_SIZE,
2377 &paddr);
2378 if (!cq_vaddress)
2379 goto create_cq_error;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302380 ret = be_fill_queue(cq, phba->params.num_cq_entries,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302381 sizeof(struct sol_cqe), cq_vaddress);
2382 if (ret) {
2383 shost_printk(KERN_ERR, phba->shost,
2384 "be_fill_queue Failed for ISCSI CQ \n");
2385 goto create_cq_error;
2386 }
2387
2388 mem->dma = paddr;
2389 ret = beiscsi_cmd_cq_create(&phba->ctrl, cq, eq, false,
2390 false, 0);
2391 if (ret) {
2392 shost_printk(KERN_ERR, phba->shost,
2393 "beiscsi_cmd_eq_create"
2394 "Failed for ISCSI CQ \n");
2395 goto create_cq_error;
2396 }
2397 SE_DEBUG(DBG_LVL_8, "iscsi cq_id is %d for eq_id %d\n",
2398 cq->id, eq->id);
2399 SE_DEBUG(DBG_LVL_8, "ISCSI CQ CREATED\n");
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302400 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302401 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302402
2403create_cq_error:
2404 for (i = 0; i < phba->num_cpus; i++) {
2405 cq = &phwi_context->be_cq[i];
2406 mem = &cq->dma_mem;
2407 if (mem->va)
2408 pci_free_consistent(phba->pcidev, num_cq_pages
2409 * PAGE_SIZE,
2410 mem->va, mem->dma);
2411 }
2412 return ret;
2413
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302414}
2415
2416static int
2417beiscsi_create_def_hdr(struct beiscsi_hba *phba,
2418 struct hwi_context_memory *phwi_context,
2419 struct hwi_controller *phwi_ctrlr,
2420 unsigned int def_pdu_ring_sz)
2421{
2422 unsigned int idx;
2423 int ret;
2424 struct be_queue_info *dq, *cq;
2425 struct be_dma_mem *mem;
2426 struct be_mem_descriptor *mem_descr;
2427 void *dq_vaddress;
2428
2429 idx = 0;
2430 dq = &phwi_context->be_def_hdrq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302431 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302432 mem = &dq->dma_mem;
2433 mem_descr = phba->init_mem;
2434 mem_descr += HWI_MEM_ASYNC_HEADER_RING;
2435 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2436 ret = be_fill_queue(dq, mem_descr->mem_array[0].size /
2437 sizeof(struct phys_addr),
2438 sizeof(struct phys_addr), dq_vaddress);
2439 if (ret) {
2440 shost_printk(KERN_ERR, phba->shost,
2441 "be_fill_queue Failed for DEF PDU HDR\n");
2442 return ret;
2443 }
2444 mem->dma = mem_descr->mem_array[idx].bus_address.u.a64.address;
2445 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dq,
2446 def_pdu_ring_sz,
2447 phba->params.defpdu_hdr_sz);
2448 if (ret) {
2449 shost_printk(KERN_ERR, phba->shost,
2450 "be_cmd_create_default_pdu_queue Failed DEFHDR\n");
2451 return ret;
2452 }
2453 phwi_ctrlr->default_pdu_hdr.id = phwi_context->be_def_hdrq.id;
2454 SE_DEBUG(DBG_LVL_8, "iscsi def pdu id is %d\n",
2455 phwi_context->be_def_hdrq.id);
2456 hwi_post_async_buffers(phba, 1);
2457 return 0;
2458}
2459
2460static int
2461beiscsi_create_def_data(struct beiscsi_hba *phba,
2462 struct hwi_context_memory *phwi_context,
2463 struct hwi_controller *phwi_ctrlr,
2464 unsigned int def_pdu_ring_sz)
2465{
2466 unsigned int idx;
2467 int ret;
2468 struct be_queue_info *dataq, *cq;
2469 struct be_dma_mem *mem;
2470 struct be_mem_descriptor *mem_descr;
2471 void *dq_vaddress;
2472
2473 idx = 0;
2474 dataq = &phwi_context->be_def_dataq;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302475 cq = &phwi_context->be_cq[0];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302476 mem = &dataq->dma_mem;
2477 mem_descr = phba->init_mem;
2478 mem_descr += HWI_MEM_ASYNC_DATA_RING;
2479 dq_vaddress = mem_descr->mem_array[idx].virtual_address;
2480 ret = be_fill_queue(dataq, mem_descr->mem_array[0].size /
2481 sizeof(struct phys_addr),
2482 sizeof(struct phys_addr), dq_vaddress);
2483 if (ret) {
2484 shost_printk(KERN_ERR, phba->shost,
2485 "be_fill_queue Failed for DEF PDU DATA\n");
2486 return ret;
2487 }
2488 mem->dma = mem_descr->mem_array[idx].bus_address.u.a64.address;
2489 ret = be_cmd_create_default_pdu_queue(&phba->ctrl, cq, dataq,
2490 def_pdu_ring_sz,
2491 phba->params.defpdu_data_sz);
2492 if (ret) {
2493 shost_printk(KERN_ERR, phba->shost,
2494 "be_cmd_create_default_pdu_queue Failed"
2495 " for DEF PDU DATA\n");
2496 return ret;
2497 }
2498 phwi_ctrlr->default_pdu_data.id = phwi_context->be_def_dataq.id;
2499 SE_DEBUG(DBG_LVL_8, "iscsi def data id is %d\n",
2500 phwi_context->be_def_dataq.id);
2501 hwi_post_async_buffers(phba, 0);
2502 SE_DEBUG(DBG_LVL_8, "DEFAULT PDU DATA RING CREATED \n");
2503 return 0;
2504}
2505
2506static int
2507beiscsi_post_pages(struct beiscsi_hba *phba)
2508{
2509 struct be_mem_descriptor *mem_descr;
2510 struct mem_array *pm_arr;
2511 unsigned int page_offset, i;
2512 struct be_dma_mem sgl;
2513 int status;
2514
2515 mem_descr = phba->init_mem;
2516 mem_descr += HWI_MEM_SGE;
2517 pm_arr = mem_descr->mem_array;
2518
2519 page_offset = (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io *
2520 phba->fw_config.iscsi_icd_start) / PAGE_SIZE;
2521 for (i = 0; i < mem_descr->num_elements; i++) {
2522 hwi_build_be_sgl_arr(phba, pm_arr, &sgl);
2523 status = be_cmd_iscsi_post_sgl_pages(&phba->ctrl, &sgl,
2524 page_offset,
2525 (pm_arr->size / PAGE_SIZE));
2526 page_offset += pm_arr->size / PAGE_SIZE;
2527 if (status != 0) {
2528 shost_printk(KERN_ERR, phba->shost,
2529 "post sgl failed.\n");
2530 return status;
2531 }
2532 pm_arr++;
2533 }
2534 SE_DEBUG(DBG_LVL_8, "POSTED PAGES \n");
2535 return 0;
2536}
2537
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302538static void be_queue_free(struct beiscsi_hba *phba, struct be_queue_info *q)
2539{
2540 struct be_dma_mem *mem = &q->dma_mem;
2541 if (mem->va)
2542 pci_free_consistent(phba->pcidev, mem->size,
2543 mem->va, mem->dma);
2544}
2545
2546static int be_queue_alloc(struct beiscsi_hba *phba, struct be_queue_info *q,
2547 u16 len, u16 entry_size)
2548{
2549 struct be_dma_mem *mem = &q->dma_mem;
2550
2551 memset(q, 0, sizeof(*q));
2552 q->len = len;
2553 q->entry_size = entry_size;
2554 mem->size = len * entry_size;
2555 mem->va = pci_alloc_consistent(phba->pcidev, mem->size, &mem->dma);
2556 if (!mem->va)
2557 return -1;
2558 memset(mem->va, 0, mem->size);
2559 return 0;
2560}
2561
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302562static int
2563beiscsi_create_wrb_rings(struct beiscsi_hba *phba,
2564 struct hwi_context_memory *phwi_context,
2565 struct hwi_controller *phwi_ctrlr)
2566{
2567 unsigned int wrb_mem_index, offset, size, num_wrb_rings;
2568 u64 pa_addr_lo;
2569 unsigned int idx, num, i;
2570 struct mem_array *pwrb_arr;
2571 void *wrb_vaddr;
2572 struct be_dma_mem sgl;
2573 struct be_mem_descriptor *mem_descr;
2574 int status;
2575
2576 idx = 0;
2577 mem_descr = phba->init_mem;
2578 mem_descr += HWI_MEM_WRB;
2579 pwrb_arr = kmalloc(sizeof(*pwrb_arr) * phba->params.cxns_per_ctrl,
2580 GFP_KERNEL);
2581 if (!pwrb_arr) {
2582 shost_printk(KERN_ERR, phba->shost,
2583 "Memory alloc failed in create wrb ring.\n");
2584 return -ENOMEM;
2585 }
2586 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2587 pa_addr_lo = mem_descr->mem_array[idx].bus_address.u.a64.address;
2588 num_wrb_rings = mem_descr->mem_array[idx].size /
2589 (phba->params.wrbs_per_cxn * sizeof(struct iscsi_wrb));
2590
2591 for (num = 0; num < phba->params.cxns_per_ctrl; num++) {
2592 if (num_wrb_rings) {
2593 pwrb_arr[num].virtual_address = wrb_vaddr;
2594 pwrb_arr[num].bus_address.u.a64.address = pa_addr_lo;
2595 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2596 sizeof(struct iscsi_wrb);
2597 wrb_vaddr += pwrb_arr[num].size;
2598 pa_addr_lo += pwrb_arr[num].size;
2599 num_wrb_rings--;
2600 } else {
2601 idx++;
2602 wrb_vaddr = mem_descr->mem_array[idx].virtual_address;
2603 pa_addr_lo = mem_descr->mem_array[idx].\
2604 bus_address.u.a64.address;
2605 num_wrb_rings = mem_descr->mem_array[idx].size /
2606 (phba->params.wrbs_per_cxn *
2607 sizeof(struct iscsi_wrb));
2608 pwrb_arr[num].virtual_address = wrb_vaddr;
2609 pwrb_arr[num].bus_address.u.a64.address\
2610 = pa_addr_lo;
2611 pwrb_arr[num].size = phba->params.wrbs_per_cxn *
2612 sizeof(struct iscsi_wrb);
2613 wrb_vaddr += pwrb_arr[num].size;
2614 pa_addr_lo += pwrb_arr[num].size;
2615 num_wrb_rings--;
2616 }
2617 }
2618 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2619 wrb_mem_index = 0;
2620 offset = 0;
2621 size = 0;
2622
2623 hwi_build_be_sgl_by_offset(phba, &pwrb_arr[i], &sgl);
2624 status = be_cmd_wrbq_create(&phba->ctrl, &sgl,
2625 &phwi_context->be_wrbq[i]);
2626 if (status != 0) {
2627 shost_printk(KERN_ERR, phba->shost,
2628 "wrbq create failed.");
2629 return status;
2630 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05302631 phwi_ctrlr->wrb_context[i * 2].cid = phwi_context->be_wrbq[i].
2632 id;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302633 }
2634 kfree(pwrb_arr);
2635 return 0;
2636}
2637
2638static void free_wrb_handles(struct beiscsi_hba *phba)
2639{
2640 unsigned int index;
2641 struct hwi_controller *phwi_ctrlr;
2642 struct hwi_wrb_context *pwrb_context;
2643
2644 phwi_ctrlr = phba->phwi_ctrlr;
2645 for (index = 0; index < phba->params.cxns_per_ctrl * 2; index += 2) {
2646 pwrb_context = &phwi_ctrlr->wrb_context[index];
2647 kfree(pwrb_context->pwrb_handle_base);
2648 kfree(pwrb_context->pwrb_handle_basestd);
2649 }
2650}
2651
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302652static void be_mcc_queues_destroy(struct beiscsi_hba *phba)
2653{
2654 struct be_queue_info *q;
2655 struct be_ctrl_info *ctrl = &phba->ctrl;
2656
2657 q = &phba->ctrl.mcc_obj.q;
2658 if (q->created)
2659 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_MCCQ);
2660 be_queue_free(phba, q);
2661
2662 q = &phba->ctrl.mcc_obj.cq;
2663 if (q->created)
2664 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
2665 be_queue_free(phba, q);
2666}
2667
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302668static void hwi_cleanup(struct beiscsi_hba *phba)
2669{
2670 struct be_queue_info *q;
2671 struct be_ctrl_info *ctrl = &phba->ctrl;
2672 struct hwi_controller *phwi_ctrlr;
2673 struct hwi_context_memory *phwi_context;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302674 int i, eq_num;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302675
2676 phwi_ctrlr = phba->phwi_ctrlr;
2677 phwi_context = phwi_ctrlr->phwi_ctxt;
2678 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
2679 q = &phwi_context->be_wrbq[i];
2680 if (q->created)
2681 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_WRBQ);
2682 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302683 free_wrb_handles(phba);
2684
2685 q = &phwi_context->be_def_hdrq;
2686 if (q->created)
2687 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
2688
2689 q = &phwi_context->be_def_dataq;
2690 if (q->created)
2691 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_DPDUQ);
2692
2693 beiscsi_cmd_q_destroy(ctrl, NULL, QTYPE_SGL);
2694
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302695 for (i = 0; i < (phba->num_cpus); i++) {
2696 q = &phwi_context->be_cq[i];
2697 if (q->created)
2698 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_CQ);
2699 }
2700 if (phba->msix_enabled)
2701 eq_num = 1;
2702 else
2703 eq_num = 0;
2704 for (i = 0; i < (phba->num_cpus + eq_num); i++) {
2705 q = &phwi_context->be_eq[i].q;
2706 if (q->created)
2707 beiscsi_cmd_q_destroy(ctrl, q, QTYPE_EQ);
2708 }
2709 be_mcc_queues_destroy(phba);
2710}
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302711
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302712static int be_mcc_queues_create(struct beiscsi_hba *phba,
2713 struct hwi_context_memory *phwi_context)
2714{
2715 struct be_queue_info *q, *cq;
2716 struct be_ctrl_info *ctrl = &phba->ctrl;
2717
2718 /* Alloc MCC compl queue */
2719 cq = &phba->ctrl.mcc_obj.cq;
2720 if (be_queue_alloc(phba, cq, MCC_CQ_LEN,
2721 sizeof(struct be_mcc_compl)))
2722 goto err;
2723 /* Ask BE to create MCC compl queue; */
2724 if (phba->msix_enabled) {
2725 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq
2726 [phba->num_cpus].q, false, true, 0))
2727 goto mcc_cq_free;
2728 } else {
2729 if (beiscsi_cmd_cq_create(ctrl, cq, &phwi_context->be_eq[0].q,
2730 false, true, 0))
2731 goto mcc_cq_free;
2732 }
2733
2734 /* Alloc MCC queue */
2735 q = &phba->ctrl.mcc_obj.q;
2736 if (be_queue_alloc(phba, q, MCC_Q_LEN, sizeof(struct be_mcc_wrb)))
2737 goto mcc_cq_destroy;
2738
2739 /* Ask BE to create MCC queue */
Jayamohan Kallickal35e66012009-10-23 11:53:49 +05302740 if (beiscsi_cmd_mccq_create(phba, q, cq))
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302741 goto mcc_q_free;
2742
2743 return 0;
2744
2745mcc_q_free:
2746 be_queue_free(phba, q);
2747mcc_cq_destroy:
2748 beiscsi_cmd_q_destroy(ctrl, cq, QTYPE_CQ);
2749mcc_cq_free:
2750 be_queue_free(phba, cq);
2751err:
2752 return -1;
2753}
2754
2755static int find_num_cpus(void)
2756{
2757 int num_cpus = 0;
2758
2759 num_cpus = num_online_cpus();
2760 if (num_cpus >= MAX_CPUS)
2761 num_cpus = MAX_CPUS - 1;
2762
2763 SE_DEBUG(DBG_LVL_8, "num_cpus = %d \n", num_cpus);
2764 return num_cpus;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302765}
2766
2767static int hwi_init_port(struct beiscsi_hba *phba)
2768{
2769 struct hwi_controller *phwi_ctrlr;
2770 struct hwi_context_memory *phwi_context;
2771 unsigned int def_pdu_ring_sz;
2772 struct be_ctrl_info *ctrl = &phba->ctrl;
2773 int status;
2774
2775 def_pdu_ring_sz =
2776 phba->params.asyncpdus_per_ctrl * sizeof(struct phys_addr);
2777 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302778 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302779 phwi_context->max_eqd = 0;
2780 phwi_context->min_eqd = 0;
2781 phwi_context->cur_eqd = 64;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302782 be_cmd_fw_initialize(&phba->ctrl);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302783
2784 status = beiscsi_create_eqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302785 if (status != 0) {
2786 shost_printk(KERN_ERR, phba->shost, "EQ not created \n");
2787 goto error;
2788 }
2789
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302790 status = be_mcc_queues_create(phba, phwi_context);
2791 if (status != 0)
2792 goto error;
2793
2794 status = mgmt_check_supported_fw(ctrl, phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302795 if (status != 0) {
2796 shost_printk(KERN_ERR, phba->shost,
2797 "Unsupported fw version \n");
2798 goto error;
2799 }
2800
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302801 status = beiscsi_create_cqs(phba, phwi_context);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302802 if (status != 0) {
2803 shost_printk(KERN_ERR, phba->shost, "CQ not created\n");
2804 goto error;
2805 }
2806
2807 status = beiscsi_create_def_hdr(phba, phwi_context, phwi_ctrlr,
2808 def_pdu_ring_sz);
2809 if (status != 0) {
2810 shost_printk(KERN_ERR, phba->shost,
2811 "Default Header not created\n");
2812 goto error;
2813 }
2814
2815 status = beiscsi_create_def_data(phba, phwi_context,
2816 phwi_ctrlr, def_pdu_ring_sz);
2817 if (status != 0) {
2818 shost_printk(KERN_ERR, phba->shost,
2819 "Default Data not created\n");
2820 goto error;
2821 }
2822
2823 status = beiscsi_post_pages(phba);
2824 if (status != 0) {
2825 shost_printk(KERN_ERR, phba->shost, "Post SGL Pages Failed\n");
2826 goto error;
2827 }
2828
2829 status = beiscsi_create_wrb_rings(phba, phwi_context, phwi_ctrlr);
2830 if (status != 0) {
2831 shost_printk(KERN_ERR, phba->shost,
2832 "WRB Rings not created\n");
2833 goto error;
2834 }
2835
2836 SE_DEBUG(DBG_LVL_8, "hwi_init_port success\n");
2837 return 0;
2838
2839error:
2840 shost_printk(KERN_ERR, phba->shost, "hwi_init_port failed");
2841 hwi_cleanup(phba);
2842 return -ENOMEM;
2843}
2844
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302845static int hwi_init_controller(struct beiscsi_hba *phba)
2846{
2847 struct hwi_controller *phwi_ctrlr;
2848
2849 phwi_ctrlr = phba->phwi_ctrlr;
2850 if (1 == phba->init_mem[HWI_MEM_ADDN_CONTEXT].num_elements) {
2851 phwi_ctrlr->phwi_ctxt = (struct hwi_context_memory *)phba->
2852 init_mem[HWI_MEM_ADDN_CONTEXT].mem_array[0].virtual_address;
2853 SE_DEBUG(DBG_LVL_8, " phwi_ctrlr->phwi_ctxt=%p \n",
2854 phwi_ctrlr->phwi_ctxt);
2855 } else {
2856 shost_printk(KERN_ERR, phba->shost,
2857 "HWI_MEM_ADDN_CONTEXT is more than one element."
2858 "Failing to load\n");
2859 return -ENOMEM;
2860 }
2861
2862 iscsi_init_global_templates(phba);
2863 beiscsi_init_wrb_handle(phba);
2864 hwi_init_async_pdu_ctx(phba);
2865 if (hwi_init_port(phba) != 0) {
2866 shost_printk(KERN_ERR, phba->shost,
2867 "hwi_init_controller failed\n");
2868 return -ENOMEM;
2869 }
2870 return 0;
2871}
2872
2873static void beiscsi_free_mem(struct beiscsi_hba *phba)
2874{
2875 struct be_mem_descriptor *mem_descr;
2876 int i, j;
2877
2878 mem_descr = phba->init_mem;
2879 i = 0;
2880 j = 0;
2881 for (i = 0; i < SE_MEM_MAX; i++) {
2882 for (j = mem_descr->num_elements; j > 0; j--) {
2883 pci_free_consistent(phba->pcidev,
2884 mem_descr->mem_array[j - 1].size,
2885 mem_descr->mem_array[j - 1].virtual_address,
2886 mem_descr->mem_array[j - 1].bus_address.
2887 u.a64.address);
2888 }
2889 kfree(mem_descr->mem_array);
2890 mem_descr++;
2891 }
2892 kfree(phba->init_mem);
2893 kfree(phba->phwi_ctrlr);
2894}
2895
2896static int beiscsi_init_controller(struct beiscsi_hba *phba)
2897{
2898 int ret = -ENOMEM;
2899
2900 ret = beiscsi_get_memory(phba);
2901 if (ret < 0) {
2902 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe -"
2903 "Failed in beiscsi_alloc_memory \n");
2904 return ret;
2905 }
2906
2907 ret = hwi_init_controller(phba);
2908 if (ret)
2909 goto free_init;
2910 SE_DEBUG(DBG_LVL_8, "Return success from beiscsi_init_controller");
2911 return 0;
2912
2913free_init:
2914 beiscsi_free_mem(phba);
2915 return -ENOMEM;
2916}
2917
2918static int beiscsi_init_sgl_handle(struct beiscsi_hba *phba)
2919{
2920 struct be_mem_descriptor *mem_descr_sglh, *mem_descr_sg;
2921 struct sgl_handle *psgl_handle;
2922 struct iscsi_sge *pfrag;
2923 unsigned int arr_index, i, idx;
2924
2925 phba->io_sgl_hndl_avbl = 0;
2926 phba->eh_sgl_hndl_avbl = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05302927
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05302928 mem_descr_sglh = phba->init_mem;
2929 mem_descr_sglh += HWI_MEM_SGLH;
2930 if (1 == mem_descr_sglh->num_elements) {
2931 phba->io_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
2932 phba->params.ios_per_ctrl,
2933 GFP_KERNEL);
2934 if (!phba->io_sgl_hndl_base) {
2935 shost_printk(KERN_ERR, phba->shost,
2936 "Mem Alloc Failed. Failing to load\n");
2937 return -ENOMEM;
2938 }
2939 phba->eh_sgl_hndl_base = kzalloc(sizeof(struct sgl_handle *) *
2940 (phba->params.icds_per_ctrl -
2941 phba->params.ios_per_ctrl),
2942 GFP_KERNEL);
2943 if (!phba->eh_sgl_hndl_base) {
2944 kfree(phba->io_sgl_hndl_base);
2945 shost_printk(KERN_ERR, phba->shost,
2946 "Mem Alloc Failed. Failing to load\n");
2947 return -ENOMEM;
2948 }
2949 } else {
2950 shost_printk(KERN_ERR, phba->shost,
2951 "HWI_MEM_SGLH is more than one element."
2952 "Failing to load\n");
2953 return -ENOMEM;
2954 }
2955
2956 arr_index = 0;
2957 idx = 0;
2958 while (idx < mem_descr_sglh->num_elements) {
2959 psgl_handle = mem_descr_sglh->mem_array[idx].virtual_address;
2960
2961 for (i = 0; i < (mem_descr_sglh->mem_array[idx].size /
2962 sizeof(struct sgl_handle)); i++) {
2963 if (arr_index < phba->params.ios_per_ctrl) {
2964 phba->io_sgl_hndl_base[arr_index] = psgl_handle;
2965 phba->io_sgl_hndl_avbl++;
2966 arr_index++;
2967 } else {
2968 phba->eh_sgl_hndl_base[arr_index -
2969 phba->params.ios_per_ctrl] =
2970 psgl_handle;
2971 arr_index++;
2972 phba->eh_sgl_hndl_avbl++;
2973 }
2974 psgl_handle++;
2975 }
2976 idx++;
2977 }
2978 SE_DEBUG(DBG_LVL_8,
2979 "phba->io_sgl_hndl_avbl=%d"
2980 "phba->eh_sgl_hndl_avbl=%d \n",
2981 phba->io_sgl_hndl_avbl,
2982 phba->eh_sgl_hndl_avbl);
2983 mem_descr_sg = phba->init_mem;
2984 mem_descr_sg += HWI_MEM_SGE;
2985 SE_DEBUG(DBG_LVL_8, "\n mem_descr_sg->num_elements=%d \n",
2986 mem_descr_sg->num_elements);
2987 arr_index = 0;
2988 idx = 0;
2989 while (idx < mem_descr_sg->num_elements) {
2990 pfrag = mem_descr_sg->mem_array[idx].virtual_address;
2991
2992 for (i = 0;
2993 i < (mem_descr_sg->mem_array[idx].size) /
2994 (sizeof(struct iscsi_sge) * phba->params.num_sge_per_io);
2995 i++) {
2996 if (arr_index < phba->params.ios_per_ctrl)
2997 psgl_handle = phba->io_sgl_hndl_base[arr_index];
2998 else
2999 psgl_handle = phba->eh_sgl_hndl_base[arr_index -
3000 phba->params.ios_per_ctrl];
3001 psgl_handle->pfrag = pfrag;
3002 AMAP_SET_BITS(struct amap_iscsi_sge, addr_hi, pfrag, 0);
3003 AMAP_SET_BITS(struct amap_iscsi_sge, addr_lo, pfrag, 0);
3004 pfrag += phba->params.num_sge_per_io;
3005 psgl_handle->sgl_index =
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303006 phba->fw_config.iscsi_icd_start + arr_index++;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303007 }
3008 idx++;
3009 }
3010 phba->io_sgl_free_index = 0;
3011 phba->io_sgl_alloc_index = 0;
3012 phba->eh_sgl_free_index = 0;
3013 phba->eh_sgl_alloc_index = 0;
3014 return 0;
3015}
3016
3017static int hba_setup_cid_tbls(struct beiscsi_hba *phba)
3018{
3019 int i, new_cid;
3020
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303021 phba->cid_array = kzalloc(sizeof(void *) * phba->params.cxns_per_ctrl,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303022 GFP_KERNEL);
3023 if (!phba->cid_array) {
3024 shost_printk(KERN_ERR, phba->shost,
3025 "Failed to allocate memory in "
3026 "hba_setup_cid_tbls\n");
3027 return -ENOMEM;
3028 }
Jayamohan Kallickalc2462282010-01-05 05:05:34 +05303029 phba->ep_array = kzalloc(sizeof(struct iscsi_endpoint *) *
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303030 phba->params.cxns_per_ctrl * 2, GFP_KERNEL);
3031 if (!phba->ep_array) {
3032 shost_printk(KERN_ERR, phba->shost,
3033 "Failed to allocate memory in "
3034 "hba_setup_cid_tbls \n");
3035 kfree(phba->cid_array);
3036 return -ENOMEM;
3037 }
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303038 new_cid = phba->fw_config.iscsi_cid_start;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303039 for (i = 0; i < phba->params.cxns_per_ctrl; i++) {
3040 phba->cid_array[i] = new_cid;
3041 new_cid += 2;
3042 }
3043 phba->avlbl_cids = phba->params.cxns_per_ctrl;
3044 return 0;
3045}
3046
3047static unsigned char hwi_enable_intr(struct beiscsi_hba *phba)
3048{
3049 struct be_ctrl_info *ctrl = &phba->ctrl;
3050 struct hwi_controller *phwi_ctrlr;
3051 struct hwi_context_memory *phwi_context;
3052 struct be_queue_info *eq;
3053 u8 __iomem *addr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303054 u32 reg, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303055 u32 enabled;
3056
3057 phwi_ctrlr = phba->phwi_ctrlr;
3058 phwi_context = phwi_ctrlr->phwi_ctxt;
3059
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303060 addr = (u8 __iomem *) ((u8 __iomem *) ctrl->pcicfg +
3061 PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET);
3062 reg = ioread32(addr);
3063 SE_DEBUG(DBG_LVL_8, "reg =x%08x \n", reg);
3064
3065 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3066 if (!enabled) {
3067 reg |= MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3068 SE_DEBUG(DBG_LVL_8, "reg =x%08x addr=%p \n", reg, addr);
3069 iowrite32(reg, addr);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303070 for (i = 0; i <= phba->num_cpus; i++) {
3071 eq = &phwi_context->be_eq[i].q;
3072 SE_DEBUG(DBG_LVL_8, "eq->id=%d \n", eq->id);
3073 hwi_ring_eq_db(phba, eq->id, 0, 0, 1, 1);
3074 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303075 } else
3076 shost_printk(KERN_WARNING, phba->shost,
3077 "In hwi_enable_intr, Not Enabled \n");
3078 return true;
3079}
3080
3081static void hwi_disable_intr(struct beiscsi_hba *phba)
3082{
3083 struct be_ctrl_info *ctrl = &phba->ctrl;
3084
3085 u8 __iomem *addr = ctrl->pcicfg + PCICFG_MEMBAR_CTRL_INT_CTRL_OFFSET;
3086 u32 reg = ioread32(addr);
3087
3088 u32 enabled = reg & MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3089 if (enabled) {
3090 reg &= ~MEMBAR_CTRL_INT_CTRL_HOSTINTR_MASK;
3091 iowrite32(reg, addr);
3092 } else
3093 shost_printk(KERN_WARNING, phba->shost,
3094 "In hwi_disable_intr, Already Disabled \n");
3095}
3096
3097static int beiscsi_init_port(struct beiscsi_hba *phba)
3098{
3099 int ret;
3100
3101 ret = beiscsi_init_controller(phba);
3102 if (ret < 0) {
3103 shost_printk(KERN_ERR, phba->shost,
3104 "beiscsi_dev_probe - Failed in"
3105 "beiscsi_init_controller \n");
3106 return ret;
3107 }
3108 ret = beiscsi_init_sgl_handle(phba);
3109 if (ret < 0) {
3110 shost_printk(KERN_ERR, phba->shost,
3111 "beiscsi_dev_probe - Failed in"
3112 "beiscsi_init_sgl_handle \n");
3113 goto do_cleanup_ctrlr;
3114 }
3115
3116 if (hba_setup_cid_tbls(phba)) {
3117 shost_printk(KERN_ERR, phba->shost,
3118 "Failed in hba_setup_cid_tbls\n");
3119 kfree(phba->io_sgl_hndl_base);
3120 kfree(phba->eh_sgl_hndl_base);
3121 goto do_cleanup_ctrlr;
3122 }
3123
3124 return ret;
3125
3126do_cleanup_ctrlr:
3127 hwi_cleanup(phba);
3128 return ret;
3129}
3130
3131static void hwi_purge_eq(struct beiscsi_hba *phba)
3132{
3133 struct hwi_controller *phwi_ctrlr;
3134 struct hwi_context_memory *phwi_context;
3135 struct be_queue_info *eq;
3136 struct be_eq_entry *eqe = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303137 int i, eq_msix;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303138 unsigned int num_processed;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303139
3140 phwi_ctrlr = phba->phwi_ctrlr;
3141 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303142 if (phba->msix_enabled)
3143 eq_msix = 1;
3144 else
3145 eq_msix = 0;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303146
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303147 for (i = 0; i < (phba->num_cpus + eq_msix); i++) {
3148 eq = &phwi_context->be_eq[i].q;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303149 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303150 num_processed = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303151 while (eqe->dw[offsetof(struct amap_eq_entry, valid) / 32]
3152 & EQE_VALID_MASK) {
3153 AMAP_SET_BITS(struct amap_eq_entry, valid, eqe, 0);
3154 queue_tail_inc(eq);
3155 eqe = queue_tail_node(eq);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303156 num_processed++;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303157 }
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303158
3159 if (num_processed)
3160 hwi_ring_eq_db(phba, eq->id, 1, num_processed, 1, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303161 }
3162}
3163
3164static void beiscsi_clean_port(struct beiscsi_hba *phba)
3165{
3166 unsigned char mgmt_status;
3167
3168 mgmt_status = mgmt_epfw_cleanup(phba, CMD_CONNECTION_CHUTE_0);
3169 if (mgmt_status)
3170 shost_printk(KERN_WARNING, phba->shost,
3171 "mgmt_epfw_cleanup FAILED \n");
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303172
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303173 hwi_purge_eq(phba);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303174 hwi_cleanup(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303175 kfree(phba->io_sgl_hndl_base);
3176 kfree(phba->eh_sgl_hndl_base);
3177 kfree(phba->cid_array);
3178 kfree(phba->ep_array);
3179}
3180
3181void
3182beiscsi_offload_connection(struct beiscsi_conn *beiscsi_conn,
3183 struct beiscsi_offload_params *params)
3184{
3185 struct wrb_handle *pwrb_handle;
3186 struct iscsi_target_context_update_wrb *pwrb = NULL;
3187 struct be_mem_descriptor *mem_descr;
3188 struct beiscsi_hba *phba = beiscsi_conn->phba;
3189 u32 doorbell = 0;
3190
3191 /*
3192 * We can always use 0 here because it is reserved by libiscsi for
3193 * login/startup related tasks.
3194 */
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303195 pwrb_handle = alloc_wrb_handle(phba, (beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303196 phba->fw_config.iscsi_cid_start));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303197 pwrb = (struct iscsi_target_context_update_wrb *)pwrb_handle->pwrb;
3198 memset(pwrb, 0, sizeof(*pwrb));
3199 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3200 max_burst_length, pwrb, params->dw[offsetof
3201 (struct amap_beiscsi_offload_params,
3202 max_burst_length) / 32]);
3203 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3204 max_send_data_segment_length, pwrb,
3205 params->dw[offsetof(struct amap_beiscsi_offload_params,
3206 max_send_data_segment_length) / 32]);
3207 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3208 first_burst_length,
3209 pwrb,
3210 params->dw[offsetof(struct amap_beiscsi_offload_params,
3211 first_burst_length) / 32]);
3212
3213 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, erl, pwrb,
3214 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3215 erl) / 32] & OFFLD_PARAMS_ERL));
3216 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, dde, pwrb,
3217 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3218 dde) / 32] & OFFLD_PARAMS_DDE) >> 2);
3219 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, hde, pwrb,
3220 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3221 hde) / 32] & OFFLD_PARAMS_HDE) >> 3);
3222 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ir2t, pwrb,
3223 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3224 ir2t) / 32] & OFFLD_PARAMS_IR2T) >> 4);
3225 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, imd, pwrb,
3226 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3227 imd) / 32] & OFFLD_PARAMS_IMD) >> 5);
3228 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, stat_sn,
3229 pwrb,
3230 (params->dw[offsetof(struct amap_beiscsi_offload_params,
3231 exp_statsn) / 32] + 1));
3232 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, type, pwrb,
3233 0x7);
3234 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, wrb_idx,
3235 pwrb, pwrb_handle->wrb_index);
3236 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, ptr2nextwrb,
3237 pwrb, pwrb_handle->nxt_wrb_index);
3238 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3239 session_state, pwrb, 0);
3240 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, compltonack,
3241 pwrb, 1);
3242 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, notpredblq,
3243 pwrb, 0);
3244 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb, mode, pwrb,
3245 0);
3246
3247 mem_descr = phba->init_mem;
3248 mem_descr += ISCSI_MEM_GLOBAL_HEADER;
3249
3250 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3251 pad_buffer_addr_hi, pwrb,
3252 mem_descr->mem_array[0].bus_address.u.a32.address_hi);
3253 AMAP_SET_BITS(struct amap_iscsi_target_context_update_wrb,
3254 pad_buffer_addr_lo, pwrb,
3255 mem_descr->mem_array[0].bus_address.u.a32.address_lo);
3256
3257 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_target_context_update_wrb));
3258
3259 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303260 doorbell |= (pwrb_handle->wrb_index & DB_DEF_PDU_WRB_INDEX_MASK)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303261 << DB_DEF_PDU_WRB_INDEX_SHIFT;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303262 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3263
3264 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3265}
3266
3267static void beiscsi_parse_pdu(struct iscsi_conn *conn, itt_t itt,
3268 int *index, int *age)
3269{
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303270 *index = (int)itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303271 if (age)
3272 *age = conn->session->age;
3273}
3274
3275/**
3276 * beiscsi_alloc_pdu - allocates pdu and related resources
3277 * @task: libiscsi task
3278 * @opcode: opcode of pdu for task
3279 *
3280 * This is called with the session lock held. It will allocate
3281 * the wrb and sgl if needed for the command. And it will prep
3282 * the pdu's itt. beiscsi_parse_pdu will later translate
3283 * the pdu itt to the libiscsi task itt.
3284 */
3285static int beiscsi_alloc_pdu(struct iscsi_task *task, uint8_t opcode)
3286{
3287 struct beiscsi_io_task *io_task = task->dd_data;
3288 struct iscsi_conn *conn = task->conn;
3289 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3290 struct beiscsi_hba *phba = beiscsi_conn->phba;
3291 struct hwi_wrb_context *pwrb_context;
3292 struct hwi_controller *phwi_ctrlr;
3293 itt_t itt;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303294 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
3295 dma_addr_t paddr;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303296
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303297 io_task->cmd_bhs = pci_pool_alloc(beiscsi_sess->bhs_pool,
3298 GFP_KERNEL, &paddr);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303299 if (!io_task->cmd_bhs)
3300 return -ENOMEM;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303301 io_task->bhs_pa.u.a64.address = paddr;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303302 io_task->libiscsi_itt = (itt_t)task->itt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303303 io_task->pwrb_handle = alloc_wrb_handle(phba,
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303304 beiscsi_conn->beiscsi_conn_cid -
Jayamohan Kallickald5431482010-01-05 05:06:21 +05303305 phba->fw_config.iscsi_cid_start
3306 );
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303307 io_task->conn = beiscsi_conn;
3308
3309 task->hdr = (struct iscsi_hdr *)&io_task->cmd_bhs->iscsi_hdr;
3310 task->hdr_max = sizeof(struct be_cmd_bhs);
3311
3312 if (task->sc) {
3313 spin_lock(&phba->io_sgl_lock);
3314 io_task->psgl_handle = alloc_io_sgl_handle(phba);
3315 spin_unlock(&phba->io_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303316 if (!io_task->psgl_handle)
3317 goto free_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303318 } else {
3319 io_task->scsi_cmnd = NULL;
Jayamohan Kallickald7aea672010-01-05 05:08:39 +05303320 if ((opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303321 if (!beiscsi_conn->login_in_progress) {
3322 spin_lock(&phba->mgmt_sgl_lock);
3323 io_task->psgl_handle = (struct sgl_handle *)
3324 alloc_mgmt_sgl_handle(phba);
3325 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303326 if (!io_task->psgl_handle)
3327 goto free_hndls;
3328
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303329 beiscsi_conn->login_in_progress = 1;
3330 beiscsi_conn->plogin_sgl_handle =
3331 io_task->psgl_handle;
3332 } else {
3333 io_task->psgl_handle =
3334 beiscsi_conn->plogin_sgl_handle;
3335 }
3336 } else {
3337 spin_lock(&phba->mgmt_sgl_lock);
3338 io_task->psgl_handle = alloc_mgmt_sgl_handle(phba);
3339 spin_unlock(&phba->mgmt_sgl_lock);
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303340 if (!io_task->psgl_handle)
3341 goto free_hndls;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303342 }
3343 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303344 itt = (itt_t) cpu_to_be32(((unsigned int)io_task->pwrb_handle->
3345 wrb_index << 16) | (unsigned int)
3346 (io_task->psgl_handle->sgl_index));
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303347 io_task->pwrb_handle->pio_handle = task;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303348
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303349 io_task->cmd_bhs->iscsi_hdr.itt = itt;
3350 return 0;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303351
3352free_hndls:
3353 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303354 pwrb_context = &phwi_ctrlr->wrb_context[
3355 beiscsi_conn->beiscsi_conn_cid -
3356 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303357 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
3358 io_task->pwrb_handle = NULL;
3359 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3360 io_task->bhs_pa.u.a64.address);
3361 SE_DEBUG(DBG_LVL_1, "Alloc of SGL_ICD Failed \n");
3362 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303363}
3364
3365static void beiscsi_cleanup_task(struct iscsi_task *task)
3366{
3367 struct beiscsi_io_task *io_task = task->dd_data;
3368 struct iscsi_conn *conn = task->conn;
3369 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3370 struct beiscsi_hba *phba = beiscsi_conn->phba;
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303371 struct beiscsi_session *beiscsi_sess = beiscsi_conn->beiscsi_sess;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303372 struct hwi_wrb_context *pwrb_context;
3373 struct hwi_controller *phwi_ctrlr;
3374
3375 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303376 pwrb_context = &phwi_ctrlr->wrb_context[beiscsi_conn->beiscsi_conn_cid
3377 - phba->fw_config.iscsi_cid_start];
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303378 if (io_task->pwrb_handle) {
3379 free_wrb_handle(phba, pwrb_context, io_task->pwrb_handle);
3380 io_task->pwrb_handle = NULL;
3381 }
3382
Jayamohan Kallickal2afc95b2009-09-22 08:22:26 +05303383 if (io_task->cmd_bhs) {
3384 pci_pool_free(beiscsi_sess->bhs_pool, io_task->cmd_bhs,
3385 io_task->bhs_pa.u.a64.address);
3386 }
3387
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303388 if (task->sc) {
3389 if (io_task->psgl_handle) {
3390 spin_lock(&phba->io_sgl_lock);
3391 free_io_sgl_handle(phba, io_task->psgl_handle);
3392 spin_unlock(&phba->io_sgl_lock);
3393 io_task->psgl_handle = NULL;
3394 }
3395 } else {
3396 if ((task->hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGIN)
3397 return;
3398 if (io_task->psgl_handle) {
3399 spin_lock(&phba->mgmt_sgl_lock);
3400 free_mgmt_sgl_handle(phba, io_task->psgl_handle);
3401 spin_unlock(&phba->mgmt_sgl_lock);
3402 io_task->psgl_handle = NULL;
3403 }
3404 }
3405}
3406
3407static int beiscsi_iotask(struct iscsi_task *task, struct scatterlist *sg,
3408 unsigned int num_sg, unsigned int xferlen,
3409 unsigned int writedir)
3410{
3411
3412 struct beiscsi_io_task *io_task = task->dd_data;
3413 struct iscsi_conn *conn = task->conn;
3414 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3415 struct beiscsi_hba *phba = beiscsi_conn->phba;
3416 struct iscsi_wrb *pwrb = NULL;
3417 unsigned int doorbell = 0;
3418
3419 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303420 io_task->cmd_bhs->iscsi_hdr.exp_statsn = 0;
3421 io_task->bhs_len = sizeof(struct be_cmd_bhs);
3422
3423 if (writedir) {
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303424 memset(&io_task->cmd_bhs->iscsi_data_pdu, 0, 48);
3425 AMAP_SET_BITS(struct amap_pdu_data_out, itt,
3426 &io_task->cmd_bhs->iscsi_data_pdu,
3427 (unsigned int)io_task->cmd_bhs->iscsi_hdr.itt);
3428 AMAP_SET_BITS(struct amap_pdu_data_out, opcode,
3429 &io_task->cmd_bhs->iscsi_data_pdu,
3430 ISCSI_OPCODE_SCSI_DATA_OUT);
3431 AMAP_SET_BITS(struct amap_pdu_data_out, final_bit,
3432 &io_task->cmd_bhs->iscsi_data_pdu, 1);
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303433 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3434 INI_WR_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303435 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303436 } else {
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303437 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3438 INI_RD_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303439 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 0);
3440 }
3441 memcpy(&io_task->cmd_bhs->iscsi_data_pdu.
3442 dw[offsetof(struct amap_pdu_data_out, lun) / 32],
3443 io_task->cmd_bhs->iscsi_hdr.lun, sizeof(struct scsi_lun));
3444
3445 AMAP_SET_BITS(struct amap_iscsi_wrb, lun, pwrb,
3446 cpu_to_be16((unsigned short)io_task->cmd_bhs->iscsi_hdr.
3447 lun[0]));
3448 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb, xferlen);
3449 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
3450 io_task->pwrb_handle->wrb_index);
3451 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
3452 be32_to_cpu(task->cmdsn));
3453 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
3454 io_task->psgl_handle->sgl_index);
3455
3456 hwi_write_sgl(pwrb, sg, num_sg, io_task);
3457
3458 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
3459 io_task->pwrb_handle->nxt_wrb_index);
3460 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
3461
3462 doorbell |= beiscsi_conn->beiscsi_conn_cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303463 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303464 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
3465 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3466
3467 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3468 return 0;
3469}
3470
3471static int beiscsi_mtask(struct iscsi_task *task)
3472{
3473 struct beiscsi_io_task *aborted_io_task, *io_task = task->dd_data;
3474 struct iscsi_conn *conn = task->conn;
3475 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3476 struct beiscsi_hba *phba = beiscsi_conn->phba;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303477 struct iscsi_session *session;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303478 struct iscsi_wrb *pwrb = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303479 struct hwi_controller *phwi_ctrlr;
3480 struct hwi_wrb_context *pwrb_context;
3481 struct wrb_handle *pwrb_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303482 unsigned int doorbell = 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303483 unsigned int i, cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303484 struct iscsi_task *aborted_task;
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303485 unsigned int tag;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303486
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303487 cid = beiscsi_conn->beiscsi_conn_cid;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303488 pwrb = io_task->pwrb_handle->pwrb;
Jayamohan Kallickalcaf818f2010-01-23 05:38:18 +05303489 memset(pwrb, 0, sizeof(*pwrb));
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303490 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb,
3491 be32_to_cpu(task->cmdsn));
3492 AMAP_SET_BITS(struct amap_iscsi_wrb, wrb_idx, pwrb,
3493 io_task->pwrb_handle->wrb_index);
3494 AMAP_SET_BITS(struct amap_iscsi_wrb, sgl_icd_idx, pwrb,
3495 io_task->psgl_handle->sgl_index);
3496
3497 switch (task->hdr->opcode & ISCSI_OPCODE_MASK) {
3498 case ISCSI_OP_LOGIN:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303499 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3500 TGT_DM_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303501 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
3502 AMAP_SET_BITS(struct amap_iscsi_wrb, cmdsn_itt, pwrb, 1);
3503 hwi_write_buffer(pwrb, task);
3504 break;
3505 case ISCSI_OP_NOOP_OUT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303506 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3507 INI_RD_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05303508 if (task->hdr->ttt == ISCSI_RESERVED_TAG)
3509 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
3510 else
3511 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 1);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303512 hwi_write_buffer(pwrb, task);
3513 break;
3514 case ISCSI_OP_TEXT:
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303515 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3516 INI_WR_CMD);
Jayamohan Kallickal0ecb0b42010-01-05 05:09:19 +05303517 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303518 AMAP_SET_BITS(struct amap_iscsi_wrb, dsp, pwrb, 1);
3519 hwi_write_buffer(pwrb, task);
3520 break;
3521 case ISCSI_OP_SCSI_TMFUNC:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303522 session = conn->session;
3523 i = ((struct iscsi_tm *)task->hdr)->rtt;
3524 phwi_ctrlr = phba->phwi_ctrlr;
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303525 pwrb_context = &phwi_ctrlr->wrb_context[cid -
3526 phba->fw_config.iscsi_cid_start];
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303527 pwrb_handle = pwrb_context->pwrb_handle_basestd[be32_to_cpu(i)
3528 >> 16];
3529 aborted_task = pwrb_handle->pio_handle;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303530 if (!aborted_task)
3531 return 0;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303532
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303533 aborted_io_task = aborted_task->dd_data;
3534 if (!aborted_io_task->scsi_cmnd)
3535 return 0;
3536
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303537 tag = mgmt_invalidate_icds(phba,
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303538 aborted_io_task->psgl_handle->sgl_index,
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303539 cid);
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303540 if (!tag) {
3541 shost_printk(KERN_WARNING, phba->shost,
3542 "mgmt_invalidate_icds could not be"
3543 " submitted\n");
3544 } else {
3545 wait_event_interruptible(phba->ctrl.mcc_wait[tag],
3546 phba->ctrl.mcc_numtag[tag]);
3547 free_mcc_tag(&phba->ctrl, tag);
3548 }
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303549 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3550 INI_TMF_CMD);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303551 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
3552 hwi_write_buffer(pwrb, task);
3553 break;
3554 case ISCSI_OP_LOGOUT:
3555 AMAP_SET_BITS(struct amap_iscsi_wrb, dmsg, pwrb, 0);
3556 AMAP_SET_BITS(struct amap_iscsi_wrb, type, pwrb,
3557 HWH_TYPE_LOGOUT);
3558 hwi_write_buffer(pwrb, task);
3559 break;
3560
3561 default:
3562 SE_DEBUG(DBG_LVL_1, "opcode =%d Not supported \n",
3563 task->hdr->opcode & ISCSI_OPCODE_MASK);
3564 return -EINVAL;
3565 }
3566
3567 AMAP_SET_BITS(struct amap_iscsi_wrb, r2t_exp_dtl, pwrb,
Jayamohan Kallickal51a46252010-01-05 05:10:01 +05303568 task->data_count);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303569 AMAP_SET_BITS(struct amap_iscsi_wrb, ptr2nextwrb, pwrb,
3570 io_task->pwrb_handle->nxt_wrb_index);
3571 be_dws_le_to_cpu(pwrb, sizeof(struct iscsi_wrb));
3572
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303573 doorbell |= cid & DB_WRB_POST_CID_MASK;
Jayamohan Kallickal32951dd2010-01-23 05:34:24 +05303574 doorbell |= (io_task->pwrb_handle->wrb_index &
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303575 DB_DEF_PDU_WRB_INDEX_MASK) << DB_DEF_PDU_WRB_INDEX_SHIFT;
3576 doorbell |= 1 << DB_DEF_PDU_NUM_POSTED_SHIFT;
3577 iowrite32(doorbell, phba->db_va + DB_TXULP0_OFFSET);
3578 return 0;
3579}
3580
3581static int beiscsi_task_xmit(struct iscsi_task *task)
3582{
3583 struct iscsi_conn *conn = task->conn;
3584 struct beiscsi_io_task *io_task = task->dd_data;
3585 struct scsi_cmnd *sc = task->sc;
3586 struct beiscsi_conn *beiscsi_conn = conn->dd_data;
3587 struct scatterlist *sg;
3588 int num_sg;
3589 unsigned int writedir = 0, xferlen = 0;
3590
3591 SE_DEBUG(DBG_LVL_4, "\n cid=%d In beiscsi_task_xmit task=%p conn=%p \t"
3592 "beiscsi_conn=%p \n", beiscsi_conn->beiscsi_conn_cid,
3593 task, conn, beiscsi_conn);
3594 if (!sc)
3595 return beiscsi_mtask(task);
3596
3597 io_task->scsi_cmnd = sc;
3598 num_sg = scsi_dma_map(sc);
3599 if (num_sg < 0) {
3600 SE_DEBUG(DBG_LVL_1, " scsi_dma_map Failed\n")
3601 return num_sg;
3602 }
3603 SE_DEBUG(DBG_LVL_4, "xferlen=0x%08x scmd=%p num_sg=%d sernum=%lu\n",
3604 (scsi_bufflen(sc)), sc, num_sg, sc->serial_number);
3605 xferlen = scsi_bufflen(sc);
3606 sg = scsi_sglist(sc);
3607 if (sc->sc_data_direction == DMA_TO_DEVICE) {
3608 writedir = 1;
3609 SE_DEBUG(DBG_LVL_4, "task->imm_count=0x%08x \n",
3610 task->imm_count);
3611 } else
3612 writedir = 0;
3613 return beiscsi_iotask(task, sg, num_sg, xferlen, writedir);
3614}
3615
3616static void beiscsi_remove(struct pci_dev *pcidev)
3617{
3618 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303619 struct hwi_controller *phwi_ctrlr;
3620 struct hwi_context_memory *phwi_context;
3621 struct be_eq_obj *pbe_eq;
3622 unsigned int i, msix_vec;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303623
3624 phba = (struct beiscsi_hba *)pci_get_drvdata(pcidev);
3625 if (!phba) {
3626 dev_err(&pcidev->dev, "beiscsi_remove called with no phba \n");
3627 return;
3628 }
3629
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303630 phwi_ctrlr = phba->phwi_ctrlr;
3631 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303632 hwi_disable_intr(phba);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303633 if (phba->msix_enabled) {
3634 for (i = 0; i <= phba->num_cpus; i++) {
3635 msix_vec = phba->msix_entries[i].vector;
3636 free_irq(msix_vec, &phwi_context->be_eq[i]);
3637 }
3638 } else
3639 if (phba->pcidev->irq)
3640 free_irq(phba->pcidev->irq, phba);
3641 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303642 destroy_workqueue(phba->wq);
3643 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303644 for (i = 0; i < phba->num_cpus; i++) {
3645 pbe_eq = &phwi_context->be_eq[i];
3646 blk_iopoll_disable(&pbe_eq->iopoll);
3647 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303648
3649 beiscsi_clean_port(phba);
3650 beiscsi_free_mem(phba);
3651 beiscsi_unmap_pci_function(phba);
3652 pci_free_consistent(phba->pcidev,
3653 phba->ctrl.mbox_mem_alloced.size,
3654 phba->ctrl.mbox_mem_alloced.va,
3655 phba->ctrl.mbox_mem_alloced.dma);
3656 iscsi_host_remove(phba->shost);
3657 pci_dev_put(phba->pcidev);
3658 iscsi_host_free(phba->shost);
3659}
3660
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303661static void beiscsi_msix_enable(struct beiscsi_hba *phba)
3662{
3663 int i, status;
3664
3665 for (i = 0; i <= phba->num_cpus; i++)
3666 phba->msix_entries[i].entry = i;
3667
3668 status = pci_enable_msix(phba->pcidev, phba->msix_entries,
3669 (phba->num_cpus + 1));
3670 if (!status)
3671 phba->msix_enabled = true;
3672
3673 return;
3674}
3675
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303676static int __devinit beiscsi_dev_probe(struct pci_dev *pcidev,
3677 const struct pci_device_id *id)
3678{
3679 struct beiscsi_hba *phba = NULL;
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303680 struct hwi_controller *phwi_ctrlr;
3681 struct hwi_context_memory *phwi_context;
3682 struct be_eq_obj *pbe_eq;
3683 int ret, msix_vec, num_cpus, i;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303684
3685 ret = beiscsi_enable_pci(pcidev);
3686 if (ret < 0) {
3687 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3688 "Failed to enable pci device \n");
3689 return ret;
3690 }
3691
3692 phba = beiscsi_hba_alloc(pcidev);
3693 if (!phba) {
3694 dev_err(&pcidev->dev, "beiscsi_dev_probe-"
3695 " Failed in beiscsi_hba_alloc \n");
3696 goto disable_pci;
3697 }
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303698 SE_DEBUG(DBG_LVL_8, " phba = %p \n", phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303699
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303700 if (enable_msix)
3701 num_cpus = find_num_cpus();
3702 else
3703 num_cpus = 1;
3704 phba->num_cpus = num_cpus;
3705 SE_DEBUG(DBG_LVL_8, "num_cpus = %d \n", phba->num_cpus);
3706
3707 if (enable_msix)
3708 beiscsi_msix_enable(phba);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303709 ret = be_ctrl_init(phba, pcidev);
3710 if (ret) {
3711 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3712 "Failed in be_ctrl_init\n");
3713 goto hba_free;
3714 }
3715
3716 spin_lock_init(&phba->io_sgl_lock);
3717 spin_lock_init(&phba->mgmt_sgl_lock);
3718 spin_lock_init(&phba->isr_lock);
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303719 ret = mgmt_get_fw_config(&phba->ctrl, phba);
3720 if (ret != 0) {
3721 shost_printk(KERN_ERR, phba->shost,
3722 "Error getting fw config\n");
3723 goto free_port;
3724 }
3725 phba->shost->max_id = phba->fw_config.iscsi_cid_count;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303726 beiscsi_get_params(phba);
Jayamohan Kallickalaa874f02010-01-05 05:12:03 +05303727 phba->shost->can_queue = phba->params.ios_per_ctrl;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303728 ret = beiscsi_init_port(phba);
3729 if (ret < 0) {
3730 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3731 "Failed in beiscsi_init_port\n");
3732 goto free_port;
3733 }
3734
Jayamohan Kallickal756d29c2010-01-05 05:10:46 +05303735 for (i = 0; i < MAX_MCC_CMD ; i++) {
3736 init_waitqueue_head(&phba->ctrl.mcc_wait[i + 1]);
3737 phba->ctrl.mcc_tag[i] = i + 1;
3738 phba->ctrl.mcc_numtag[i + 1] = 0;
3739 phba->ctrl.mcc_tag_available++;
3740 }
3741
3742 phba->ctrl.mcc_alloc_index = phba->ctrl.mcc_free_index = 0;
3743
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303744 snprintf(phba->wq_name, sizeof(phba->wq_name), "beiscsi_q_irq%u",
3745 phba->shost->host_no);
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303746 phba->wq = create_workqueue(phba->wq_name);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303747 if (!phba->wq) {
3748 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3749 "Failed to allocate work queue\n");
3750 goto free_twq;
3751 }
3752
3753 INIT_WORK(&phba->work_cqs, beiscsi_process_all_cqs);
3754
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303755 phwi_ctrlr = phba->phwi_ctrlr;
3756 phwi_context = phwi_ctrlr->phwi_ctxt;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303757 if (blk_iopoll_enabled) {
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303758 for (i = 0; i < phba->num_cpus; i++) {
3759 pbe_eq = &phwi_context->be_eq[i];
3760 blk_iopoll_init(&pbe_eq->iopoll, be_iopoll_budget,
3761 be_iopoll);
3762 blk_iopoll_enable(&pbe_eq->iopoll);
3763 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303764 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303765 ret = beiscsi_init_irqs(phba);
3766 if (ret < 0) {
3767 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3768 "Failed to beiscsi_init_irqs\n");
3769 goto free_blkenbld;
3770 }
3771 ret = hwi_enable_intr(phba);
3772 if (ret < 0) {
3773 shost_printk(KERN_ERR, phba->shost, "beiscsi_dev_probe-"
3774 "Failed to hwi_enable_intr\n");
3775 goto free_ctrlr;
3776 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303777 SE_DEBUG(DBG_LVL_8, "\n\n\n SUCCESS - DRIVER LOADED \n\n\n");
3778 return 0;
3779
3780free_ctrlr:
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303781 if (phba->msix_enabled) {
3782 for (i = 0; i <= phba->num_cpus; i++) {
3783 msix_vec = phba->msix_entries[i].vector;
3784 free_irq(msix_vec, &phwi_context->be_eq[i]);
3785 }
3786 } else
3787 if (phba->pcidev->irq)
3788 free_irq(phba->pcidev->irq, phba);
3789 pci_disable_msix(phba->pcidev);
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303790free_blkenbld:
3791 destroy_workqueue(phba->wq);
3792 if (blk_iopoll_enabled)
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303793 for (i = 0; i < phba->num_cpus; i++) {
3794 pbe_eq = &phwi_context->be_eq[i];
3795 blk_iopoll_disable(&pbe_eq->iopoll);
3796 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303797free_twq:
3798 beiscsi_clean_port(phba);
3799 beiscsi_free_mem(phba);
3800free_port:
3801 pci_free_consistent(phba->pcidev,
3802 phba->ctrl.mbox_mem_alloced.size,
3803 phba->ctrl.mbox_mem_alloced.va,
3804 phba->ctrl.mbox_mem_alloced.dma);
3805 beiscsi_unmap_pci_function(phba);
3806hba_free:
3807 iscsi_host_remove(phba->shost);
3808 pci_dev_put(phba->pcidev);
3809 iscsi_host_free(phba->shost);
3810disable_pci:
3811 pci_disable_device(pcidev);
3812 return ret;
3813}
3814
3815struct iscsi_transport beiscsi_iscsi_transport = {
3816 .owner = THIS_MODULE,
3817 .name = DRV_NAME,
Jayamohan Kallickal9db0fb32010-01-05 05:12:43 +05303818 .caps = CAP_RECOVERY_L0 | CAP_HDRDGST | CAP_TEXT_NEGO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303819 CAP_MULTI_R2T | CAP_DATADGST | CAP_DATA_PATH_OFFLOAD,
3820 .param_mask = ISCSI_MAX_RECV_DLENGTH |
3821 ISCSI_MAX_XMIT_DLENGTH |
3822 ISCSI_HDRDGST_EN |
3823 ISCSI_DATADGST_EN |
3824 ISCSI_INITIAL_R2T_EN |
3825 ISCSI_MAX_R2T |
3826 ISCSI_IMM_DATA_EN |
3827 ISCSI_FIRST_BURST |
3828 ISCSI_MAX_BURST |
3829 ISCSI_PDU_INORDER_EN |
3830 ISCSI_DATASEQ_INORDER_EN |
3831 ISCSI_ERL |
3832 ISCSI_CONN_PORT |
3833 ISCSI_CONN_ADDRESS |
3834 ISCSI_EXP_STATSN |
3835 ISCSI_PERSISTENT_PORT |
3836 ISCSI_PERSISTENT_ADDRESS |
3837 ISCSI_TARGET_NAME | ISCSI_TPGT |
3838 ISCSI_USERNAME | ISCSI_PASSWORD |
3839 ISCSI_USERNAME_IN | ISCSI_PASSWORD_IN |
3840 ISCSI_FAST_ABORT | ISCSI_ABORT_TMO |
Jayamohan Kallickal7da50872010-01-05 05:04:12 +05303841 ISCSI_LU_RESET_TMO |
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303842 ISCSI_PING_TMO | ISCSI_RECV_TMO |
3843 ISCSI_IFACE_NAME | ISCSI_INITIATOR_NAME,
3844 .host_param_mask = ISCSI_HOST_HWADDRESS | ISCSI_HOST_IPADDRESS |
3845 ISCSI_HOST_INITIATOR_NAME,
3846 .create_session = beiscsi_session_create,
3847 .destroy_session = beiscsi_session_destroy,
3848 .create_conn = beiscsi_conn_create,
3849 .bind_conn = beiscsi_conn_bind,
3850 .destroy_conn = iscsi_conn_teardown,
3851 .set_param = beiscsi_set_param,
3852 .get_conn_param = beiscsi_conn_get_param,
3853 .get_session_param = iscsi_session_get_param,
3854 .get_host_param = beiscsi_get_host_param,
3855 .start_conn = beiscsi_conn_start,
3856 .stop_conn = beiscsi_conn_stop,
3857 .send_pdu = iscsi_conn_send_pdu,
3858 .xmit_task = beiscsi_task_xmit,
3859 .cleanup_task = beiscsi_cleanup_task,
3860 .alloc_pdu = beiscsi_alloc_pdu,
3861 .parse_pdu_itt = beiscsi_parse_pdu,
3862 .get_stats = beiscsi_conn_get_stats,
3863 .ep_connect = beiscsi_ep_connect,
3864 .ep_poll = beiscsi_ep_poll,
3865 .ep_disconnect = beiscsi_ep_disconnect,
3866 .session_recovery_timedout = iscsi_session_recovery_timedout,
3867};
3868
3869static struct pci_driver beiscsi_pci_driver = {
3870 .name = DRV_NAME,
3871 .probe = beiscsi_dev_probe,
3872 .remove = beiscsi_remove,
3873 .id_table = beiscsi_pci_id_table
3874};
3875
Jayamohan Kallickalbfead3b2009-10-23 11:52:33 +05303876
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303877static int __init beiscsi_module_init(void)
3878{
3879 int ret;
3880
3881 beiscsi_scsi_transport =
3882 iscsi_register_transport(&beiscsi_iscsi_transport);
3883 if (!beiscsi_scsi_transport) {
3884 SE_DEBUG(DBG_LVL_1,
3885 "beiscsi_module_init - Unable to register beiscsi"
3886 "transport.\n");
Jayamohan Kallickalf55a24f2010-01-23 05:37:40 +05303887 return -ENOMEM;
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303888 }
3889 SE_DEBUG(DBG_LVL_8, "In beiscsi_module_init, tt=%p \n",
3890 &beiscsi_iscsi_transport);
3891
3892 ret = pci_register_driver(&beiscsi_pci_driver);
3893 if (ret) {
3894 SE_DEBUG(DBG_LVL_1,
3895 "beiscsi_module_init - Unable to register"
3896 "beiscsi pci driver.\n");
3897 goto unregister_iscsi_transport;
3898 }
Jayamohan Kallickal6733b392009-09-05 07:36:35 +05303899 return 0;
3900
3901unregister_iscsi_transport:
3902 iscsi_unregister_transport(&beiscsi_iscsi_transport);
3903 return ret;
3904}
3905
3906static void __exit beiscsi_module_exit(void)
3907{
3908 pci_unregister_driver(&beiscsi_pci_driver);
3909 iscsi_unregister_transport(&beiscsi_iscsi_transport);
3910}
3911
3912module_init(beiscsi_module_init);
3913module_exit(beiscsi_module_exit);