blob: 7bd0ee3ed2d6c623b137986ed6c4182381b42c54 [file] [log] [blame]
Jeff Garzikb5762942007-10-25 20:58:22 -04001/*
Andy Yan20b09c22009-05-08 17:46:40 -04002 * Marvell 88SE64xx/88SE94xx main function
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 Garzikb5762942007-10-25 20:58:22 -040025
Jeff Garzikdd4969a2009-05-08 17:44:01 -040026#include "mv_sas.h"
Ke Wei8f261aa2008-02-23 21:15:27 +080027
Jeff Garzikdd4969a2009-05-08 17:44:01 -040028static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
29{
30 if (task->lldd_task) {
31 struct mvs_slot_info *slot;
Andy Yanf9da3be2009-05-14 20:41:21 -040032 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -040033 *tag = slot->slot_tag;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040034 return 1;
35 }
36 return 0;
37}
Ke Wei8f261aa2008-02-23 21:15:27 +080038
Andy Yan20b09c22009-05-08 17:46:40 -040039void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040040{
Xiangliang Yub89e8f52011-05-24 22:35:09 +080041 void *bitmap = mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040042 clear_bit(tag, bitmap);
43}
Jeff Garzikb5762942007-10-25 20:58:22 -040044
Andy Yan20b09c22009-05-08 17:46:40 -040045void mvs_tag_free(struct mvs_info *mvi, u32 tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040046{
47 mvs_tag_clear(mvi, tag);
48}
Jeff Garzikb5762942007-10-25 20:58:22 -040049
Andy Yan20b09c22009-05-08 17:46:40 -040050void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040051{
Xiangliang Yub89e8f52011-05-24 22:35:09 +080052 void *bitmap = mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040053 set_bit(tag, bitmap);
54}
55
Andy Yan20b09c22009-05-08 17:46:40 -040056inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040057{
58 unsigned int index, tag;
Xiangliang Yub89e8f52011-05-24 22:35:09 +080059 void *bitmap = mvi->tags;
Jeff Garzikdd4969a2009-05-08 17:44:01 -040060
Andy Yan20b09c22009-05-08 17:46:40 -040061 index = find_first_zero_bit(bitmap, mvi->tags_num);
Jeff Garzikdd4969a2009-05-08 17:44:01 -040062 tag = index;
Andy Yan20b09c22009-05-08 17:46:40 -040063 if (tag >= mvi->tags_num)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040064 return -SAS_QUEUE_FULL;
65 mvs_tag_set(mvi, tag);
66 *tag_out = tag;
67 return 0;
68}
69
70void mvs_tag_init(struct mvs_info *mvi)
71{
72 int i;
Andy Yan20b09c22009-05-08 17:46:40 -040073 for (i = 0; i < mvi->tags_num; ++i)
Jeff Garzikdd4969a2009-05-08 17:44:01 -040074 mvs_tag_clear(mvi, i);
75}
Jeff Garzikb5762942007-10-25 20:58:22 -040076
Andy Yan20b09c22009-05-08 17:46:40 -040077struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
78{
79 unsigned long i = 0, j = 0, hi = 0;
80 struct sas_ha_struct *sha = dev->port->ha;
81 struct mvs_info *mvi = NULL;
82 struct asd_sas_phy *phy;
Ke Wei8f261aa2008-02-23 21:15:27 +080083
Andy Yan20b09c22009-05-08 17:46:40 -040084 while (sha->sas_port[i]) {
85 if (sha->sas_port[i] == dev->port) {
86 phy = container_of(sha->sas_port[i]->phy_list.next,
87 struct asd_sas_phy, port_phy_el);
88 j = 0;
89 while (sha->sas_phy[j]) {
90 if (sha->sas_phy[j] == phy)
91 break;
92 j++;
Jeff Garzikb5762942007-10-25 20:58:22 -040093 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -040094 break;
Jeff Garzikb5762942007-10-25 20:58:22 -040095 }
Andy Yan20b09c22009-05-08 17:46:40 -040096 i++;
97 }
98 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
99 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
100
101 return mvi;
102
103}
104
105/* FIXME */
106int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
107{
108 unsigned long i = 0, j = 0, n = 0, num = 0;
Andy Yan9870d9a2009-05-11 22:19:25 +0800109 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
110 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -0400111 struct sas_ha_struct *sha = dev->port->ha;
112
113 while (sha->sas_port[i]) {
114 if (sha->sas_port[i] == dev->port) {
115 struct asd_sas_phy *phy;
116 list_for_each_entry(phy,
117 &sha->sas_port[i]->phy_list, port_phy_el) {
118 j = 0;
119 while (sha->sas_phy[j]) {
120 if (sha->sas_phy[j] == phy)
121 break;
122 j++;
123 }
124 phyno[n] = (j >= mvi->chip->n_phy) ?
125 (j - mvi->chip->n_phy) : j;
126 num++;
127 n++;
128 }
129 break;
130 }
131 i++;
132 }
133 return num;
134}
135
Xiangliang Yu534ff102011-05-24 22:26:50 +0800136struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
137 u8 reg_set)
138{
139 u32 dev_no;
140 for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
141 if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
142 continue;
143
144 if (mvi->devices[dev_no].taskfileset == reg_set)
145 return &mvi->devices[dev_no];
146 }
147 return NULL;
148}
149
Andy Yan20b09c22009-05-08 17:46:40 -0400150static inline void mvs_free_reg_set(struct mvs_info *mvi,
151 struct mvs_device *dev)
152{
153 if (!dev) {
154 mv_printk("device has been free.\n");
155 return;
156 }
Andy Yan20b09c22009-05-08 17:46:40 -0400157 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
158 return;
159 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
160}
161
162static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
163 struct mvs_device *dev)
164{
165 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
166 return 0;
167 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
168}
169
170void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
171{
172 u32 no;
173 for_each_phy(phy_mask, phy_mask, no) {
174 if (!(phy_mask & 1))
175 continue;
176 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
177 }
178}
179
180/* FIXME: locking? */
181int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
182 void *funcdata)
183{
184 int rc = 0, phy_id = sas_phy->id;
185 u32 tmp, i = 0, hi;
186 struct sas_ha_struct *sha = sas_phy->ha;
187 struct mvs_info *mvi = NULL;
188
189 while (sha->sas_phy[i]) {
190 if (sha->sas_phy[i] == sas_phy)
191 break;
192 i++;
193 }
194 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
195 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
196
197 switch (func) {
198 case PHY_FUNC_SET_LINK_RATE:
199 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
200 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400201
202 case PHY_FUNC_HARD_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400203 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400204 if (tmp & PHY_RST_HARD)
205 break;
Andy Yan20b09c22009-05-08 17:46:40 -0400206 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400207 break;
208
209 case PHY_FUNC_LINK_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400210 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
211 MVS_CHIP_DISP->phy_reset(mvi, phy_id, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400212 break;
213
214 case PHY_FUNC_DISABLE:
Andy Yan20b09c22009-05-08 17:46:40 -0400215 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
216 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400217 case PHY_FUNC_RELEASE_SPINUP_HOLD:
218 default:
219 rc = -EOPNOTSUPP;
Jeff Garzikb5762942007-10-25 20:58:22 -0400220 }
Andy Yan20b09c22009-05-08 17:46:40 -0400221 msleep(200);
Jeff Garzikb5762942007-10-25 20:58:22 -0400222 return rc;
223}
224
Andy Yan20b09c22009-05-08 17:46:40 -0400225void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
226 u32 off_lo, u32 off_hi, u64 sas_addr)
227{
228 u32 lo = (u32)sas_addr;
229 u32 hi = (u32)(sas_addr>>32);
230
231 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
232 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
233 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
234 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
235}
236
Ke Wei8f261aa2008-02-23 21:15:27 +0800237static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
238{
239 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400240 struct asd_sas_phy *sas_phy = &phy->sas_phy;
241 struct sas_ha_struct *sas_ha;
Ke Wei8f261aa2008-02-23 21:15:27 +0800242 if (!phy->phy_attached)
243 return;
244
Andy Yan20b09c22009-05-08 17:46:40 -0400245 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
246 && phy->phy_type & PORT_TYPE_SAS) {
247 return;
248 }
249
250 sas_ha = mvi->sas;
251 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
252
Ke Weiee1f1c22008-03-27 14:53:47 +0800253 if (sas_phy->phy) {
254 struct sas_phy *sphy = sas_phy->phy;
255
256 sphy->negotiated_linkrate = sas_phy->linkrate;
257 sphy->minimum_linkrate = phy->minimum_linkrate;
258 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
259 sphy->maximum_linkrate = phy->maximum_linkrate;
Andy Yan20b09c22009-05-08 17:46:40 -0400260 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
Ke Weiee1f1c22008-03-27 14:53:47 +0800261 }
262
Ke Wei8f261aa2008-02-23 21:15:27 +0800263 if (phy->phy_type & PORT_TYPE_SAS) {
264 struct sas_identify_frame *id;
265
266 id = (struct sas_identify_frame *)phy->frame_rcvd;
267 id->dev_type = phy->identify.device_type;
268 id->initiator_bits = SAS_PROTOCOL_ALL;
269 id->target_bits = phy->identify.target_port_protocols;
270 } else if (phy->phy_type & PORT_TYPE_SATA) {
Andy Yan20b09c22009-05-08 17:46:40 -0400271 /*Nothing*/
Ke Wei8f261aa2008-02-23 21:15:27 +0800272 }
Andy Yan20b09c22009-05-08 17:46:40 -0400273 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
274
275 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
276
277 mvi->sas->notify_port_event(sas_phy,
Ke Wei8f261aa2008-02-23 21:15:27 +0800278 PORTE_BYTES_DMAED);
279}
280
Andy Yan20b09c22009-05-08 17:46:40 -0400281int mvs_slave_alloc(struct scsi_device *scsi_dev)
282{
283 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
284 if (dev_is_sata(dev)) {
285 /* We don't need to rescan targets
286 * if REPORT_LUNS request is failed
287 */
288 if (scsi_dev->lun > 0)
289 return -ENXIO;
290 scsi_dev->tagged_supported = 1;
291 }
292
293 return sas_slave_alloc(scsi_dev);
294}
295
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400296int mvs_slave_configure(struct scsi_device *sdev)
Ke Wei8f261aa2008-02-23 21:15:27 +0800297{
Ke Weiee1f1c22008-03-27 14:53:47 +0800298 struct domain_device *dev = sdev_to_domain_dev(sdev);
299 int ret = sas_slave_configure(sdev);
Ke Wei8f261aa2008-02-23 21:15:27 +0800300
Ke Weiee1f1c22008-03-27 14:53:47 +0800301 if (ret)
302 return ret;
Ke Weiee1f1c22008-03-27 14:53:47 +0800303 if (dev_is_sata(dev)) {
Ke Weiee1f1c22008-03-27 14:53:47 +0800304 }
305 return 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400306}
307
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400308void mvs_scan_start(struct Scsi_Host *shost)
Jeff Garzikb5762942007-10-25 20:58:22 -0400309{
Andy Yan20b09c22009-05-08 17:46:40 -0400310 int i, j;
311 unsigned short core_nr;
312 struct mvs_info *mvi;
313 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
Jeff Garzikb5762942007-10-25 20:58:22 -0400314
Andy Yan20b09c22009-05-08 17:46:40 -0400315 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
316
317 for (j = 0; j < core_nr; j++) {
318 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
319 for (i = 0; i < mvi->chip->n_phy; ++i)
320 mvs_bytes_dmaed(mvi, i);
Jeff Garzikb5762942007-10-25 20:58:22 -0400321 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400322}
323
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400324int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
Jeff Garzikb5762942007-10-25 20:58:22 -0400325{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400326 /* give the phy enabling interrupt event time to come in (1s
327 * is empirically about all it takes) */
328 if (time < HZ)
Ke Wei8f261aa2008-02-23 21:15:27 +0800329 return 0;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400330 /* Wait for discovery to finish */
331 scsi_flush_work(shost);
332 return 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400333}
334
Ke Wei8f261aa2008-02-23 21:15:27 +0800335static int mvs_task_prep_smp(struct mvs_info *mvi,
336 struct mvs_task_exec_info *tei)
Jeff Garzikb5762942007-10-25 20:58:22 -0400337{
Ke Wei8f261aa2008-02-23 21:15:27 +0800338 int elem, rc, i;
339 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400340 struct mvs_cmd_hdr *hdr = tei->hdr;
Andy Yan20b09c22009-05-08 17:46:40 -0400341 struct domain_device *dev = task->dev;
342 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400343 struct scatterlist *sg_req, *sg_resp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800344 u32 req_len, resp_len, tag = tei->tag;
345 void *buf_tmp;
346 u8 *buf_oaf;
347 dma_addr_t buf_tmp_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400348 void *buf_prd;
Ke Wei8f261aa2008-02-23 21:15:27 +0800349 struct mvs_slot_info *slot = &mvi->slot_info[tag];
Ke Wei8f261aa2008-02-23 21:15:27 +0800350 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800351
Jeff Garzikb5762942007-10-25 20:58:22 -0400352 /*
353 * DMA-map SMP request, response buffers
354 */
Ke Wei8f261aa2008-02-23 21:15:27 +0800355 sg_req = &task->smp_task.smp_req;
Andy Yan20b09c22009-05-08 17:46:40 -0400356 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400357 if (!elem)
358 return -ENOMEM;
359 req_len = sg_dma_len(sg_req);
360
Ke Wei8f261aa2008-02-23 21:15:27 +0800361 sg_resp = &task->smp_task.smp_resp;
Andy Yan20b09c22009-05-08 17:46:40 -0400362 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400363 if (!elem) {
364 rc = -ENOMEM;
365 goto err_out;
366 }
Andy Yan20b09c22009-05-08 17:46:40 -0400367 resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400368
369 /* must be in dwords */
370 if ((req_len & 0x3) || (resp_len & 0x3)) {
371 rc = -EINVAL;
372 goto err_out_2;
373 }
374
375 /*
Ke Wei8f261aa2008-02-23 21:15:27 +0800376 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
Jeff Garzikb5762942007-10-25 20:58:22 -0400377 */
378
Andy Yan20b09c22009-05-08 17:46:40 -0400379 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800380 buf_tmp = slot->buf;
381 buf_tmp_dma = slot->buf_dma;
Jeff Garzikb5762942007-10-25 20:58:22 -0400382
Ke Wei8f261aa2008-02-23 21:15:27 +0800383 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
Ke Wei8f261aa2008-02-23 21:15:27 +0800384
385 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
386 buf_oaf = buf_tmp;
387 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
388
389 buf_tmp += MVS_OAF_SZ;
390 buf_tmp_dma += MVS_OAF_SZ;
391
Andy Yan20b09c22009-05-08 17:46:40 -0400392 /* region 3: PRD table *********************************** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800393 buf_prd = buf_tmp;
394 if (tei->n_elem)
395 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
396 else
397 hdr->prd_tbl = 0;
398
Andy Yan20b09c22009-05-08 17:46:40 -0400399 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800400 buf_tmp += i;
401 buf_tmp_dma += i;
402
403 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
404 slot->response = buf_tmp;
405 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400406 if (mvi->flags & MVF_FLAG_SOC)
407 hdr->reserved[0] = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800408
409 /*
410 * Fill in TX ring and command slot header
411 */
412 slot->tx = mvi->tx_prod;
413 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
414 TXQ_MODE_I | tag |
415 (sas_port->phy_mask << TXQ_PHY_SHIFT));
416
417 hdr->flags |= flags;
418 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
Jeff Garzikb5762942007-10-25 20:58:22 -0400419 hdr->tags = cpu_to_le32(tag);
420 hdr->data_len = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400421
Ke Wei8f261aa2008-02-23 21:15:27 +0800422 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400423 /* initiator, SMP, ftype 1h */
424 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
425 buf_oaf[1] = dev->linkrate & 0xf;
Ke Wei8f261aa2008-02-23 21:15:27 +0800426 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
Andy Yan20b09c22009-05-08 17:46:40 -0400427 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800428
429 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400430 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Ke Wei8f261aa2008-02-23 21:15:27 +0800431
Jeff Garzikb5762942007-10-25 20:58:22 -0400432 return 0;
433
434err_out_2:
Andy Yan20b09c22009-05-08 17:46:40 -0400435 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400436 PCI_DMA_FROMDEVICE);
437err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400438 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400439 PCI_DMA_TODEVICE);
440 return rc;
441}
442
Ke Wei4e52fc02008-03-27 14:54:50 +0800443static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
Ke Wei8f261aa2008-02-23 21:15:27 +0800444{
Ke Wei8f261aa2008-02-23 21:15:27 +0800445 struct ata_queued_cmd *qc = task->uldd_task;
446
Ke Wei4e52fc02008-03-27 14:54:50 +0800447 if (qc) {
448 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
449 qc->tf.command == ATA_CMD_FPDMA_READ) {
450 *tag = qc->tag;
451 return 1;
452 }
453 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800454
Ke Wei4e52fc02008-03-27 14:54:50 +0800455 return 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800456}
457
Jeff Garzikb5762942007-10-25 20:58:22 -0400458static int mvs_task_prep_ata(struct mvs_info *mvi,
459 struct mvs_task_exec_info *tei)
460{
461 struct sas_task *task = tei->task;
462 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400463 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400464 struct mvs_cmd_hdr *hdr = tei->hdr;
465 struct asd_sas_port *sas_port = dev->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800466 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400467 void *buf_prd;
468 u32 tag = tei->tag, hdr_tag;
469 u32 flags, del_q;
Jeff Garzikb5762942007-10-25 20:58:22 -0400470 void *buf_tmp;
471 u8 *buf_cmd, *buf_oaf;
472 dma_addr_t buf_tmp_dma;
Ke Wei8f261aa2008-02-23 21:15:27 +0800473 u32 i, req_len, resp_len;
474 const u32 max_resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400475
Andy Yan20b09c22009-05-08 17:46:40 -0400476 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
477 mv_dprintk("Have not enough regiset for dev %d.\n",
478 mvi_dev->device_id);
Ke Wei8f261aa2008-02-23 21:15:27 +0800479 return -EBUSY;
Andy Yan20b09c22009-05-08 17:46:40 -0400480 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800481 slot = &mvi->slot_info[tag];
482 slot->tx = mvi->tx_prod;
Andy Yan20b09c22009-05-08 17:46:40 -0400483 del_q = TXQ_MODE_I | tag |
484 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
485 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
486 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
487 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
Jeff Garzikb5762942007-10-25 20:58:22 -0400488
Andy Yan20b09c22009-05-08 17:46:40 -0400489 if (task->data_dir == DMA_FROM_DEVICE)
490 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
491 else
492 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800493
Jeff Garzikb5762942007-10-25 20:58:22 -0400494 if (task->ata_task.use_ncq)
495 flags |= MCH_FPDMA;
Ke Wei8f261aa2008-02-23 21:15:27 +0800496 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
497 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
498 flags |= MCH_ATAPI;
499 }
500
Jeff Garzikb5762942007-10-25 20:58:22 -0400501 /* FIXME: fill in port multiplier number */
502
503 hdr->flags = cpu_to_le32(flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800504
505 /* FIXME: the low order order 5 bits for the TAG if enable NCQ */
Andy Yan20b09c22009-05-08 17:46:40 -0400506 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
507 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
Ke Wei4e52fc02008-03-27 14:54:50 +0800508 else
Andy Yan20b09c22009-05-08 17:46:40 -0400509 hdr_tag = tag;
510
511 hdr->tags = cpu_to_le32(hdr_tag);
512
Jeff Garzikb5762942007-10-25 20:58:22 -0400513 hdr->data_len = cpu_to_le32(task->total_xfer_len);
514
515 /*
516 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
517 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400518
Ke Wei8f261aa2008-02-23 21:15:27 +0800519 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
520 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400521 buf_tmp_dma = slot->buf_dma;
522
523 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
524
525 buf_tmp += MVS_ATA_CMD_SZ;
526 buf_tmp_dma += MVS_ATA_CMD_SZ;
527
Ke Wei8f261aa2008-02-23 21:15:27 +0800528 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400529 /* used for STP. unused for SATA? */
530 buf_oaf = buf_tmp;
531 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
532
533 buf_tmp += MVS_OAF_SZ;
534 buf_tmp_dma += MVS_OAF_SZ;
535
Ke Wei8f261aa2008-02-23 21:15:27 +0800536 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400537 buf_prd = buf_tmp;
Andy Yan20b09c22009-05-08 17:46:40 -0400538
Ke Wei8f261aa2008-02-23 21:15:27 +0800539 if (tei->n_elem)
540 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
541 else
542 hdr->prd_tbl = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400543 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
Jeff Garzikb5762942007-10-25 20:58:22 -0400544
Jeff Garzikb5762942007-10-25 20:58:22 -0400545 buf_tmp += i;
546 buf_tmp_dma += i;
547
Ke Wei8f261aa2008-02-23 21:15:27 +0800548 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400549 /* FIXME: probably unused, for SATA. kept here just in case
550 * we get a STP/SATA error information record
551 */
552 slot->response = buf_tmp;
553 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400554 if (mvi->flags & MVF_FLAG_SOC)
555 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400556
Ke Wei8f261aa2008-02-23 21:15:27 +0800557 req_len = sizeof(struct host_to_dev_fis);
Jeff Garzikb5762942007-10-25 20:58:22 -0400558 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800559 sizeof(struct mvs_err_info) - i;
Jeff Garzikb5762942007-10-25 20:58:22 -0400560
561 /* request, response lengths */
Ke Wei8f261aa2008-02-23 21:15:27 +0800562 resp_len = min(resp_len, max_resp_len);
Jeff Garzikb5762942007-10-25 20:58:22 -0400563 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
564
Andy Yan20b09c22009-05-08 17:46:40 -0400565 if (likely(!task->ata_task.device_control_reg_update))
566 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
Jeff Garzikb5762942007-10-25 20:58:22 -0400567 /* fill in command FIS and ATAPI CDB */
Ke Wei8f261aa2008-02-23 21:15:27 +0800568 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
569 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
570 memcpy(buf_cmd + STP_ATAPI_CMD,
571 task->ata_task.atapi_packet, 16);
572
573 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400574 /* initiator, STP, ftype 1h */
575 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
576 buf_oaf[1] = dev->linkrate & 0xf;
577 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
578 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400579
580 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400581 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800582
Andy Yan20b09c22009-05-08 17:46:40 -0400583 if (task->data_dir == DMA_FROM_DEVICE)
Xiangliang Yu8882f082011-05-24 22:33:11 +0800584 MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
Andy Yan20b09c22009-05-08 17:46:40 -0400585 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800586
Jeff Garzikb5762942007-10-25 20:58:22 -0400587 return 0;
588}
589
590static int mvs_task_prep_ssp(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -0400591 struct mvs_task_exec_info *tei, int is_tmf,
592 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400593{
594 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400595 struct mvs_cmd_hdr *hdr = tei->hdr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800596 struct mvs_port *port = tei->port;
Andy Yan20b09c22009-05-08 17:46:40 -0400597 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400598 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400599 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400600 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400601 void *buf_prd;
Jeff Garzikb5762942007-10-25 20:58:22 -0400602 struct ssp_frame_hdr *ssp_hdr;
603 void *buf_tmp;
604 u8 *buf_cmd, *buf_oaf, fburst = 0;
605 dma_addr_t buf_tmp_dma;
606 u32 flags;
Ke Wei8f261aa2008-02-23 21:15:27 +0800607 u32 resp_len, req_len, i, tag = tei->tag;
608 const u32 max_resp_len = SB_RFB_MAX;
Andy Yan20b09c22009-05-08 17:46:40 -0400609 u32 phy_mask;
Jeff Garzikb5762942007-10-25 20:58:22 -0400610
611 slot = &mvi->slot_info[tag];
612
Andy Yan20b09c22009-05-08 17:46:40 -0400613 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
614 sas_port->phy_mask) & TXQ_PHY_MASK;
615
Ke Wei8f261aa2008-02-23 21:15:27 +0800616 slot->tx = mvi->tx_prod;
617 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
618 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
Ke Wei4e52fc02008-03-27 14:54:50 +0800619 (phy_mask << TXQ_PHY_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400620
621 flags = MCH_RETRY;
622 if (task->ssp_task.enable_first_burst) {
623 flags |= MCH_FBURST;
624 fburst = (1 << 7);
625 }
Andy Yan2b288132009-05-11 20:01:55 +0800626 if (is_tmf)
627 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
Andy Yan2b288132009-05-11 20:01:55 +0800628 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400629 hdr->tags = cpu_to_le32(tag);
630 hdr->data_len = cpu_to_le32(task->total_xfer_len);
631
632 /*
633 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
634 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400635
Ke Wei8f261aa2008-02-23 21:15:27 +0800636 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
637 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400638 buf_tmp_dma = slot->buf_dma;
639
640 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
641
642 buf_tmp += MVS_SSP_CMD_SZ;
643 buf_tmp_dma += MVS_SSP_CMD_SZ;
644
Ke Wei8f261aa2008-02-23 21:15:27 +0800645 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400646 buf_oaf = buf_tmp;
647 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
648
649 buf_tmp += MVS_OAF_SZ;
650 buf_tmp_dma += MVS_OAF_SZ;
651
Ke Wei8f261aa2008-02-23 21:15:27 +0800652 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400653 buf_prd = buf_tmp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800654 if (tei->n_elem)
655 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
656 else
657 hdr->prd_tbl = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400658
Andy Yan20b09c22009-05-08 17:46:40 -0400659 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Jeff Garzikb5762942007-10-25 20:58:22 -0400660 buf_tmp += i;
661 buf_tmp_dma += i;
662
Ke Wei8f261aa2008-02-23 21:15:27 +0800663 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400664 slot->response = buf_tmp;
665 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400666 if (mvi->flags & MVF_FLAG_SOC)
667 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400668
Jeff Garzikb5762942007-10-25 20:58:22 -0400669 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800670 sizeof(struct mvs_err_info) - i;
671 resp_len = min(resp_len, max_resp_len);
672
673 req_len = sizeof(struct ssp_frame_hdr) + 28;
Jeff Garzikb5762942007-10-25 20:58:22 -0400674
675 /* request, response lengths */
676 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
677
678 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400679 /* initiator, SSP, ftype 1h */
680 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
681 buf_oaf[1] = dev->linkrate & 0xf;
682 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
683 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400684
Ke Wei8f261aa2008-02-23 21:15:27 +0800685 /* fill in SSP frame header (Command Table.SSP frame header) */
686 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
Andy Yan20b09c22009-05-08 17:46:40 -0400687
688 if (is_tmf)
689 ssp_hdr->frame_type = SSP_TASK;
690 else
691 ssp_hdr->frame_type = SSP_COMMAND;
692
693 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
Jeff Garzikb5762942007-10-25 20:58:22 -0400694 HASHED_SAS_ADDR_SIZE);
695 memcpy(ssp_hdr->hashed_src_addr,
Andy Yan20b09c22009-05-08 17:46:40 -0400696 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400697 ssp_hdr->tag = cpu_to_be16(tag);
698
Andy Yan20b09c22009-05-08 17:46:40 -0400699 /* fill in IU for TASK and Command Frame */
Jeff Garzikb5762942007-10-25 20:58:22 -0400700 buf_cmd += sizeof(*ssp_hdr);
701 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
Jeff Garzikb5762942007-10-25 20:58:22 -0400702
Andy Yan20b09c22009-05-08 17:46:40 -0400703 if (ssp_hdr->frame_type != SSP_TASK) {
704 buf_cmd[9] = fburst | task->ssp_task.task_attr |
705 (task->ssp_task.task_prio << 3);
706 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
707 } else{
708 buf_cmd[10] = tmf->tmf;
709 switch (tmf->tmf) {
710 case TMF_ABORT_TASK:
711 case TMF_QUERY_TASK:
712 buf_cmd[12] =
713 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
714 buf_cmd[13] =
715 tmf->tag_of_task_to_be_managed & 0xff;
716 break;
717 default:
718 break;
719 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400720 }
Andy Yan20b09c22009-05-08 17:46:40 -0400721 /* fill in PRD (scatter/gather) table, if any */
722 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Jeff Garzikb5762942007-10-25 20:58:22 -0400723 return 0;
724}
725
Andy Yan20b09c22009-05-08 17:46:40 -0400726#define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700727static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
728 struct mvs_tmf_task *tmf, int *pass)
Jeff Garzikb5762942007-10-25 20:58:22 -0400729{
Ke Wei8f261aa2008-02-23 21:15:27 +0800730 struct domain_device *dev = task->dev;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700731 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400732 struct mvs_task_exec_info tei;
Ke Wei4e52fc02008-03-27 14:54:50 +0800733 struct mvs_slot_info *slot;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700734 u32 tag = 0xdeadbeef, n_elem = 0;
735 int rc = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400736
Andy Yan20b09c22009-05-08 17:46:40 -0400737 if (!dev->port) {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700738 struct task_status_struct *tsm = &task->task_status;
Andy Yan20b09c22009-05-08 17:46:40 -0400739
740 tsm->resp = SAS_TASK_UNDELIVERED;
741 tsm->stat = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700742 /*
743 * libsas will use dev->port, should
744 * not call task_done for sata
745 */
Srinivas9dc9fd92010-02-15 00:00:00 -0600746 if (dev->dev_type != SATA_DEV)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700747 task->task_done(task);
748 return rc;
Andy Yan20b09c22009-05-08 17:46:40 -0400749 }
750
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700751 if (DEV_IS_GONE(mvi_dev)) {
752 if (mvi_dev)
753 mv_dprintk("device %d not ready.\n",
754 mvi_dev->device_id);
755 else
756 mv_dprintk("device %016llx not ready.\n",
757 SAS_ADDR(dev->sas_addr));
Andy Yan20b09c22009-05-08 17:46:40 -0400758
759 rc = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700760 return rc;
761 }
762 tei.port = dev->port->lldd_port;
763 if (tei.port && !tei.port->port_attached && !tmf) {
764 if (sas_protocol_ata(task->task_proto)) {
765 struct task_status_struct *ts = &task->task_status;
766 mv_dprintk("SATA/STP port %d does not attach"
767 "device.\n", dev->port->id);
768 ts->resp = SAS_TASK_COMPLETE;
769 ts->stat = SAS_PHY_DOWN;
Andy Yan20b09c22009-05-08 17:46:40 -0400770
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700771 task->task_done(task);
Jeff Garzikb5762942007-10-25 20:58:22 -0400772
Ke Wei8f261aa2008-02-23 21:15:27 +0800773 } else {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700774 struct task_status_struct *ts = &task->task_status;
775 mv_dprintk("SAS port %d does not attach"
776 "device.\n", dev->port->id);
777 ts->resp = SAS_TASK_UNDELIVERED;
778 ts->stat = SAS_PHY_DOWN;
779 task->task_done(task);
Ke Wei8f261aa2008-02-23 21:15:27 +0800780 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700781 return rc;
782 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400783
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700784 if (!sas_protocol_ata(task->task_proto)) {
785 if (task->num_scatter) {
786 n_elem = dma_map_sg(mvi->dev,
787 task->scatter,
788 task->num_scatter,
789 task->data_dir);
790 if (!n_elem) {
791 rc = -ENOMEM;
792 goto prep_out;
793 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800794 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700795 } else {
796 n_elem = task->num_scatter;
797 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400798
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700799 rc = mvs_tag_alloc(mvi, &tag);
800 if (rc)
801 goto err_out;
Jeff Garzikb5762942007-10-25 20:58:22 -0400802
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700803 slot = &mvi->slot_info[tag];
Srinivas9dc9fd92010-02-15 00:00:00 -0600804
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700805 task->lldd_task = NULL;
806 slot->n_elem = n_elem;
807 slot->slot_tag = tag;
Jeff Garzikb5762942007-10-25 20:58:22 -0400808
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700809 slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
810 if (!slot->buf)
811 goto err_out_tag;
812 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
813
814 tei.task = task;
815 tei.hdr = &mvi->slot[tag];
816 tei.tag = tag;
817 tei.n_elem = n_elem;
818 switch (task->task_proto) {
819 case SAS_PROTOCOL_SMP:
820 rc = mvs_task_prep_smp(mvi, &tei);
821 break;
822 case SAS_PROTOCOL_SSP:
823 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
824 break;
825 case SAS_PROTOCOL_SATA:
826 case SAS_PROTOCOL_STP:
827 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
828 rc = mvs_task_prep_ata(mvi, &tei);
829 break;
830 default:
831 dev_printk(KERN_ERR, mvi->dev,
832 "unknown sas_task proto: 0x%x\n",
833 task->task_proto);
834 rc = -EINVAL;
835 break;
836 }
837
838 if (rc) {
839 mv_dprintk("rc is %x\n", rc);
840 goto err_out_slot_buf;
841 }
842 slot->task = task;
843 slot->port = tei.port;
844 task->lldd_task = slot;
845 list_add_tail(&slot->entry, &tei.port->list);
846 spin_lock(&task->task_state_lock);
847 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
848 spin_unlock(&task->task_state_lock);
849
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700850 mvi_dev->running_req++;
851 ++(*pass);
852 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
853
854 return rc;
855
856err_out_slot_buf:
857 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
Jeff Garzikb5762942007-10-25 20:58:22 -0400858err_out_tag:
Ke Wei8f261aa2008-02-23 21:15:27 +0800859 mvs_tag_free(mvi, tag);
Jeff Garzikb5762942007-10-25 20:58:22 -0400860err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400861
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700862 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
863 if (!sas_protocol_ata(task->task_proto))
Ke Wei8f261aa2008-02-23 21:15:27 +0800864 if (n_elem)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700865 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
866 task->data_dir);
867prep_out:
868 return rc;
869}
870
871static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
872{
873 struct mvs_task_list *first = NULL;
874
875 for (; *num > 0; --*num) {
876 struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
877
878 if (!mvs_list)
879 break;
880
881 INIT_LIST_HEAD(&mvs_list->list);
882 if (!first)
883 first = mvs_list;
884 else
885 list_add_tail(&mvs_list->list, &first->list);
886
887 }
888
889 return first;
890}
891
892static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
893{
894 LIST_HEAD(list);
895 struct list_head *pos, *a;
896 struct mvs_task_list *mlist = NULL;
897
898 __list_add(&list, mvs_list->list.prev, &mvs_list->list);
899
900 list_for_each_safe(pos, a, &list) {
901 list_del_init(pos);
902 mlist = list_entry(pos, struct mvs_task_list, list);
903 kmem_cache_free(mvs_task_list_cache, mlist);
904 }
905}
906
907static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
908 struct completion *completion, int is_tmf,
909 struct mvs_tmf_task *tmf)
910{
911 struct domain_device *dev = task->dev;
912 struct mvs_info *mvi = NULL;
913 u32 rc = 0;
914 u32 pass = 0;
915 unsigned long flags = 0;
916
917 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
918
919 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
920 spin_unlock_irq(dev->sata_dev.ap->lock);
921
922 spin_lock_irqsave(&mvi->lock, flags);
923 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
924 if (rc)
925 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
926
927 if (likely(pass))
928 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
929 (MVS_CHIP_SLOT_SZ - 1));
Andy Yan0b84b702009-05-11 20:05:26 +0800930 spin_unlock_irqrestore(&mvi->lock, flags);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700931
932 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
933 spin_lock_irq(dev->sata_dev.ap->lock);
934
935 return rc;
936}
937
938static int mvs_collector_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
939 struct completion *completion, int is_tmf,
940 struct mvs_tmf_task *tmf)
941{
942 struct domain_device *dev = task->dev;
943 struct mvs_prv_info *mpi = dev->port->ha->lldd_ha;
944 struct mvs_info *mvi = NULL;
945 struct sas_task *t = task;
946 struct mvs_task_list *mvs_list = NULL, *a;
947 LIST_HEAD(q);
948 int pass[2] = {0};
949 u32 rc = 0;
950 u32 n = num;
951 unsigned long flags = 0;
952
953 mvs_list = mvs_task_alloc_list(&n, gfp_flags);
954 if (n) {
955 printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
956 rc = -ENOMEM;
957 goto free_list;
958 }
959
960 __list_add(&q, mvs_list->list.prev, &mvs_list->list);
961
962 list_for_each_entry(a, &q, list) {
963 a->task = t;
964 t = list_entry(t->list.next, struct sas_task, list);
965 }
966
967 list_for_each_entry(a, &q , list) {
968
969 t = a->task;
970 mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
971
972 spin_lock_irqsave(&mvi->lock, flags);
973 rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
974 if (rc)
975 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
976 spin_unlock_irqrestore(&mvi->lock, flags);
977 }
978
979 if (likely(pass[0]))
980 MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
981 (mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
982
983 if (likely(pass[1]))
984 MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
985 (mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
986
987 list_del_init(&q);
988
989free_list:
990 if (mvs_list)
991 mvs_task_free_list(mvs_list);
992
Jeff Garzikb5762942007-10-25 20:58:22 -0400993 return rc;
994}
995
Andy Yan20b09c22009-05-08 17:46:40 -0400996int mvs_queue_command(struct sas_task *task, const int num,
997 gfp_t gfp_flags)
998{
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700999 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1000 struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
1001
1002 if (sas->lldd_max_execute_num < 2)
1003 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
1004 else
1005 return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04001006}
1007
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001008static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
Ke Wei8f261aa2008-02-23 21:15:27 +08001009{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001010 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1011 mvs_tag_clear(mvi, slot_idx);
1012}
Ke Wei8f261aa2008-02-23 21:15:27 +08001013
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001014static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
1015 struct mvs_slot_info *slot, u32 slot_idx)
1016{
Andy Yan20b09c22009-05-08 17:46:40 -04001017 if (!slot->task)
1018 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001019 if (!sas_protocol_ata(task->task_proto))
1020 if (slot->n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -04001021 dma_unmap_sg(mvi->dev, task->scatter,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001022 slot->n_elem, task->data_dir);
Ke Wei8f261aa2008-02-23 21:15:27 +08001023
Ke Wei8f261aa2008-02-23 21:15:27 +08001024 switch (task->task_proto) {
1025 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -04001026 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001027 PCI_DMA_FROMDEVICE);
Andy Yan20b09c22009-05-08 17:46:40 -04001028 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001029 PCI_DMA_TODEVICE);
Ke Wei8f261aa2008-02-23 21:15:27 +08001030 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001031
Ke Wei8f261aa2008-02-23 21:15:27 +08001032 case SAS_PROTOCOL_SATA:
1033 case SAS_PROTOCOL_STP:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001034 case SAS_PROTOCOL_SSP:
Ke Wei8f261aa2008-02-23 21:15:27 +08001035 default:
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001036 /* do nothing */
Ke Wei8f261aa2008-02-23 21:15:27 +08001037 break;
1038 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001039
1040 if (slot->buf) {
1041 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1042 slot->buf = NULL;
1043 }
Andy Yan20b09c22009-05-08 17:46:40 -04001044 list_del_init(&slot->entry);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001045 task->lldd_task = NULL;
1046 slot->task = NULL;
1047 slot->port = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001048 slot->slot_tag = 0xFFFFFFFF;
1049 mvs_slot_free(mvi, slot_idx);
Ke Wei8f261aa2008-02-23 21:15:27 +08001050}
1051
1052static void mvs_update_wideport(struct mvs_info *mvi, int i)
1053{
1054 struct mvs_phy *phy = &mvi->phy[i];
1055 struct mvs_port *port = phy->port;
1056 int j, no;
1057
Andy Yan20b09c22009-05-08 17:46:40 -04001058 for_each_phy(port->wide_port_phymap, j, no) {
1059 if (j & 1) {
1060 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1061 PHYR_WIDE_PORT);
1062 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
Ke Wei8f261aa2008-02-23 21:15:27 +08001063 port->wide_port_phymap);
1064 } else {
Andy Yan20b09c22009-05-08 17:46:40 -04001065 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1066 PHYR_WIDE_PORT);
1067 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1068 0);
Ke Wei8f261aa2008-02-23 21:15:27 +08001069 }
Andy Yan20b09c22009-05-08 17:46:40 -04001070 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001071}
1072
1073static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1074{
1075 u32 tmp;
1076 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001077 struct mvs_port *port = phy->port;
Ke Wei8f261aa2008-02-23 21:15:27 +08001078
Andy Yan20b09c22009-05-08 17:46:40 -04001079 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001080 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
Ke Wei963829e2008-03-27 14:55:23 +08001081 if (!port)
Ke Wei8f261aa2008-02-23 21:15:27 +08001082 phy->phy_attached = 1;
1083 return tmp;
1084 }
1085
Ke Wei8f261aa2008-02-23 21:15:27 +08001086 if (port) {
1087 if (phy->phy_type & PORT_TYPE_SAS) {
1088 port->wide_port_phymap &= ~(1U << i);
1089 if (!port->wide_port_phymap)
1090 port->port_attached = 0;
1091 mvs_update_wideport(mvi, i);
1092 } else if (phy->phy_type & PORT_TYPE_SATA)
1093 port->port_attached = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +08001094 phy->port = NULL;
1095 phy->phy_attached = 0;
1096 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1097 }
1098 return 0;
1099}
1100
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001101static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1102{
1103 u32 *s = (u32 *) buf;
1104
1105 if (!s)
1106 return NULL;
1107
Andy Yan20b09c22009-05-08 17:46:40 -04001108 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1109 s[3] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001110
Andy Yan20b09c22009-05-08 17:46:40 -04001111 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1112 s[2] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001113
Andy Yan20b09c22009-05-08 17:46:40 -04001114 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1115 s[1] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001116
Andy Yan20b09c22009-05-08 17:46:40 -04001117 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1118 s[0] = MVS_CHIP_DISP->read_port_cfg_data(mvi, i);
1119
1120 /* Workaround: take some ATAPI devices for ATA */
1121 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1122 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001123
Andy Yanf9da3be2009-05-14 20:41:21 -04001124 return s;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001125}
1126
1127static u32 mvs_is_sig_fis_received(u32 irq_status)
1128{
1129 return irq_status & PHYEV_SIG_FIS;
1130}
1131
Xiangliang Yu8882f082011-05-24 22:33:11 +08001132static void mvs_sig_remove_timer(struct mvs_phy *phy)
1133{
1134 if (phy->timer.function)
1135 del_timer(&phy->timer);
1136 phy->timer.function = NULL;
1137}
1138
Andy Yan20b09c22009-05-08 17:46:40 -04001139void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
Ke Wei8f261aa2008-02-23 21:15:27 +08001140{
1141 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001142 struct sas_identify_frame *id;
Ke Wei8f261aa2008-02-23 21:15:27 +08001143
Andy Yan20b09c22009-05-08 17:46:40 -04001144 id = (struct sas_identify_frame *)phy->frame_rcvd;
Ke Wei8f261aa2008-02-23 21:15:27 +08001145
1146 if (get_st) {
Andy Yan20b09c22009-05-08 17:46:40 -04001147 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001148 phy->phy_status = mvs_is_phy_ready(mvi, i);
1149 }
1150
1151 if (phy->phy_status) {
Andy Yan20b09c22009-05-08 17:46:40 -04001152 int oob_done = 0;
1153 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
Ke Wei8f261aa2008-02-23 21:15:27 +08001154
Andy Yan20b09c22009-05-08 17:46:40 -04001155 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001156
Andy Yan20b09c22009-05-08 17:46:40 -04001157 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1158 if (phy->phy_type & PORT_TYPE_SATA) {
1159 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1160 if (mvs_is_sig_fis_received(phy->irq_status)) {
Xiangliang Yu8882f082011-05-24 22:33:11 +08001161 mvs_sig_remove_timer(phy);
Andy Yan20b09c22009-05-08 17:46:40 -04001162 phy->phy_attached = 1;
1163 phy->att_dev_sas_addr =
1164 i + mvi->id * mvi->chip->n_phy;
1165 if (oob_done)
1166 sas_phy->oob_mode = SATA_OOB_MODE;
1167 phy->frame_rcvd_size =
1168 sizeof(struct dev_to_host_fis);
Andy Yanf9da3be2009-05-14 20:41:21 -04001169 mvs_get_d2h_reg(mvi, i, id);
Andy Yan20b09c22009-05-08 17:46:40 -04001170 } else {
1171 u32 tmp;
1172 dev_printk(KERN_DEBUG, mvi->dev,
1173 "Phy%d : No sig fis\n", i);
1174 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1175 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1176 tmp | PHYEV_SIG_FIS);
1177 phy->phy_attached = 0;
1178 phy->phy_type &= ~PORT_TYPE_SATA;
Andy Yan20b09c22009-05-08 17:46:40 -04001179 goto out_done;
1180 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001181 } else if (phy->phy_type & PORT_TYPE_SAS
Andy Yan20b09c22009-05-08 17:46:40 -04001182 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1183 phy->phy_attached = 1;
Ke Wei8f261aa2008-02-23 21:15:27 +08001184 phy->identify.device_type =
Andy Yan20b09c22009-05-08 17:46:40 -04001185 phy->att_dev_info & PORT_DEV_TYPE_MASK;
Ke Wei8f261aa2008-02-23 21:15:27 +08001186
1187 if (phy->identify.device_type == SAS_END_DEV)
1188 phy->identify.target_port_protocols =
1189 SAS_PROTOCOL_SSP;
1190 else if (phy->identify.device_type != NO_DEVICE)
1191 phy->identify.target_port_protocols =
1192 SAS_PROTOCOL_SMP;
Andy Yan20b09c22009-05-08 17:46:40 -04001193 if (oob_done)
Ke Wei8f261aa2008-02-23 21:15:27 +08001194 sas_phy->oob_mode = SAS_OOB_MODE;
1195 phy->frame_rcvd_size =
1196 sizeof(struct sas_identify_frame);
Ke Wei8f261aa2008-02-23 21:15:27 +08001197 }
Andy Yan20b09c22009-05-08 17:46:40 -04001198 memcpy(sas_phy->attached_sas_addr,
1199 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
Ke Weie9ff91b2008-03-27 14:55:33 +08001200
Andy Yan20b09c22009-05-08 17:46:40 -04001201 if (MVS_CHIP_DISP->phy_work_around)
1202 MVS_CHIP_DISP->phy_work_around(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001203 }
Andy Yan20b09c22009-05-08 17:46:40 -04001204 mv_dprintk("port %d attach dev info is %x\n",
1205 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1206 mv_dprintk("port %d attach sas addr is %llx\n",
1207 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
Ke Weie9ff91b2008-03-27 14:55:33 +08001208out_done:
Ke Wei8f261aa2008-02-23 21:15:27 +08001209 if (get_st)
Andy Yan20b09c22009-05-08 17:46:40 -04001210 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
Ke Wei8f261aa2008-02-23 21:15:27 +08001211}
1212
Andy Yan20b09c22009-05-08 17:46:40 -04001213static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1214{
1215 struct sas_ha_struct *sas_ha = sas_phy->ha;
1216 struct mvs_info *mvi = NULL; int i = 0, hi;
1217 struct mvs_phy *phy = sas_phy->lldd_phy;
1218 struct asd_sas_port *sas_port = sas_phy->port;
1219 struct mvs_port *port;
1220 unsigned long flags = 0;
1221 if (!sas_port)
1222 return;
1223
1224 while (sas_ha->sas_phy[i]) {
1225 if (sas_ha->sas_phy[i] == sas_phy)
1226 break;
1227 i++;
1228 }
1229 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1230 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1231 if (sas_port->id >= mvi->chip->n_phy)
1232 port = &mvi->port[sas_port->id - mvi->chip->n_phy];
1233 else
1234 port = &mvi->port[sas_port->id];
1235 if (lock)
1236 spin_lock_irqsave(&mvi->lock, flags);
1237 port->port_attached = 1;
1238 phy->port = port;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001239 sas_port->lldd_port = port;
Andy Yan20b09c22009-05-08 17:46:40 -04001240 if (phy->phy_type & PORT_TYPE_SAS) {
1241 port->wide_port_phymap = sas_port->phy_mask;
1242 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1243 mvs_update_wideport(mvi, sas_phy->id);
1244 }
1245 if (lock)
1246 spin_unlock_irqrestore(&mvi->lock, flags);
1247}
1248
1249static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1250{
Srinivas9dc9fd92010-02-15 00:00:00 -06001251 struct domain_device *dev;
1252 struct mvs_phy *phy = sas_phy->lldd_phy;
1253 struct mvs_info *mvi = phy->mvi;
1254 struct asd_sas_port *port = sas_phy->port;
1255 int phy_no = 0;
1256
1257 while (phy != &mvi->phy[phy_no]) {
1258 phy_no++;
1259 if (phy_no >= MVS_MAX_PHYS)
1260 return;
1261 }
1262 list_for_each_entry(dev, &port->dev_list, dev_list_node)
1263 mvs_do_release_task(phy->mvi, phy_no, NULL);
1264
Andy Yan20b09c22009-05-08 17:46:40 -04001265}
1266
1267
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001268void mvs_port_formed(struct asd_sas_phy *sas_phy)
Ke Wei8f261aa2008-02-23 21:15:27 +08001269{
Andy Yan20b09c22009-05-08 17:46:40 -04001270 mvs_port_notify_formed(sas_phy, 1);
1271}
Ke Wei8f261aa2008-02-23 21:15:27 +08001272
Andy Yan20b09c22009-05-08 17:46:40 -04001273void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1274{
1275 mvs_port_notify_deformed(sas_phy, 1);
1276}
1277
1278struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1279{
1280 u32 dev;
1281 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1282 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1283 mvi->devices[dev].device_id = dev;
1284 return &mvi->devices[dev];
1285 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001286 }
Andy Yan20b09c22009-05-08 17:46:40 -04001287
1288 if (dev == MVS_MAX_DEVICES)
1289 mv_printk("max support %d devices, ignore ..\n",
1290 MVS_MAX_DEVICES);
1291
1292 return NULL;
1293}
1294
1295void mvs_free_dev(struct mvs_device *mvi_dev)
1296{
1297 u32 id = mvi_dev->device_id;
1298 memset(mvi_dev, 0, sizeof(*mvi_dev));
1299 mvi_dev->device_id = id;
1300 mvi_dev->dev_type = NO_DEVICE;
1301 mvi_dev->dev_status = MVS_DEV_NORMAL;
1302 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1303}
1304
1305int mvs_dev_found_notify(struct domain_device *dev, int lock)
1306{
1307 unsigned long flags = 0;
1308 int res = 0;
1309 struct mvs_info *mvi = NULL;
1310 struct domain_device *parent_dev = dev->parent;
1311 struct mvs_device *mvi_device;
1312
1313 mvi = mvs_find_dev_mvi(dev);
1314
1315 if (lock)
1316 spin_lock_irqsave(&mvi->lock, flags);
1317
1318 mvi_device = mvs_alloc_dev(mvi);
1319 if (!mvi_device) {
1320 res = -1;
1321 goto found_out;
1322 }
Andy Yanf9da3be2009-05-14 20:41:21 -04001323 dev->lldd_dev = mvi_device;
Srinivas9dc9fd92010-02-15 00:00:00 -06001324 mvi_device->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001325 mvi_device->dev_type = dev->dev_type;
Andy Yan9870d9a2009-05-11 22:19:25 +08001326 mvi_device->mvi_info = mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001327 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1328 int phy_id;
1329 u8 phy_num = parent_dev->ex_dev.num_phys;
1330 struct ex_phy *phy;
1331 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1332 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1333 if (SAS_ADDR(phy->attached_sas_addr) ==
1334 SAS_ADDR(dev->sas_addr)) {
1335 mvi_device->attached_phy = phy_id;
1336 break;
1337 }
1338 }
1339
1340 if (phy_id == phy_num) {
1341 mv_printk("Error: no attached dev:%016llx"
1342 "at ex:%016llx.\n",
1343 SAS_ADDR(dev->sas_addr),
1344 SAS_ADDR(parent_dev->sas_addr));
1345 res = -1;
1346 }
1347 }
1348
1349found_out:
1350 if (lock)
1351 spin_unlock_irqrestore(&mvi->lock, flags);
1352 return res;
1353}
1354
1355int mvs_dev_found(struct domain_device *dev)
1356{
1357 return mvs_dev_found_notify(dev, 1);
1358}
1359
Srinivas9dc9fd92010-02-15 00:00:00 -06001360void mvs_dev_gone_notify(struct domain_device *dev)
Andy Yan20b09c22009-05-08 17:46:40 -04001361{
1362 unsigned long flags = 0;
Andy Yanf9da3be2009-05-14 20:41:21 -04001363 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001364 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001365
Srinivas9dc9fd92010-02-15 00:00:00 -06001366 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001367
1368 if (mvi_dev) {
1369 mv_dprintk("found dev[%d:%x] is gone.\n",
1370 mvi_dev->device_id, mvi_dev->dev_type);
Srinivas9dc9fd92010-02-15 00:00:00 -06001371 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001372 mvs_free_reg_set(mvi, mvi_dev);
1373 mvs_free_dev(mvi_dev);
1374 } else {
1375 mv_dprintk("found dev has gone.\n");
1376 }
1377 dev->lldd_dev = NULL;
1378
Srinivas9dc9fd92010-02-15 00:00:00 -06001379 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001380}
1381
1382
1383void mvs_dev_gone(struct domain_device *dev)
1384{
Srinivas9dc9fd92010-02-15 00:00:00 -06001385 mvs_dev_gone_notify(dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001386}
1387
1388static struct sas_task *mvs_alloc_task(void)
1389{
1390 struct sas_task *task = kzalloc(sizeof(struct sas_task), GFP_KERNEL);
1391
1392 if (task) {
1393 INIT_LIST_HEAD(&task->list);
1394 spin_lock_init(&task->task_state_lock);
1395 task->task_state_flags = SAS_TASK_STATE_PENDING;
1396 init_timer(&task->timer);
1397 init_completion(&task->completion);
1398 }
1399 return task;
1400}
1401
1402static void mvs_free_task(struct sas_task *task)
1403{
1404 if (task) {
1405 BUG_ON(!list_empty(&task->list));
1406 kfree(task);
1407 }
1408}
1409
1410static void mvs_task_done(struct sas_task *task)
1411{
1412 if (!del_timer(&task->timer))
1413 return;
1414 complete(&task->completion);
1415}
1416
1417static void mvs_tmf_timedout(unsigned long data)
1418{
1419 struct sas_task *task = (struct sas_task *)data;
1420
1421 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1422 complete(&task->completion);
1423}
1424
1425/* XXX */
1426#define MVS_TASK_TIMEOUT 20
1427static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1428 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1429{
1430 int res, retry;
1431 struct sas_task *task = NULL;
1432
1433 for (retry = 0; retry < 3; retry++) {
1434 task = mvs_alloc_task();
1435 if (!task)
1436 return -ENOMEM;
1437
1438 task->dev = dev;
1439 task->task_proto = dev->tproto;
1440
1441 memcpy(&task->ssp_task, parameter, para_len);
1442 task->task_done = mvs_task_done;
1443
1444 task->timer.data = (unsigned long) task;
1445 task->timer.function = mvs_tmf_timedout;
1446 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1447 add_timer(&task->timer);
1448
Andy Yan0b84b702009-05-11 20:05:26 +08001449 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
Andy Yan20b09c22009-05-08 17:46:40 -04001450
1451 if (res) {
1452 del_timer(&task->timer);
1453 mv_printk("executing internel task failed:%d\n", res);
1454 goto ex_err;
1455 }
1456
1457 wait_for_completion(&task->completion);
1458 res = -TMF_RESP_FUNC_FAILED;
1459 /* Even TMF timed out, return direct. */
1460 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1461 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1462 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1463 goto ex_err;
1464 }
1465 }
1466
1467 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -05001468 task->task_status.stat == SAM_STAT_GOOD) {
Andy Yan20b09c22009-05-08 17:46:40 -04001469 res = TMF_RESP_FUNC_COMPLETE;
1470 break;
1471 }
1472
1473 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1474 task->task_status.stat == SAS_DATA_UNDERRUN) {
1475 /* no error, but return the number of bytes of
1476 * underrun */
1477 res = task->task_status.residual;
1478 break;
1479 }
1480
1481 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1482 task->task_status.stat == SAS_DATA_OVERRUN) {
1483 mv_dprintk("blocked task error.\n");
1484 res = -EMSGSIZE;
1485 break;
1486 } else {
1487 mv_dprintk(" task to dev %016llx response: 0x%x "
1488 "status 0x%x\n",
1489 SAS_ADDR(dev->sas_addr),
1490 task->task_status.resp,
1491 task->task_status.stat);
1492 mvs_free_task(task);
1493 task = NULL;
1494
1495 }
1496 }
1497ex_err:
1498 BUG_ON(retry == 3 && task != NULL);
1499 if (task != NULL)
1500 mvs_free_task(task);
1501 return res;
1502}
1503
1504static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1505 u8 *lun, struct mvs_tmf_task *tmf)
1506{
1507 struct sas_ssp_task ssp_task;
1508 DECLARE_COMPLETION_ONSTACK(completion);
1509 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1510 return TMF_RESP_FUNC_ESUPP;
1511
1512 strncpy((u8 *)&ssp_task.LUN, lun, 8);
1513
1514 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1515 sizeof(ssp_task), tmf);
1516}
1517
1518
1519/* Standard mandates link reset for ATA (type 0)
1520 and hard reset for SSP (type 1) , only for RECOVERY */
1521static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1522{
1523 int rc;
1524 struct sas_phy *phy = sas_find_local_phy(dev);
1525 int reset_type = (dev->dev_type == SATA_DEV ||
1526 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1527 rc = sas_phy_reset(phy, reset_type);
1528 msleep(2000);
1529 return rc;
1530}
1531
1532/* mandatory SAM-3 */
1533int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1534{
1535 unsigned long flags;
1536 int i, phyno[WIDE_PORT_MAX_PHY], num , rc = TMF_RESP_FUNC_FAILED;
1537 struct mvs_tmf_task tmf_task;
Andy Yanf9da3be2009-05-14 20:41:21 -04001538 struct mvs_device * mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001539 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001540
1541 tmf_task.tmf = TMF_LU_RESET;
1542 mvi_dev->dev_status = MVS_DEV_EH;
1543 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1544 if (rc == TMF_RESP_FUNC_COMPLETE) {
1545 num = mvs_find_dev_phyno(dev, phyno);
1546 spin_lock_irqsave(&mvi->lock, flags);
1547 for (i = 0; i < num; i++)
Srinivas9dc9fd92010-02-15 00:00:00 -06001548 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001549 spin_unlock_irqrestore(&mvi->lock, flags);
1550 }
1551 /* If failed, fall-through I_T_Nexus reset */
1552 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1553 mvi_dev->device_id, rc);
1554 return rc;
Jeff Garzikb5762942007-10-25 20:58:22 -04001555}
1556
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001557int mvs_I_T_nexus_reset(struct domain_device *dev)
Ke Weie9ff91b2008-03-27 14:55:33 +08001558{
Andy Yan20b09c22009-05-08 17:46:40 -04001559 unsigned long flags;
Srinivas9dc9fd92010-02-15 00:00:00 -06001560 int rc = TMF_RESP_FUNC_FAILED;
1561 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001562 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001563
1564 if (mvi_dev->dev_status != MVS_DEV_EH)
1565 return TMF_RESP_FUNC_COMPLETE;
1566 rc = mvs_debug_I_T_nexus_reset(dev);
1567 mv_printk("%s for device[%x]:rc= %d\n",
1568 __func__, mvi_dev->device_id, rc);
1569
1570 /* housekeeper */
Andy Yan20b09c22009-05-08 17:46:40 -04001571 spin_lock_irqsave(&mvi->lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001572 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001573 spin_unlock_irqrestore(&mvi->lock, flags);
1574
1575 return rc;
1576}
1577/* optional SAM-3 */
1578int mvs_query_task(struct sas_task *task)
1579{
1580 u32 tag;
1581 struct scsi_lun lun;
1582 struct mvs_tmf_task tmf_task;
1583 int rc = TMF_RESP_FUNC_FAILED;
1584
1585 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1586 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1587 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001588 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1589 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001590
1591 int_to_scsilun(cmnd->device->lun, &lun);
1592 rc = mvs_find_tag(mvi, task, &tag);
1593 if (rc == 0) {
1594 rc = TMF_RESP_FUNC_FAILED;
1595 return rc;
1596 }
1597
1598 tmf_task.tmf = TMF_QUERY_TASK;
1599 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1600
1601 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1602 switch (rc) {
1603 /* The task is still in Lun, release it then */
1604 case TMF_RESP_FUNC_SUCC:
1605 /* The task is not in Lun or failed, reset the phy */
1606 case TMF_RESP_FUNC_FAILED:
1607 case TMF_RESP_FUNC_COMPLETE:
1608 break;
Srinivas9dc9fd92010-02-15 00:00:00 -06001609 default:
1610 rc = TMF_RESP_FUNC_COMPLETE;
1611 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001612 }
1613 }
1614 mv_printk("%s:rc= %d\n", __func__, rc);
1615 return rc;
1616}
1617
1618/* mandatory SAM-3, still need free task/slot info */
1619int mvs_abort_task(struct sas_task *task)
1620{
1621 struct scsi_lun lun;
1622 struct mvs_tmf_task tmf_task;
1623 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001624 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
Jiri Slaby24ae1632010-06-22 13:42:02 +02001625 struct mvs_info *mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001626 int rc = TMF_RESP_FUNC_FAILED;
1627 unsigned long flags;
1628 u32 tag;
Andy Yan9870d9a2009-05-11 22:19:25 +08001629
Srinivas9dc9fd92010-02-15 00:00:00 -06001630 if (!mvi_dev) {
1631 mv_printk("%s:%d TMF_RESP_FUNC_FAILED\n", __func__, __LINE__);
1632 rc = TMF_RESP_FUNC_FAILED;
1633 }
1634
Jiri Slaby24ae1632010-06-22 13:42:02 +02001635 mvi = mvi_dev->mvi_info;
1636
Andy Yan20b09c22009-05-08 17:46:40 -04001637 spin_lock_irqsave(&task->task_state_lock, flags);
1638 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1639 spin_unlock_irqrestore(&task->task_state_lock, flags);
1640 rc = TMF_RESP_FUNC_COMPLETE;
1641 goto out;
1642 }
1643 spin_unlock_irqrestore(&task->task_state_lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001644 mvi_dev->dev_status = MVS_DEV_EH;
Andy Yan20b09c22009-05-08 17:46:40 -04001645 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1646 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1647
1648 int_to_scsilun(cmnd->device->lun, &lun);
1649 rc = mvs_find_tag(mvi, task, &tag);
1650 if (rc == 0) {
1651 mv_printk("No such tag in %s\n", __func__);
1652 rc = TMF_RESP_FUNC_FAILED;
1653 return rc;
1654 }
1655
1656 tmf_task.tmf = TMF_ABORT_TASK;
1657 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1658
1659 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1660
1661 /* if successful, clear the task and callback forwards.*/
1662 if (rc == TMF_RESP_FUNC_COMPLETE) {
1663 u32 slot_no;
1664 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -04001665
1666 if (task->lldd_task) {
Andy Yanf9da3be2009-05-14 20:41:21 -04001667 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001668 slot_no = (u32) (slot - mvi->slot_info);
Srinivas9dc9fd92010-02-15 00:00:00 -06001669 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001670 mvs_slot_complete(mvi, slot_no, 1);
Srinivas9dc9fd92010-02-15 00:00:00 -06001671 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001672 }
1673 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001674
Andy Yan20b09c22009-05-08 17:46:40 -04001675 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1676 task->task_proto & SAS_PROTOCOL_STP) {
1677 /* to do free register_set */
Srinivas9dc9fd92010-02-15 00:00:00 -06001678 if (SATA_DEV == dev->dev_type) {
1679 struct mvs_slot_info *slot = task->lldd_task;
1680 struct task_status_struct *tstat;
1681 u32 slot_idx = (u32)(slot - mvi->slot_info);
1682 tstat = &task->task_status;
1683 mv_dprintk(KERN_DEBUG "mv_abort_task() mvi=%p task=%p "
1684 "slot=%p slot_idx=x%x\n",
1685 mvi, task, slot, slot_idx);
1686 tstat->stat = SAS_ABORTED_TASK;
1687 if (mvi_dev && mvi_dev->running_req)
1688 mvi_dev->running_req--;
1689 if (sas_protocol_ata(task->task_proto))
1690 mvs_free_reg_set(mvi, mvi_dev);
1691 mvs_slot_task_free(mvi, task, slot, slot_idx);
1692 return -1;
1693 }
Andy Yan20b09c22009-05-08 17:46:40 -04001694 } else {
1695 /* SMP */
1696
1697 }
1698out:
1699 if (rc != TMF_RESP_FUNC_COMPLETE)
1700 mv_printk("%s:rc= %d\n", __func__, rc);
1701 return rc;
1702}
1703
1704int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1705{
1706 int rc = TMF_RESP_FUNC_FAILED;
1707 struct mvs_tmf_task tmf_task;
1708
1709 tmf_task.tmf = TMF_ABORT_TASK_SET;
1710 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1711
1712 return rc;
1713}
1714
1715int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1716{
1717 int rc = TMF_RESP_FUNC_FAILED;
1718 struct mvs_tmf_task tmf_task;
1719
1720 tmf_task.tmf = TMF_CLEAR_ACA;
1721 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1722
1723 return rc;
1724}
1725
1726int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1727{
1728 int rc = TMF_RESP_FUNC_FAILED;
1729 struct mvs_tmf_task tmf_task;
1730
1731 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1732 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1733
1734 return rc;
Ke Weie9ff91b2008-03-27 14:55:33 +08001735}
1736
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001737static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1738 u32 slot_idx, int err)
1739{
Andy Yanf9da3be2009-05-14 20:41:21 -04001740 struct mvs_device *mvi_dev = task->dev->lldd_dev;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001741 struct task_status_struct *tstat = &task->task_status;
1742 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001743 int stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001744
Andy Yan20b09c22009-05-08 17:46:40 -04001745
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001746 resp->frame_len = sizeof(struct dev_to_host_fis);
1747 memcpy(&resp->ending_fis[0],
Andy Yan20b09c22009-05-08 17:46:40 -04001748 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001749 sizeof(struct dev_to_host_fis));
1750 tstat->buf_valid_size = sizeof(*resp);
Srinivas9dc9fd92010-02-15 00:00:00 -06001751 if (unlikely(err)) {
1752 if (unlikely(err & CMD_ISS_STPD))
1753 stat = SAS_OPEN_REJECT;
1754 else
1755 stat = SAS_PROTO_RESPONSE;
1756 }
1757
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001758 return stat;
1759}
1760
1761static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1762 u32 slot_idx)
1763{
1764 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
Andy Yan20b09c22009-05-08 17:46:40 -04001765 int stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001766 u32 err_dw0 = le32_to_cpu(*(u32 *) (slot->response));
Andy Yan20b09c22009-05-08 17:46:40 -04001767 u32 tfs = 0;
1768 enum mvs_port_type type = PORT_TYPE_SAS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001769
Andy Yan20b09c22009-05-08 17:46:40 -04001770 if (err_dw0 & CMD_ISS_STPD)
1771 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1772
1773 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1774
James Bottomleydf64d3c2010-07-27 15:51:13 -05001775 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001776 switch (task->task_proto) {
1777 case SAS_PROTOCOL_SSP:
Andy Yan20b09c22009-05-08 17:46:40 -04001778 stat = SAS_ABORTED_TASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001779 break;
1780 case SAS_PROTOCOL_SMP:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001781 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001782 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001783
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001784 case SAS_PROTOCOL_SATA:
1785 case SAS_PROTOCOL_STP:
1786 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
Andy Yan20b09c22009-05-08 17:46:40 -04001787 {
1788 if (err_dw0 == 0x80400002)
1789 mv_printk("find reserved error, why?\n");
1790
1791 task->ata_task.use_ncq = 0;
Srinivas9dc9fd92010-02-15 00:00:00 -06001792 mvs_sata_done(mvi, task, slot_idx, err_dw0);
Andy Yan20b09c22009-05-08 17:46:40 -04001793 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001794 break;
1795 default:
1796 break;
1797 }
1798
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001799 return stat;
1800}
1801
Andy Yan20b09c22009-05-08 17:46:40 -04001802int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001803{
1804 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1805 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1806 struct sas_task *task = slot->task;
Andy Yan20b09c22009-05-08 17:46:40 -04001807 struct mvs_device *mvi_dev = NULL;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001808 struct task_status_struct *tstat;
Srinivas9dc9fd92010-02-15 00:00:00 -06001809 struct domain_device *dev;
1810 u32 aborted;
Andy Yan20b09c22009-05-08 17:46:40 -04001811
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001812 void *to;
Andy Yan20b09c22009-05-08 17:46:40 -04001813 enum exec_status sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001814
Srinivas9dc9fd92010-02-15 00:00:00 -06001815 if (unlikely(!task || !task->lldd_task || !task->dev))
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001816 return -1;
1817
Andy Yan20b09c22009-05-08 17:46:40 -04001818 tstat = &task->task_status;
Srinivas9dc9fd92010-02-15 00:00:00 -06001819 dev = task->dev;
1820 mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001821
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001822 spin_lock(&task->task_state_lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001823 task->task_state_flags &=
1824 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1825 task->task_state_flags |= SAS_TASK_STATE_DONE;
1826 /* race condition*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001827 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001828 spin_unlock(&task->task_state_lock);
1829
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001830 memset(tstat, 0, sizeof(*tstat));
1831 tstat->resp = SAS_TASK_COMPLETE;
1832
Andy Yan20b09c22009-05-08 17:46:40 -04001833 if (unlikely(aborted)) {
1834 tstat->stat = SAS_ABORTED_TASK;
Srinivas9dc9fd92010-02-15 00:00:00 -06001835 if (mvi_dev && mvi_dev->running_req)
1836 mvi_dev->running_req--;
Andy Yan20b09c22009-05-08 17:46:40 -04001837 if (sas_protocol_ata(task->task_proto))
1838 mvs_free_reg_set(mvi, mvi_dev);
1839
1840 mvs_slot_task_free(mvi, task, slot, slot_idx);
1841 return -1;
1842 }
1843
Srinivas9dc9fd92010-02-15 00:00:00 -06001844 if (unlikely(!mvi_dev || flags)) {
1845 if (!mvi_dev)
1846 mv_dprintk("port has not device.\n");
Andy Yan20b09c22009-05-08 17:46:40 -04001847 tstat->stat = SAS_PHY_DOWN;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001848 goto out;
1849 }
1850
1851 /* error info record present */
1852 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
1853 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
Srinivas9dc9fd92010-02-15 00:00:00 -06001854 tstat->resp = SAS_TASK_COMPLETE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001855 goto out;
1856 }
1857
1858 switch (task->task_proto) {
1859 case SAS_PROTOCOL_SSP:
1860 /* hw says status == 0, datapres == 0 */
1861 if (rx_desc & RXQ_GOOD) {
James Bottomleydf64d3c2010-07-27 15:51:13 -05001862 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001863 tstat->resp = SAS_TASK_COMPLETE;
1864 }
1865 /* response frame present */
1866 else if (rx_desc & RXQ_RSP) {
Andy Yan20b09c22009-05-08 17:46:40 -04001867 struct ssp_response_iu *iu = slot->response +
1868 sizeof(struct mvs_err_info);
1869 sas_ssp_task_response(mvi->dev, task, iu);
1870 } else
James Bottomleydf64d3c2010-07-27 15:51:13 -05001871 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001872 break;
1873
1874 case SAS_PROTOCOL_SMP: {
1875 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001876 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001877 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
1878 memcpy(to + sg_resp->offset,
1879 slot->response + sizeof(struct mvs_err_info),
1880 sg_dma_len(sg_resp));
1881 kunmap_atomic(to, KM_IRQ0);
1882 break;
1883 }
1884
1885 case SAS_PROTOCOL_SATA:
1886 case SAS_PROTOCOL_STP:
1887 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1888 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1889 break;
1890 }
1891
1892 default:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001893 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001894 break;
1895 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001896 if (!slot->port->port_attached) {
1897 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1898 tstat->stat = SAS_PHY_DOWN;
1899 }
1900
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001901
1902out:
Srinivas9dc9fd92010-02-15 00:00:00 -06001903 if (mvi_dev && mvi_dev->running_req) {
1904 mvi_dev->running_req--;
1905 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
Andy Yan0f980a82009-05-11 21:49:52 +08001906 mvs_free_reg_set(mvi, mvi_dev);
1907 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001908 mvs_slot_task_free(mvi, task, slot, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001909 sts = tstat->stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001910
1911 spin_unlock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001912 if (task->task_done)
1913 task->task_done(task);
1914 else
1915 mv_dprintk("why has not task_done.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001916 spin_lock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001917
1918 return sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001919}
1920
Srinivas9dc9fd92010-02-15 00:00:00 -06001921void mvs_do_release_task(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -04001922 int phy_no, struct domain_device *dev)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001923{
Srinivas9dc9fd92010-02-15 00:00:00 -06001924 u32 slot_idx;
Andy Yan20b09c22009-05-08 17:46:40 -04001925 struct mvs_phy *phy;
1926 struct mvs_port *port;
1927 struct mvs_slot_info *slot, *slot2;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001928
Andy Yan20b09c22009-05-08 17:46:40 -04001929 phy = &mvi->phy[phy_no];
1930 port = phy->port;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001931 if (!port)
1932 return;
Srinivas9dc9fd92010-02-15 00:00:00 -06001933 /* clean cmpl queue in case request is already finished */
1934 mvs_int_rx(mvi, false);
1935
1936
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001937
Andy Yan20b09c22009-05-08 17:46:40 -04001938 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1939 struct sas_task *task;
1940 slot_idx = (u32) (slot - mvi->slot_info);
1941 task = slot->task;
1942
1943 if (dev && task->dev != dev)
1944 continue;
1945
1946 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1947 slot_idx, slot->slot_tag, task);
Srinivas9dc9fd92010-02-15 00:00:00 -06001948 MVS_CHIP_DISP->command_active(mvi, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001949
1950 mvs_slot_complete(mvi, slot_idx, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001951 }
1952}
1953
Srinivas9dc9fd92010-02-15 00:00:00 -06001954void mvs_release_task(struct mvs_info *mvi,
1955 struct domain_device *dev)
1956{
1957 int i, phyno[WIDE_PORT_MAX_PHY], num;
1958 /* housekeeper */
1959 num = mvs_find_dev_phyno(dev, phyno);
1960 for (i = 0; i < num; i++)
1961 mvs_do_release_task(mvi, phyno[i], dev);
1962}
1963
Andy Yan20b09c22009-05-08 17:46:40 -04001964static void mvs_phy_disconnected(struct mvs_phy *phy)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001965{
Andy Yan20b09c22009-05-08 17:46:40 -04001966 phy->phy_attached = 0;
1967 phy->att_dev_info = 0;
1968 phy->att_dev_sas_addr = 0;
1969}
1970
1971static void mvs_work_queue(struct work_struct *work)
1972{
1973 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1974 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
1975 struct mvs_info *mvi = mwq->mvi;
1976 unsigned long flags;
1977
1978 spin_lock_irqsave(&mvi->lock, flags);
1979 if (mwq->handler & PHY_PLUG_EVENT) {
1980 u32 phy_no = (unsigned long) mwq->data;
1981 struct sas_ha_struct *sas_ha = mvi->sas;
1982 struct mvs_phy *phy = &mvi->phy[phy_no];
1983 struct asd_sas_phy *sas_phy = &phy->sas_phy;
1984
1985 if (phy->phy_event & PHY_PLUG_OUT) {
1986 u32 tmp;
1987 struct sas_identify_frame *id;
1988 id = (struct sas_identify_frame *)phy->frame_rcvd;
1989 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
1990 phy->phy_event &= ~PHY_PLUG_OUT;
1991 if (!(tmp & PHY_READY_MASK)) {
1992 sas_phy_disconnected(sas_phy);
1993 mvs_phy_disconnected(phy);
1994 sas_ha->notify_phy_event(sas_phy,
1995 PHYE_LOSS_OF_SIGNAL);
1996 mv_dprintk("phy%d Removed Device\n", phy_no);
1997 } else {
1998 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
1999 mvs_update_phyinfo(mvi, phy_no, 1);
2000 mvs_bytes_dmaed(mvi, phy_no);
2001 mvs_port_notify_formed(sas_phy, 0);
2002 mv_dprintk("phy%d Attached Device\n", phy_no);
2003 }
2004 }
2005 }
2006 list_del(&mwq->entry);
2007 spin_unlock_irqrestore(&mvi->lock, flags);
2008 kfree(mwq);
2009}
2010
2011static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2012{
2013 struct mvs_wq *mwq;
2014 int ret = 0;
2015
2016 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2017 if (mwq) {
2018 mwq->mvi = mvi;
2019 mwq->data = data;
2020 mwq->handler = handler;
2021 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2022 list_add_tail(&mwq->entry, &mvi->wq_list);
2023 schedule_delayed_work(&mwq->work_q, HZ * 2);
2024 } else
2025 ret = -ENOMEM;
2026
2027 return ret;
2028}
2029
2030static void mvs_sig_time_out(unsigned long tphy)
2031{
2032 struct mvs_phy *phy = (struct mvs_phy *)tphy;
2033 struct mvs_info *mvi = phy->mvi;
2034 u8 phy_no;
2035
2036 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2037 if (&mvi->phy[phy_no] == phy) {
2038 mv_dprintk("Get signature time out, reset phy %d\n",
2039 phy_no+mvi->id*mvi->chip->n_phy);
2040 MVS_CHIP_DISP->phy_reset(mvi, phy_no, 1);
2041 }
2042 }
2043}
2044
Andy Yan20b09c22009-05-08 17:46:40 -04002045void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2046{
2047 u32 tmp;
2048 struct sas_ha_struct *sas_ha = mvi->sas;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002049 struct mvs_phy *phy = &mvi->phy[phy_no];
2050 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2051
Andy Yan20b09c22009-05-08 17:46:40 -04002052 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2053 mv_dprintk("port %d ctrl sts=0x%X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2054 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2055 mv_dprintk("Port %d irq sts = 0x%X\n", phy_no+mvi->id*mvi->chip->n_phy,
2056 phy->irq_status);
2057
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002058 /*
2059 * events is port event now ,
2060 * we need check the interrupt status which belongs to per port.
2061 */
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002062
Srinivas9dc9fd92010-02-15 00:00:00 -06002063 if (phy->irq_status & PHYEV_DCDR_ERR) {
Andy Yan20b09c22009-05-08 17:46:40 -04002064 mv_dprintk("port %d STP decoding error.\n",
Srinivas9dc9fd92010-02-15 00:00:00 -06002065 phy_no + mvi->id*mvi->chip->n_phy);
2066 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002067
Andy Yan20b09c22009-05-08 17:46:40 -04002068 if (phy->irq_status & PHYEV_POOF) {
2069 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2070 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2071 int ready;
Srinivas9dc9fd92010-02-15 00:00:00 -06002072 mvs_do_release_task(mvi, phy_no, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04002073 phy->phy_event |= PHY_PLUG_OUT;
Srinivas9dc9fd92010-02-15 00:00:00 -06002074 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
Andy Yan20b09c22009-05-08 17:46:40 -04002075 mvs_handle_event(mvi,
2076 (void *)(unsigned long)phy_no,
2077 PHY_PLUG_EVENT);
2078 ready = mvs_is_phy_ready(mvi, phy_no);
2079 if (!ready)
2080 mv_dprintk("phy%d Unplug Notice\n",
2081 phy_no +
2082 mvi->id * mvi->chip->n_phy);
2083 if (ready || dev_sata) {
2084 if (MVS_CHIP_DISP->stp_reset)
2085 MVS_CHIP_DISP->stp_reset(mvi,
2086 phy_no);
2087 else
2088 MVS_CHIP_DISP->phy_reset(mvi,
2089 phy_no, 0);
2090 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002091 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002092 }
2093 }
Andy Yan20b09c22009-05-08 17:46:40 -04002094
2095 if (phy->irq_status & PHYEV_COMWAKE) {
2096 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2097 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2098 tmp | PHYEV_SIG_FIS);
2099 if (phy->timer.function == NULL) {
2100 phy->timer.data = (unsigned long)phy;
2101 phy->timer.function = mvs_sig_time_out;
2102 phy->timer.expires = jiffies + 10*HZ;
2103 add_timer(&phy->timer);
2104 }
2105 }
2106 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2107 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002108 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2109 if (phy->phy_status) {
2110 mdelay(10);
2111 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2112 if (phy->phy_type & PORT_TYPE_SATA) {
2113 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2114 mvi, phy_no);
2115 tmp &= ~PHYEV_SIG_FIS;
2116 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2117 phy_no, tmp);
2118 }
2119 mvs_update_phyinfo(mvi, phy_no, 0);
Srinivas9dc9fd92010-02-15 00:00:00 -06002120 if (phy->phy_type & PORT_TYPE_SAS) {
2121 MVS_CHIP_DISP->phy_reset(mvi, phy_no, 2);
2122 mdelay(10);
2123 }
2124
Andy Yan20b09c22009-05-08 17:46:40 -04002125 mvs_bytes_dmaed(mvi, phy_no);
2126 /* whether driver is going to handle hot plug */
2127 if (phy->phy_event & PHY_PLUG_OUT) {
2128 mvs_port_notify_formed(sas_phy, 0);
2129 phy->phy_event &= ~PHY_PLUG_OUT;
2130 }
2131 } else {
2132 mv_dprintk("plugin interrupt but phy%d is gone\n",
2133 phy_no + mvi->id*mvi->chip->n_phy);
2134 }
2135 } else if (phy->irq_status & PHYEV_BROAD_CH) {
2136 mv_dprintk("port %d broadcast change.\n",
2137 phy_no + mvi->id*mvi->chip->n_phy);
2138 /* exception for Samsung disk drive*/
2139 mdelay(1000);
2140 sas_ha->notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
2141 }
2142 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002143}
2144
Andy Yan20b09c22009-05-08 17:46:40 -04002145int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002146{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002147 u32 rx_prod_idx, rx_desc;
2148 bool attn = false;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002149
2150 /* the first dword in the RX ring is special: it contains
2151 * a mirror of the hardware's RX producer index, so that
2152 * we don't have to stall the CPU reading that register.
2153 * The actual RX ring is offset by one dword, due to this.
2154 */
2155 rx_prod_idx = mvi->rx_cons;
2156 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2157 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2158 return 0;
2159
2160 /* The CMPL_Q may come late, read from register and try again
2161 * note: if coalescing is enabled,
2162 * it will need to read from register every time for sure
2163 */
Andy Yan20b09c22009-05-08 17:46:40 -04002164 if (unlikely(mvi->rx_cons == rx_prod_idx))
2165 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002166
2167 if (mvi->rx_cons == rx_prod_idx)
2168 return 0;
2169
2170 while (mvi->rx_cons != rx_prod_idx) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002171 /* increment our internal RX consumer pointer */
2172 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002173 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2174
2175 if (likely(rx_desc & RXQ_DONE))
2176 mvs_slot_complete(mvi, rx_desc, 0);
2177 if (rx_desc & RXQ_ATTN) {
2178 attn = true;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002179 } else if (rx_desc & RXQ_ERR) {
2180 if (!(rx_desc & RXQ_DONE))
2181 mvs_slot_complete(mvi, rx_desc, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002182 } else if (rx_desc & RXQ_SLOT_RESET) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002183 mvs_slot_free(mvi, rx_desc);
2184 }
2185 }
2186
2187 if (attn && self_clear)
Andy Yan20b09c22009-05-08 17:46:40 -04002188 MVS_CHIP_DISP->int_full(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002189 return 0;
2190}
2191