blob: bf7d90cfbcfc2726473dd29f36696d2af27b721f [file] [log] [blame]
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001/*
Andy Yan20b09c22009-05-08 17:46:40 -04002 * Marvell 88SE64xx/88SE94xx pci init
3 *
4 * Copyright 2007 Red Hat, Inc.
5 * Copyright 2008 Marvell. <kewei@marvell.com>
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07006 * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
Andy Yan20b09c22009-05-08 17:46:40 -04007 *
8 * This file is licensed under GPLv2.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; version 2 of the
13 * License.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -040025
Jeff Garzikdd4969a2009-05-08 17:44:01 -040026
27#include "mv_sas.h"
Jeff Garzikdd4969a2009-05-08 17:44:01 -040028
Xiangliang Yu0b15fb12011-04-26 06:36:51 -070029static int lldd_max_execute_num = 1;
30module_param_named(collector, lldd_max_execute_num, int, S_IRUGO);
31MODULE_PARM_DESC(collector, "\n"
32 "\tIf greater than one, tells the SAS Layer to run in Task Collector\n"
33 "\tMode. If 1 or 0, tells the SAS Layer to run in Direct Mode.\n"
34 "\tThe mvsas SAS LLDD supports both modes.\n"
35 "\tDefault: 1 (Direct Mode).\n");
36
Xiangliang Yu83c7b612011-05-24 22:31:47 +080037int interrupt_coalescing = 0x80;
38
Jeff Garzikdd4969a2009-05-08 17:44:01 -040039static struct scsi_transport_template *mvs_stt;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -070040struct kmem_cache *mvs_task_list_cache;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040041static const struct mvs_chip_info mvs_chips[] = {
Xiangliang Yua4632aa2011-05-24 22:36:02 +080042 [chip_6320] = { 1, 2, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
43 [chip_6440] = { 1, 4, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
44 [chip_6485] = { 1, 8, 0x800, 33, 32, 6, 10, &mvs_64xx_dispatch, },
45 [chip_9180] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
46 [chip_9480] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
47 [chip_9445] = { 1, 4, 0x800, 17, 64, 8, 11, &mvs_94xx_dispatch, },
48 [chip_9485] = { 2, 4, 0x800, 17, 64, 8, 11, &mvs_94xx_dispatch, },
49 [chip_1300] = { 1, 4, 0x400, 17, 16, 6, 9, &mvs_64xx_dispatch, },
50 [chip_1320] = { 2, 4, 0x800, 17, 64, 8, 9, &mvs_94xx_dispatch, },
Jeff Garzikdd4969a2009-05-08 17:44:01 -040051};
52
Xiangliang Yu83c7b612011-05-24 22:31:47 +080053struct device_attribute *mvst_host_attrs[];
54
Andy Yan20b09c22009-05-08 17:46:40 -040055#define SOC_SAS_NUM 2
56
Jeff Garzikdd4969a2009-05-08 17:44:01 -040057static struct scsi_host_template mvs_sht = {
58 .module = THIS_MODULE,
59 .name = DRV_NAME,
60 .queuecommand = sas_queuecommand,
61 .target_alloc = sas_target_alloc,
62 .slave_configure = mvs_slave_configure,
63 .slave_destroy = sas_slave_destroy,
64 .scan_finished = mvs_scan_finished,
65 .scan_start = mvs_scan_start,
66 .change_queue_depth = sas_change_queue_depth,
67 .change_queue_type = sas_change_queue_type,
68 .bios_param = sas_bios_param,
69 .can_queue = 1,
70 .cmd_per_lun = 1,
71 .this_id = -1,
Xiangliang Yub89e8f52011-05-24 22:35:09 +080072 .sg_tablesize = SG_ALL,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040073 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
74 .use_clustering = ENABLE_CLUSTERING,
Srinivas9dc9fd92010-02-15 00:00:00 -060075 .eh_device_reset_handler = sas_eh_device_reset_handler,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040076 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
Andy Yan20b09c22009-05-08 17:46:40 -040077 .slave_alloc = mvs_slave_alloc,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040078 .target_destroy = sas_target_destroy,
79 .ioctl = sas_ioctl,
Xiangliang Yu83c7b612011-05-24 22:31:47 +080080 .shost_attrs = mvst_host_attrs,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040081};
82
83static struct sas_domain_function_template mvs_transport_ops = {
Andy Yan20b09c22009-05-08 17:46:40 -040084 .lldd_dev_found = mvs_dev_found,
Srinivas9dc9fd92010-02-15 00:00:00 -060085 .lldd_dev_gone = mvs_dev_gone,
Andy Yan20b09c22009-05-08 17:46:40 -040086 .lldd_execute_task = mvs_queue_command,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040087 .lldd_control_phy = mvs_phy_control,
Andy Yan20b09c22009-05-08 17:46:40 -040088
89 .lldd_abort_task = mvs_abort_task,
90 .lldd_abort_task_set = mvs_abort_task_set,
91 .lldd_clear_aca = mvs_clear_aca,
Srinivas9dc9fd92010-02-15 00:00:00 -060092 .lldd_clear_task_set = mvs_clear_task_set,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040093 .lldd_I_T_nexus_reset = mvs_I_T_nexus_reset,
Andy Yan20b09c22009-05-08 17:46:40 -040094 .lldd_lu_reset = mvs_lu_reset,
95 .lldd_query_task = mvs_query_task,
Andy Yan20b09c22009-05-08 17:46:40 -040096 .lldd_port_formed = mvs_port_formed,
97 .lldd_port_deformed = mvs_port_deformed,
98
Jeff Garzikdd4969a2009-05-08 17:44:01 -040099};
100
101static void __devinit mvs_phy_init(struct mvs_info *mvi, int phy_id)
102{
103 struct mvs_phy *phy = &mvi->phy[phy_id];
104 struct asd_sas_phy *sas_phy = &phy->sas_phy;
105
Andy Yan20b09c22009-05-08 17:46:40 -0400106 phy->mvi = mvi;
107 init_timer(&phy->timer);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400108 sas_phy->enabled = (phy_id < mvi->chip->n_phy) ? 1 : 0;
109 sas_phy->class = SAS;
110 sas_phy->iproto = SAS_PROTOCOL_ALL;
111 sas_phy->tproto = 0;
112 sas_phy->type = PHY_TYPE_PHYSICAL;
113 sas_phy->role = PHY_ROLE_INITIATOR;
114 sas_phy->oob_mode = OOB_NOT_CONNECTED;
115 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
116
117 sas_phy->id = phy_id;
118 sas_phy->sas_addr = &mvi->sas_addr[0];
119 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
Andy Yan20b09c22009-05-08 17:46:40 -0400120 sas_phy->ha = (struct sas_ha_struct *)mvi->shost->hostdata;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400121 sas_phy->lldd_phy = phy;
122}
123
124static void mvs_free(struct mvs_info *mvi)
125{
Andy Yan20b09c22009-05-08 17:46:40 -0400126 struct mvs_wq *mwq;
127 int slot_nr;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400128
129 if (!mvi)
130 return;
131
Andy Yan20b09c22009-05-08 17:46:40 -0400132 if (mvi->flags & MVF_FLAG_SOC)
133 slot_nr = MVS_SOC_SLOTS;
134 else
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800135 slot_nr = MVS_CHIP_SLOT_SZ;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400136
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700137 if (mvi->dma_pool)
138 pci_pool_destroy(mvi->dma_pool);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400139
140 if (mvi->tx)
Andy Yan20b09c22009-05-08 17:46:40 -0400141 dma_free_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400142 sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
143 mvi->tx, mvi->tx_dma);
144 if (mvi->rx_fis)
Andy Yan20b09c22009-05-08 17:46:40 -0400145 dma_free_coherent(mvi->dev, MVS_RX_FISL_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400146 mvi->rx_fis, mvi->rx_fis_dma);
147 if (mvi->rx)
Andy Yan20b09c22009-05-08 17:46:40 -0400148 dma_free_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400149 sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
150 mvi->rx, mvi->rx_dma);
151 if (mvi->slot)
Andy Yan20b09c22009-05-08 17:46:40 -0400152 dma_free_coherent(mvi->dev,
153 sizeof(*mvi->slot) * slot_nr,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400154 mvi->slot, mvi->slot_dma);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800155
Andy Yan20b09c22009-05-08 17:46:40 -0400156 if (mvi->bulk_buffer)
157 dma_free_coherent(mvi->dev, TRASH_BUCKET_SIZE,
158 mvi->bulk_buffer, mvi->bulk_buffer_dma);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800159 if (mvi->bulk_buffer1)
160 dma_free_coherent(mvi->dev, TRASH_BUCKET_SIZE,
161 mvi->bulk_buffer1, mvi->bulk_buffer_dma1);
Andy Yan20b09c22009-05-08 17:46:40 -0400162
163 MVS_CHIP_DISP->chip_iounmap(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400164 if (mvi->shost)
165 scsi_host_put(mvi->shost);
Andy Yan20b09c22009-05-08 17:46:40 -0400166 list_for_each_entry(mwq, &mvi->wq_list, entry)
167 cancel_delayed_work(&mwq->work_q);
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800168 kfree(mvi->tags);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400169 kfree(mvi);
170}
171
172#ifdef MVS_USE_TASKLET
Andy Yan20b09c22009-05-08 17:46:40 -0400173struct tasklet_struct mv_tasklet;
174static void mvs_tasklet(unsigned long opaque)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400175{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400176 unsigned long flags;
Andy Yan20b09c22009-05-08 17:46:40 -0400177 u32 stat;
178 u16 core_nr, i = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400179
Andy Yan20b09c22009-05-08 17:46:40 -0400180 struct mvs_info *mvi;
181 struct sas_ha_struct *sha = (struct sas_ha_struct *)opaque;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400182
Andy Yan20b09c22009-05-08 17:46:40 -0400183 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
184 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
185
186 if (unlikely(!mvi))
187 BUG_ON(1);
188
189 for (i = 0; i < core_nr; i++) {
190 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
191 stat = MVS_CHIP_DISP->isr_status(mvi, mvi->irq);
192 if (stat)
193 MVS_CHIP_DISP->isr(mvi, mvi->irq, stat);
194 }
195
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400196}
197#endif
198
199static irqreturn_t mvs_interrupt(int irq, void *opaque)
200{
Andy Yan20b09c22009-05-08 17:46:40 -0400201 u32 core_nr, i = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400202 u32 stat;
Andy Yan20b09c22009-05-08 17:46:40 -0400203 struct mvs_info *mvi;
204 struct sas_ha_struct *sha = opaque;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400205
Andy Yan20b09c22009-05-08 17:46:40 -0400206 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
207 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400208
Andy Yan20b09c22009-05-08 17:46:40 -0400209 if (unlikely(!mvi))
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400210 return IRQ_NONE;
211
Andy Yan20b09c22009-05-08 17:46:40 -0400212 stat = MVS_CHIP_DISP->isr_status(mvi, irq);
213 if (!stat)
214 return IRQ_NONE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400215
Andy Yan20b09c22009-05-08 17:46:40 -0400216#ifdef MVS_USE_TASKLET
217 tasklet_schedule(&mv_tasklet);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400218#else
Andy Yan20b09c22009-05-08 17:46:40 -0400219 for (i = 0; i < core_nr; i++) {
220 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
221 MVS_CHIP_DISP->isr(mvi, irq, stat);
222 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400223#endif
224 return IRQ_HANDLED;
225}
226
Andy Yan20b09c22009-05-08 17:46:40 -0400227static int __devinit mvs_alloc(struct mvs_info *mvi, struct Scsi_Host *shost)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400228{
Srinivas9dc9fd92010-02-15 00:00:00 -0600229 int i = 0, slot_nr;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700230 char pool_name[32];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400231
Andy Yan20b09c22009-05-08 17:46:40 -0400232 if (mvi->flags & MVF_FLAG_SOC)
233 slot_nr = MVS_SOC_SLOTS;
234 else
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800235 slot_nr = MVS_CHIP_SLOT_SZ;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400236
237 spin_lock_init(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -0400238 for (i = 0; i < mvi->chip->n_phy; i++) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400239 mvs_phy_init(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400240 mvi->port[i].wide_port_phymap = 0;
241 mvi->port[i].port_attached = 0;
242 INIT_LIST_HEAD(&mvi->port[i].list);
243 }
Andy Yan20b09c22009-05-08 17:46:40 -0400244 for (i = 0; i < MVS_MAX_DEVICES; i++) {
245 mvi->devices[i].taskfileset = MVS_ID_NOT_MAPPED;
246 mvi->devices[i].dev_type = NO_DEVICE;
247 mvi->devices[i].device_id = i;
248 mvi->devices[i].dev_status = MVS_DEV_NORMAL;
Srinivas9dc9fd92010-02-15 00:00:00 -0600249 init_timer(&mvi->devices[i].timer);
Andy Yan20b09c22009-05-08 17:46:40 -0400250 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400251
252 /*
253 * alloc and init our DMA areas
254 */
Andy Yan20b09c22009-05-08 17:46:40 -0400255 mvi->tx = dma_alloc_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400256 sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
257 &mvi->tx_dma, GFP_KERNEL);
258 if (!mvi->tx)
259 goto err_out;
260 memset(mvi->tx, 0, sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ);
Andy Yan20b09c22009-05-08 17:46:40 -0400261 mvi->rx_fis = dma_alloc_coherent(mvi->dev, MVS_RX_FISL_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400262 &mvi->rx_fis_dma, GFP_KERNEL);
263 if (!mvi->rx_fis)
264 goto err_out;
265 memset(mvi->rx_fis, 0, MVS_RX_FISL_SZ);
266
Andy Yan20b09c22009-05-08 17:46:40 -0400267 mvi->rx = dma_alloc_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400268 sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
269 &mvi->rx_dma, GFP_KERNEL);
270 if (!mvi->rx)
271 goto err_out;
272 memset(mvi->rx, 0, sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400273 mvi->rx[0] = cpu_to_le32(0xfff);
274 mvi->rx_cons = 0xfff;
275
Andy Yan20b09c22009-05-08 17:46:40 -0400276 mvi->slot = dma_alloc_coherent(mvi->dev,
277 sizeof(*mvi->slot) * slot_nr,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400278 &mvi->slot_dma, GFP_KERNEL);
279 if (!mvi->slot)
280 goto err_out;
Andy Yan20b09c22009-05-08 17:46:40 -0400281 memset(mvi->slot, 0, sizeof(*mvi->slot) * slot_nr);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400282
Andy Yan20b09c22009-05-08 17:46:40 -0400283 mvi->bulk_buffer = dma_alloc_coherent(mvi->dev,
284 TRASH_BUCKET_SIZE,
285 &mvi->bulk_buffer_dma, GFP_KERNEL);
286 if (!mvi->bulk_buffer)
287 goto err_out;
Xiangliang Yu8882f082011-05-24 22:33:11 +0800288
289 mvi->bulk_buffer1 = dma_alloc_coherent(mvi->dev,
290 TRASH_BUCKET_SIZE,
291 &mvi->bulk_buffer_dma1, GFP_KERNEL);
292 if (!mvi->bulk_buffer1)
293 goto err_out;
294
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700295 sprintf(pool_name, "%s%d", "mvs_dma_pool", mvi->id);
296 mvi->dma_pool = pci_pool_create(pool_name, mvi->pdev, MVS_SLOT_BUF_SZ, 16, 0);
297 if (!mvi->dma_pool) {
298 printk(KERN_DEBUG "failed to create dma pool %s.\n", pool_name);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400299 goto err_out;
Andy Yan20b09c22009-05-08 17:46:40 -0400300 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700301 mvi->tags_num = slot_nr;
302
Andy Yan20b09c22009-05-08 17:46:40 -0400303 /* Initialize tags */
304 mvs_tag_init(mvi);
305 return 0;
306err_out:
307 return 1;
308}
309
310
311int mvs_ioremap(struct mvs_info *mvi, int bar, int bar_ex)
312{
313 unsigned long res_start, res_len, res_flag, res_flag_ex = 0;
314 struct pci_dev *pdev = mvi->pdev;
315 if (bar_ex != -1) {
316 /*
317 * ioremap main and peripheral registers
318 */
319 res_start = pci_resource_start(pdev, bar_ex);
320 res_len = pci_resource_len(pdev, bar_ex);
321 if (!res_start || !res_len)
322 goto err_out;
323
324 res_flag_ex = pci_resource_flags(pdev, bar_ex);
325 if (res_flag_ex & IORESOURCE_MEM) {
326 if (res_flag_ex & IORESOURCE_CACHEABLE)
327 mvi->regs_ex = ioremap(res_start, res_len);
328 else
329 mvi->regs_ex = ioremap_nocache(res_start,
330 res_len);
331 } else
332 mvi->regs_ex = (void *)res_start;
333 if (!mvi->regs_ex)
334 goto err_out;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400335 }
336
Andy Yan20b09c22009-05-08 17:46:40 -0400337 res_start = pci_resource_start(pdev, bar);
338 res_len = pci_resource_len(pdev, bar);
339 if (!res_start || !res_len)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400340 goto err_out;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400341
Andy Yan20b09c22009-05-08 17:46:40 -0400342 res_flag = pci_resource_flags(pdev, bar);
343 if (res_flag & IORESOURCE_CACHEABLE)
344 mvi->regs = ioremap(res_start, res_len);
345 else
346 mvi->regs = ioremap_nocache(res_start, res_len);
347
348 if (!mvi->regs) {
349 if (mvi->regs_ex && (res_flag_ex & IORESOURCE_MEM))
350 iounmap(mvi->regs_ex);
351 mvi->regs_ex = NULL;
352 goto err_out;
353 }
354
355 return 0;
356err_out:
357 return -1;
358}
359
360void mvs_iounmap(void __iomem *regs)
361{
362 iounmap(regs);
363}
364
365static struct mvs_info *__devinit mvs_pci_alloc(struct pci_dev *pdev,
366 const struct pci_device_id *ent,
367 struct Scsi_Host *shost, unsigned int id)
368{
369 struct mvs_info *mvi;
370 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
371
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800372 mvi = kzalloc(sizeof(*mvi) +
373 (1L << mvs_chips[ent->driver_data].slot_width) *
374 sizeof(struct mvs_slot_info), GFP_KERNEL);
Andy Yan20b09c22009-05-08 17:46:40 -0400375 if (!mvi)
376 return NULL;
377
378 mvi->pdev = pdev;
379 mvi->dev = &pdev->dev;
380 mvi->chip_id = ent->driver_data;
381 mvi->chip = &mvs_chips[mvi->chip_id];
382 INIT_LIST_HEAD(&mvi->wq_list);
Andy Yan20b09c22009-05-08 17:46:40 -0400383
384 ((struct mvs_prv_info *)sha->lldd_ha)->mvi[id] = mvi;
385 ((struct mvs_prv_info *)sha->lldd_ha)->n_phy = mvi->chip->n_phy;
386
387 mvi->id = id;
388 mvi->sas = sha;
389 mvi->shost = shost;
390#ifdef MVS_USE_TASKLET
391 tasklet_init(&mv_tasklet, mvs_tasklet, (unsigned long)sha);
392#endif
393
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800394 mvi->tags = kzalloc(MVS_CHIP_SLOT_SZ>>3, GFP_KERNEL);
395 if (!mvi->tags)
396 goto err_out;
397
Andy Yan20b09c22009-05-08 17:46:40 -0400398 if (MVS_CHIP_DISP->chip_ioremap(mvi))
399 goto err_out;
400 if (!mvs_alloc(mvi, shost))
401 return mvi;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400402err_out:
403 mvs_free(mvi);
404 return NULL;
405}
406
407/* move to PCI layer or libata core? */
408static int pci_go_64(struct pci_dev *pdev)
409{
410 int rc;
411
412 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
413 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
414 if (rc) {
415 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
416 if (rc) {
417 dev_printk(KERN_ERR, &pdev->dev,
418 "64-bit DMA enable failed\n");
419 return rc;
420 }
421 }
422 } else {
423 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
424 if (rc) {
425 dev_printk(KERN_ERR, &pdev->dev,
426 "32-bit DMA enable failed\n");
427 return rc;
428 }
429 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
430 if (rc) {
431 dev_printk(KERN_ERR, &pdev->dev,
432 "32-bit consistent DMA enable failed\n");
433 return rc;
434 }
435 }
436
437 return rc;
438}
439
Andy Yan20b09c22009-05-08 17:46:40 -0400440static int __devinit mvs_prep_sas_ha_init(struct Scsi_Host *shost,
441 const struct mvs_chip_info *chip_info)
442{
443 int phy_nr, port_nr; unsigned short core_nr;
444 struct asd_sas_phy **arr_phy;
445 struct asd_sas_port **arr_port;
446 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
447
448 core_nr = chip_info->n_host;
449 phy_nr = core_nr * chip_info->n_phy;
450 port_nr = phy_nr;
451
452 memset(sha, 0x00, sizeof(struct sas_ha_struct));
453 arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
454 arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
455 if (!arr_phy || !arr_port)
456 goto exit_free;
457
458 sha->sas_phy = arr_phy;
459 sha->sas_port = arr_port;
Srinivas9dc9fd92010-02-15 00:00:00 -0600460 sha->core.shost = shost;
Andy Yan20b09c22009-05-08 17:46:40 -0400461
462 sha->lldd_ha = kzalloc(sizeof(struct mvs_prv_info), GFP_KERNEL);
463 if (!sha->lldd_ha)
464 goto exit_free;
465
466 ((struct mvs_prv_info *)sha->lldd_ha)->n_host = core_nr;
467
468 shost->transportt = mvs_stt;
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800469 shost->max_id = MVS_MAX_DEVICES;
Andy Yan20b09c22009-05-08 17:46:40 -0400470 shost->max_lun = ~0;
471 shost->max_channel = 1;
472 shost->max_cmd_len = 16;
473
474 return 0;
475exit_free:
476 kfree(arr_phy);
477 kfree(arr_port);
478 return -1;
479
480}
481
482static void __devinit mvs_post_sas_ha_init(struct Scsi_Host *shost,
483 const struct mvs_chip_info *chip_info)
484{
485 int can_queue, i = 0, j = 0;
486 struct mvs_info *mvi = NULL;
487 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
488 unsigned short nr_core = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
489
490 for (j = 0; j < nr_core; j++) {
491 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
492 for (i = 0; i < chip_info->n_phy; i++) {
493 sha->sas_phy[j * chip_info->n_phy + i] =
494 &mvi->phy[i].sas_phy;
495 sha->sas_port[j * chip_info->n_phy + i] =
496 &mvi->port[i].sas_port;
497 }
498 }
499
500 sha->sas_ha_name = DRV_NAME;
501 sha->dev = mvi->dev;
502 sha->lldd_module = THIS_MODULE;
503 sha->sas_addr = &mvi->sas_addr[0];
504
505 sha->num_phys = nr_core * chip_info->n_phy;
506
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700507 sha->lldd_max_execute_num = lldd_max_execute_num;
Andy Yan20b09c22009-05-08 17:46:40 -0400508
509 if (mvi->flags & MVF_FLAG_SOC)
510 can_queue = MVS_SOC_CAN_QUEUE;
511 else
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800512 can_queue = MVS_CHIP_SLOT_SZ;
Andy Yan20b09c22009-05-08 17:46:40 -0400513
514 sha->lldd_queue_size = can_queue;
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800515 shost->sg_tablesize = min_t(u16, SG_ALL, MVS_MAX_SG);
Andy Yan20b09c22009-05-08 17:46:40 -0400516 shost->can_queue = can_queue;
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800517 mvi->shost->cmd_per_lun = MVS_QUEUE_SIZE;
Andy Yan20b09c22009-05-08 17:46:40 -0400518 sha->core.shost = mvi->shost;
519}
520
521static void mvs_init_sas_add(struct mvs_info *mvi)
522{
523 u8 i;
524 for (i = 0; i < mvi->chip->n_phy; i++) {
525 mvi->phy[i].dev_sas_addr = 0x5005043011ab0000ULL;
526 mvi->phy[i].dev_sas_addr =
527 cpu_to_be64((u64)(*(u64 *)&mvi->phy[i].dev_sas_addr));
528 }
529
530 memcpy(mvi->sas_addr, &mvi->phy[0].dev_sas_addr, SAS_ADDR_SIZE);
531}
532
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400533static int __devinit mvs_pci_init(struct pci_dev *pdev,
534 const struct pci_device_id *ent)
535{
Andy Yan20b09c22009-05-08 17:46:40 -0400536 unsigned int rc, nhost = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400537 struct mvs_info *mvi;
538 irq_handler_t irq_handler = mvs_interrupt;
Andy Yan20b09c22009-05-08 17:46:40 -0400539 struct Scsi_Host *shost = NULL;
540 const struct mvs_chip_info *chip;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400541
Andy Yan20b09c22009-05-08 17:46:40 -0400542 dev_printk(KERN_INFO, &pdev->dev,
543 "mvsas: driver version %s\n", DRV_VERSION);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400544 rc = pci_enable_device(pdev);
545 if (rc)
Andy Yan20b09c22009-05-08 17:46:40 -0400546 goto err_out_enable;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400547
548 pci_set_master(pdev);
549
550 rc = pci_request_regions(pdev, DRV_NAME);
551 if (rc)
552 goto err_out_disable;
553
554 rc = pci_go_64(pdev);
555 if (rc)
556 goto err_out_regions;
557
Andy Yan20b09c22009-05-08 17:46:40 -0400558 shost = scsi_host_alloc(&mvs_sht, sizeof(void *));
559 if (!shost) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400560 rc = -ENOMEM;
561 goto err_out_regions;
562 }
563
Andy Yan20b09c22009-05-08 17:46:40 -0400564 chip = &mvs_chips[ent->driver_data];
565 SHOST_TO_SAS_HA(shost) =
566 kcalloc(1, sizeof(struct sas_ha_struct), GFP_KERNEL);
567 if (!SHOST_TO_SAS_HA(shost)) {
568 kfree(shost);
569 rc = -ENOMEM;
570 goto err_out_regions;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400571 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400572
Andy Yan20b09c22009-05-08 17:46:40 -0400573 rc = mvs_prep_sas_ha_init(shost, chip);
574 if (rc) {
575 kfree(shost);
576 rc = -ENOMEM;
577 goto err_out_regions;
578 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400579
Andy Yan20b09c22009-05-08 17:46:40 -0400580 pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400581
Andy Yan20b09c22009-05-08 17:46:40 -0400582 do {
583 mvi = mvs_pci_alloc(pdev, ent, shost, nhost);
584 if (!mvi) {
585 rc = -ENOMEM;
586 goto err_out_regions;
587 }
588
Xiangliang Yuf1f82a92011-05-24 22:28:31 +0800589 memset(&mvi->hba_info_param, 0xFF,
590 sizeof(struct hba_info_page));
591
Andy Yan20b09c22009-05-08 17:46:40 -0400592 mvs_init_sas_add(mvi);
593
594 mvi->instance = nhost;
595 rc = MVS_CHIP_DISP->chip_init(mvi);
596 if (rc) {
597 mvs_free(mvi);
598 goto err_out_regions;
599 }
600 nhost++;
601 } while (nhost < chip->n_host);
Srinivas9dc9fd92010-02-15 00:00:00 -0600602#ifdef MVS_USE_TASKLET
603 tasklet_init(&mv_tasklet, mvs_tasklet,
604 (unsigned long)SHOST_TO_SAS_HA(shost));
605#endif
Andy Yan20b09c22009-05-08 17:46:40 -0400606
607 mvs_post_sas_ha_init(shost, chip);
608
609 rc = scsi_add_host(shost, &pdev->dev);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400610 if (rc)
611 goto err_out_shost;
612
Andy Yan20b09c22009-05-08 17:46:40 -0400613 rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
614 if (rc)
615 goto err_out_shost;
616 rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED,
617 DRV_NAME, SHOST_TO_SAS_HA(shost));
618 if (rc)
619 goto err_not_sas;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400620
Andy Yan20b09c22009-05-08 17:46:40 -0400621 MVS_CHIP_DISP->interrupt_enable(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400622
623 scsi_scan_host(mvi->shost);
624
625 return 0;
626
Andy Yan20b09c22009-05-08 17:46:40 -0400627err_not_sas:
628 sas_unregister_ha(SHOST_TO_SAS_HA(shost));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400629err_out_shost:
630 scsi_remove_host(mvi->shost);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400631err_out_regions:
632 pci_release_regions(pdev);
633err_out_disable:
634 pci_disable_device(pdev);
Andy Yan20b09c22009-05-08 17:46:40 -0400635err_out_enable:
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400636 return rc;
637}
638
639static void __devexit mvs_pci_remove(struct pci_dev *pdev)
640{
Andy Yan20b09c22009-05-08 17:46:40 -0400641 unsigned short core_nr, i = 0;
642 struct sas_ha_struct *sha = pci_get_drvdata(pdev);
643 struct mvs_info *mvi = NULL;
644
645 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
646 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
647
648#ifdef MVS_USE_TASKLET
649 tasklet_kill(&mv_tasklet);
650#endif
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400651
652 pci_set_drvdata(pdev, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -0400653 sas_unregister_ha(sha);
654 sas_remove_host(mvi->shost);
655 scsi_remove_host(mvi->shost);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400656
Andy Yan20b09c22009-05-08 17:46:40 -0400657 MVS_CHIP_DISP->interrupt_disable(mvi);
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800658 free_irq(mvi->pdev->irq, sha);
Andy Yan20b09c22009-05-08 17:46:40 -0400659 for (i = 0; i < core_nr; i++) {
660 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400661 mvs_free(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400662 }
Andy Yan20b09c22009-05-08 17:46:40 -0400663 kfree(sha->sas_phy);
664 kfree(sha->sas_port);
665 kfree(sha);
666 pci_release_regions(pdev);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400667 pci_disable_device(pdev);
Andy Yan20b09c22009-05-08 17:46:40 -0400668 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400669}
670
671static struct pci_device_id __devinitdata mvs_pci_table[] = {
672 { PCI_VDEVICE(MARVELL, 0x6320), chip_6320 },
673 { PCI_VDEVICE(MARVELL, 0x6340), chip_6440 },
674 {
675 .vendor = PCI_VENDOR_ID_MARVELL,
676 .device = 0x6440,
677 .subvendor = PCI_ANY_ID,
678 .subdevice = 0x6480,
679 .class = 0,
680 .class_mask = 0,
Andy Yan20b09c22009-05-08 17:46:40 -0400681 .driver_data = chip_6485,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400682 },
683 { PCI_VDEVICE(MARVELL, 0x6440), chip_6440 },
Andy Yan20b09c22009-05-08 17:46:40 -0400684 { PCI_VDEVICE(MARVELL, 0x6485), chip_6485 },
685 { PCI_VDEVICE(MARVELL, 0x9480), chip_9480 },
686 { PCI_VDEVICE(MARVELL, 0x9180), chip_9180 },
Nick Chengf31491d2009-09-08 19:03:07 +0800687 { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1300), chip_1300 },
688 { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1320), chip_1320 },
Srinivas7ec4ad02009-11-24 20:07:39 +0530689 { PCI_VDEVICE(ADAPTEC2, 0x0450), chip_6440 },
HighPoint Linux Team463b8972011-02-23 16:28:44 +0800690 { PCI_VDEVICE(TTI, 0x2710), chip_9480 },
691 { PCI_VDEVICE(TTI, 0x2720), chip_9480 },
692 { PCI_VDEVICE(TTI, 0x2721), chip_9480 },
693 { PCI_VDEVICE(TTI, 0x2722), chip_9480 },
694 { PCI_VDEVICE(TTI, 0x2740), chip_9480 },
695 { PCI_VDEVICE(TTI, 0x2744), chip_9480 },
696 { PCI_VDEVICE(TTI, 0x2760), chip_9480 },
Xiangliang Yu82140282011-04-26 06:34:01 -0700697 {
698 .vendor = 0x1b4b,
699 .device = 0x9445,
700 .subvendor = PCI_ANY_ID,
701 .subdevice = 0x9480,
702 .class = 0,
703 .class_mask = 0,
704 .driver_data = chip_9445,
705 },
706 {
707 .vendor = 0x1b4b,
708 .device = 0x9485,
709 .subvendor = PCI_ANY_ID,
710 .subdevice = 0x9480,
711 .class = 0,
712 .class_mask = 0,
713 .driver_data = chip_9485,
714 },
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400715
716 { } /* terminate list */
717};
718
719static struct pci_driver mvs_pci_driver = {
720 .name = DRV_NAME,
721 .id_table = mvs_pci_table,
722 .probe = mvs_pci_init,
723 .remove = __devexit_p(mvs_pci_remove),
724};
725
Xiangliang Yu83c7b612011-05-24 22:31:47 +0800726static ssize_t
727mvs_show_driver_version(struct device *cdev,
728 struct device_attribute *attr, char *buffer)
729{
730 return snprintf(buffer, PAGE_SIZE, "%s\n", DRV_VERSION);
731}
732
733static DEVICE_ATTR(driver_version,
734 S_IRUGO,
735 mvs_show_driver_version,
736 NULL);
737
738static ssize_t
739mvs_store_interrupt_coalescing(struct device *cdev,
740 struct device_attribute *attr,
741 const char *buffer, size_t size)
742{
743 int val = 0;
744 struct mvs_info *mvi = NULL;
745 struct Scsi_Host *shost = class_to_shost(cdev);
746 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
747 u8 i, core_nr;
748 if (buffer == NULL)
749 return size;
750
751 if (sscanf(buffer, "%d", &val) != 1)
752 return -EINVAL;
753
754 if (val >= 0x10000) {
755 mv_dprintk("interrupt coalescing timer %d us is"
756 "too long\n", val);
757 return strlen(buffer);
758 }
759
760 interrupt_coalescing = val;
761
762 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
763 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
764
765 if (unlikely(!mvi))
766 return -EINVAL;
767
768 for (i = 0; i < core_nr; i++) {
769 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
770 if (MVS_CHIP_DISP->tune_interrupt)
771 MVS_CHIP_DISP->tune_interrupt(mvi,
772 interrupt_coalescing);
773 }
774 mv_dprintk("set interrupt coalescing time to %d us\n",
775 interrupt_coalescing);
776 return strlen(buffer);
777}
778
779static ssize_t mvs_show_interrupt_coalescing(struct device *cdev,
780 struct device_attribute *attr, char *buffer)
781{
782 return snprintf(buffer, PAGE_SIZE, "%d\n", interrupt_coalescing);
783}
784
785static DEVICE_ATTR(interrupt_coalescing,
786 S_IRUGO|S_IWUSR,
787 mvs_show_interrupt_coalescing,
788 mvs_store_interrupt_coalescing);
789
Andy Yan20b09c22009-05-08 17:46:40 -0400790/* task handler */
791struct task_struct *mvs_th;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400792static int __init mvs_init(void)
793{
794 int rc;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400795 mvs_stt = sas_domain_attach_transport(&mvs_transport_ops);
796 if (!mvs_stt)
797 return -ENOMEM;
798
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700799 mvs_task_list_cache = kmem_cache_create("mvs_task_list", sizeof(struct mvs_task_list),
800 0, SLAB_HWCACHE_ALIGN, NULL);
801 if (!mvs_task_list_cache) {
802 rc = -ENOMEM;
803 mv_printk("%s: mvs_task_list_cache alloc failed! \n", __func__);
804 goto err_out;
805 }
806
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400807 rc = pci_register_driver(&mvs_pci_driver);
Andy Yan20b09c22009-05-08 17:46:40 -0400808
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400809 if (rc)
810 goto err_out;
811
812 return 0;
813
814err_out:
815 sas_release_transport(mvs_stt);
816 return rc;
817}
818
819static void __exit mvs_exit(void)
820{
821 pci_unregister_driver(&mvs_pci_driver);
822 sas_release_transport(mvs_stt);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700823 kmem_cache_destroy(mvs_task_list_cache);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400824}
825
Xiangliang Yu83c7b612011-05-24 22:31:47 +0800826struct device_attribute *mvst_host_attrs[] = {
827 &dev_attr_driver_version,
828 &dev_attr_interrupt_coalescing,
829 NULL,
830};
831
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400832module_init(mvs_init);
833module_exit(mvs_exit);
834
835MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
836MODULE_DESCRIPTION("Marvell 88SE6440 SAS/SATA controller driver");
837MODULE_VERSION(DRV_VERSION);
838MODULE_LICENSE("GPL");
Andy Yan20b09c22009-05-08 17:46:40 -0400839#ifdef CONFIG_PCI
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400840MODULE_DEVICE_TABLE(pci, mvs_pci_table);
Andy Yan20b09c22009-05-08 17:46:40 -0400841#endif