blob: 0123c6b6db9662ec4bab37c4da16f61549526184 [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>
6 *
7 * This file is licensed under GPLv2.
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; version 2 of the
12 * License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -040024
Jeff Garzikdd4969a2009-05-08 17:44:01 -040025
26#include "mv_sas.h"
Jeff Garzikdd4969a2009-05-08 17:44:01 -040027
28static struct scsi_transport_template *mvs_stt;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040029static const struct mvs_chip_info mvs_chips[] = {
Andy Yan20b09c22009-05-08 17:46:40 -040030 [chip_6320] = { 1, 2, 0x400, 17, 16, 9, &mvs_64xx_dispatch, },
31 [chip_6440] = { 1, 4, 0x400, 17, 16, 9, &mvs_64xx_dispatch, },
32 [chip_6485] = { 1, 8, 0x800, 33, 32, 10, &mvs_64xx_dispatch, },
33 [chip_9180] = { 2, 4, 0x800, 17, 64, 9, &mvs_94xx_dispatch, },
34 [chip_9480] = { 2, 4, 0x800, 17, 64, 9, &mvs_94xx_dispatch, },
Xiangliang Yu82140282011-04-26 06:34:01 -070035 [chip_9445] = { 1, 4, 0x800, 17, 64, 11, &mvs_94xx_dispatch, },
36 [chip_9485] = { 2, 4, 0x800, 17, 64, 11, &mvs_94xx_dispatch, },
Nick Chengf31491d2009-09-08 19:03:07 +080037 [chip_1300] = { 1, 4, 0x400, 17, 16, 9, &mvs_64xx_dispatch, },
38 [chip_1320] = { 2, 4, 0x800, 17, 64, 9, &mvs_94xx_dispatch, },
Jeff Garzikdd4969a2009-05-08 17:44:01 -040039};
40
Andy Yan20b09c22009-05-08 17:46:40 -040041#define SOC_SAS_NUM 2
Srinivas9dc9fd92010-02-15 00:00:00 -060042#define SG_MX 64
Andy Yan20b09c22009-05-08 17:46:40 -040043
Jeff Garzikdd4969a2009-05-08 17:44:01 -040044static struct scsi_host_template mvs_sht = {
45 .module = THIS_MODULE,
46 .name = DRV_NAME,
47 .queuecommand = sas_queuecommand,
48 .target_alloc = sas_target_alloc,
49 .slave_configure = mvs_slave_configure,
50 .slave_destroy = sas_slave_destroy,
51 .scan_finished = mvs_scan_finished,
52 .scan_start = mvs_scan_start,
53 .change_queue_depth = sas_change_queue_depth,
54 .change_queue_type = sas_change_queue_type,
55 .bios_param = sas_bios_param,
56 .can_queue = 1,
57 .cmd_per_lun = 1,
58 .this_id = -1,
Srinivas9dc9fd92010-02-15 00:00:00 -060059 .sg_tablesize = SG_MX,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040060 .max_sectors = SCSI_DEFAULT_MAX_SECTORS,
61 .use_clustering = ENABLE_CLUSTERING,
Srinivas9dc9fd92010-02-15 00:00:00 -060062 .eh_device_reset_handler = sas_eh_device_reset_handler,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040063 .eh_bus_reset_handler = sas_eh_bus_reset_handler,
Andy Yan20b09c22009-05-08 17:46:40 -040064 .slave_alloc = mvs_slave_alloc,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040065 .target_destroy = sas_target_destroy,
66 .ioctl = sas_ioctl,
67};
68
69static struct sas_domain_function_template mvs_transport_ops = {
Andy Yan20b09c22009-05-08 17:46:40 -040070 .lldd_dev_found = mvs_dev_found,
Srinivas9dc9fd92010-02-15 00:00:00 -060071 .lldd_dev_gone = mvs_dev_gone,
Andy Yan20b09c22009-05-08 17:46:40 -040072 .lldd_execute_task = mvs_queue_command,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040073 .lldd_control_phy = mvs_phy_control,
Andy Yan20b09c22009-05-08 17:46:40 -040074
75 .lldd_abort_task = mvs_abort_task,
76 .lldd_abort_task_set = mvs_abort_task_set,
77 .lldd_clear_aca = mvs_clear_aca,
Srinivas9dc9fd92010-02-15 00:00:00 -060078 .lldd_clear_task_set = mvs_clear_task_set,
Jeff Garzikdd4969a2009-05-08 17:44:01 -040079 .lldd_I_T_nexus_reset = mvs_I_T_nexus_reset,
Andy Yan20b09c22009-05-08 17:46:40 -040080 .lldd_lu_reset = mvs_lu_reset,
81 .lldd_query_task = mvs_query_task,
Andy Yan20b09c22009-05-08 17:46:40 -040082 .lldd_port_formed = mvs_port_formed,
83 .lldd_port_deformed = mvs_port_deformed,
84
Jeff Garzikdd4969a2009-05-08 17:44:01 -040085};
86
87static void __devinit mvs_phy_init(struct mvs_info *mvi, int phy_id)
88{
89 struct mvs_phy *phy = &mvi->phy[phy_id];
90 struct asd_sas_phy *sas_phy = &phy->sas_phy;
91
Andy Yan20b09c22009-05-08 17:46:40 -040092 phy->mvi = mvi;
93 init_timer(&phy->timer);
Jeff Garzikdd4969a2009-05-08 17:44:01 -040094 sas_phy->enabled = (phy_id < mvi->chip->n_phy) ? 1 : 0;
95 sas_phy->class = SAS;
96 sas_phy->iproto = SAS_PROTOCOL_ALL;
97 sas_phy->tproto = 0;
98 sas_phy->type = PHY_TYPE_PHYSICAL;
99 sas_phy->role = PHY_ROLE_INITIATOR;
100 sas_phy->oob_mode = OOB_NOT_CONNECTED;
101 sas_phy->linkrate = SAS_LINK_RATE_UNKNOWN;
102
103 sas_phy->id = phy_id;
104 sas_phy->sas_addr = &mvi->sas_addr[0];
105 sas_phy->frame_rcvd = &phy->frame_rcvd[0];
Andy Yan20b09c22009-05-08 17:46:40 -0400106 sas_phy->ha = (struct sas_ha_struct *)mvi->shost->hostdata;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400107 sas_phy->lldd_phy = phy;
108}
109
110static void mvs_free(struct mvs_info *mvi)
111{
112 int i;
Andy Yan20b09c22009-05-08 17:46:40 -0400113 struct mvs_wq *mwq;
114 int slot_nr;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400115
116 if (!mvi)
117 return;
118
Andy Yan20b09c22009-05-08 17:46:40 -0400119 if (mvi->flags & MVF_FLAG_SOC)
120 slot_nr = MVS_SOC_SLOTS;
121 else
122 slot_nr = MVS_SLOTS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400123
Andy Yan20b09c22009-05-08 17:46:40 -0400124 for (i = 0; i < mvi->tags_num; i++) {
125 struct mvs_slot_info *slot = &mvi->slot_info[i];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400126 if (slot->buf)
Andy Yan20b09c22009-05-08 17:46:40 -0400127 dma_free_coherent(mvi->dev, MVS_SLOT_BUF_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400128 slot->buf, slot->buf_dma);
129 }
130
131 if (mvi->tx)
Andy Yan20b09c22009-05-08 17:46:40 -0400132 dma_free_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400133 sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
134 mvi->tx, mvi->tx_dma);
135 if (mvi->rx_fis)
Andy Yan20b09c22009-05-08 17:46:40 -0400136 dma_free_coherent(mvi->dev, MVS_RX_FISL_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400137 mvi->rx_fis, mvi->rx_fis_dma);
138 if (mvi->rx)
Andy Yan20b09c22009-05-08 17:46:40 -0400139 dma_free_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400140 sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
141 mvi->rx, mvi->rx_dma);
142 if (mvi->slot)
Andy Yan20b09c22009-05-08 17:46:40 -0400143 dma_free_coherent(mvi->dev,
144 sizeof(*mvi->slot) * slot_nr,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400145 mvi->slot, mvi->slot_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400146#ifndef DISABLE_HOTPLUG_DMA_FIX
147 if (mvi->bulk_buffer)
148 dma_free_coherent(mvi->dev, TRASH_BUCKET_SIZE,
149 mvi->bulk_buffer, mvi->bulk_buffer_dma);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400150#endif
Andy Yan20b09c22009-05-08 17:46:40 -0400151
152 MVS_CHIP_DISP->chip_iounmap(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400153 if (mvi->shost)
154 scsi_host_put(mvi->shost);
Andy Yan20b09c22009-05-08 17:46:40 -0400155 list_for_each_entry(mwq, &mvi->wq_list, entry)
156 cancel_delayed_work(&mwq->work_q);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400157 kfree(mvi);
158}
159
160#ifdef MVS_USE_TASKLET
Andy Yan20b09c22009-05-08 17:46:40 -0400161struct tasklet_struct mv_tasklet;
162static void mvs_tasklet(unsigned long opaque)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400163{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400164 unsigned long flags;
Andy Yan20b09c22009-05-08 17:46:40 -0400165 u32 stat;
166 u16 core_nr, i = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400167
Andy Yan20b09c22009-05-08 17:46:40 -0400168 struct mvs_info *mvi;
169 struct sas_ha_struct *sha = (struct sas_ha_struct *)opaque;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400170
Andy Yan20b09c22009-05-08 17:46:40 -0400171 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
172 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
173
174 if (unlikely(!mvi))
175 BUG_ON(1);
176
177 for (i = 0; i < core_nr; i++) {
178 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
179 stat = MVS_CHIP_DISP->isr_status(mvi, mvi->irq);
180 if (stat)
181 MVS_CHIP_DISP->isr(mvi, mvi->irq, stat);
182 }
183
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400184}
185#endif
186
187static irqreturn_t mvs_interrupt(int irq, void *opaque)
188{
Andy Yan20b09c22009-05-08 17:46:40 -0400189 u32 core_nr, i = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400190 u32 stat;
Andy Yan20b09c22009-05-08 17:46:40 -0400191 struct mvs_info *mvi;
192 struct sas_ha_struct *sha = opaque;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400193
Andy Yan20b09c22009-05-08 17:46:40 -0400194 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
195 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400196
Andy Yan20b09c22009-05-08 17:46:40 -0400197 if (unlikely(!mvi))
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400198 return IRQ_NONE;
199
Andy Yan20b09c22009-05-08 17:46:40 -0400200 stat = MVS_CHIP_DISP->isr_status(mvi, irq);
201 if (!stat)
202 return IRQ_NONE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400203
Andy Yan20b09c22009-05-08 17:46:40 -0400204#ifdef MVS_USE_TASKLET
205 tasklet_schedule(&mv_tasklet);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400206#else
Andy Yan20b09c22009-05-08 17:46:40 -0400207 for (i = 0; i < core_nr; i++) {
208 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
209 MVS_CHIP_DISP->isr(mvi, irq, stat);
210 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400211#endif
212 return IRQ_HANDLED;
213}
214
Andy Yan20b09c22009-05-08 17:46:40 -0400215static int __devinit mvs_alloc(struct mvs_info *mvi, struct Scsi_Host *shost)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400216{
Srinivas9dc9fd92010-02-15 00:00:00 -0600217 int i = 0, slot_nr;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400218
Andy Yan20b09c22009-05-08 17:46:40 -0400219 if (mvi->flags & MVF_FLAG_SOC)
220 slot_nr = MVS_SOC_SLOTS;
221 else
222 slot_nr = MVS_SLOTS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400223
224 spin_lock_init(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -0400225 for (i = 0; i < mvi->chip->n_phy; i++) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400226 mvs_phy_init(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400227 mvi->port[i].wide_port_phymap = 0;
228 mvi->port[i].port_attached = 0;
229 INIT_LIST_HEAD(&mvi->port[i].list);
230 }
Andy Yan20b09c22009-05-08 17:46:40 -0400231 for (i = 0; i < MVS_MAX_DEVICES; i++) {
232 mvi->devices[i].taskfileset = MVS_ID_NOT_MAPPED;
233 mvi->devices[i].dev_type = NO_DEVICE;
234 mvi->devices[i].device_id = i;
235 mvi->devices[i].dev_status = MVS_DEV_NORMAL;
Srinivas9dc9fd92010-02-15 00:00:00 -0600236 init_timer(&mvi->devices[i].timer);
Andy Yan20b09c22009-05-08 17:46:40 -0400237 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400238
239 /*
240 * alloc and init our DMA areas
241 */
Andy Yan20b09c22009-05-08 17:46:40 -0400242 mvi->tx = dma_alloc_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400243 sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ,
244 &mvi->tx_dma, GFP_KERNEL);
245 if (!mvi->tx)
246 goto err_out;
247 memset(mvi->tx, 0, sizeof(*mvi->tx) * MVS_CHIP_SLOT_SZ);
Andy Yan20b09c22009-05-08 17:46:40 -0400248 mvi->rx_fis = dma_alloc_coherent(mvi->dev, MVS_RX_FISL_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400249 &mvi->rx_fis_dma, GFP_KERNEL);
250 if (!mvi->rx_fis)
251 goto err_out;
252 memset(mvi->rx_fis, 0, MVS_RX_FISL_SZ);
253
Andy Yan20b09c22009-05-08 17:46:40 -0400254 mvi->rx = dma_alloc_coherent(mvi->dev,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400255 sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1),
256 &mvi->rx_dma, GFP_KERNEL);
257 if (!mvi->rx)
258 goto err_out;
259 memset(mvi->rx, 0, sizeof(*mvi->rx) * (MVS_RX_RING_SZ + 1));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400260 mvi->rx[0] = cpu_to_le32(0xfff);
261 mvi->rx_cons = 0xfff;
262
Andy Yan20b09c22009-05-08 17:46:40 -0400263 mvi->slot = dma_alloc_coherent(mvi->dev,
264 sizeof(*mvi->slot) * slot_nr,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400265 &mvi->slot_dma, GFP_KERNEL);
266 if (!mvi->slot)
267 goto err_out;
Andy Yan20b09c22009-05-08 17:46:40 -0400268 memset(mvi->slot, 0, sizeof(*mvi->slot) * slot_nr);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400269
Andy Yan20b09c22009-05-08 17:46:40 -0400270#ifndef DISABLE_HOTPLUG_DMA_FIX
271 mvi->bulk_buffer = dma_alloc_coherent(mvi->dev,
272 TRASH_BUCKET_SIZE,
273 &mvi->bulk_buffer_dma, GFP_KERNEL);
274 if (!mvi->bulk_buffer)
275 goto err_out;
276#endif
277 for (i = 0; i < slot_nr; i++) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400278 struct mvs_slot_info *slot = &mvi->slot_info[i];
279
Andy Yan20b09c22009-05-08 17:46:40 -0400280 slot->buf = dma_alloc_coherent(mvi->dev, MVS_SLOT_BUF_SZ,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400281 &slot->buf_dma, GFP_KERNEL);
Andy Yan20b09c22009-05-08 17:46:40 -0400282 if (!slot->buf) {
283 printk(KERN_DEBUG"failed to allocate slot->buf.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400284 goto err_out;
Andy Yan20b09c22009-05-08 17:46:40 -0400285 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400286 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
Andy Yan20b09c22009-05-08 17:46:40 -0400287 ++mvi->tags_num;
288 }
289 /* Initialize tags */
290 mvs_tag_init(mvi);
291 return 0;
292err_out:
293 return 1;
294}
295
296
297int mvs_ioremap(struct mvs_info *mvi, int bar, int bar_ex)
298{
299 unsigned long res_start, res_len, res_flag, res_flag_ex = 0;
300 struct pci_dev *pdev = mvi->pdev;
301 if (bar_ex != -1) {
302 /*
303 * ioremap main and peripheral registers
304 */
305 res_start = pci_resource_start(pdev, bar_ex);
306 res_len = pci_resource_len(pdev, bar_ex);
307 if (!res_start || !res_len)
308 goto err_out;
309
310 res_flag_ex = pci_resource_flags(pdev, bar_ex);
311 if (res_flag_ex & IORESOURCE_MEM) {
312 if (res_flag_ex & IORESOURCE_CACHEABLE)
313 mvi->regs_ex = ioremap(res_start, res_len);
314 else
315 mvi->regs_ex = ioremap_nocache(res_start,
316 res_len);
317 } else
318 mvi->regs_ex = (void *)res_start;
319 if (!mvi->regs_ex)
320 goto err_out;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400321 }
322
Andy Yan20b09c22009-05-08 17:46:40 -0400323 res_start = pci_resource_start(pdev, bar);
324 res_len = pci_resource_len(pdev, bar);
325 if (!res_start || !res_len)
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400326 goto err_out;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400327
Andy Yan20b09c22009-05-08 17:46:40 -0400328 res_flag = pci_resource_flags(pdev, bar);
329 if (res_flag & IORESOURCE_CACHEABLE)
330 mvi->regs = ioremap(res_start, res_len);
331 else
332 mvi->regs = ioremap_nocache(res_start, res_len);
333
334 if (!mvi->regs) {
335 if (mvi->regs_ex && (res_flag_ex & IORESOURCE_MEM))
336 iounmap(mvi->regs_ex);
337 mvi->regs_ex = NULL;
338 goto err_out;
339 }
340
341 return 0;
342err_out:
343 return -1;
344}
345
346void mvs_iounmap(void __iomem *regs)
347{
348 iounmap(regs);
349}
350
351static struct mvs_info *__devinit mvs_pci_alloc(struct pci_dev *pdev,
352 const struct pci_device_id *ent,
353 struct Scsi_Host *shost, unsigned int id)
354{
355 struct mvs_info *mvi;
356 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
357
358 mvi = kzalloc(sizeof(*mvi) + MVS_SLOTS * sizeof(struct mvs_slot_info),
359 GFP_KERNEL);
360 if (!mvi)
361 return NULL;
362
363 mvi->pdev = pdev;
364 mvi->dev = &pdev->dev;
365 mvi->chip_id = ent->driver_data;
366 mvi->chip = &mvs_chips[mvi->chip_id];
367 INIT_LIST_HEAD(&mvi->wq_list);
368 mvi->irq = pdev->irq;
369
370 ((struct mvs_prv_info *)sha->lldd_ha)->mvi[id] = mvi;
371 ((struct mvs_prv_info *)sha->lldd_ha)->n_phy = mvi->chip->n_phy;
372
373 mvi->id = id;
374 mvi->sas = sha;
375 mvi->shost = shost;
376#ifdef MVS_USE_TASKLET
377 tasklet_init(&mv_tasklet, mvs_tasklet, (unsigned long)sha);
378#endif
379
380 if (MVS_CHIP_DISP->chip_ioremap(mvi))
381 goto err_out;
382 if (!mvs_alloc(mvi, shost))
383 return mvi;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400384err_out:
385 mvs_free(mvi);
386 return NULL;
387}
388
389/* move to PCI layer or libata core? */
390static int pci_go_64(struct pci_dev *pdev)
391{
392 int rc;
393
394 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
395 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
396 if (rc) {
397 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
398 if (rc) {
399 dev_printk(KERN_ERR, &pdev->dev,
400 "64-bit DMA enable failed\n");
401 return rc;
402 }
403 }
404 } else {
405 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
406 if (rc) {
407 dev_printk(KERN_ERR, &pdev->dev,
408 "32-bit DMA enable failed\n");
409 return rc;
410 }
411 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
412 if (rc) {
413 dev_printk(KERN_ERR, &pdev->dev,
414 "32-bit consistent DMA enable failed\n");
415 return rc;
416 }
417 }
418
419 return rc;
420}
421
Andy Yan20b09c22009-05-08 17:46:40 -0400422static int __devinit mvs_prep_sas_ha_init(struct Scsi_Host *shost,
423 const struct mvs_chip_info *chip_info)
424{
425 int phy_nr, port_nr; unsigned short core_nr;
426 struct asd_sas_phy **arr_phy;
427 struct asd_sas_port **arr_port;
428 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
429
430 core_nr = chip_info->n_host;
431 phy_nr = core_nr * chip_info->n_phy;
432 port_nr = phy_nr;
433
434 memset(sha, 0x00, sizeof(struct sas_ha_struct));
435 arr_phy = kcalloc(phy_nr, sizeof(void *), GFP_KERNEL);
436 arr_port = kcalloc(port_nr, sizeof(void *), GFP_KERNEL);
437 if (!arr_phy || !arr_port)
438 goto exit_free;
439
440 sha->sas_phy = arr_phy;
441 sha->sas_port = arr_port;
Srinivas9dc9fd92010-02-15 00:00:00 -0600442 sha->core.shost = shost;
Andy Yan20b09c22009-05-08 17:46:40 -0400443
444 sha->lldd_ha = kzalloc(sizeof(struct mvs_prv_info), GFP_KERNEL);
445 if (!sha->lldd_ha)
446 goto exit_free;
447
448 ((struct mvs_prv_info *)sha->lldd_ha)->n_host = core_nr;
449
450 shost->transportt = mvs_stt;
451 shost->max_id = 128;
452 shost->max_lun = ~0;
453 shost->max_channel = 1;
454 shost->max_cmd_len = 16;
455
456 return 0;
457exit_free:
458 kfree(arr_phy);
459 kfree(arr_port);
460 return -1;
461
462}
463
464static void __devinit mvs_post_sas_ha_init(struct Scsi_Host *shost,
465 const struct mvs_chip_info *chip_info)
466{
467 int can_queue, i = 0, j = 0;
468 struct mvs_info *mvi = NULL;
469 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
470 unsigned short nr_core = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
471
472 for (j = 0; j < nr_core; j++) {
473 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
474 for (i = 0; i < chip_info->n_phy; i++) {
475 sha->sas_phy[j * chip_info->n_phy + i] =
476 &mvi->phy[i].sas_phy;
477 sha->sas_port[j * chip_info->n_phy + i] =
478 &mvi->port[i].sas_port;
479 }
480 }
481
482 sha->sas_ha_name = DRV_NAME;
483 sha->dev = mvi->dev;
484 sha->lldd_module = THIS_MODULE;
485 sha->sas_addr = &mvi->sas_addr[0];
486
487 sha->num_phys = nr_core * chip_info->n_phy;
488
489 sha->lldd_max_execute_num = 1;
490
491 if (mvi->flags & MVF_FLAG_SOC)
492 can_queue = MVS_SOC_CAN_QUEUE;
493 else
494 can_queue = MVS_CAN_QUEUE;
495
496 sha->lldd_queue_size = can_queue;
497 shost->can_queue = can_queue;
498 mvi->shost->cmd_per_lun = MVS_SLOTS/sha->num_phys;
499 sha->core.shost = mvi->shost;
500}
501
502static void mvs_init_sas_add(struct mvs_info *mvi)
503{
504 u8 i;
505 for (i = 0; i < mvi->chip->n_phy; i++) {
506 mvi->phy[i].dev_sas_addr = 0x5005043011ab0000ULL;
507 mvi->phy[i].dev_sas_addr =
508 cpu_to_be64((u64)(*(u64 *)&mvi->phy[i].dev_sas_addr));
509 }
510
511 memcpy(mvi->sas_addr, &mvi->phy[0].dev_sas_addr, SAS_ADDR_SIZE);
512}
513
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400514static int __devinit mvs_pci_init(struct pci_dev *pdev,
515 const struct pci_device_id *ent)
516{
Andy Yan20b09c22009-05-08 17:46:40 -0400517 unsigned int rc, nhost = 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400518 struct mvs_info *mvi;
519 irq_handler_t irq_handler = mvs_interrupt;
Andy Yan20b09c22009-05-08 17:46:40 -0400520 struct Scsi_Host *shost = NULL;
521 const struct mvs_chip_info *chip;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400522
Andy Yan20b09c22009-05-08 17:46:40 -0400523 dev_printk(KERN_INFO, &pdev->dev,
524 "mvsas: driver version %s\n", DRV_VERSION);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400525 rc = pci_enable_device(pdev);
526 if (rc)
Andy Yan20b09c22009-05-08 17:46:40 -0400527 goto err_out_enable;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400528
529 pci_set_master(pdev);
530
531 rc = pci_request_regions(pdev, DRV_NAME);
532 if (rc)
533 goto err_out_disable;
534
535 rc = pci_go_64(pdev);
536 if (rc)
537 goto err_out_regions;
538
Andy Yan20b09c22009-05-08 17:46:40 -0400539 shost = scsi_host_alloc(&mvs_sht, sizeof(void *));
540 if (!shost) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400541 rc = -ENOMEM;
542 goto err_out_regions;
543 }
544
Andy Yan20b09c22009-05-08 17:46:40 -0400545 chip = &mvs_chips[ent->driver_data];
546 SHOST_TO_SAS_HA(shost) =
547 kcalloc(1, sizeof(struct sas_ha_struct), GFP_KERNEL);
548 if (!SHOST_TO_SAS_HA(shost)) {
549 kfree(shost);
550 rc = -ENOMEM;
551 goto err_out_regions;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400552 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400553
Andy Yan20b09c22009-05-08 17:46:40 -0400554 rc = mvs_prep_sas_ha_init(shost, chip);
555 if (rc) {
556 kfree(shost);
557 rc = -ENOMEM;
558 goto err_out_regions;
559 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400560
Andy Yan20b09c22009-05-08 17:46:40 -0400561 pci_set_drvdata(pdev, SHOST_TO_SAS_HA(shost));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400562
Andy Yan20b09c22009-05-08 17:46:40 -0400563 do {
564 mvi = mvs_pci_alloc(pdev, ent, shost, nhost);
565 if (!mvi) {
566 rc = -ENOMEM;
567 goto err_out_regions;
568 }
569
570 mvs_init_sas_add(mvi);
571
572 mvi->instance = nhost;
573 rc = MVS_CHIP_DISP->chip_init(mvi);
574 if (rc) {
575 mvs_free(mvi);
576 goto err_out_regions;
577 }
578 nhost++;
579 } while (nhost < chip->n_host);
Srinivas9dc9fd92010-02-15 00:00:00 -0600580#ifdef MVS_USE_TASKLET
581 tasklet_init(&mv_tasklet, mvs_tasklet,
582 (unsigned long)SHOST_TO_SAS_HA(shost));
583#endif
Andy Yan20b09c22009-05-08 17:46:40 -0400584
585 mvs_post_sas_ha_init(shost, chip);
586
587 rc = scsi_add_host(shost, &pdev->dev);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400588 if (rc)
589 goto err_out_shost;
590
Andy Yan20b09c22009-05-08 17:46:40 -0400591 rc = sas_register_ha(SHOST_TO_SAS_HA(shost));
592 if (rc)
593 goto err_out_shost;
594 rc = request_irq(pdev->irq, irq_handler, IRQF_SHARED,
595 DRV_NAME, SHOST_TO_SAS_HA(shost));
596 if (rc)
597 goto err_not_sas;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400598
Andy Yan20b09c22009-05-08 17:46:40 -0400599 MVS_CHIP_DISP->interrupt_enable(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400600
601 scsi_scan_host(mvi->shost);
602
603 return 0;
604
Andy Yan20b09c22009-05-08 17:46:40 -0400605err_not_sas:
606 sas_unregister_ha(SHOST_TO_SAS_HA(shost));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400607err_out_shost:
608 scsi_remove_host(mvi->shost);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400609err_out_regions:
610 pci_release_regions(pdev);
611err_out_disable:
612 pci_disable_device(pdev);
Andy Yan20b09c22009-05-08 17:46:40 -0400613err_out_enable:
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400614 return rc;
615}
616
617static void __devexit mvs_pci_remove(struct pci_dev *pdev)
618{
Andy Yan20b09c22009-05-08 17:46:40 -0400619 unsigned short core_nr, i = 0;
620 struct sas_ha_struct *sha = pci_get_drvdata(pdev);
621 struct mvs_info *mvi = NULL;
622
623 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
624 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[0];
625
626#ifdef MVS_USE_TASKLET
627 tasklet_kill(&mv_tasklet);
628#endif
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400629
630 pci_set_drvdata(pdev, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -0400631 sas_unregister_ha(sha);
632 sas_remove_host(mvi->shost);
633 scsi_remove_host(mvi->shost);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400634
Andy Yan20b09c22009-05-08 17:46:40 -0400635 MVS_CHIP_DISP->interrupt_disable(mvi);
636 free_irq(mvi->irq, sha);
637 for (i = 0; i < core_nr; i++) {
638 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[i];
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400639 mvs_free(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400640 }
Andy Yan20b09c22009-05-08 17:46:40 -0400641 kfree(sha->sas_phy);
642 kfree(sha->sas_port);
643 kfree(sha);
644 pci_release_regions(pdev);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400645 pci_disable_device(pdev);
Andy Yan20b09c22009-05-08 17:46:40 -0400646 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400647}
648
649static struct pci_device_id __devinitdata mvs_pci_table[] = {
650 { PCI_VDEVICE(MARVELL, 0x6320), chip_6320 },
651 { PCI_VDEVICE(MARVELL, 0x6340), chip_6440 },
652 {
653 .vendor = PCI_VENDOR_ID_MARVELL,
654 .device = 0x6440,
655 .subvendor = PCI_ANY_ID,
656 .subdevice = 0x6480,
657 .class = 0,
658 .class_mask = 0,
Andy Yan20b09c22009-05-08 17:46:40 -0400659 .driver_data = chip_6485,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400660 },
661 { PCI_VDEVICE(MARVELL, 0x6440), chip_6440 },
Andy Yan20b09c22009-05-08 17:46:40 -0400662 { PCI_VDEVICE(MARVELL, 0x6485), chip_6485 },
663 { PCI_VDEVICE(MARVELL, 0x9480), chip_9480 },
664 { PCI_VDEVICE(MARVELL, 0x9180), chip_9180 },
Nick Chengf31491d2009-09-08 19:03:07 +0800665 { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1300), chip_1300 },
666 { PCI_VDEVICE(ARECA, PCI_DEVICE_ID_ARECA_1320), chip_1320 },
Srinivas7ec4ad02009-11-24 20:07:39 +0530667 { PCI_VDEVICE(ADAPTEC2, 0x0450), chip_6440 },
HighPoint Linux Team463b8972011-02-23 16:28:44 +0800668 { PCI_VDEVICE(TTI, 0x2710), chip_9480 },
669 { PCI_VDEVICE(TTI, 0x2720), chip_9480 },
670 { PCI_VDEVICE(TTI, 0x2721), chip_9480 },
671 { PCI_VDEVICE(TTI, 0x2722), chip_9480 },
672 { PCI_VDEVICE(TTI, 0x2740), chip_9480 },
673 { PCI_VDEVICE(TTI, 0x2744), chip_9480 },
674 { PCI_VDEVICE(TTI, 0x2760), chip_9480 },
Xiangliang Yu82140282011-04-26 06:34:01 -0700675 {
676 .vendor = 0x1b4b,
677 .device = 0x9445,
678 .subvendor = PCI_ANY_ID,
679 .subdevice = 0x9480,
680 .class = 0,
681 .class_mask = 0,
682 .driver_data = chip_9445,
683 },
684 {
685 .vendor = 0x1b4b,
686 .device = 0x9485,
687 .subvendor = PCI_ANY_ID,
688 .subdevice = 0x9480,
689 .class = 0,
690 .class_mask = 0,
691 .driver_data = chip_9485,
692 },
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400693
694 { } /* terminate list */
695};
696
697static struct pci_driver mvs_pci_driver = {
698 .name = DRV_NAME,
699 .id_table = mvs_pci_table,
700 .probe = mvs_pci_init,
701 .remove = __devexit_p(mvs_pci_remove),
702};
703
Andy Yan20b09c22009-05-08 17:46:40 -0400704/* task handler */
705struct task_struct *mvs_th;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400706static int __init mvs_init(void)
707{
708 int rc;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400709 mvs_stt = sas_domain_attach_transport(&mvs_transport_ops);
710 if (!mvs_stt)
711 return -ENOMEM;
712
713 rc = pci_register_driver(&mvs_pci_driver);
Andy Yan20b09c22009-05-08 17:46:40 -0400714
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400715 if (rc)
716 goto err_out;
717
718 return 0;
719
720err_out:
721 sas_release_transport(mvs_stt);
722 return rc;
723}
724
725static void __exit mvs_exit(void)
726{
727 pci_unregister_driver(&mvs_pci_driver);
728 sas_release_transport(mvs_stt);
729}
730
731module_init(mvs_init);
732module_exit(mvs_exit);
733
734MODULE_AUTHOR("Jeff Garzik <jgarzik@pobox.com>");
735MODULE_DESCRIPTION("Marvell 88SE6440 SAS/SATA controller driver");
736MODULE_VERSION(DRV_VERSION);
737MODULE_LICENSE("GPL");
Andy Yan20b09c22009-05-08 17:46:40 -0400738#ifdef CONFIG_PCI
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400739MODULE_DEVICE_TABLE(pci, mvs_pci_table);
Andy Yan20b09c22009-05-08 17:46:40 -0400740#endif