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;
524*45924117SJustin Stitt strscpy(hw->drv_version, CSIO_DRV_VERSION,
525*45924117SJustin Stitt sizeof(hw->drv_version));
526a3667aaeSNaresh Kumar Inna
527a3667aaeSNaresh Kumar Inna /* memory pool/DMA pool allocation */
528a3667aaeSNaresh Kumar Inna if (csio_resource_alloc(hw))
529a3667aaeSNaresh Kumar Inna goto err_free_hw;
530a3667aaeSNaresh Kumar Inna
531a3667aaeSNaresh Kumar Inna /* Get the start address of registers from BAR 0 */
5324bdc0d67SChristoph Hellwig hw->regstart = ioremap(pci_resource_start(pdev, 0),
533a3667aaeSNaresh Kumar Inna pci_resource_len(pdev, 0));
534a3667aaeSNaresh Kumar Inna if (!hw->regstart) {
535a3667aaeSNaresh Kumar Inna csio_err(hw, "Could not map BAR 0, regstart = %p\n",
536a3667aaeSNaresh Kumar Inna hw->regstart);
537a3667aaeSNaresh Kumar Inna goto err_resource_free;
538a3667aaeSNaresh Kumar Inna }
539a3667aaeSNaresh Kumar Inna
540a3667aaeSNaresh Kumar Inna csio_hw_init_workers(hw);
541a3667aaeSNaresh Kumar Inna
542a3667aaeSNaresh Kumar Inna if (csio_hw_init(hw))
543a3667aaeSNaresh Kumar Inna goto err_unmap_bar;
544a3667aaeSNaresh Kumar Inna
545a3667aaeSNaresh Kumar Inna csio_dfs_create(hw);
546a3667aaeSNaresh Kumar Inna
547a3667aaeSNaresh Kumar Inna csio_dbg(hw, "hw:%p\n", hw);
548a3667aaeSNaresh Kumar Inna
549a3667aaeSNaresh Kumar Inna return hw;
550a3667aaeSNaresh Kumar Inna
551a3667aaeSNaresh Kumar Inna err_unmap_bar:
552a3667aaeSNaresh Kumar Inna csio_hw_exit_workers(hw);
553a3667aaeSNaresh Kumar Inna iounmap(hw->regstart);
554a3667aaeSNaresh Kumar Inna err_resource_free:
555a3667aaeSNaresh Kumar Inna csio_resource_free(hw);
556a3667aaeSNaresh Kumar Inna err_free_hw:
557a3667aaeSNaresh Kumar Inna kfree(hw);
558a3667aaeSNaresh Kumar Inna err:
559a3667aaeSNaresh Kumar Inna return NULL;
560a3667aaeSNaresh Kumar Inna }
561a3667aaeSNaresh Kumar Inna
562a3667aaeSNaresh Kumar Inna /*
563a3667aaeSNaresh Kumar Inna * csio_hw_free - Uninitialize and free the HW module.
564a3667aaeSNaresh Kumar Inna * @hw: The HW module
565a3667aaeSNaresh Kumar Inna *
566a3667aaeSNaresh Kumar Inna * Disable interrupts, uninit the HW module, free resources, free hw.
567a3667aaeSNaresh Kumar Inna */
568a3667aaeSNaresh Kumar Inna static void
csio_hw_free(struct csio_hw * hw)569a3667aaeSNaresh Kumar Inna csio_hw_free(struct csio_hw *hw)
570a3667aaeSNaresh Kumar Inna {
571a3667aaeSNaresh Kumar Inna csio_intr_disable(hw, true);
572a3667aaeSNaresh Kumar Inna csio_hw_exit_workers(hw);
573a3667aaeSNaresh Kumar Inna csio_hw_exit(hw);
574a3667aaeSNaresh Kumar Inna iounmap(hw->regstart);
575a3667aaeSNaresh Kumar Inna csio_dfs_destroy(hw);
576a3667aaeSNaresh Kumar Inna csio_resource_free(hw);
577a3667aaeSNaresh Kumar Inna kfree(hw);
578a3667aaeSNaresh Kumar Inna }
579a3667aaeSNaresh Kumar Inna
580a3667aaeSNaresh Kumar Inna /**
581a3667aaeSNaresh Kumar Inna * csio_shost_init - Create and initialize the lnode module.
582a3667aaeSNaresh Kumar Inna * @hw: The HW module.
583a3667aaeSNaresh Kumar Inna * @dev: The device associated with this invocation.
584a3667aaeSNaresh Kumar Inna * @probe: Called from probe context or not?
5855446a91dSLee Jones * @pln: Parent lnode if any.
586a3667aaeSNaresh Kumar Inna *
587a3667aaeSNaresh Kumar Inna * Allocates lnode structure via scsi_host_alloc, initializes
588a3667aaeSNaresh Kumar Inna * shost, initializes lnode module and registers with SCSI ML
589a3667aaeSNaresh Kumar Inna * via scsi_host_add. This function is shared between physical and
590a3667aaeSNaresh Kumar Inna * virtual node ports.
591a3667aaeSNaresh Kumar Inna */
592a3667aaeSNaresh Kumar Inna struct csio_lnode *
csio_shost_init(struct csio_hw * hw,struct device * dev,bool probe,struct csio_lnode * pln)593a3667aaeSNaresh Kumar Inna csio_shost_init(struct csio_hw *hw, struct device *dev,
594a3667aaeSNaresh Kumar Inna bool probe, struct csio_lnode *pln)
595a3667aaeSNaresh Kumar Inna {
596a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = NULL;
597a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
598a3667aaeSNaresh Kumar Inna
599a3667aaeSNaresh Kumar Inna csio_fcoe_shost_template.cmd_per_lun = csio_lun_qdepth;
600a3667aaeSNaresh Kumar Inna csio_fcoe_shost_vport_template.cmd_per_lun = csio_lun_qdepth;
601a3667aaeSNaresh Kumar Inna
602a3667aaeSNaresh Kumar Inna /*
603a3667aaeSNaresh Kumar Inna * hw->pdev is the physical port's PCI dev structure,
604a3667aaeSNaresh Kumar Inna * which will be different from the NPIV dev structure.
605a3667aaeSNaresh Kumar Inna */
606a3667aaeSNaresh Kumar Inna if (dev == &hw->pdev->dev)
607a3667aaeSNaresh Kumar Inna shost = scsi_host_alloc(
608a3667aaeSNaresh Kumar Inna &csio_fcoe_shost_template,
609a3667aaeSNaresh Kumar Inna sizeof(struct csio_lnode));
610a3667aaeSNaresh Kumar Inna else
611a3667aaeSNaresh Kumar Inna shost = scsi_host_alloc(
612a3667aaeSNaresh Kumar Inna &csio_fcoe_shost_vport_template,
613a3667aaeSNaresh Kumar Inna sizeof(struct csio_lnode));
614a3667aaeSNaresh Kumar Inna
615a3667aaeSNaresh Kumar Inna if (!shost)
616a3667aaeSNaresh Kumar Inna goto err;
617a3667aaeSNaresh Kumar Inna
618a3667aaeSNaresh Kumar Inna ln = shost_priv(shost);
619a3667aaeSNaresh Kumar Inna memset(ln, 0, sizeof(struct csio_lnode));
620a3667aaeSNaresh Kumar Inna
621a3667aaeSNaresh Kumar Inna /* Link common lnode to this lnode */
622a3667aaeSNaresh Kumar Inna ln->dev_num = (shost->host_no << 16);
623a3667aaeSNaresh Kumar Inna
624a3667aaeSNaresh Kumar Inna shost->can_queue = CSIO_MAX_QUEUE;
625a3667aaeSNaresh Kumar Inna shost->this_id = -1;
626a3667aaeSNaresh Kumar Inna shost->unique_id = shost->host_no;
627a3667aaeSNaresh Kumar Inna shost->max_cmd_len = 16; /* Max CDB length supported */
628a3667aaeSNaresh Kumar Inna shost->max_id = min_t(uint32_t, csio_fcoe_rnodes,
629a3667aaeSNaresh Kumar Inna hw->fres_info.max_ssns);
630a3667aaeSNaresh Kumar Inna shost->max_lun = CSIO_MAX_LUN;
631a3667aaeSNaresh Kumar Inna if (dev == &hw->pdev->dev)
632a3667aaeSNaresh Kumar Inna shost->transportt = csio_fcoe_transport;
633a3667aaeSNaresh Kumar Inna else
634a3667aaeSNaresh Kumar Inna shost->transportt = csio_fcoe_transport_vport;
635a3667aaeSNaresh Kumar Inna
636a3667aaeSNaresh Kumar Inna /* root lnode */
637a3667aaeSNaresh Kumar Inna if (!hw->rln)
638a3667aaeSNaresh Kumar Inna hw->rln = ln;
639a3667aaeSNaresh Kumar Inna
640a3667aaeSNaresh Kumar Inna /* Other initialization here: Common, Transport specific */
641a3667aaeSNaresh Kumar Inna if (csio_lnode_init(ln, hw, pln))
642a3667aaeSNaresh Kumar Inna goto err_shost_put;
643a3667aaeSNaresh Kumar Inna
6449934613eSVarun Prakash if (scsi_add_host_with_dma(shost, dev, &hw->pdev->dev))
645a3667aaeSNaresh Kumar Inna goto err_lnode_exit;
646a3667aaeSNaresh Kumar Inna
647a3667aaeSNaresh Kumar Inna return ln;
648a3667aaeSNaresh Kumar Inna
649a3667aaeSNaresh Kumar Inna err_lnode_exit:
650a3667aaeSNaresh Kumar Inna csio_lnode_exit(ln);
651a3667aaeSNaresh Kumar Inna err_shost_put:
652a3667aaeSNaresh Kumar Inna scsi_host_put(shost);
653a3667aaeSNaresh Kumar Inna err:
654a3667aaeSNaresh Kumar Inna return NULL;
655a3667aaeSNaresh Kumar Inna }
656a3667aaeSNaresh Kumar Inna
657a3667aaeSNaresh Kumar Inna /**
658a3667aaeSNaresh Kumar Inna * csio_shost_exit - De-instantiate the shost.
659a3667aaeSNaresh Kumar Inna * @ln: The lnode module corresponding to the shost.
660a3667aaeSNaresh Kumar Inna *
661a3667aaeSNaresh Kumar Inna */
662a3667aaeSNaresh Kumar Inna void
csio_shost_exit(struct csio_lnode * ln)663a3667aaeSNaresh Kumar Inna csio_shost_exit(struct csio_lnode *ln)
664a3667aaeSNaresh Kumar Inna {
665a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = csio_ln_to_shost(ln);
666a3667aaeSNaresh Kumar Inna struct csio_hw *hw = csio_lnode_to_hw(ln);
667a3667aaeSNaresh Kumar Inna
668a3667aaeSNaresh Kumar Inna /* Inform transport */
669a3667aaeSNaresh Kumar Inna fc_remove_host(shost);
670a3667aaeSNaresh Kumar Inna
671a3667aaeSNaresh Kumar Inna /* Inform SCSI ML */
672a3667aaeSNaresh Kumar Inna scsi_remove_host(shost);
673a3667aaeSNaresh Kumar Inna
674a3667aaeSNaresh Kumar Inna /* Flush all the events, so that any rnode removal events
675a3667aaeSNaresh Kumar Inna * already queued are all handled, before we remove the lnode.
676a3667aaeSNaresh Kumar Inna */
677a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
678a3667aaeSNaresh Kumar Inna csio_evtq_flush(hw);
679a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
680a3667aaeSNaresh Kumar Inna
681a3667aaeSNaresh Kumar Inna csio_lnode_exit(ln);
682a3667aaeSNaresh Kumar Inna scsi_host_put(shost);
683a3667aaeSNaresh Kumar Inna }
684a3667aaeSNaresh Kumar Inna
685a3667aaeSNaresh Kumar Inna struct csio_lnode *
csio_lnode_alloc(struct csio_hw * hw)686a3667aaeSNaresh Kumar Inna csio_lnode_alloc(struct csio_hw *hw)
687a3667aaeSNaresh Kumar Inna {
688a3667aaeSNaresh Kumar Inna return csio_shost_init(hw, &hw->pdev->dev, false, NULL);
689a3667aaeSNaresh Kumar Inna }
690a3667aaeSNaresh Kumar Inna
691a3667aaeSNaresh Kumar Inna void
csio_lnodes_block_request(struct csio_hw * hw)692a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(struct csio_hw *hw)
693a3667aaeSNaresh Kumar Inna {
694a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
695a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
696a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
697a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
698a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
699a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
700a3667aaeSNaresh Kumar Inna
701a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
702a3667aaeSNaresh Kumar Inna GFP_KERNEL);
703a3667aaeSNaresh Kumar Inna if (!lnode_list) {
704a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
705a3667aaeSNaresh Kumar Inna return;
706a3667aaeSNaresh Kumar Inna }
707a3667aaeSNaresh Kumar Inna
708a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
709a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
710a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
711a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
712a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
713a3667aaeSNaresh Kumar Inna
714a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
715a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
716a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
717a3667aaeSNaresh Kumar Inna }
718a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
719a3667aaeSNaresh Kumar Inna
720a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
721a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
722a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
723a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
724a3667aaeSNaresh Kumar Inna scsi_block_requests(shost);
725a3667aaeSNaresh Kumar Inna
726a3667aaeSNaresh Kumar Inna }
727a3667aaeSNaresh Kumar Inna kfree(lnode_list);
728a3667aaeSNaresh Kumar Inna }
729a3667aaeSNaresh Kumar Inna
730a3667aaeSNaresh Kumar Inna void
csio_lnodes_unblock_request(struct csio_hw * hw)731a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(struct csio_hw *hw)
732a3667aaeSNaresh Kumar Inna {
733a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
734a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
735a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
736a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
737a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
738a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
739a3667aaeSNaresh Kumar Inna
740a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
741a3667aaeSNaresh Kumar Inna GFP_KERNEL);
742a3667aaeSNaresh Kumar Inna if (!lnode_list) {
743a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
744a3667aaeSNaresh Kumar Inna return;
745a3667aaeSNaresh Kumar Inna }
746a3667aaeSNaresh Kumar Inna
747a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
748a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
749a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
750a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
751a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
752a3667aaeSNaresh Kumar Inna
753a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
754a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
755a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
756a3667aaeSNaresh Kumar Inna }
757a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
758a3667aaeSNaresh Kumar Inna
759a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
760a3667aaeSNaresh Kumar Inna csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
761a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
762a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
763a3667aaeSNaresh Kumar Inna scsi_unblock_requests(shost);
764a3667aaeSNaresh Kumar Inna }
765a3667aaeSNaresh Kumar Inna kfree(lnode_list);
766a3667aaeSNaresh Kumar Inna }
767a3667aaeSNaresh Kumar Inna
768a3667aaeSNaresh Kumar Inna void
csio_lnodes_block_by_port(struct csio_hw * hw,uint8_t portid)769a3667aaeSNaresh Kumar Inna csio_lnodes_block_by_port(struct csio_hw *hw, uint8_t portid)
770a3667aaeSNaresh Kumar Inna {
771a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
772a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
773a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
774a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
775a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
776a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
777a3667aaeSNaresh Kumar Inna
778a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
779a3667aaeSNaresh Kumar Inna GFP_KERNEL);
780a3667aaeSNaresh Kumar Inna if (!lnode_list) {
781a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
782a3667aaeSNaresh Kumar Inna return;
783a3667aaeSNaresh Kumar Inna }
784a3667aaeSNaresh Kumar Inna
785a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
786a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
787a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
788a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
789a3667aaeSNaresh Kumar Inna if (sln->portid != portid)
790a3667aaeSNaresh Kumar Inna continue;
791a3667aaeSNaresh Kumar Inna
792a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
793a3667aaeSNaresh Kumar Inna
794a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
795a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
796a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
797a3667aaeSNaresh Kumar Inna }
798a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
799a3667aaeSNaresh Kumar Inna
800a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
801a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Blocking IOs on lnode: %p\n", lnode_list[ii]);
802a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
803a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
804a3667aaeSNaresh Kumar Inna scsi_block_requests(shost);
805a3667aaeSNaresh Kumar Inna }
806a3667aaeSNaresh Kumar Inna kfree(lnode_list);
807a3667aaeSNaresh Kumar Inna }
808a3667aaeSNaresh Kumar Inna
809a3667aaeSNaresh Kumar Inna void
csio_lnodes_unblock_by_port(struct csio_hw * hw,uint8_t portid)810a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_by_port(struct csio_hw *hw, uint8_t portid)
811a3667aaeSNaresh Kumar Inna {
812a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
813a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost;
814a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
815a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
816a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
817a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
818a3667aaeSNaresh Kumar Inna
819a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
820a3667aaeSNaresh Kumar Inna GFP_KERNEL);
821a3667aaeSNaresh Kumar Inna if (!lnode_list) {
822a3667aaeSNaresh Kumar Inna csio_err(hw, "Failed to allocate lnodes_list");
823a3667aaeSNaresh Kumar Inna return;
824a3667aaeSNaresh Kumar Inna }
825a3667aaeSNaresh Kumar Inna
826a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
827a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
828a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
829a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
830a3667aaeSNaresh Kumar Inna if (sln->portid != portid)
831a3667aaeSNaresh Kumar Inna continue;
832a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
833a3667aaeSNaresh Kumar Inna
834a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
835a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
836a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
837a3667aaeSNaresh Kumar Inna }
838a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
839a3667aaeSNaresh Kumar Inna
840a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
841a3667aaeSNaresh Kumar Inna csio_dbg(hw, "unblocking IOs on lnode: %p\n", lnode_list[ii]);
842a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
843a3667aaeSNaresh Kumar Inna shost = csio_ln_to_shost(ln);
844a3667aaeSNaresh Kumar Inna scsi_unblock_requests(shost);
845a3667aaeSNaresh Kumar Inna }
846a3667aaeSNaresh Kumar Inna kfree(lnode_list);
847a3667aaeSNaresh Kumar Inna }
848a3667aaeSNaresh Kumar Inna
849a3667aaeSNaresh Kumar Inna void
csio_lnodes_exit(struct csio_hw * hw,bool npiv)850a3667aaeSNaresh Kumar Inna csio_lnodes_exit(struct csio_hw *hw, bool npiv)
851a3667aaeSNaresh Kumar Inna {
852a3667aaeSNaresh Kumar Inna struct csio_lnode *sln;
853a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
854a3667aaeSNaresh Kumar Inna struct list_head *cur_ln, *cur_cln;
855a3667aaeSNaresh Kumar Inna struct csio_lnode **lnode_list;
856a3667aaeSNaresh Kumar Inna int cur_cnt = 0, ii;
857a3667aaeSNaresh Kumar Inna
858a3667aaeSNaresh Kumar Inna lnode_list = kzalloc((sizeof(struct csio_lnode *) * hw->num_lns),
859a3667aaeSNaresh Kumar Inna GFP_KERNEL);
860a3667aaeSNaresh Kumar Inna if (!lnode_list) {
861a3667aaeSNaresh Kumar Inna csio_err(hw, "lnodes_exit: Failed to allocate lnodes_list.\n");
862a3667aaeSNaresh Kumar Inna return;
863a3667aaeSNaresh Kumar Inna }
864a3667aaeSNaresh Kumar Inna
865a3667aaeSNaresh Kumar Inna /* Get all child lnodes(NPIV ports) */
866a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
867a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
868a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
869a3667aaeSNaresh Kumar Inna
870a3667aaeSNaresh Kumar Inna /* Traverse children lnodes */
871a3667aaeSNaresh Kumar Inna list_for_each(cur_cln, &sln->cln_head)
872a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = (struct csio_lnode *) cur_cln;
873a3667aaeSNaresh Kumar Inna }
874a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
875a3667aaeSNaresh Kumar Inna
876a3667aaeSNaresh Kumar Inna /* Delete NPIV lnodes */
877a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
878a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Deleting child lnode: %p\n", lnode_list[ii]);
879a3667aaeSNaresh Kumar Inna ln = lnode_list[ii];
880a3667aaeSNaresh Kumar Inna fc_vport_terminate(ln->fc_vport);
881a3667aaeSNaresh Kumar Inna }
882a3667aaeSNaresh Kumar Inna
883a3667aaeSNaresh Kumar Inna /* Delete only npiv lnodes */
884a3667aaeSNaresh Kumar Inna if (npiv)
885a3667aaeSNaresh Kumar Inna goto free_lnodes;
886a3667aaeSNaresh Kumar Inna
887a3667aaeSNaresh Kumar Inna cur_cnt = 0;
888a3667aaeSNaresh Kumar Inna /* Get all physical lnodes */
889a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
890a3667aaeSNaresh Kumar Inna /* Traverse sibling lnodes */
891a3667aaeSNaresh Kumar Inna list_for_each(cur_ln, &hw->sln_head) {
892a3667aaeSNaresh Kumar Inna sln = (struct csio_lnode *) cur_ln;
893a3667aaeSNaresh Kumar Inna lnode_list[cur_cnt++] = sln;
894a3667aaeSNaresh Kumar Inna }
895a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
896a3667aaeSNaresh Kumar Inna
897a3667aaeSNaresh Kumar Inna /* Delete physical lnodes */
898a3667aaeSNaresh Kumar Inna for (ii = 0; ii < cur_cnt; ii++) {
899a3667aaeSNaresh Kumar Inna csio_dbg(hw, "Deleting parent lnode: %p\n", lnode_list[ii]);
900a3667aaeSNaresh Kumar Inna csio_shost_exit(lnode_list[ii]);
901a3667aaeSNaresh Kumar Inna }
902a3667aaeSNaresh Kumar Inna
903a3667aaeSNaresh Kumar Inna free_lnodes:
904a3667aaeSNaresh Kumar Inna kfree(lnode_list);
905a3667aaeSNaresh Kumar Inna }
906a3667aaeSNaresh Kumar Inna
907a3667aaeSNaresh Kumar Inna /*
908a3667aaeSNaresh Kumar Inna * csio_lnode_init_post: Set lnode attributes after starting HW.
909a3667aaeSNaresh Kumar Inna * @ln: lnode.
910a3667aaeSNaresh Kumar Inna *
911a3667aaeSNaresh Kumar Inna */
912a3667aaeSNaresh Kumar Inna static void
csio_lnode_init_post(struct csio_lnode * ln)913a3667aaeSNaresh Kumar Inna csio_lnode_init_post(struct csio_lnode *ln)
914a3667aaeSNaresh Kumar Inna {
915a3667aaeSNaresh Kumar Inna struct Scsi_Host *shost = csio_ln_to_shost(ln);
916a3667aaeSNaresh Kumar Inna
917a3667aaeSNaresh Kumar Inna csio_fchost_attr_init(ln);
918a3667aaeSNaresh Kumar Inna
919a3667aaeSNaresh Kumar Inna scsi_scan_host(shost);
920a3667aaeSNaresh Kumar Inna }
921a3667aaeSNaresh Kumar Inna
922a3667aaeSNaresh Kumar Inna /*
923a3667aaeSNaresh Kumar Inna * csio_probe_one - Instantiate this function.
924a3667aaeSNaresh Kumar Inna * @pdev: PCI device
925a3667aaeSNaresh Kumar Inna * @id: Device ID
926a3667aaeSNaresh Kumar Inna *
927a3667aaeSNaresh Kumar Inna * This is the .probe() callback of the driver. This function:
928a3667aaeSNaresh Kumar Inna * - Initializes the PCI function by enabling MMIO, setting bus
929a3667aaeSNaresh Kumar Inna * mastership and setting DMA mask.
930a3667aaeSNaresh Kumar Inna * - Allocates HW structure, DMA, memory resources, maps BARS to
931a3667aaeSNaresh Kumar Inna * host memory and initializes HW module.
932a3667aaeSNaresh Kumar Inna * - Allocates lnode structure via scsi_host_alloc, initializes
933a3667aaeSNaresh Kumar Inna * shost, initialized lnode module and registers with SCSI ML
934a3667aaeSNaresh Kumar Inna * via scsi_host_add.
935a3667aaeSNaresh Kumar Inna * - Enables interrupts, and starts the chip by kicking off the
936a3667aaeSNaresh Kumar Inna * HW state machine.
937a3667aaeSNaresh Kumar Inna * - Once hardware is ready, initiated scan of the host via
938a3667aaeSNaresh Kumar Inna * scsi_scan_host.
939a3667aaeSNaresh Kumar Inna */
csio_probe_one(struct pci_dev * pdev,const struct pci_device_id * id)9406f039790SGreg Kroah-Hartman static int csio_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
941a3667aaeSNaresh Kumar Inna {
942a3667aaeSNaresh Kumar Inna int rv;
943a3667aaeSNaresh Kumar Inna int bars;
944a3667aaeSNaresh Kumar Inna int i;
945a3667aaeSNaresh Kumar Inna struct csio_hw *hw;
946a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
947a3667aaeSNaresh Kumar Inna
9484bbd458eSVarun Prakash /* probe only T5 and T6 cards */
9494bbd458eSVarun Prakash if (!csio_is_t5((pdev->device & CSIO_HW_CHIP_MASK)) &&
9504bbd458eSVarun Prakash !csio_is_t6((pdev->device & CSIO_HW_CHIP_MASK)))
9513fedeab1SHariprasad Shenai return -ENODEV;
9523fedeab1SHariprasad Shenai
953a3667aaeSNaresh Kumar Inna rv = csio_pci_init(pdev, &bars);
954a3667aaeSNaresh Kumar Inna if (rv)
955a3667aaeSNaresh Kumar Inna goto err;
956a3667aaeSNaresh Kumar Inna
957a3667aaeSNaresh Kumar Inna hw = csio_hw_alloc(pdev);
958a3667aaeSNaresh Kumar Inna if (!hw) {
959a3667aaeSNaresh Kumar Inna rv = -ENODEV;
960a3667aaeSNaresh Kumar Inna goto err_pci_exit;
961a3667aaeSNaresh Kumar Inna }
962a3667aaeSNaresh Kumar Inna
963ff6e88f1SVarun Prakash if (!pcie_relaxed_ordering_enabled(pdev))
964ff6e88f1SVarun Prakash hw->flags |= CSIO_HWF_ROOT_NO_RELAXED_ORDERING;
965ff6e88f1SVarun Prakash
966a3667aaeSNaresh Kumar Inna pci_set_drvdata(pdev, hw);
967a3667aaeSNaresh Kumar Inna
96882f0fd06SVarun Prakash rv = csio_hw_start(hw);
96982f0fd06SVarun Prakash if (rv) {
97082f0fd06SVarun Prakash if (rv == -EINVAL) {
971a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev,
972a3667aaeSNaresh Kumar Inna "Failed to start FW, continuing in debug mode.\n");
973a3667aaeSNaresh Kumar Inna return 0;
974a3667aaeSNaresh Kumar Inna }
97582f0fd06SVarun Prakash goto err_lnode_exit;
97682f0fd06SVarun Prakash }
977a3667aaeSNaresh Kumar Inna
978a3667aaeSNaresh Kumar Inna sprintf(hw->fwrev_str, "%u.%u.%u.%u\n",
979b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MAJOR_G(hw->fwrev),
980b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MINOR_G(hw->fwrev),
981b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_MICRO_G(hw->fwrev),
982b2e1a3f0SHariprasad Shenai FW_HDR_FW_VER_BUILD_G(hw->fwrev));
983a3667aaeSNaresh Kumar Inna
984a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
985a3667aaeSNaresh Kumar Inna ln = csio_shost_init(hw, &pdev->dev, true, NULL);
986a3667aaeSNaresh Kumar Inna if (!ln) {
987a3667aaeSNaresh Kumar Inna rv = -ENODEV;
988a3667aaeSNaresh Kumar Inna break;
989a3667aaeSNaresh Kumar Inna }
990a3667aaeSNaresh Kumar Inna /* Initialize portid */
991a3667aaeSNaresh Kumar Inna ln->portid = hw->pport[i].portid;
992a3667aaeSNaresh Kumar Inna
993a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
994a3667aaeSNaresh Kumar Inna if (csio_lnode_start(ln) != 0)
995a3667aaeSNaresh Kumar Inna rv = -ENODEV;
996a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
997a3667aaeSNaresh Kumar Inna
998a3667aaeSNaresh Kumar Inna if (rv)
999a3667aaeSNaresh Kumar Inna break;
1000a3667aaeSNaresh Kumar Inna
1001a3667aaeSNaresh Kumar Inna csio_lnode_init_post(ln);
1002a3667aaeSNaresh Kumar Inna }
1003a3667aaeSNaresh Kumar Inna
1004a3667aaeSNaresh Kumar Inna if (rv)
1005a3667aaeSNaresh Kumar Inna goto err_lnode_exit;
1006a3667aaeSNaresh Kumar Inna
1007a3667aaeSNaresh Kumar Inna return 0;
1008a3667aaeSNaresh Kumar Inna
1009a3667aaeSNaresh Kumar Inna err_lnode_exit:
1010a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1011a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1012a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1013a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1014a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1015a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1016a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1017a3667aaeSNaresh Kumar Inna err_pci_exit:
1018a3667aaeSNaresh Kumar Inna csio_pci_exit(pdev, &bars);
1019a3667aaeSNaresh Kumar Inna err:
1020a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "probe of device failed: %d\n", rv);
1021a3667aaeSNaresh Kumar Inna return rv;
1022a3667aaeSNaresh Kumar Inna }
1023a3667aaeSNaresh Kumar Inna
1024a3667aaeSNaresh Kumar Inna /*
1025a3667aaeSNaresh Kumar Inna * csio_remove_one - Remove one instance of the driver at this PCI function.
1026a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1027a3667aaeSNaresh Kumar Inna *
1028a3667aaeSNaresh Kumar Inna * Used during hotplug operation.
1029a3667aaeSNaresh Kumar Inna */
csio_remove_one(struct pci_dev * pdev)10306f039790SGreg Kroah-Hartman static void csio_remove_one(struct pci_dev *pdev)
1031a3667aaeSNaresh Kumar Inna {
1032a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1033a3667aaeSNaresh Kumar Inna int bars = pci_select_bars(pdev, IORESOURCE_MEM);
1034a3667aaeSNaresh Kumar Inna
1035a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1036a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1037a3667aaeSNaresh Kumar Inna
1038a3667aaeSNaresh Kumar Inna /* Stops lnode, Rnode s/m
1039a3667aaeSNaresh Kumar Inna * Quiesce IOs.
1040a3667aaeSNaresh Kumar Inna * All sessions with remote ports are unregistered.
1041a3667aaeSNaresh Kumar Inna */
1042a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1043a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1044a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1045a3667aaeSNaresh Kumar Inna
1046a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1047a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1048a3667aaeSNaresh Kumar Inna csio_pci_exit(pdev, &bars);
1049a3667aaeSNaresh Kumar Inna }
1050a3667aaeSNaresh Kumar Inna
1051a3667aaeSNaresh Kumar Inna /*
1052a3667aaeSNaresh Kumar Inna * csio_pci_error_detected - PCI error was detected
1053a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1054a3667aaeSNaresh Kumar Inna *
1055a3667aaeSNaresh Kumar Inna */
1056a3667aaeSNaresh Kumar Inna static pci_ers_result_t
csio_pci_error_detected(struct pci_dev * pdev,pci_channel_state_t state)1057a3667aaeSNaresh Kumar Inna csio_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
1058a3667aaeSNaresh Kumar Inna {
1059a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1060a3667aaeSNaresh Kumar Inna
1061a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1062a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1063a3667aaeSNaresh Kumar Inna
1064a3667aaeSNaresh Kumar Inna /* Post PCI error detected evt to HW s/m
1065a3667aaeSNaresh Kumar Inna * HW s/m handles this evt by quiescing IOs, unregisters rports
1066a3667aaeSNaresh Kumar Inna * and finally takes the device to offline.
1067a3667aaeSNaresh Kumar Inna */
1068a3667aaeSNaresh Kumar Inna csio_post_event(&hw->sm, CSIO_HWE_PCIERR_DETECTED);
1069a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1070a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1071a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1072a3667aaeSNaresh Kumar Inna csio_intr_disable(hw, true);
1073a3667aaeSNaresh Kumar Inna pci_disable_device(pdev);
1074a3667aaeSNaresh Kumar Inna return state == pci_channel_io_perm_failure ?
1075a3667aaeSNaresh Kumar Inna PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_NEED_RESET;
1076a3667aaeSNaresh Kumar Inna }
1077a3667aaeSNaresh Kumar Inna
1078a3667aaeSNaresh Kumar Inna /*
1079a3667aaeSNaresh Kumar Inna * csio_pci_slot_reset - PCI slot has been reset.
1080a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1081a3667aaeSNaresh Kumar Inna *
1082a3667aaeSNaresh Kumar Inna */
1083a3667aaeSNaresh Kumar Inna static pci_ers_result_t
csio_pci_slot_reset(struct pci_dev * pdev)1084a3667aaeSNaresh Kumar Inna csio_pci_slot_reset(struct pci_dev *pdev)
1085a3667aaeSNaresh Kumar Inna {
1086a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1087a3667aaeSNaresh Kumar Inna int ready;
1088a3667aaeSNaresh Kumar Inna
1089a3667aaeSNaresh Kumar Inna if (pci_enable_device(pdev)) {
1090a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "cannot re-enable device in slot reset\n");
1091a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_DISCONNECT;
1092a3667aaeSNaresh Kumar Inna }
1093a3667aaeSNaresh Kumar Inna
1094a3667aaeSNaresh Kumar Inna pci_set_master(pdev);
1095a3667aaeSNaresh Kumar Inna pci_restore_state(pdev);
1096a3667aaeSNaresh Kumar Inna pci_save_state(pdev);
1097a3667aaeSNaresh Kumar Inna
1098a3667aaeSNaresh Kumar Inna /* Bring HW s/m to ready state.
1099a3667aaeSNaresh Kumar Inna * but don't resume IOs.
1100a3667aaeSNaresh Kumar Inna */
1101a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1102a3667aaeSNaresh Kumar Inna csio_post_event(&hw->sm, CSIO_HWE_PCIERR_SLOT_RESET);
1103a3667aaeSNaresh Kumar Inna ready = csio_is_hw_ready(hw);
1104a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1105a3667aaeSNaresh Kumar Inna
1106a3667aaeSNaresh Kumar Inna if (ready) {
1107a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_RECOVERED;
1108a3667aaeSNaresh Kumar Inna } else {
1109a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "Can't initialize HW when in slot reset\n");
1110a3667aaeSNaresh Kumar Inna return PCI_ERS_RESULT_DISCONNECT;
1111a3667aaeSNaresh Kumar Inna }
1112a3667aaeSNaresh Kumar Inna }
1113a3667aaeSNaresh Kumar Inna
1114a3667aaeSNaresh Kumar Inna /*
1115a3667aaeSNaresh Kumar Inna * csio_pci_resume - Resume normal operations
1116a3667aaeSNaresh Kumar Inna * @pdev: PCI device
1117a3667aaeSNaresh Kumar Inna *
1118a3667aaeSNaresh Kumar Inna */
1119a3667aaeSNaresh Kumar Inna static void
csio_pci_resume(struct pci_dev * pdev)1120a3667aaeSNaresh Kumar Inna csio_pci_resume(struct pci_dev *pdev)
1121a3667aaeSNaresh Kumar Inna {
1122a3667aaeSNaresh Kumar Inna struct csio_hw *hw = pci_get_drvdata(pdev);
1123a3667aaeSNaresh Kumar Inna struct csio_lnode *ln;
1124a3667aaeSNaresh Kumar Inna int rv = 0;
1125a3667aaeSNaresh Kumar Inna int i;
1126a3667aaeSNaresh Kumar Inna
1127a3667aaeSNaresh Kumar Inna /* Bring the LINK UP and Resume IO */
1128a3667aaeSNaresh Kumar Inna
1129a3667aaeSNaresh Kumar Inna for (i = 0; i < hw->num_pports; i++) {
1130a3667aaeSNaresh Kumar Inna ln = csio_shost_init(hw, &pdev->dev, true, NULL);
1131a3667aaeSNaresh Kumar Inna if (!ln) {
1132a3667aaeSNaresh Kumar Inna rv = -ENODEV;
1133a3667aaeSNaresh Kumar Inna break;
1134a3667aaeSNaresh Kumar Inna }
1135a3667aaeSNaresh Kumar Inna /* Initialize portid */
1136a3667aaeSNaresh Kumar Inna ln->portid = hw->pport[i].portid;
1137a3667aaeSNaresh Kumar Inna
1138a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1139a3667aaeSNaresh Kumar Inna if (csio_lnode_start(ln) != 0)
1140a3667aaeSNaresh Kumar Inna rv = -ENODEV;
1141a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1142a3667aaeSNaresh Kumar Inna
1143a3667aaeSNaresh Kumar Inna if (rv)
1144a3667aaeSNaresh Kumar Inna break;
1145a3667aaeSNaresh Kumar Inna
1146a3667aaeSNaresh Kumar Inna csio_lnode_init_post(ln);
1147a3667aaeSNaresh Kumar Inna }
1148a3667aaeSNaresh Kumar Inna
1149a3667aaeSNaresh Kumar Inna if (rv)
1150a3667aaeSNaresh Kumar Inna goto err_resume_exit;
1151a3667aaeSNaresh Kumar Inna
1152a3667aaeSNaresh Kumar Inna return;
1153a3667aaeSNaresh Kumar Inna
1154a3667aaeSNaresh Kumar Inna err_resume_exit:
1155a3667aaeSNaresh Kumar Inna csio_lnodes_block_request(hw);
1156a3667aaeSNaresh Kumar Inna spin_lock_irq(&hw->lock);
1157a3667aaeSNaresh Kumar Inna csio_hw_stop(hw);
1158a3667aaeSNaresh Kumar Inna spin_unlock_irq(&hw->lock);
1159a3667aaeSNaresh Kumar Inna csio_lnodes_unblock_request(hw);
1160a3667aaeSNaresh Kumar Inna csio_lnodes_exit(hw, 0);
1161a3667aaeSNaresh Kumar Inna csio_hw_free(hw);
1162a3667aaeSNaresh Kumar Inna dev_err(&pdev->dev, "resume of device failed: %d\n", rv);
1163a3667aaeSNaresh Kumar Inna }
1164a3667aaeSNaresh Kumar Inna
1165a3667aaeSNaresh Kumar Inna static struct pci_error_handlers csio_err_handler = {
1166a3667aaeSNaresh Kumar Inna .error_detected = csio_pci_error_detected,
1167a3667aaeSNaresh Kumar Inna .slot_reset = csio_pci_slot_reset,
1168a3667aaeSNaresh Kumar Inna .resume = csio_pci_resume,
1169a3667aaeSNaresh Kumar Inna };
1170a3667aaeSNaresh Kumar Inna
11713fedeab1SHariprasad Shenai /*
11723fedeab1SHariprasad Shenai * Macros needed to support the PCI Device ID Table ...
11733fedeab1SHariprasad Shenai */
11743fedeab1SHariprasad Shenai #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
1175768ffc66SHariprasad Shenai static const struct pci_device_id csio_pci_tbl[] = {
11763fb4c22eSPraveen Madhavan /* Define for FCoE uses PF6 */
11773fb4c22eSPraveen Madhavan #define CH_PCI_DEVICE_ID_FUNCTION 0x6
1178a3667aaeSNaresh Kumar Inna
11793fedeab1SHariprasad Shenai #define CH_PCI_ID_TABLE_ENTRY(devid) \
11803fedeab1SHariprasad Shenai { PCI_VDEVICE(CHELSIO, (devid)), 0 }
11813fedeab1SHariprasad Shenai
11823fedeab1SHariprasad Shenai #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END { 0, } }
11833fedeab1SHariprasad Shenai
11843fedeab1SHariprasad Shenai #include "t4_pci_id_tbl.h"
1185a3667aaeSNaresh Kumar Inna
1186a3667aaeSNaresh Kumar Inna static struct pci_driver csio_pci_driver = {
1187a3667aaeSNaresh Kumar Inna .name = KBUILD_MODNAME,
1188a3667aaeSNaresh Kumar Inna .id_table = csio_pci_tbl,
1189a3667aaeSNaresh Kumar Inna .probe = csio_probe_one,
1190a3667aaeSNaresh Kumar Inna .remove = csio_remove_one,
1191a3667aaeSNaresh Kumar Inna .err_handler = &csio_err_handler,
1192a3667aaeSNaresh Kumar Inna };
1193a3667aaeSNaresh Kumar Inna
1194a3667aaeSNaresh Kumar Inna /*
1195a3667aaeSNaresh Kumar Inna * csio_init - Chelsio storage driver initialization function.
1196a3667aaeSNaresh Kumar Inna *
1197a3667aaeSNaresh Kumar Inna */
1198a3667aaeSNaresh Kumar Inna static int __init
csio_init(void)1199a3667aaeSNaresh Kumar Inna csio_init(void)
1200a3667aaeSNaresh Kumar Inna {
1201a3667aaeSNaresh Kumar Inna int rv = -ENOMEM;
1202a3667aaeSNaresh Kumar Inna
1203a3667aaeSNaresh Kumar Inna pr_info("%s %s\n", CSIO_DRV_DESC, CSIO_DRV_VERSION);
1204a3667aaeSNaresh Kumar Inna
1205a3667aaeSNaresh Kumar Inna csio_dfs_init();
1206a3667aaeSNaresh Kumar Inna
1207a3667aaeSNaresh Kumar Inna csio_fcoe_transport = fc_attach_transport(&csio_fc_transport_funcs);
1208a3667aaeSNaresh Kumar Inna if (!csio_fcoe_transport)
1209a3667aaeSNaresh Kumar Inna goto err;
1210a3667aaeSNaresh Kumar Inna
1211a3667aaeSNaresh Kumar Inna csio_fcoe_transport_vport =
1212a3667aaeSNaresh Kumar Inna fc_attach_transport(&csio_fc_transport_vport_funcs);
1213a3667aaeSNaresh Kumar Inna if (!csio_fcoe_transport_vport)
1214a3667aaeSNaresh Kumar Inna goto err_vport;
1215a3667aaeSNaresh Kumar Inna
1216a3667aaeSNaresh Kumar Inna rv = pci_register_driver(&csio_pci_driver);
1217a3667aaeSNaresh Kumar Inna if (rv)
1218a3667aaeSNaresh Kumar Inna goto err_pci;
1219a3667aaeSNaresh Kumar Inna
1220a3667aaeSNaresh Kumar Inna return 0;
1221a3667aaeSNaresh Kumar Inna
1222a3667aaeSNaresh Kumar Inna err_pci:
1223a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport_vport);
1224a3667aaeSNaresh Kumar Inna err_vport:
1225a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport);
1226a3667aaeSNaresh Kumar Inna err:
1227a3667aaeSNaresh Kumar Inna csio_dfs_exit();
1228a3667aaeSNaresh Kumar Inna return rv;
1229a3667aaeSNaresh Kumar Inna }
1230a3667aaeSNaresh Kumar Inna
1231a3667aaeSNaresh Kumar Inna /*
1232a3667aaeSNaresh Kumar Inna * csio_exit - Chelsio storage driver uninitialization .
1233a3667aaeSNaresh Kumar Inna *
1234a3667aaeSNaresh Kumar Inna * Function that gets called in the unload path.
1235a3667aaeSNaresh Kumar Inna */
1236a3667aaeSNaresh Kumar Inna static void __exit
csio_exit(void)1237a3667aaeSNaresh Kumar Inna csio_exit(void)
1238a3667aaeSNaresh Kumar Inna {
1239a3667aaeSNaresh Kumar Inna pci_unregister_driver(&csio_pci_driver);
1240a3667aaeSNaresh Kumar Inna csio_dfs_exit();
1241a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport_vport);
1242a3667aaeSNaresh Kumar Inna fc_release_transport(csio_fcoe_transport);
1243a3667aaeSNaresh Kumar Inna }
1244a3667aaeSNaresh Kumar Inna
1245a3667aaeSNaresh Kumar Inna module_init(csio_init);
1246a3667aaeSNaresh Kumar Inna module_exit(csio_exit);
1247a3667aaeSNaresh Kumar Inna MODULE_AUTHOR(CSIO_DRV_AUTHOR);
1248a3667aaeSNaresh Kumar Inna MODULE_DESCRIPTION(CSIO_DRV_DESC);
124943af2146SGreg Kroah-Hartman MODULE_LICENSE("Dual BSD/GPL");
1250a3667aaeSNaresh Kumar Inna MODULE_DEVICE_TABLE(pci, csio_pci_tbl);
1251a3667aaeSNaresh Kumar Inna MODULE_VERSION(CSIO_DRV_VERSION);
12527cc16380SArvind Bhushan MODULE_FIRMWARE(FW_FNAME_T5);
12534bbd458eSVarun Prakash MODULE_FIRMWARE(FW_FNAME_T6);
125479a74822SRahul Lakkireddy MODULE_SOFTDEP("pre: cxgb4");
1255