1a3667aaeSNaresh Kumar Inna /*
2a3667aaeSNaresh Kumar Inna * This file is part of the Chelsio FCoE driver for Linux.
3a3667aaeSNaresh Kumar Inna *
4a3667aaeSNaresh Kumar Inna * Copyright (c) 2008-2012 Chelsio Communications, Inc. All rights reserved.
5a3667aaeSNaresh Kumar Inna *
6a3667aaeSNaresh Kumar Inna * This software is available to you under a choice of one of two
7a3667aaeSNaresh Kumar Inna * licenses. You may choose to be licensed under the terms of the GNU
8a3667aaeSNaresh Kumar Inna * General Public License (GPL) Version 2, available from the file
9a3667aaeSNaresh Kumar Inna * COPYING in the main directory of this source tree, or the
10a3667aaeSNaresh Kumar Inna * OpenIB.org BSD license below:
11a3667aaeSNaresh Kumar Inna *
12a3667aaeSNaresh Kumar Inna * Redistribution and use in source and binary forms, with or
13a3667aaeSNaresh Kumar Inna * without modification, are permitted provided that the following
14a3667aaeSNaresh Kumar Inna * conditions are met:
15a3667aaeSNaresh Kumar Inna *
16a3667aaeSNaresh Kumar Inna * - Redistributions of source code must retain the above
17a3667aaeSNaresh Kumar Inna * copyright notice, this list of conditions and the following
18a3667aaeSNaresh Kumar Inna * disclaimer.
19a3667aaeSNaresh Kumar Inna *
20a3667aaeSNaresh Kumar Inna * - Redistributions in binary form must reproduce the above
21a3667aaeSNaresh Kumar Inna * copyright notice, this list of conditions and the following
22a3667aaeSNaresh Kumar Inna * disclaimer in the documentation and/or other materials
23a3667aaeSNaresh Kumar Inna * provided with the distribution.
24a3667aaeSNaresh Kumar Inna *
25a3667aaeSNaresh Kumar Inna * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26a3667aaeSNaresh Kumar Inna * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27a3667aaeSNaresh Kumar Inna * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28a3667aaeSNaresh Kumar Inna * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29a3667aaeSNaresh Kumar Inna * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30a3667aaeSNaresh Kumar Inna * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31a3667aaeSNaresh Kumar Inna * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32a3667aaeSNaresh Kumar Inna * SOFTWARE.
33a3667aaeSNaresh Kumar Inna */
34a3667aaeSNaresh Kumar Inna
35a3667aaeSNaresh Kumar Inna #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
36a3667aaeSNaresh Kumar Inna
37a3667aaeSNaresh Kumar Inna #include <linux/kernel.h>
38a3667aaeSNaresh Kumar Inna #include <linux/module.h>
39a3667aaeSNaresh Kumar Inna #include <linux/init.h>
40a3667aaeSNaresh Kumar Inna #include <linux/pci.h>
41a3667aaeSNaresh Kumar Inna #include <linux/mm.h>
42a3667aaeSNaresh Kumar Inna #include <linux/notifier.h>
43a3667aaeSNaresh Kumar Inna #include <linux/kdebug.h>
44a3667aaeSNaresh Kumar Inna #include <linux/seq_file.h>
45a3667aaeSNaresh Kumar Inna #include <linux/debugfs.h>
46a3667aaeSNaresh Kumar Inna #include <linux/string.h>
47a3667aaeSNaresh Kumar Inna #include <linux/export.h>
48a3667aaeSNaresh Kumar Inna
49a3667aaeSNaresh Kumar Inna #include "csio_init.h"
50a3667aaeSNaresh Kumar Inna #include "csio_defs.h"
51a3667aaeSNaresh Kumar Inna
52a3667aaeSNaresh Kumar Inna #define CSIO_MIN_MEMPOOL_SZ 64
53a3667aaeSNaresh Kumar Inna
54a3667aaeSNaresh Kumar Inna static struct dentry *csio_debugfs_root;
55a3667aaeSNaresh Kumar Inna
56a3667aaeSNaresh Kumar Inna static struct scsi_transport_template *csio_fcoe_transport;
57a3667aaeSNaresh Kumar Inna static struct scsi_transport_template *csio_fcoe_transport_vport;
58a3667aaeSNaresh Kumar Inna
59a3667aaeSNaresh Kumar Inna /*
60a3667aaeSNaresh Kumar Inna * debugfs support
61a3667aaeSNaresh Kumar Inna */
62a3667aaeSNaresh Kumar Inna static ssize_t
csio_mem_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)63a3667aaeSNaresh Kumar Inna csio_mem_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
64a3667aaeSNaresh Kumar Inna {
65a3667aaeSNaresh Kumar Inna loff_t pos = *ppos;
66496ad9aaSAl Viro loff_t avail = file_inode(file)->i_size;
67a3667aaeSNaresh Kumar Inna unsigned int mem = (uintptr_t)file->private_data & 3;
68a3667aaeSNaresh Kumar Inna struct csio_hw *hw = file->private_data - mem;
69a3667aaeSNaresh Kumar Inna
70a3667aaeSNaresh Kumar Inna if (pos < 0)
71a3667aaeSNaresh Kumar Inna return -EINVAL;
72a3667aaeSNaresh Kumar Inna if (pos >= avail)
73a3667aaeSNaresh Kumar Inna return 0;
74a3667aaeSNaresh Kumar Inna if (count > avail - pos)
75a3667aaeSNaresh Kumar Inna count = avail - pos;
76a3667aaeSNaresh Kumar Inna
77a3667aaeSNaresh Kumar Inna while (count) {
78a3667aaeSNaresh Kumar Inna size_t len;
79a3667aaeSNaresh Kumar Inna int ret, ofst;
80a3667aaeSNaresh Kumar Inna __be32 data[16];
81a3667aaeSNaresh Kumar Inna
82a3667aaeSNaresh Kumar Inna if (mem == MEM_MC)
837cc16380SArvind Bhushan ret = hw->chip_ops->chip_mc_read(hw, 0, pos,
847cc16380SArvind Bhushan data, NULL);
85a3667aaeSNaresh Kumar Inna else
867cc16380SArvind Bhushan ret = hw->chip_ops->chip_edc_read(hw, mem, pos,
877cc16380SArvind Bhushan data, NULL);
88a3667aaeSNaresh Kumar Inna if (ret)
89a3667aaeSNaresh Kumar Inna return ret;
90a3667aaeSNaresh Kumar Inna
91a3667aaeSNaresh Kumar Inna ofst = pos % sizeof(data);
92a3667aaeSNaresh Kumar Inna len = min(count, sizeof(data) - ofst);
93a3667aaeSNaresh Kumar Inna if (copy_to_user(buf, (u8 *)data + ofst, len))
94a3667aaeSNaresh Kumar Inna return -EFAULT;
95a3667aaeSNaresh Kumar Inna
96a3667aaeSNaresh Kumar Inna buf += len;
97a3667aaeSNaresh Kumar Inna pos += len;
98a3667aaeSNaresh Kumar Inna count -= len;
99a3667aaeSNaresh Kumar Inna }
100a3667aaeSNaresh Kumar Inna count = pos - *ppos;
101a3667aaeSNaresh Kumar Inna *ppos = pos;
102a3667aaeSNaresh Kumar Inna return count;
103a3667aaeSNaresh Kumar Inna }
104a3667aaeSNaresh Kumar Inna
105a3667aaeSNaresh Kumar Inna static const struct file_operations csio_mem_debugfs_fops = {
106a3667aaeSNaresh Kumar Inna .owner = THIS_MODULE,
107f07cda8fSWei Yongjun .open = simple_open,
108a3667aaeSNaresh Kumar Inna .read = csio_mem_read,
109a3667aaeSNaresh Kumar Inna .llseek = default_llseek,
110a3667aaeSNaresh Kumar Inna };
111a3667aaeSNaresh Kumar Inna
csio_add_debugfs_mem(struct csio_hw * hw,const char * name,unsigned int idx,unsigned int size_mb)1127cc16380SArvind Bhushan void csio_add_debugfs_mem(struct csio_hw *hw, const char *name,
113a3667aaeSNaresh Kumar Inna unsigned int idx, unsigned int size_mb)
114a3667aaeSNaresh Kumar Inna {
115e59b4e91SDavid Howells debugfs_create_file_size(name, S_IRUSR, hw->debugfs_root,
116e59b4e91SDavid Howells (void *)hw + idx, &csio_mem_debugfs_fops,
117e59b4e91SDavid Howells size_mb << 20);
118a3667aaeSNaresh Kumar Inna }
119a3667aaeSNaresh Kumar Inna
csio_setup_debugfs(struct csio_hw * hw)1206f039790SGreg Kroah-Hartman static int csio_setup_debugfs(struct csio_hw *hw)
121a3667aaeSNaresh Kumar Inna {
122a3667aaeSNaresh Kumar Inna int i;
123a3667aaeSNaresh Kumar Inna
124a3667aaeSNaresh Kumar Inna if (IS_ERR_OR_NULL(hw->debugfs_root))
125a3667aaeSNaresh Kumar Inna return -1;
126a3667aaeSNaresh Kumar Inna
1276559a7e8SHariprasad Shenai i = csio_rd_reg32(hw, MA_TARGET_MEM_ENABLE_A);
1286559a7e8SHariprasad Shenai if (i & EDRAM0_ENABLE_F)
129a3667aaeSNaresh Kumar Inna csio_add_debugfs_mem(hw, "edc0", MEM_EDC0, 5);
1306559a7e8SHariprasad Shenai if (i & EDRAM1_ENABLE_F)
131a3667aaeSNaresh Kumar Inna csio_add_debugfs_mem(hw, "edc1", MEM_EDC1, 5);
1327cc16380SArvind Bhushan
1337cc16380SArvind Bhushan hw->chip_ops->chip_dfs_create_ext_mem(hw);
134a3667aaeSNaresh Kumar Inna return 0;
135a3667aaeSNaresh Kumar Inna }
136a3667aaeSNaresh Kumar Inna
137a3667aaeSNaresh Kumar Inna /*
138a3667aaeSNaresh Kumar Inna * csio_dfs_create - Creates and sets up per-hw debugfs.
139a3667aaeSNaresh Kumar Inna *
140a3667aaeSNaresh Kumar Inna */
141a3667aaeSNaresh Kumar Inna static int
csio_dfs_create(struct csio_hw * hw)142a3667aaeSNaresh Kumar Inna csio_dfs_create(struct csio_hw *hw)
143a3667aaeSNaresh Kumar Inna {
144a3667aaeSNaresh Kumar Inna if (csio_debugfs_root) {
145a3667aaeSNaresh Kumar Inna hw->debugfs_root = debugfs_create_dir(pci_name(hw->pdev),
146a3667aaeSNaresh Kumar Inna csio_debugfs_root);
147a3667aaeSNaresh Kumar Inna csio_setup_debugfs(hw);
148a3667aaeSNaresh Kumar Inna }
149a3667aaeSNaresh Kumar Inna
150a3667aaeSNaresh Kumar Inna return 0;
151a3667aaeSNaresh Kumar Inna }
152a3667aaeSNaresh Kumar Inna
153a3667aaeSNaresh Kumar Inna /*
154a3667aaeSNaresh Kumar Inna * csio_dfs_destroy - Destroys per-hw debugfs.
155a3667aaeSNaresh Kumar Inna */
15664dc4f34SSaurav Girepunje static void
csio_dfs_destroy(struct csio_hw * hw)157a3667aaeSNaresh Kumar Inna csio_dfs_destroy(struct csio_hw *hw)
158a3667aaeSNaresh Kumar Inna {
159a3667aaeSNaresh Kumar Inna debugfs_remove_recursive(hw->debugfs_root);
160a3667aaeSNaresh Kumar Inna }
161a3667aaeSNaresh Kumar Inna
162a3667aaeSNaresh Kumar Inna /*
163a3667aaeSNaresh Kumar Inna * csio_dfs_init - Debug filesystem initialization for the module.
164a3667aaeSNaresh Kumar Inna *
165a3667aaeSNaresh Kumar Inna */
166b6533640SGreg Kroah-Hartman static void
csio_dfs_init(void)167a3667aaeSNaresh Kumar Inna csio_dfs_init(void)
168a3667aaeSNaresh Kumar Inna {
169a3667aaeSNaresh Kumar Inna csio_debugfs_root = debugfs_create_dir(KBUILD_MODNAME, NULL);
170a3667aaeSNaresh Kumar Inna }
171a3667aaeSNaresh Kumar Inna
172a3667aaeSNaresh Kumar Inna /*
173a3667aaeSNaresh Kumar Inna * csio_dfs_exit - debugfs cleanup for the module.
174a3667aaeSNaresh Kumar Inna */
175a3667aaeSNaresh Kumar Inna static void
csio_dfs_exit(void)176a3667aaeSNaresh Kumar Inna csio_dfs_exit(void)
177a3667aaeSNaresh Kumar Inna {
178a3667aaeSNaresh Kumar Inna debugfs_remove(csio_debugfs_root);
179a3667aaeSNaresh Kumar Inna }
180a3667aaeSNaresh Kumar Inna
181a3667aaeSNaresh Kumar Inna /*
182a3667aaeSNaresh Kumar Inna * csio_pci_init - PCI initialization.
183a3667aaeSNaresh Kumar Inna * @pdev: PCI device.
184a3667aaeSNaresh Kumar Inna * @bars: Bitmask of bars to be requested.
185a3667aaeSNaresh Kumar Inna *
186a3667aaeSNaresh Kumar Inna * Initializes the PCI function by enabling MMIO, setting bus
187a3667aaeSNaresh Kumar Inna * mastership and setting DMA mask.
188a3667aaeSNaresh Kumar Inna */
189a3667aaeSNaresh Kumar Inna static int
csio_pci_init(struct pci_dev * pdev,int * bars)190a3667aaeSNaresh Kumar Inna csio_pci_init(struct pci_dev *pdev, int *bars)
191a3667aaeSNaresh Kumar Inna {
192a3667aaeSNaresh Kumar Inna int rv = -ENODEV;
193a3667aaeSNaresh Kumar Inna
194a3667aaeSNaresh Kumar Inna *bars = pci_select_bars(pdev, IORESOURCE_MEM);
195a3667aaeSNaresh Kumar Inna
196a3667aaeSNaresh Kumar Inna if (pci_enable_device_mem(pdev))
197a3667aaeSNaresh Kumar Inna goto err;
198a3667aaeSNaresh Kumar Inna
199a3667aaeSNaresh Kumar Inna if (pci_request_selected_regions(pdev, *bars, KBUILD_MODNAME))
200a3667aaeSNaresh Kumar Inna goto err_disable_device;
201a3667aaeSNaresh Kumar Inna
202a3667aaeSNaresh Kumar Inna pci_set_master(pdev);
203a3667aaeSNaresh Kumar Inna pci_try_set_mwi(pdev);
204a3667aaeSNaresh Kumar Inna
205732f3238SHannes Reinecke rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
206732f3238SHannes Reinecke if (rv)
207732f3238SHannes Reinecke rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
208732f3238SHannes Reinecke if (rv) {
209732f3238SHannes Reinecke rv = -ENODEV;
210a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "No suitable DMA available.\n");
211a3667aaeSNaresh Kumar Inna goto err_release_regions;
212a3667aaeSNaresh Kumar Inna }
213a3667aaeSNaresh Kumar Inna
214a3667aaeSNaresh Kumar Inna return 0;
215a3667aaeSNaresh Kumar Inna
216a3667aaeSNaresh Kumar Inna err_release_regions:
217a3667aaeSNaresh Kumar Inna pci_release_selected_regions(pdev, *bars);
218a3667aaeSNaresh Kumar Inna err_disable_device:
219a3667aaeSNaresh Kumar Inna pci_disable_device(pdev);
220a3667aaeSNaresh Kumar Inna err:
221a3667aaeSNaresh Kumar Inna return rv;
222a3667aaeSNaresh Kumar Inna
223a3667aaeSNaresh Kumar Inna }
224a3667aaeSNaresh Kumar Inna
225a3667aaeSNaresh Kumar Inna /*
226a3667aaeSNaresh Kumar Inna * csio_pci_exit - PCI unitialization.
227a3667aaeSNaresh Kumar Inna * @pdev: PCI device.
228a3667aaeSNaresh Kumar Inna * @bars: Bars to be released.
229a3667aaeSNaresh Kumar Inna *
230a3667aaeSNaresh Kumar Inna */
231a3667aaeSNaresh Kumar Inna static void
csio_pci_exit(struct pci_dev * pdev,int * bars)232a3667aaeSNaresh Kumar Inna csio_pci_exit(struct pci_dev *pdev, int *bars)
233a3667aaeSNaresh Kumar Inna {
234a3667aaeSNaresh Kumar Inna pci_release_selected_regions(pdev, *bars);
235a3667aaeSNaresh Kumar Inna pci_disable_device(pdev);
236a3667aaeSNaresh Kumar Inna }
237a3667aaeSNaresh Kumar Inna
238a3667aaeSNaresh Kumar Inna /*
239a3667aaeSNaresh Kumar Inna * csio_hw_init_workers - Initialize the HW module's worker threads.
240a3667aaeSNaresh Kumar Inna * @hw: HW module.
241a3667aaeSNaresh Kumar Inna *
242a3667aaeSNaresh Kumar Inna */
243a3667aaeSNaresh Kumar Inna static void
csio_hw_init_workers(struct csio_hw * hw)244a3667aaeSNaresh Kumar Inna csio_hw_init_workers(struct csio_hw *hw)
245a3667aaeSNaresh Kumar Inna {
246a3667aaeSNaresh Kumar Inna INIT_WORK(&hw->evtq_work, csio_evtq_worker);
247a3667aaeSNaresh Kumar Inna }
248a3667aaeSNaresh Kumar Inna
249a3667aaeSNaresh Kumar Inna static void
csio_hw_exit_workers(struct csio_hw * hw)250a3667aaeSNaresh Kumar Inna csio_hw_exit_workers(struct csio_hw *hw)
251a3667aaeSNaresh Kumar Inna {
252a3667aaeSNaresh Kumar Inna cancel_work_sync(&hw->evtq_work);
253a3667aaeSNaresh Kumar Inna }
254a3667aaeSNaresh Kumar Inna
255a3667aaeSNaresh Kumar Inna static int
csio_create_queues(struct csio_hw * hw)256a3667aaeSNaresh Kumar Inna csio_create_queues(struct csio_hw *hw)
257a3667aaeSNaresh Kumar Inna {
258a3667aaeSNaresh Kumar Inna int i, j;
259a3667aaeSNaresh Kumar Inna struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
260a3667aaeSNaresh Kumar Inna int rv;
261a3667aaeSNaresh Kumar Inna struct csio_scsi_cpu_info *info;
262a3667aaeSNaresh Kumar Inna
263a3667aaeSNaresh Kumar Inna if (hw->flags & CSIO_HWF_Q_FW_ALLOCED)
264a3667aaeSNaresh Kumar Inna return 0;
265a3667aaeSNaresh Kumar Inna
266a3667aaeSNaresh Kumar Inna if (hw->intr_mode != CSIO_IM_MSIX) {
267a3667aaeSNaresh Kumar Inna rv = csio_wr_iq_create(hw, NULL, hw->intr_iq_idx,
268a3667aaeSNaresh Kumar Inna 0, hw->pport[0].portid, false, NULL);
269a3667aaeSNaresh Kumar Inna if (rv != 0) {
270a3667aaeSNaresh Kumar Inna csio_err(hw, " Forward Interrupt IQ failed!: %d\n", rv);
271a3667aaeSNaresh Kumar Inna return rv;
272a3667aaeSNaresh Kumar Inna }
273a3667aaeSNaresh Kumar Inna }
274a3667aaeSNaresh Kumar Inna
275a3667aaeSNaresh Kumar Inna /* FW event queue */
276a3667aaeSNaresh Kumar Inna rv = csio_wr_iq_create(hw, NULL, hw->fwevt_iq_idx,
277a3667aaeSNaresh Kumar Inna csio_get_fwevt_intr_idx(hw),
278a3667aaeSNaresh Kumar Inna hw->pport[0].portid, true, NULL);
279a3667aaeSNaresh Kumar Inna if (rv != 0) {
280a3667aaeSNaresh Kumar Inna csio_err(hw, "FW event IQ config failed!: %d\n", rv);
281a3667aaeSNaresh Kumar Inna return rv;
282a3667aaeSNaresh Kumar Inna }
283a3667aaeSNaresh Kumar Inna
284a3667aaeSNaresh Kumar Inna /* Create mgmt queue */
285a3667aaeSNaresh Kumar Inna rv = csio_wr_eq_create(hw, NULL, mgmtm->eq_idx,
286a3667aaeSNaresh Kumar Inna mgmtm->iq_idx, hw->pport[0].portid, NULL);
287a3667aaeSNaresh Kumar Inna
288a3667aaeSNaresh Kumar Inna if (rv != 0) {
289a3667aaeSNaresh Kumar Inna csio_err(hw, "Mgmt EQ create failed!: %d\n", rv);
290a3667aaeSNaresh Kumar Inna goto err;
291a3667aaeSNaresh Kumar Inna }
292a3667aaeSNaresh Kumar Inna
293a3667aaeSNaresh Kumar Inna /* Create SCSI queues */
294a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
295a3667aaeSNaresh Kumar Inna info = &hw->scsi_cpu_info[i];
296a3667aaeSNaresh Kumar Inna
297a3667aaeSNaresh Kumar Inna for (j = 0; j < info->max_cpus; j++) {
298a3667aaeSNaresh Kumar Inna struct csio_scsi_qset *sqset = &hw->sqset[i][j];
299a3667aaeSNaresh Kumar Inna
300a3667aaeSNaresh Kumar Inna rv = csio_wr_iq_create(hw, NULL, sqset->iq_idx,
301a3667aaeSNaresh Kumar Inna sqset->intr_idx, i, false, NULL);
302a3667aaeSNaresh Kumar Inna if (rv != 0) {
303a3667aaeSNaresh Kumar Inna csio_err(hw,
304a3667aaeSNaresh Kumar Inna "SCSI module IQ config failed [%d][%d]:%d\n",
305a3667aaeSNaresh Kumar Inna i, j, rv);
306a3667aaeSNaresh Kumar Inna goto err;
307a3667aaeSNaresh Kumar Inna }
308a3667aaeSNaresh Kumar Inna rv = csio_wr_eq_create(hw, NULL, sqset->eq_idx,
309a3667aaeSNaresh Kumar Inna sqset->iq_idx, i, NULL);
310a3667aaeSNaresh Kumar Inna if (rv != 0) {
311a3667aaeSNaresh Kumar Inna csio_err(hw,
312a3667aaeSNaresh Kumar Inna "SCSI module EQ config failed [%d][%d]:%d\n",
313a3667aaeSNaresh Kumar Inna i, j, rv);
314a3667aaeSNaresh Kumar Inna goto err;
315a3667aaeSNaresh Kumar Inna }
316a3667aaeSNaresh Kumar Inna } /* for all CPUs */
317a3667aaeSNaresh Kumar Inna } /* For all ports */
318a3667aaeSNaresh Kumar Inna
319a3667aaeSNaresh Kumar Inna hw->flags |= CSIO_HWF_Q_FW_ALLOCED;
320a3667aaeSNaresh Kumar Inna return 0;
321a3667aaeSNaresh Kumar Inna err:
322a3667aaeSNaresh Kumar Inna csio_wr_destroy_queues(hw, true);
323a3667aaeSNaresh Kumar Inna return -EINVAL;
324a3667aaeSNaresh Kumar Inna }
325a3667aaeSNaresh Kumar Inna
326a3667aaeSNaresh Kumar Inna /*
327a3667aaeSNaresh Kumar Inna * csio_config_queues - Configure the DMA queues.
328a3667aaeSNaresh Kumar Inna * @hw: HW module.
329a3667aaeSNaresh Kumar Inna *
330a3667aaeSNaresh Kumar Inna * Allocates memory for queues are registers them with FW.
331a3667aaeSNaresh Kumar Inna */
332a3667aaeSNaresh Kumar Inna int
csio_config_queues(struct csio_hw * hw)333a3667aaeSNaresh Kumar Inna csio_config_queues(struct csio_hw *hw)
334a3667aaeSNaresh Kumar Inna {
335a3667aaeSNaresh Kumar Inna int i, j, idx, k = 0;
336a3667aaeSNaresh Kumar Inna int rv;
337a3667aaeSNaresh Kumar Inna struct csio_scsi_qset *sqset;
338a3667aaeSNaresh Kumar Inna struct csio_mgmtm *mgmtm = csio_hw_to_mgmtm(hw);
339a3667aaeSNaresh Kumar Inna struct csio_scsi_qset *orig;
340a3667aaeSNaresh Kumar Inna struct csio_scsi_cpu_info *info;
341a3667aaeSNaresh Kumar Inna
342a3667aaeSNaresh Kumar Inna if (hw->flags & CSIO_HWF_Q_MEM_ALLOCED)
343a3667aaeSNaresh Kumar Inna return csio_create_queues(hw);
344a3667aaeSNaresh Kumar Inna
345a3667aaeSNaresh Kumar Inna /* Calculate number of SCSI queues for MSIX we would like */
346a3667aaeSNaresh Kumar Inna hw->num_scsi_msix_cpus = num_online_cpus();
347a3667aaeSNaresh Kumar Inna hw->num_sqsets = num_online_cpus() * hw->num_pports;
348a3667aaeSNaresh Kumar Inna
349a3667aaeSNaresh Kumar Inna if (hw->num_sqsets > CSIO_MAX_SCSI_QSETS) {
350a3667aaeSNaresh Kumar Inna hw->num_sqsets = CSIO_MAX_SCSI_QSETS;
351a3667aaeSNaresh Kumar Inna hw->num_scsi_msix_cpus = CSIO_MAX_SCSI_CPU;
352a3667aaeSNaresh Kumar Inna }
353a3667aaeSNaresh Kumar Inna
354a3667aaeSNaresh Kumar Inna /* Initialize max_cpus, may get reduced during msix allocations */
355a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++)
356a3667aaeSNaresh Kumar Inna hw->scsi_cpu_info[i].max_cpus = hw->num_scsi_msix_cpus;
357a3667aaeSNaresh Kumar Inna
358a3667aaeSNaresh Kumar Inna csio_dbg(hw, "nsqsets:%d scpus:%d\n",
359a3667aaeSNaresh Kumar Inna hw->num_sqsets, hw->num_scsi_msix_cpus);
360a3667aaeSNaresh Kumar Inna
361a3667aaeSNaresh Kumar Inna csio_intr_enable(hw);
362a3667aaeSNaresh Kumar Inna
363a3667aaeSNaresh Kumar Inna if (hw->intr_mode != CSIO_IM_MSIX) {
364a3667aaeSNaresh Kumar Inna
365a3667aaeSNaresh Kumar Inna /* Allocate Forward interrupt iq. */
366a3667aaeSNaresh Kumar Inna hw->intr_iq_idx = csio_wr_alloc_q(hw, CSIO_INTR_IQSIZE,
367a3667aaeSNaresh Kumar Inna CSIO_INTR_WRSIZE, CSIO_INGRESS,
368a3667aaeSNaresh Kumar Inna (void *)hw, 0, 0, NULL);
369a3667aaeSNaresh Kumar Inna if (hw->intr_iq_idx == -1) {
370a3667aaeSNaresh Kumar Inna csio_err(hw,
371a3667aaeSNaresh Kumar Inna "Forward interrupt queue creation failed\n");
372a3667aaeSNaresh Kumar Inna goto intr_disable;
373a3667aaeSNaresh Kumar Inna }
374a3667aaeSNaresh Kumar Inna }
375a3667aaeSNaresh Kumar Inna
376a3667aaeSNaresh Kumar Inna /* Allocate the FW evt queue */
377a3667aaeSNaresh Kumar Inna hw->fwevt_iq_idx = csio_wr_alloc_q(hw, CSIO_FWEVT_IQSIZE,
378a3667aaeSNaresh Kumar Inna CSIO_FWEVT_WRSIZE,
379a3667aaeSNaresh Kumar Inna CSIO_INGRESS, (void *)hw,
380a3667aaeSNaresh Kumar Inna CSIO_FWEVT_FLBUFS, 0,
381a3667aaeSNaresh Kumar Inna csio_fwevt_intx_handler);
382a3667aaeSNaresh Kumar Inna if (hw->fwevt_iq_idx == -1) {
383a3667aaeSNaresh Kumar Inna csio_err(hw, "FW evt queue creation failed\n");
384a3667aaeSNaresh Kumar Inna goto intr_disable;
385a3667aaeSNaresh Kumar Inna }
386a3667aaeSNaresh Kumar Inna
387a3667aaeSNaresh Kumar Inna /* Allocate the mgmt queue */
388a3667aaeSNaresh Kumar Inna mgmtm->eq_idx = csio_wr_alloc_q(hw, CSIO_MGMT_EQSIZE,
389a3667aaeSNaresh Kumar Inna CSIO_MGMT_EQ_WRSIZE,
390a3667aaeSNaresh Kumar Inna CSIO_EGRESS, (void *)hw, 0, 0, NULL);
391a3667aaeSNaresh Kumar Inna if (mgmtm->eq_idx == -1) {
392a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to alloc egress queue for mgmt module\n");
393a3667aaeSNaresh Kumar Inna goto intr_disable;
394a3667aaeSNaresh Kumar Inna }
395a3667aaeSNaresh Kumar Inna
396a3667aaeSNaresh Kumar Inna /* Use FW IQ for MGMT req completion */
397a3667aaeSNaresh Kumar Inna mgmtm->iq_idx = hw->fwevt_iq_idx;
398a3667aaeSNaresh Kumar Inna
399a3667aaeSNaresh Kumar Inna /* Allocate SCSI queues */
400a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
401a3667aaeSNaresh Kumar Inna info = &hw->scsi_cpu_info[i];
402a3667aaeSNaresh Kumar Inna
403a3667aaeSNaresh Kumar Inna for (j = 0; j < hw->num_scsi_msix_cpus; j++) {
404a3667aaeSNaresh Kumar Inna sqset = &hw->sqset[i][j];
405a3667aaeSNaresh Kumar Inna
406a3667aaeSNaresh Kumar Inna if (j >= info->max_cpus) {
407a3667aaeSNaresh Kumar Inna k = j % info->max_cpus;
408a3667aaeSNaresh Kumar Inna orig = &hw->sqset[i][k];
409a3667aaeSNaresh Kumar Inna sqset->eq_idx = orig->eq_idx;
410a3667aaeSNaresh Kumar Inna sqset->iq_idx = orig->iq_idx;
411a3667aaeSNaresh Kumar Inna continue;
412a3667aaeSNaresh Kumar Inna }
413a3667aaeSNaresh Kumar Inna
414a3667aaeSNaresh Kumar Inna idx = csio_wr_alloc_q(hw, csio_scsi_eqsize, 0,
415a3667aaeSNaresh Kumar Inna CSIO_EGRESS, (void *)hw, 0, 0,
416a3667aaeSNaresh Kumar Inna NULL);
417a3667aaeSNaresh Kumar Inna if (idx == -1) {
418a3667aaeSNaresh Kumar Inna csio_err(hw, "EQ creation failed for idx:%d\n",
419a3667aaeSNaresh Kumar Inna idx);
420a3667aaeSNaresh Kumar Inna goto intr_disable;
421a3667aaeSNaresh Kumar Inna }
422a3667aaeSNaresh Kumar Inna
423a3667aaeSNaresh Kumar Inna sqset->eq_idx = idx;
424a3667aaeSNaresh Kumar Inna
425a3667aaeSNaresh Kumar Inna idx = csio_wr_alloc_q(hw, CSIO_SCSI_IQSIZE,
426a3667aaeSNaresh Kumar Inna CSIO_SCSI_IQ_WRSZ, CSIO_INGRESS,
427a3667aaeSNaresh Kumar Inna (void *)hw, 0, 0,
428a3667aaeSNaresh Kumar Inna csio_scsi_intx_handler);
429a3667aaeSNaresh Kumar Inna if (idx == -1) {
430a3667aaeSNaresh Kumar Inna csio_err(hw, "IQ creation failed for idx:%d\n",
431a3667aaeSNaresh Kumar Inna idx);
432a3667aaeSNaresh Kumar Inna goto intr_disable;
433a3667aaeSNaresh Kumar Inna }
434a3667aaeSNaresh Kumar Inna sqset->iq_idx = idx;
435a3667aaeSNaresh Kumar Inna } /* for all CPUs */
436a3667aaeSNaresh Kumar Inna } /* For all ports */
437a3667aaeSNaresh Kumar Inna
438a3667aaeSNaresh Kumar Inna hw->flags |= CSIO_HWF_Q_MEM_ALLOCED;
439a3667aaeSNaresh Kumar Inna
440a3667aaeSNaresh Kumar Inna rv = csio_create_queues(hw);
441a3667aaeSNaresh Kumar Inna if (rv != 0)
442a3667aaeSNaresh Kumar Inna goto intr_disable;
443a3667aaeSNaresh Kumar Inna
444a3667aaeSNaresh Kumar Inna /*
445a3667aaeSNaresh Kumar Inna * Now request IRQs for the vectors. In the event of a failure,
446a3667aaeSNaresh Kumar Inna * cleanup is handled internally by this function.
447a3667aaeSNaresh Kumar Inna */
448a3667aaeSNaresh Kumar Inna rv = csio_request_irqs(hw);
449a3667aaeSNaresh Kumar Inna if (rv != 0)
450a3667aaeSNaresh Kumar Inna return -EINVAL;
451a3667aaeSNaresh Kumar Inna
452a3667aaeSNaresh Kumar Inna return 0;
453a3667aaeSNaresh Kumar Inna
454a3667aaeSNaresh Kumar Inna intr_disable:
455a3667aaeSNaresh Kumar Inna csio_intr_disable(hw, false);
456a3667aaeSNaresh Kumar Inna
457a3667aaeSNaresh Kumar Inna return -EINVAL;
458a3667aaeSNaresh Kumar Inna }
459a3667aaeSNaresh Kumar Inna
460a3667aaeSNaresh Kumar Inna static int
csio_resource_alloc(struct csio_hw * hw)461a3667aaeSNaresh Kumar Inna csio_resource_alloc(struct csio_hw *hw)
462a3667aaeSNaresh Kumar Inna {
463a3667aaeSNaresh Kumar Inna struct csio_wrm *wrm = csio_hw_to_wrm(hw);
464a3667aaeSNaresh Kumar Inna int rv = -ENOMEM;
465a3667aaeSNaresh Kumar Inna
466a3667aaeSNaresh Kumar Inna wrm->num_q = ((CSIO_MAX_SCSI_QSETS * 2) + CSIO_HW_NIQ +
467a3667aaeSNaresh Kumar Inna CSIO_HW_NEQ + CSIO_HW_NFLQ + CSIO_HW_NINTXQ);
468a3667aaeSNaresh Kumar Inna
469a3667aaeSNaresh Kumar Inna hw->mb_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
470a3667aaeSNaresh Kumar Inna sizeof(struct csio_mb));
471a3667aaeSNaresh Kumar Inna if (!hw->mb_mempool)
472a3667aaeSNaresh Kumar Inna goto err;
473a3667aaeSNaresh Kumar Inna
474a3667aaeSNaresh Kumar Inna hw->rnode_mempool = mempool_create_kmalloc_pool(CSIO_MIN_MEMPOOL_SZ,
475a3667aaeSNaresh Kumar Inna sizeof(struct csio_rnode));
476a3667aaeSNaresh Kumar Inna if (!hw->rnode_mempool)
477a3667aaeSNaresh Kumar Inna goto err_free_mb_mempool;
478a3667aaeSNaresh Kumar Inna
479decab9a6SRomain Perier hw->scsi_dma_pool = dma_pool_create("csio_scsi_dma_pool",
480decab9a6SRomain Perier &hw->pdev->dev, CSIO_SCSI_RSP_LEN,
481decab9a6SRomain Perier 8, 0);
482decab9a6SRomain Perier if (!hw->scsi_dma_pool)
483a3667aaeSNaresh Kumar Inna goto err_free_rn_pool;
484a3667aaeSNaresh Kumar Inna
485a3667aaeSNaresh Kumar Inna return 0;
486a3667aaeSNaresh Kumar Inna
487a3667aaeSNaresh Kumar Inna err_free_rn_pool:
488a3667aaeSNaresh Kumar Inna mempool_destroy(hw->rnode_mempool);
489a3667aaeSNaresh Kumar Inna hw->rnode_mempool = NULL;
490a3667aaeSNaresh Kumar Inna err_free_mb_mempool:
491a3667aaeSNaresh Kumar Inna mempool_destroy(hw->mb_mempool);
492a3667aaeSNaresh Kumar Inna hw->mb_mempool = NULL;
493a3667aaeSNaresh Kumar Inna err:
494a3667aaeSNaresh Kumar Inna return rv;
495a3667aaeSNaresh Kumar Inna }
496a3667aaeSNaresh Kumar Inna
497a3667aaeSNaresh Kumar Inna static void
csio_resource_free(struct csio_hw * hw)498a3667aaeSNaresh Kumar Inna csio_resource_free(struct csio_hw *hw)
499a3667aaeSNaresh Kumar Inna {
500decab9a6SRomain Perier dma_pool_destroy(hw->scsi_dma_pool);
501decab9a6SRomain Perier hw->scsi_dma_pool = NULL;
502a3667aaeSNaresh Kumar Inna mempool_destroy(hw->rnode_mempool);
503a3667aaeSNaresh Kumar Inna hw->rnode_mempool = NULL;
504a3667aaeSNaresh Kumar Inna mempool_destroy(hw->mb_mempool);
505a3667aaeSNaresh Kumar Inna hw->mb_mempool = NULL;
506a3667aaeSNaresh Kumar Inna }
507a3667aaeSNaresh Kumar Inna
508a3667aaeSNaresh Kumar Inna /*
509a3667aaeSNaresh Kumar Inna * csio_hw_alloc - Allocate and initialize the HW module.
510a3667aaeSNaresh Kumar Inna * @pdev: PCI device.
511a3667aaeSNaresh Kumar Inna *
512a3667aaeSNaresh Kumar Inna * Allocates HW structure, DMA, memory resources, maps BARS to
513a3667aaeSNaresh Kumar Inna * host memory and initializes HW module.
514a3667aaeSNaresh Kumar Inna */
csio_hw_alloc(struct pci_dev * pdev)5156f039790SGreg Kroah-Hartman static struct csio_hw *csio_hw_alloc(struct pci_dev *pdev)
516a3667aaeSNaresh Kumar Inna {
517a3667aaeSNaresh Kumar Inna struct csio_hw *hw;
518a3667aaeSNaresh Kumar Inna
519a3667aaeSNaresh Kumar Inna hw = kzalloc(sizeof(struct csio_hw), GFP_KERNEL);
520a3667aaeSNaresh Kumar Inna if (!hw)
521a3667aaeSNaresh Kumar Inna goto err;
522a3667aaeSNaresh Kumar Inna
523a3667aaeSNaresh Kumar Inna hw->pdev = pdev;
524a3667aaeSNaresh Kumar Inna strncpy(hw->drv_version, CSIO_DRV_VERSION, 32);
525a3667aaeSNaresh Kumar Inna
526a3667aaeSNaresh Kumar Inna /* memory pool/DMA pool allocation */
527a3667aaeSNaresh Kumar Inna if (csio_resource_alloc(hw))
528a3667aaeSNaresh Kumar Inna goto err_free_hw;
529a3667aaeSNaresh Kumar Inna
530a3667aaeSNaresh Kumar Inna /* Get the start address of registers from BAR 0 */
5314bdc0d67SChristoph Hellwig hw->regstart = ioremap(pci_resource_start(pdev, 0),
532a3667aaeSNaresh Kumar Inna pci_resource_len(pdev, 0));
533a3667aaeSNaresh Kumar Inna if (!hw->regstart) {
534a3667aaeSNaresh Kumar Inna csio_err(hw, "Could not map BAR 0, regstart = %p\n",
535a3667aaeSNaresh Kumar Inna hw->regstart);
536a3667aaeSNaresh Kumar Inna goto err_resource_free;
537a3667aaeSNaresh Kumar Inna }
538a3667aaeSNaresh Kumar Inna
539a3667aaeSNaresh Kumar Inna csio_hw_init_workers(hw);
540a3667aaeSNaresh Kumar Inna
541a3667aaeSNaresh Kumar Inna if (csio_hw_init(hw))
542a3667aaeSNaresh Kumar Inna goto err_unmap_bar;
543a3667aaeSNaresh Kumar Inna
544a3667aaeSNaresh Kumar Inna csio_dfs_create(hw);
545a3667aaeSNaresh Kumar Inna
546a3667aaeSNaresh Kumar Inna csio_dbg(hw, "hw:%p\n", hw);
547a3667aaeSNaresh Kumar Inna
548a3667aaeSNaresh Kumar Inna return hw;
549a3667aaeSNaresh Kumar Inna
550a3667aaeSNaresh Kumar Inna err_unmap_bar:
551a3667aaeSNaresh Kumar Inna csio_hw_exit_workers(hw);
552a3667aaeSNaresh Kumar Inna iounmap(hw->regstart);
553a3667aaeSNaresh Kumar Inna err_resource_free:
554a3667aaeSNaresh Kumar Inna csio_resource_free(hw);
555a3667aaeSNaresh Kumar Inna err_free_hw:
556a3667aaeSNaresh Kumar Inna kfree(hw);
557a3667aaeSNaresh Kumar Inna err:
558a3667aaeSNaresh Kumar Inna return NULL;
559a3667aaeSNaresh Kumar Inna }
560a3667aaeSNaresh Kumar Inna
561a3667aaeSNaresh Kumar Inna /*
562a3667aaeSNaresh Kumar Inna * csio_hw_free - Uninitialize and free the HW module.
563a3667aaeSNaresh Kumar Inna * @hw: The HW module
564a3667aaeSNaresh Kumar Inna *
565a3667aaeSNaresh Kumar Inna * Disable interrupts, uninit the HW module, free resources, free hw.
566a3667aaeSNaresh Kumar Inna */
567a3667aaeSNaresh Kumar Inna static void
csio_hw_free(struct csio_hw * hw)568a3667aaeSNaresh Kumar Inna csio_hw_free(struct csio_hw *hw)
569a3667aaeSNaresh Kumar Inna {
570a3667aaeSNaresh Kumar Inna csio_intr_disable(hw, true);
571a3667aaeSNaresh Kumar Inna csio_hw_exit_workers(hw);
572a3667aaeSNaresh Kumar Inna csio_hw_exit(hw);
573a3667aaeSNaresh Kumar Inna iounmap(hw->regstart);
574a3667aaeSNaresh Kumar Inna csio_dfs_destroy(hw);
575a3667aaeSNaresh Kumar Inna csio_resource_free(hw);
576a3667aaeSNaresh Kumar Inna kfree(hw);
577a3667aaeSNaresh Kumar Inna }
578a3667aaeSNaresh Kumar Inna
579a3667aaeSNaresh Kumar Inna /**
580a3667aaeSNaresh Kumar Inna * csio_shost_init - Create and initialize the lnode module.
581a3667aaeSNaresh Kumar Inna * @hw: The HW module.
582a3667aaeSNaresh Kumar Inna * @dev: The device associated with this invocation.
583a3667aaeSNaresh Kumar Inna * @probe: Called from probe context or not?
5845446a91dSLee Jones * @pln: Parent lnode if any.
585a3667aaeSNaresh Kumar Inna *
586a3667aaeSNaresh Kumar Inna * Allocates lnode structure via scsi_host_alloc, initializes
587a3667aaeSNaresh Kumar Inna * shost, initializes lnode module and registers with SCSI ML
588a3667aaeSNaresh Kumar Inna * via scsi_host_add. This function is shared between physical and
589a3667aaeSNaresh Kumar Inna * virtual node ports.
590a3667aaeSNaresh Kumar Inna */
591a3667aaeSNaresh Kumar Inna struct csio_lnode *
csio_shost_init(struct csio_hw * hw,struct device * dev,bool probe,struct csio_lnode * pln)592a3667aaeSNaresh Kumar Inna csio_shost_init(struct csio_hw *hw, struct device *dev,
593a3667aaeSNaresh Kumar Inna bool probe, struct csio_lnode *pln)
594a3667aaeSNaresh Kumar Inna {
595a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = NULL;
596a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
597a3667aaeSNaresh Kumar Inna
598a3667aaeSNaresh Kumar Inna csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
599a3667aaeSNaresh Kumar Inna csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
600a3667aaeSNaresh Kumar Inna
601a3667aaeSNaresh Kumar Inna /*
602a3667aaeSNaresh Kumar Inna * hw->pdev is the physical port's PCI dev structure,
603a3667aaeSNaresh Kumar Inna * which will be different from the NPIV dev structure.
604a3667aaeSNaresh Kumar Inna */
605a3667aaeSNaresh Kumar Inna if (dev == &hw->pdev->dev)
606a3667aaeSNaresh Kumar Inna shost = scsi_host_alloc(
607a3667aaeSNaresh Kumar Inna &csio_fcoe_shost_template,
608a3667aaeSNaresh Kumar Inna sizeof(struct csio_lnode));
609a3667aaeSNaresh Kumar Inna else
610a3667aaeSNaresh Kumar Inna shost = scsi_host_alloc(
611a3667aaeSNaresh Kumar Inna &csio_fcoe_shost_vport_template,
612a3667aaeSNaresh Kumar Inna sizeof(struct csio_lnode));
613a3667aaeSNaresh Kumar Inna
614a3667aaeSNaresh Kumar Inna if (!shost)
615a3667aaeSNaresh Kumar Inna goto err;
616a3667aaeSNaresh Kumar Inna
617a3667aaeSNaresh Kumar Inna ln = shost_priv(shost);
618a3667aaeSNaresh Kumar Inna memset(ln, 0, sizeof(struct csio_lnode));
619a3667aaeSNaresh Kumar Inna
620a3667aaeSNaresh Kumar Inna /* Link common lnode to this lnode */
621a3667aaeSNaresh Kumar Inna ln->dev_num = (shost->host_no << 16);
622a3667aaeSNaresh Kumar Inna
623a3667aaeSNaresh Kumar Inna shost->can_queue = CSIO_MAX_QUEUE;
624a3667aaeSNaresh Kumar Inna shost->this_id = -1;
625a3667aaeSNaresh Kumar Inna shost->unique_id = shost->host_no;
626a3667aaeSNaresh Kumar Inna shost->max_cmd_len = 16; /* Max CDB length supported */
627a3667aaeSNaresh Kumar Inna shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
628a3667aaeSNaresh Kumar Inna hw->fres_info.max_ssns);
629a3667aaeSNaresh Kumar Inna shost->max_lun = CSIO_MAX_LUN;
630a3667aaeSNaresh Kumar Inna if (dev == &hw->pdev->dev)
631a3667aaeSNaresh Kumar Inna shost->transportt = csio_fcoe_transport;
632a3667aaeSNaresh Kumar Inna else
633a3667aaeSNaresh Kumar Inna shost->transportt = csio_fcoe_transport_vport;
634a3667aaeSNaresh Kumar Inna
635a3667aaeSNaresh Kumar Inna /* root lnode */
636a3667aaeSNaresh Kumar Inna if (!hw->rln)
637a3667aaeSNaresh Kumar Inna hw->rln = ln;
638a3667aaeSNaresh Kumar Inna
639a3667aaeSNaresh Kumar Inna /* Other initialization here: Common, Transport specific */
640a3667aaeSNaresh Kumar Inna if (csio_lnode_init(ln, hw, pln))
641a3667aaeSNaresh Kumar Inna goto err_shost_put;
642a3667aaeSNaresh Kumar Inna
6439934613eSVarun Prakash if (scsi_add_host_with_dma(shost, dev, &hw->pdev->dev))
644a3667aaeSNaresh Kumar Inna goto err_lnode_exit;
645a3667aaeSNaresh Kumar Inna
646a3667aaeSNaresh Kumar Inna return ln;
647a3667aaeSNaresh Kumar Inna
648a3667aaeSNaresh Kumar Inna err_lnode_exit:
649a3667aaeSNaresh Kumar Inna csio_lnode_exit(ln);
650a3667aaeSNaresh Kumar Inna err_shost_put:
651a3667aaeSNaresh Kumar Inna scsi_host_put(shost);
652a3667aaeSNaresh Kumar Inna err:
653a3667aaeSNaresh Kumar Inna return NULL;
654a3667aaeSNaresh Kumar Inna }
655a3667aaeSNaresh Kumar Inna
656a3667aaeSNaresh Kumar Inna /**
657a3667aaeSNaresh Kumar Inna * csio_shost_exit - De-instantiate the shost.
658a3667aaeSNaresh Kumar Inna * @ln: The lnode module corresponding to the shost.
659a3667aaeSNaresh Kumar Inna *
660a3667aaeSNaresh Kumar Inna */
661a3667aaeSNaresh Kumar Inna void
csio_shost_exit(struct csio_lnode * ln)662a3667aaeSNaresh Kumar Inna csio_shost_exit(struct csio_lnode *ln)
663a3667aaeSNaresh Kumar Inna {
664a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = csio_ln_to_shost(ln);
665a3667aaeSNaresh Kumar Inna struct csio_hw *hw = csio_lnode_to_hw(ln);
666a3667aaeSNaresh Kumar Inna
667a3667aaeSNaresh Kumar Inna /* Inform transport */
668a3667aaeSNaresh Kumar Inna fc_remove_host(shost);
669a3667aaeSNaresh Kumar Inna
670a3667aaeSNaresh Kumar Inna /* Inform SCSI ML */
671a3667aaeSNaresh Kumar Inna scsi_remove_host(shost);
672a3667aaeSNaresh Kumar Inna
673a3667aaeSNaresh Kumar Inna /* Flush all the events, so that any rnode removal events
674a3667aaeSNaresh Kumar Inna * already queued are all handled, before we remove the lnode.
675a3667aaeSNaresh Kumar Inna */
676a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
677a3667aaeSNaresh Kumar Inna csio_evtq_flush(hw);
678a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
679a3667aaeSNaresh Kumar Inna
680a3667aaeSNaresh Kumar Inna csio_lnode_exit(ln);
681a3667aaeSNaresh Kumar Inna scsi_host_put(shost);
682a3667aaeSNaresh Kumar Inna }
683a3667aaeSNaresh Kumar Inna
684a3667aaeSNaresh Kumar Inna struct csio_lnode *
csio_lnode_alloc(struct csio_hw * hw)685a3667aaeSNaresh Kumar Inna csio_lnode_alloc(struct csio_hw *hw)
686a3667aaeSNaresh Kumar Inna {
687a3667aaeSNaresh Kumar Inna return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
688a3667aaeSNaresh Kumar Inna }
689a3667aaeSNaresh Kumar Inna
690a3667aaeSNaresh Kumar Inna void
csio_lnodes_block_request(struct csio_hw * hw)691a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(struct csio_hw *hw)
692a3667aaeSNaresh Kumar Inna {
693a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
694a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
695a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
696a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
697a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
698a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
699a3667aaeSNaresh Kumar Inna
700a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
701a3667aaeSNaresh Kumar Inna GFP_KERNEL);
702a3667aaeSNaresh Kumar Inna if (!lnode_list) {
703a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
704a3667aaeSNaresh Kumar Inna return;
705a3667aaeSNaresh Kumar Inna }
706a3667aaeSNaresh Kumar Inna
707a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
708a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
709a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
710a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
711a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
712a3667aaeSNaresh Kumar Inna
713a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
714a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
715a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
716a3667aaeSNaresh Kumar Inna }
717a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
718a3667aaeSNaresh Kumar Inna
719a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
720a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
721a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
722a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
723a3667aaeSNaresh Kumar Inna scsi_block_requests(shost);
724a3667aaeSNaresh Kumar Inna
725a3667aaeSNaresh Kumar Inna }
726a3667aaeSNaresh Kumar Inna kfree(lnode_list);
727a3667aaeSNaresh Kumar Inna }
728a3667aaeSNaresh Kumar Inna
729a3667aaeSNaresh Kumar Inna void
csio_lnodes_unblock_request(struct csio_hw * hw)730a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(struct csio_hw *hw)
731a3667aaeSNaresh Kumar Inna {
732a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
733a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
734a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
735a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
736a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
737a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
738a3667aaeSNaresh Kumar Inna
739a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
740a3667aaeSNaresh Kumar Inna GFP_KERNEL);
741a3667aaeSNaresh Kumar Inna if (!lnode_list) {
742a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
743a3667aaeSNaresh Kumar Inna return;
744a3667aaeSNaresh Kumar Inna }
745a3667aaeSNaresh Kumar Inna
746a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
747a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
748a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
749a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
750a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
751a3667aaeSNaresh Kumar Inna
752a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
753a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
754a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
755a3667aaeSNaresh Kumar Inna }
756a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
757a3667aaeSNaresh Kumar Inna
758a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
759a3667aaeSNaresh Kumar Inna csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
760a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
761a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
762a3667aaeSNaresh Kumar Inna scsi_unblock_requests(shost);
763a3667aaeSNaresh Kumar Inna }
764a3667aaeSNaresh Kumar Inna kfree(lnode_list);
765a3667aaeSNaresh Kumar Inna }
766a3667aaeSNaresh Kumar Inna
767a3667aaeSNaresh Kumar Inna void
csio_lnodes_block_by_port(struct csio_hw * hw,uint8_t portid)768a3667aaeSNaresh Kumar Inna csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
769a3667aaeSNaresh Kumar Inna {
770a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
771a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
772a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
773a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
774a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
775a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
776a3667aaeSNaresh Kumar Inna
777a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
778a3667aaeSNaresh Kumar Inna GFP_KERNEL);
779a3667aaeSNaresh Kumar Inna if (!lnode_list) {
780a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
781a3667aaeSNaresh Kumar Inna return;
782a3667aaeSNaresh Kumar Inna }
783a3667aaeSNaresh Kumar Inna
784a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
785a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
786a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
787a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
788a3667aaeSNaresh Kumar Inna if (sln->portid != portid)
789a3667aaeSNaresh Kumar Inna continue;
790a3667aaeSNaresh Kumar Inna
791a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
792a3667aaeSNaresh Kumar Inna
793a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
794a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
795a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
796a3667aaeSNaresh Kumar Inna }
797a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
798a3667aaeSNaresh Kumar Inna
799a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
800a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
801a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
802a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
803a3667aaeSNaresh Kumar Inna scsi_block_requests(shost);
804a3667aaeSNaresh Kumar Inna }
805a3667aaeSNaresh Kumar Inna kfree(lnode_list);
806a3667aaeSNaresh Kumar Inna }
807a3667aaeSNaresh Kumar Inna
808a3667aaeSNaresh Kumar Inna void
csio_lnodes_unblock_by_port(struct csio_hw * hw,uint8_t portid)809a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
810a3667aaeSNaresh Kumar Inna {
811a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
812a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
813a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
814a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
815a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
816a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
817a3667aaeSNaresh Kumar Inna
818a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
819a3667aaeSNaresh Kumar Inna GFP_KERNEL);
820a3667aaeSNaresh Kumar Inna if (!lnode_list) {
821a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
822a3667aaeSNaresh Kumar Inna return;
823a3667aaeSNaresh Kumar Inna }
824a3667aaeSNaresh Kumar Inna
825a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
826a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
827a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
828a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
829a3667aaeSNaresh Kumar Inna if (sln->portid != portid)
830a3667aaeSNaresh Kumar Inna continue;
831a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
832a3667aaeSNaresh Kumar Inna
833a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
834a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
835a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
836a3667aaeSNaresh Kumar Inna }
837a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
838a3667aaeSNaresh Kumar Inna
839a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
840a3667aaeSNaresh Kumar Inna csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
841a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
842a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
843a3667aaeSNaresh Kumar Inna scsi_unblock_requests(shost);
844a3667aaeSNaresh Kumar Inna }
845a3667aaeSNaresh Kumar Inna kfree(lnode_list);
846a3667aaeSNaresh Kumar Inna }
847a3667aaeSNaresh Kumar Inna
848a3667aaeSNaresh Kumar Inna void
csio_lnodes_exit(struct csio_hw * hw,bool npiv)849a3667aaeSNaresh Kumar Inna csio_lnodes_exit(struct csio_hw *hw, bool npiv)
850a3667aaeSNaresh Kumar Inna {
851a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
852a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
853a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
854a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
855a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
856a3667aaeSNaresh Kumar Inna
857a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
858a3667aaeSNaresh Kumar Inna GFP_KERNEL);
859a3667aaeSNaresh Kumar Inna if (!lnode_list) {
860a3667aaeSNaresh Kumar Inna csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
861a3667aaeSNaresh Kumar Inna return;
862a3667aaeSNaresh Kumar Inna }
863a3667aaeSNaresh Kumar Inna
864a3667aaeSNaresh Kumar Inna /* Get all child lnodes(NPIV ports) */
865a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
866a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
867a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
868a3667aaeSNaresh Kumar Inna
869a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
870a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
871a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
872a3667aaeSNaresh Kumar Inna }
873a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
874a3667aaeSNaresh Kumar Inna
875a3667aaeSNaresh Kumar Inna /* Delete NPIV lnodes */
876a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
877a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
878a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
879a3667aaeSNaresh Kumar Inna fc_vport_terminate(ln->fc_vport);
880a3667aaeSNaresh Kumar Inna }
881a3667aaeSNaresh Kumar Inna
882a3667aaeSNaresh Kumar Inna /* Delete only npiv lnodes */
883a3667aaeSNaresh Kumar Inna if (npiv)
884a3667aaeSNaresh Kumar Inna goto free_lnodes;
885a3667aaeSNaresh Kumar Inna
886a3667aaeSNaresh Kumar Inna cur_cnt = 0;
887a3667aaeSNaresh Kumar Inna /* Get all physical lnodes */
888a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
889a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
890a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
891a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
892a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
893a3667aaeSNaresh Kumar Inna }
894a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
895a3667aaeSNaresh Kumar Inna
896a3667aaeSNaresh Kumar Inna /* Delete physical lnodes */
897a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
898a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
899a3667aaeSNaresh Kumar Inna csio_shost_exit(lnode_list[ii]);
900a3667aaeSNaresh Kumar Inna }
901a3667aaeSNaresh Kumar Inna
902a3667aaeSNaresh Kumar Inna free_lnodes:
903a3667aaeSNaresh Kumar Inna kfree(lnode_list);
904a3667aaeSNaresh Kumar Inna }
905a3667aaeSNaresh Kumar Inna
906a3667aaeSNaresh Kumar Inna /*
907a3667aaeSNaresh Kumar Inna * csio_lnode_init_post: Set lnode attributes after starting HW.
908a3667aaeSNaresh Kumar Inna * @ln: lnode.
909a3667aaeSNaresh Kumar Inna *
910a3667aaeSNaresh Kumar Inna */
911a3667aaeSNaresh Kumar Inna static void
csio_lnode_init_post(struct csio_lnode * ln)912a3667aaeSNaresh Kumar Inna csio_lnode_init_post(struct csio_lnode *ln)
913a3667aaeSNaresh Kumar Inna {
914a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = csio_ln_to_shost(ln);
915a3667aaeSNaresh Kumar Inna
916a3667aaeSNaresh Kumar Inna csio_fchost_attr_init(ln);
917a3667aaeSNaresh Kumar Inna
918a3667aaeSNaresh Kumar Inna scsi_scan_host(shost);
919a3667aaeSNaresh Kumar Inna }
920a3667aaeSNaresh Kumar Inna
921a3667aaeSNaresh Kumar Inna /*
922a3667aaeSNaresh Kumar Inna * csio_probe_one - Instantiate this function.
923a3667aaeSNaresh Kumar Inna * @pdev: PCI device
924a3667aaeSNaresh Kumar Inna * @id: Device ID
925a3667aaeSNaresh Kumar Inna *
926a3667aaeSNaresh Kumar Inna * This is the .probe() callback of the driver. This function:
927a3667aaeSNaresh Kumar Inna * - Initializes the PCI function by enabling MMIO, setting bus
928a3667aaeSNaresh Kumar Inna * mastership and setting DMA mask.
929a3667aaeSNaresh Kumar Inna * - Allocates HW structure, DMA, memory resources, maps BARS to
930a3667aaeSNaresh Kumar Inna * host memory and initializes HW module.
931a3667aaeSNaresh Kumar Inna * - Allocates lnode structure via scsi_host_alloc, initializes
932a3667aaeSNaresh Kumar Inna * shost, initialized lnode module and registers with SCSI ML
933a3667aaeSNaresh Kumar Inna * via scsi_host_add.
934a3667aaeSNaresh Kumar Inna * - Enables interrupts, and starts the chip by kicking off the
935a3667aaeSNaresh Kumar Inna * HW state machine.
936a3667aaeSNaresh Kumar Inna * - Once hardware is ready, initiated scan of the host via
937a3667aaeSNaresh Kumar Inna * scsi_scan_host.
938a3667aaeSNaresh Kumar Inna */
csio_probe_one(struct pci_dev * pdev,const struct pci_device_id * id)9396f039790SGreg Kroah-Hartman static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
940a3667aaeSNaresh Kumar Inna {
941a3667aaeSNaresh Kumar Inna int rv;
942a3667aaeSNaresh Kumar Inna int bars;
943a3667aaeSNaresh Kumar Inna int i;
944a3667aaeSNaresh Kumar Inna struct csio_hw *hw;
945a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
946a3667aaeSNaresh Kumar Inna
9474bbd458eSVarun Prakash /* probe only T5 and T6 cards */
9484bbd458eSVarun Prakash if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)) &&
9494bbd458eSVarun Prakash !csio_is_t6((pdev->device & CSIO_HW_CHIP_MASK)))
9503fedeab1SHariprasad Shenai return -ENODEV;
9513fedeab1SHariprasad Shenai
952a3667aaeSNaresh Kumar Inna rv = csio_pci_init(pdev, &bars);
953a3667aaeSNaresh Kumar Inna if (rv)
954a3667aaeSNaresh Kumar Inna goto err;
955a3667aaeSNaresh Kumar Inna
956a3667aaeSNaresh Kumar Inna hw = csio_hw_alloc(pdev);
957a3667aaeSNaresh Kumar Inna if (!hw) {
958a3667aaeSNaresh Kumar Inna rv = -ENODEV;
959a3667aaeSNaresh Kumar Inna goto err_pci_exit;
960a3667aaeSNaresh Kumar Inna }
961a3667aaeSNaresh Kumar Inna
962ff6e88f1SVarun Prakash if (!pcie_relaxed_ordering_enabled(pdev))
963ff6e88f1SVarun Prakash hw->flags |= CSIO_HWF_ROOT_NO_RELAXED_ORDERING;
964ff6e88f1SVarun Prakash
965a3667aaeSNaresh Kumar Inna pci_set_drvdata(pdev, hw);
966a3667aaeSNaresh Kumar Inna
96782f0fd06SVarun Prakash rv = csio_hw_start(hw);
96882f0fd06SVarun Prakash if (rv) {
96982f0fd06SVarun Prakash if (rv == -EINVAL) {
970a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev,
971a3667aaeSNaresh Kumar Inna "Failed to start FW, continuing in debug mode.\n");
972a3667aaeSNaresh Kumar Inna return 0;
973a3667aaeSNaresh Kumar Inna }
97482f0fd06SVarun Prakash goto err_lnode_exit;
97582f0fd06SVarun Prakash }
976a3667aaeSNaresh Kumar Inna
977a3667aaeSNaresh Kumar Inna sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
978b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
979b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MINOR_G(hw->fwrev),
980b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MICRO_G(hw->fwrev),
981b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_BUILD_G(hw->fwrev));
982a3667aaeSNaresh Kumar Inna
983a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
984a3667aaeSNaresh Kumar Inna ln = csio_shost_init(hw, &pdev->dev, true, NULL);
985a3667aaeSNaresh Kumar Inna if (!ln) {
986a3667aaeSNaresh Kumar Inna rv = -ENODEV;
987a3667aaeSNaresh Kumar Inna break;
988a3667aaeSNaresh Kumar Inna }
989a3667aaeSNaresh Kumar Inna /* Initialize portid */
990a3667aaeSNaresh Kumar Inna ln->portid = hw->pport[i].portid;
991a3667aaeSNaresh Kumar Inna
992a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
993a3667aaeSNaresh Kumar Inna if (csio_lnode_start(ln) != 0)
994a3667aaeSNaresh Kumar Inna rv = -ENODEV;
995a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
996a3667aaeSNaresh Kumar Inna
997a3667aaeSNaresh Kumar Inna if (rv)
998a3667aaeSNaresh Kumar Inna break;
999a3667aaeSNaresh Kumar Inna
1000a3667aaeSNaresh Kumar Inna csio_lnode_init_post(ln);
1001a3667aaeSNaresh Kumar Inna }
1002a3667aaeSNaresh Kumar Inna
1003a3667aaeSNaresh Kumar Inna if (rv)
1004a3667aaeSNaresh Kumar Inna goto err_lnode_exit;
1005a3667aaeSNaresh Kumar Inna
1006a3667aaeSNaresh Kumar Inna return 0;
1007a3667aaeSNaresh Kumar Inna
1008a3667aaeSNaresh Kumar Inna err_lnode_exit:
1009a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1010a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1011a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1012a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1013a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1014a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1015a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1016a3667aaeSNaresh Kumar Inna err_pci_exit:
1017a3667aaeSNaresh Kumar Inna csio_pci_exit(pdev, &bars);
1018a3667aaeSNaresh Kumar Inna err:
1019a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
1020a3667aaeSNaresh Kumar Inna return rv;
1021a3667aaeSNaresh Kumar Inna }
1022a3667aaeSNaresh Kumar Inna
1023a3667aaeSNaresh Kumar Inna /*
1024a3667aaeSNaresh Kumar Inna * csio_remove_one - Remove one instance of the driver at this PCI function.
1025a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1026a3667aaeSNaresh Kumar Inna *
1027a3667aaeSNaresh Kumar Inna * Used during hotplug operation.
1028a3667aaeSNaresh Kumar Inna */
csio_remove_one(struct pci_dev * pdev)10296f039790SGreg Kroah-Hartman static void csio_remove_one(struct pci_dev *pdev)
1030a3667aaeSNaresh Kumar Inna {
1031a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1032a3667aaeSNaresh Kumar Inna int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1033a3667aaeSNaresh Kumar Inna
1034a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1035a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1036a3667aaeSNaresh Kumar Inna
1037a3667aaeSNaresh Kumar Inna /* Stops lnode, Rnode s/m
1038a3667aaeSNaresh Kumar Inna * Quiesce IOs.
1039a3667aaeSNaresh Kumar Inna * All sessions with remote ports are unregistered.
1040a3667aaeSNaresh Kumar Inna */
1041a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1042a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1043a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1044a3667aaeSNaresh Kumar Inna
1045a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1046a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1047a3667aaeSNaresh Kumar Inna csio_pci_exit(pdev, &bars);
1048a3667aaeSNaresh Kumar Inna }
1049a3667aaeSNaresh Kumar Inna
1050a3667aaeSNaresh Kumar Inna /*
1051a3667aaeSNaresh Kumar Inna * csio_pci_error_detected - PCI error was detected
1052a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1053a3667aaeSNaresh Kumar Inna *
1054a3667aaeSNaresh Kumar Inna */
1055a3667aaeSNaresh Kumar Inna static pci_ers_result_t
csio_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)1056a3667aaeSNaresh Kumar Inna csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1057a3667aaeSNaresh Kumar Inna {
1058a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1059a3667aaeSNaresh Kumar Inna
1060a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1061a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1062a3667aaeSNaresh Kumar Inna
1063a3667aaeSNaresh Kumar Inna /* Post PCI error detected evt to HW s/m
1064a3667aaeSNaresh Kumar Inna * HW s/m handles this evt by quiescing IOs, unregisters rports
1065a3667aaeSNaresh Kumar Inna * and finally takes the device to offline.
1066a3667aaeSNaresh Kumar Inna */
1067a3667aaeSNaresh Kumar Inna csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
1068a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1069a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1070a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1071a3667aaeSNaresh Kumar Inna csio_intr_disable(hw, true);
1072a3667aaeSNaresh Kumar Inna pci_disable_device(pdev);
1073a3667aaeSNaresh Kumar Inna return state == pci_channel_io_perm_failure ?
1074a3667aaeSNaresh Kumar Inna PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1075a3667aaeSNaresh Kumar Inna }
1076a3667aaeSNaresh Kumar Inna
1077a3667aaeSNaresh Kumar Inna /*
1078a3667aaeSNaresh Kumar Inna * csio_pci_slot_reset - PCI slot has been reset.
1079a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1080a3667aaeSNaresh Kumar Inna *
1081a3667aaeSNaresh Kumar Inna */
1082a3667aaeSNaresh Kumar Inna static pci_ers_result_t
csio_pci_slot_reset(struct pci_dev * pdev)1083a3667aaeSNaresh Kumar Inna csio_pci_slot_reset(struct pci_dev *pdev)
1084a3667aaeSNaresh Kumar Inna {
1085a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1086a3667aaeSNaresh Kumar Inna int ready;
1087a3667aaeSNaresh Kumar Inna
1088a3667aaeSNaresh Kumar Inna if (pci_enable_device(pdev)) {
1089a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
1090a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_DISCONNECT;
1091a3667aaeSNaresh Kumar Inna }
1092a3667aaeSNaresh Kumar Inna
1093a3667aaeSNaresh Kumar Inna pci_set_master(pdev);
1094a3667aaeSNaresh Kumar Inna pci_restore_state(pdev);
1095a3667aaeSNaresh Kumar Inna pci_save_state(pdev);
1096a3667aaeSNaresh Kumar Inna
1097a3667aaeSNaresh Kumar Inna /* Bring HW s/m to ready state.
1098a3667aaeSNaresh Kumar Inna * but don't resume IOs.
1099a3667aaeSNaresh Kumar Inna */
1100a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1101a3667aaeSNaresh Kumar Inna csio_post_event(&hw->sm, CSIO_HWE_PCIERR_SLOT_RESET);
1102a3667aaeSNaresh Kumar Inna ready = csio_is_hw_ready(hw);
1103a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1104a3667aaeSNaresh Kumar Inna
1105a3667aaeSNaresh Kumar Inna if (ready) {
1106a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_RECOVERED;
1107a3667aaeSNaresh Kumar Inna } else {
1108a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
1109a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_DISCONNECT;
1110a3667aaeSNaresh Kumar Inna }
1111a3667aaeSNaresh Kumar Inna }
1112a3667aaeSNaresh Kumar Inna
1113a3667aaeSNaresh Kumar Inna /*
1114a3667aaeSNaresh Kumar Inna * csio_pci_resume - Resume normal operations
1115a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1116a3667aaeSNaresh Kumar Inna *
1117a3667aaeSNaresh Kumar Inna */
1118a3667aaeSNaresh Kumar Inna static void
csio_pci_resume(struct pci_dev * pdev)1119a3667aaeSNaresh Kumar Inna csio_pci_resume(struct pci_dev *pdev)
1120a3667aaeSNaresh Kumar Inna {
1121a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1122a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
1123a3667aaeSNaresh Kumar Inna int rv = 0;
1124a3667aaeSNaresh Kumar Inna int i;
1125a3667aaeSNaresh Kumar Inna
1126a3667aaeSNaresh Kumar Inna /* Bring the LINK UP and Resume IO */
1127a3667aaeSNaresh Kumar Inna
1128a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
1129a3667aaeSNaresh Kumar Inna ln = csio_shost_init(hw, &pdev->dev, true, NULL);
1130a3667aaeSNaresh Kumar Inna if (!ln) {
1131a3667aaeSNaresh Kumar Inna rv = -ENODEV;
1132a3667aaeSNaresh Kumar Inna break;
1133a3667aaeSNaresh Kumar Inna }
1134a3667aaeSNaresh Kumar Inna /* Initialize portid */
1135a3667aaeSNaresh Kumar Inna ln->portid = hw->pport[i].portid;
1136a3667aaeSNaresh Kumar Inna
1137a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1138a3667aaeSNaresh Kumar Inna if (csio_lnode_start(ln) != 0)
1139a3667aaeSNaresh Kumar Inna rv = -ENODEV;
1140a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1141a3667aaeSNaresh Kumar Inna
1142a3667aaeSNaresh Kumar Inna if (rv)
1143a3667aaeSNaresh Kumar Inna break;
1144a3667aaeSNaresh Kumar Inna
1145a3667aaeSNaresh Kumar Inna csio_lnode_init_post(ln);
1146a3667aaeSNaresh Kumar Inna }
1147a3667aaeSNaresh Kumar Inna
1148a3667aaeSNaresh Kumar Inna if (rv)
1149a3667aaeSNaresh Kumar Inna goto err_resume_exit;
1150a3667aaeSNaresh Kumar Inna
1151a3667aaeSNaresh Kumar Inna return;
1152a3667aaeSNaresh Kumar Inna
1153a3667aaeSNaresh Kumar Inna err_resume_exit:
1154a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1155a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1156a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1157a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1158a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1159a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1160a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1161a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
1162a3667aaeSNaresh Kumar Inna }
1163a3667aaeSNaresh Kumar Inna
1164a3667aaeSNaresh Kumar Inna static struct pci_error_handlers csio_err_handler = {
1165a3667aaeSNaresh Kumar Inna .error_detected = csio_pci_error_detected,
1166a3667aaeSNaresh Kumar Inna .slot_reset = csio_pci_slot_reset,
1167a3667aaeSNaresh Kumar Inna .resume = csio_pci_resume,
1168a3667aaeSNaresh Kumar Inna };
1169a3667aaeSNaresh Kumar Inna
11703fedeab1SHariprasad Shenai /*
11713fedeab1SHariprasad Shenai * Macros needed to support the PCI Device ID Table ...
11723fedeab1SHariprasad Shenai */
11733fedeab1SHariprasad Shenai #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
1174768ffc66SHariprasad Shenai static const struct pci_device_id csio_pci_tbl[] = {
11753fb4c22eSPraveen Madhavan /* Define for FCoE uses PF6 */
11763fb4c22eSPraveen Madhavan #define CH_PCI_DEVICE_ID_FUNCTION 0x6
1177a3667aaeSNaresh Kumar Inna
11783fedeab1SHariprasad Shenai #define CH_PCI_ID_TABLE_ENTRY(devid) \
11793fedeab1SHariprasad Shenai { PCI_VDEVICE(CHELSIO, (devid)), 0 }
11803fedeab1SHariprasad Shenai
11813fedeab1SHariprasad Shenai #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
11823fedeab1SHariprasad Shenai
11833fedeab1SHariprasad Shenai #include "t4_pci_id_tbl.h"
1184a3667aaeSNaresh Kumar Inna
1185a3667aaeSNaresh Kumar Inna static struct pci_driver csio_pci_driver = {
1186a3667aaeSNaresh Kumar Inna .name = KBUILD_MODNAME,
1187a3667aaeSNaresh Kumar Inna .driver = {
1188a3667aaeSNaresh Kumar Inna .owner = THIS_MODULE,
1189a3667aaeSNaresh Kumar Inna },
1190a3667aaeSNaresh Kumar Inna .id_table = csio_pci_tbl,
1191a3667aaeSNaresh Kumar Inna .probe = csio_probe_one,
1192a3667aaeSNaresh Kumar Inna .remove = csio_remove_one,
1193a3667aaeSNaresh Kumar Inna .err_handler = &csio_err_handler,
1194a3667aaeSNaresh Kumar Inna };
1195a3667aaeSNaresh Kumar Inna
1196a3667aaeSNaresh Kumar Inna /*
1197a3667aaeSNaresh Kumar Inna * csio_init - Chelsio storage driver initialization function.
1198a3667aaeSNaresh Kumar Inna *
1199a3667aaeSNaresh Kumar Inna */
1200a3667aaeSNaresh Kumar Inna static int __init
csio_init(void)1201a3667aaeSNaresh Kumar Inna csio_init(void)
1202a3667aaeSNaresh Kumar Inna {
1203a3667aaeSNaresh Kumar Inna int rv = -ENOMEM;
1204a3667aaeSNaresh Kumar Inna
1205a3667aaeSNaresh Kumar Inna pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
1206a3667aaeSNaresh Kumar Inna
1207a3667aaeSNaresh Kumar Inna csio_dfs_init();
1208a3667aaeSNaresh Kumar Inna
1209a3667aaeSNaresh Kumar Inna csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
1210a3667aaeSNaresh Kumar Inna if (!csio_fcoe_transport)
1211a3667aaeSNaresh Kumar Inna goto err;
1212a3667aaeSNaresh Kumar Inna
1213a3667aaeSNaresh Kumar Inna csio_fcoe_transport_vport =
1214a3667aaeSNaresh Kumar Inna fc_attach_transport(&csio_fc_transport_vport_funcs);
1215a3667aaeSNaresh Kumar Inna if (!csio_fcoe_transport_vport)
1216a3667aaeSNaresh Kumar Inna goto err_vport;
1217a3667aaeSNaresh Kumar Inna
1218a3667aaeSNaresh Kumar Inna rv = pci_register_driver(&csio_pci_driver);
1219a3667aaeSNaresh Kumar Inna if (rv)
1220a3667aaeSNaresh Kumar Inna goto err_pci;
1221a3667aaeSNaresh Kumar Inna
1222a3667aaeSNaresh Kumar Inna return 0;
1223a3667aaeSNaresh Kumar Inna
1224a3667aaeSNaresh Kumar Inna err_pci:
1225a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport_vport);
1226a3667aaeSNaresh Kumar Inna err_vport:
1227a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport);
1228a3667aaeSNaresh Kumar Inna err:
1229a3667aaeSNaresh Kumar Inna csio_dfs_exit();
1230a3667aaeSNaresh Kumar Inna return rv;
1231a3667aaeSNaresh Kumar Inna }
1232a3667aaeSNaresh Kumar Inna
1233a3667aaeSNaresh Kumar Inna /*
1234a3667aaeSNaresh Kumar Inna * csio_exit - Chelsio storage driver uninitialization .
1235a3667aaeSNaresh Kumar Inna *
1236a3667aaeSNaresh Kumar Inna * Function that gets called in the unload path.
1237a3667aaeSNaresh Kumar Inna */
1238a3667aaeSNaresh Kumar Inna static void __exit
csio_exit(void)1239a3667aaeSNaresh Kumar Inna csio_exit(void)
1240a3667aaeSNaresh Kumar Inna {
1241a3667aaeSNaresh Kumar Inna pci_unregister_driver(&csio_pci_driver);
1242a3667aaeSNaresh Kumar Inna csio_dfs_exit();
1243a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport_vport);
1244a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport);
1245a3667aaeSNaresh Kumar Inna }
1246a3667aaeSNaresh Kumar Inna
1247a3667aaeSNaresh Kumar Inna module_init(csio_init);
1248a3667aaeSNaresh Kumar Inna module_exit(csio_exit);
1249a3667aaeSNaresh Kumar Inna MODULE_AUTHOR(CSIO_DRV_AUTHOR);
1250a3667aaeSNaresh Kumar Inna MODULE_DESCRIPTION(CSIO_DRV_DESC);
125143af2146SGreg Kroah-Hartman MODULE_LICENSE("Dual BSD/GPL");
1252a3667aaeSNaresh Kumar Inna MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
1253a3667aaeSNaresh Kumar Inna MODULE_VERSION(CSIO_DRV_VERSION);
12547cc16380SArvind Bhushan MODULE_FIRMWARE(FW_FNAME_T5);
12554bbd458eSVarun Prakash MODULE_FIRMWARE(FW_FNAME_T6);
1256*79a74822SRahul Lakkireddy MODULE_SOFTDEP("pre: cxgb4");
1257