blob: ee81d10252e043fa1ed505d429ed7a7dbcebde3d [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
Baoyou Xie14bf41d2016-09-25 13:48:50 +080077static struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
Andy Yan20b09c22009-05-08 17:46:40 -040078{
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
Baoyou Xie14bf41d2016-09-25 13:48:50 +0800105static int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
Andy Yan20b09c22009-05-08 17:46:40 -0400106{
107 unsigned long i = 0, j = 0, n = 0, num = 0;
Andy Yan9870d9a2009-05-11 22:19:25 +0800108 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
109 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -0400110 struct sas_ha_struct *sha = dev->port->ha;
111
112 while (sha->sas_port[i]) {
113 if (sha->sas_port[i] == dev->port) {
114 struct asd_sas_phy *phy;
115 list_for_each_entry(phy,
116 &sha->sas_port[i]->phy_list, port_phy_el) {
117 j = 0;
118 while (sha->sas_phy[j]) {
119 if (sha->sas_phy[j] == phy)
120 break;
121 j++;
122 }
123 phyno[n] = (j >= mvi->chip->n_phy) ?
124 (j - mvi->chip->n_phy) : j;
125 num++;
126 n++;
127 }
128 break;
129 }
130 i++;
131 }
132 return num;
133}
134
Xiangliang Yu534ff102011-05-24 22:26:50 +0800135struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
136 u8 reg_set)
137{
138 u32 dev_no;
139 for (dev_no = 0; dev_no < MVS_MAX_DEVICES; dev_no++) {
140 if (mvi->devices[dev_no].taskfileset == MVS_ID_NOT_MAPPED)
141 continue;
142
143 if (mvi->devices[dev_no].taskfileset == reg_set)
144 return &mvi->devices[dev_no];
145 }
146 return NULL;
147}
148
Andy Yan20b09c22009-05-08 17:46:40 -0400149static inline void mvs_free_reg_set(struct mvs_info *mvi,
150 struct mvs_device *dev)
151{
152 if (!dev) {
153 mv_printk("device has been free.\n");
154 return;
155 }
Andy Yan20b09c22009-05-08 17:46:40 -0400156 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
157 return;
158 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
159}
160
161static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
162 struct mvs_device *dev)
163{
164 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
165 return 0;
166 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
167}
168
169void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
170{
171 u32 no;
172 for_each_phy(phy_mask, phy_mask, no) {
173 if (!(phy_mask & 1))
174 continue;
175 MVS_CHIP_DISP->phy_reset(mvi, no, hard);
176 }
177}
178
Andy Yan20b09c22009-05-08 17:46:40 -0400179int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
180 void *funcdata)
181{
182 int rc = 0, phy_id = sas_phy->id;
183 u32 tmp, i = 0, hi;
184 struct sas_ha_struct *sha = sas_phy->ha;
185 struct mvs_info *mvi = NULL;
186
187 while (sha->sas_phy[i]) {
188 if (sha->sas_phy[i] == sas_phy)
189 break;
190 i++;
191 }
192 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
193 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
194
195 switch (func) {
196 case PHY_FUNC_SET_LINK_RATE:
197 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
198 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400199
200 case PHY_FUNC_HARD_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400201 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400202 if (tmp & PHY_RST_HARD)
203 break;
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800204 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400205 break;
206
207 case PHY_FUNC_LINK_RESET:
Andy Yan20b09c22009-05-08 17:46:40 -0400208 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
Xiangliang Yua4632aa2011-05-24 22:36:02 +0800209 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_SOFT_RESET);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400210 break;
211
212 case PHY_FUNC_DISABLE:
Andy Yan20b09c22009-05-08 17:46:40 -0400213 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
214 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400215 case PHY_FUNC_RELEASE_SPINUP_HOLD:
216 default:
Dan Williamsac013ed12011-09-28 18:48:02 -0700217 rc = -ENOSYS;
Jeff Garzikb5762942007-10-25 20:58:22 -0400218 }
Andy Yan20b09c22009-05-08 17:46:40 -0400219 msleep(200);
Jeff Garzikb5762942007-10-25 20:58:22 -0400220 return rc;
221}
222
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -0800223void mvs_set_sas_addr(struct mvs_info *mvi, int port_id, u32 off_lo,
224 u32 off_hi, u64 sas_addr)
Andy Yan20b09c22009-05-08 17:46:40 -0400225{
226 u32 lo = (u32)sas_addr;
227 u32 hi = (u32)(sas_addr>>32);
228
229 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_lo);
230 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, lo);
231 MVS_CHIP_DISP->write_port_cfg_addr(mvi, port_id, off_hi);
232 MVS_CHIP_DISP->write_port_cfg_data(mvi, port_id, hi);
233}
234
Ke Wei8f261aa2008-02-23 21:15:27 +0800235static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
236{
237 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400238 struct asd_sas_phy *sas_phy = &phy->sas_phy;
239 struct sas_ha_struct *sas_ha;
Ke Wei8f261aa2008-02-23 21:15:27 +0800240 if (!phy->phy_attached)
241 return;
242
Andy Yan20b09c22009-05-08 17:46:40 -0400243 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
244 && phy->phy_type & PORT_TYPE_SAS) {
245 return;
246 }
247
248 sas_ha = mvi->sas;
249 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
250
Ke Weiee1f1c22008-03-27 14:53:47 +0800251 if (sas_phy->phy) {
252 struct sas_phy *sphy = sas_phy->phy;
253
254 sphy->negotiated_linkrate = sas_phy->linkrate;
255 sphy->minimum_linkrate = phy->minimum_linkrate;
256 sphy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
257 sphy->maximum_linkrate = phy->maximum_linkrate;
Andy Yan20b09c22009-05-08 17:46:40 -0400258 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
Ke Weiee1f1c22008-03-27 14:53:47 +0800259 }
260
Ke Wei8f261aa2008-02-23 21:15:27 +0800261 if (phy->phy_type & PORT_TYPE_SAS) {
262 struct sas_identify_frame *id;
263
264 id = (struct sas_identify_frame *)phy->frame_rcvd;
265 id->dev_type = phy->identify.device_type;
266 id->initiator_bits = SAS_PROTOCOL_ALL;
267 id->target_bits = phy->identify.target_port_protocols;
Xiangliang Yu477f6d12011-09-29 00:33:49 -0700268
269 /* direct attached SAS device */
270 if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
271 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
272 MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x00);
273 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800274 } else if (phy->phy_type & PORT_TYPE_SATA) {
Andy Yan20b09c22009-05-08 17:46:40 -0400275 /*Nothing*/
Ke Wei8f261aa2008-02-23 21:15:27 +0800276 }
Andy Yan20b09c22009-05-08 17:46:40 -0400277 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
278
279 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
280
281 mvi->sas->notify_port_event(sas_phy,
Ke Wei8f261aa2008-02-23 21:15:27 +0800282 PORTE_BYTES_DMAED);
283}
284
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400285void mvs_scan_start(struct Scsi_Host *shost)
Jeff Garzikb5762942007-10-25 20:58:22 -0400286{
Andy Yan20b09c22009-05-08 17:46:40 -0400287 int i, j;
288 unsigned short core_nr;
289 struct mvs_info *mvi;
290 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800291 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
Jeff Garzikb5762942007-10-25 20:58:22 -0400292
Andy Yan20b09c22009-05-08 17:46:40 -0400293 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
294
295 for (j = 0; j < core_nr; j++) {
296 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
297 for (i = 0; i < mvi->chip->n_phy; ++i)
298 mvs_bytes_dmaed(mvi, i);
Jeff Garzikb5762942007-10-25 20:58:22 -0400299 }
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800300 mvs_prv->scan_finished = 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400301}
302
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400303int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
Jeff Garzikb5762942007-10-25 20:58:22 -0400304{
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800305 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
306 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
307
308 if (mvs_prv->scan_finished == 0)
Ke Wei8f261aa2008-02-23 21:15:27 +0800309 return 0;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800310
Dan Williamsb1124cd2011-12-19 16:42:34 -0800311 sas_drain_work(sha);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400312 return 1;
Jeff Garzikb5762942007-10-25 20:58:22 -0400313}
314
Ke Wei8f261aa2008-02-23 21:15:27 +0800315static int mvs_task_prep_smp(struct mvs_info *mvi,
316 struct mvs_task_exec_info *tei)
Jeff Garzikb5762942007-10-25 20:58:22 -0400317{
Ke Wei8f261aa2008-02-23 21:15:27 +0800318 int elem, rc, i;
Xiangliang Yu7c237c52013-01-30 00:25:53 +0800319 struct sas_ha_struct *sha = mvi->sas;
Ke Wei8f261aa2008-02-23 21:15:27 +0800320 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400321 struct mvs_cmd_hdr *hdr = tei->hdr;
Andy Yan20b09c22009-05-08 17:46:40 -0400322 struct domain_device *dev = task->dev;
323 struct asd_sas_port *sas_port = dev->port;
Xiangliang Yu7c237c52013-01-30 00:25:53 +0800324 struct sas_phy *sphy = dev->phy;
325 struct asd_sas_phy *sas_phy = sha->sas_phy[sphy->number];
Jeff Garzikb5762942007-10-25 20:58:22 -0400326 struct scatterlist *sg_req, *sg_resp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800327 u32 req_len, resp_len, tag = tei->tag;
328 void *buf_tmp;
329 u8 *buf_oaf;
330 dma_addr_t buf_tmp_dma;
Andy Yan20b09c22009-05-08 17:46:40 -0400331 void *buf_prd;
Ke Wei8f261aa2008-02-23 21:15:27 +0800332 struct mvs_slot_info *slot = &mvi->slot_info[tag];
Ke Wei8f261aa2008-02-23 21:15:27 +0800333 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yub89e8f52011-05-24 22:35:09 +0800334
Jeff Garzikb5762942007-10-25 20:58:22 -0400335 /*
336 * DMA-map SMP request, response buffers
337 */
Ke Wei8f261aa2008-02-23 21:15:27 +0800338 sg_req = &task->smp_task.smp_req;
Andy Yan20b09c22009-05-08 17:46:40 -0400339 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400340 if (!elem)
341 return -ENOMEM;
342 req_len = sg_dma_len(sg_req);
343
Ke Wei8f261aa2008-02-23 21:15:27 +0800344 sg_resp = &task->smp_task.smp_resp;
Andy Yan20b09c22009-05-08 17:46:40 -0400345 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400346 if (!elem) {
347 rc = -ENOMEM;
348 goto err_out;
349 }
Andy Yan20b09c22009-05-08 17:46:40 -0400350 resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400351
352 /* must be in dwords */
353 if ((req_len & 0x3) || (resp_len & 0x3)) {
354 rc = -EINVAL;
355 goto err_out_2;
356 }
357
358 /*
Ke Wei8f261aa2008-02-23 21:15:27 +0800359 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
Jeff Garzikb5762942007-10-25 20:58:22 -0400360 */
361
Andy Yan20b09c22009-05-08 17:46:40 -0400362 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800363 buf_tmp = slot->buf;
364 buf_tmp_dma = slot->buf_dma;
Jeff Garzikb5762942007-10-25 20:58:22 -0400365
Ke Wei8f261aa2008-02-23 21:15:27 +0800366 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
Ke Wei8f261aa2008-02-23 21:15:27 +0800367
368 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
369 buf_oaf = buf_tmp;
370 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
371
372 buf_tmp += MVS_OAF_SZ;
373 buf_tmp_dma += MVS_OAF_SZ;
374
Andy Yan20b09c22009-05-08 17:46:40 -0400375 /* region 3: PRD table *********************************** */
Ke Wei8f261aa2008-02-23 21:15:27 +0800376 buf_prd = buf_tmp;
377 if (tei->n_elem)
378 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
379 else
380 hdr->prd_tbl = 0;
381
Andy Yan20b09c22009-05-08 17:46:40 -0400382 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Ke Wei8f261aa2008-02-23 21:15:27 +0800383 buf_tmp += i;
384 buf_tmp_dma += i;
385
386 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
387 slot->response = buf_tmp;
388 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400389 if (mvi->flags & MVF_FLAG_SOC)
390 hdr->reserved[0] = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800391
392 /*
393 * Fill in TX ring and command slot header
394 */
395 slot->tx = mvi->tx_prod;
396 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
397 TXQ_MODE_I | tag |
Xiangliang Yu7c237c52013-01-30 00:25:53 +0800398 (MVS_PHY_ID << TXQ_PHY_SHIFT));
Ke Wei8f261aa2008-02-23 21:15:27 +0800399
400 hdr->flags |= flags;
401 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
Jeff Garzikb5762942007-10-25 20:58:22 -0400402 hdr->tags = cpu_to_le32(tag);
403 hdr->data_len = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400404
Ke Wei8f261aa2008-02-23 21:15:27 +0800405 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400406 /* initiator, SMP, ftype 1h */
407 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
Xiangliang Yu6ceae7c2011-09-29 00:32:37 -0700408 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
Ke Wei8f261aa2008-02-23 21:15:27 +0800409 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
Andy Yan20b09c22009-05-08 17:46:40 -0400410 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800411
412 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400413 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Ke Wei8f261aa2008-02-23 21:15:27 +0800414
Jeff Garzikb5762942007-10-25 20:58:22 -0400415 return 0;
416
417err_out_2:
Andy Yan20b09c22009-05-08 17:46:40 -0400418 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400419 PCI_DMA_FROMDEVICE);
420err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400421 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
Jeff Garzikb5762942007-10-25 20:58:22 -0400422 PCI_DMA_TODEVICE);
423 return rc;
424}
425
Ke Wei4e52fc02008-03-27 14:54:50 +0800426static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
Ke Wei8f261aa2008-02-23 21:15:27 +0800427{
Ke Wei8f261aa2008-02-23 21:15:27 +0800428 struct ata_queued_cmd *qc = task->uldd_task;
429
Ke Wei4e52fc02008-03-27 14:54:50 +0800430 if (qc) {
431 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
Hannes Reineckeef026b12016-04-25 12:45:44 +0200432 qc->tf.command == ATA_CMD_FPDMA_READ ||
433 qc->tf.command == ATA_CMD_FPDMA_RECV ||
Hannes Reinecke661ce1f2016-04-25 12:45:45 +0200434 qc->tf.command == ATA_CMD_FPDMA_SEND ||
435 qc->tf.command == ATA_CMD_NCQ_NON_DATA) {
Ke Wei4e52fc02008-03-27 14:54:50 +0800436 *tag = qc->tag;
437 return 1;
438 }
439 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800440
Ke Wei4e52fc02008-03-27 14:54:50 +0800441 return 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800442}
443
Jeff Garzikb5762942007-10-25 20:58:22 -0400444static int mvs_task_prep_ata(struct mvs_info *mvi,
445 struct mvs_task_exec_info *tei)
446{
447 struct sas_task *task = tei->task;
448 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400449 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400450 struct mvs_cmd_hdr *hdr = tei->hdr;
451 struct asd_sas_port *sas_port = dev->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800452 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400453 void *buf_prd;
454 u32 tag = tei->tag, hdr_tag;
455 u32 flags, del_q;
Jeff Garzikb5762942007-10-25 20:58:22 -0400456 void *buf_tmp;
457 u8 *buf_cmd, *buf_oaf;
458 dma_addr_t buf_tmp_dma;
Ke Wei8f261aa2008-02-23 21:15:27 +0800459 u32 i, req_len, resp_len;
460 const u32 max_resp_len = SB_RFB_MAX;
Jeff Garzikb5762942007-10-25 20:58:22 -0400461
Andy Yan20b09c22009-05-08 17:46:40 -0400462 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
463 mv_dprintk("Have not enough regiset for dev %d.\n",
464 mvi_dev->device_id);
Ke Wei8f261aa2008-02-23 21:15:27 +0800465 return -EBUSY;
Andy Yan20b09c22009-05-08 17:46:40 -0400466 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800467 slot = &mvi->slot_info[tag];
468 slot->tx = mvi->tx_prod;
Andy Yan20b09c22009-05-08 17:46:40 -0400469 del_q = TXQ_MODE_I | tag |
470 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
James Bottomley56cbd0c2015-04-15 22:16:01 -0700471 ((sas_port->phy_mask & TXQ_PHY_MASK) << TXQ_PHY_SHIFT) |
Andy Yan20b09c22009-05-08 17:46:40 -0400472 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
473 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
Jeff Garzikb5762942007-10-25 20:58:22 -0400474
Andy Yan20b09c22009-05-08 17:46:40 -0400475 if (task->data_dir == DMA_FROM_DEVICE)
476 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
477 else
478 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800479
Jeff Garzikb5762942007-10-25 20:58:22 -0400480 if (task->ata_task.use_ncq)
481 flags |= MCH_FPDMA;
Hannes Reinecke1cbd7722014-11-05 13:08:20 +0100482 if (dev->sata_dev.class == ATA_DEV_ATAPI) {
Ke Wei8f261aa2008-02-23 21:15:27 +0800483 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
484 flags |= MCH_ATAPI;
485 }
486
Jeff Garzikb5762942007-10-25 20:58:22 -0400487 hdr->flags = cpu_to_le32(flags);
Ke Wei8f261aa2008-02-23 21:15:27 +0800488
Andy Yan20b09c22009-05-08 17:46:40 -0400489 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
490 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
Ke Wei4e52fc02008-03-27 14:54:50 +0800491 else
Andy Yan20b09c22009-05-08 17:46:40 -0400492 hdr_tag = tag;
493
494 hdr->tags = cpu_to_le32(hdr_tag);
495
Jeff Garzikb5762942007-10-25 20:58:22 -0400496 hdr->data_len = cpu_to_le32(task->total_xfer_len);
497
498 /*
499 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
500 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400501
Ke Wei8f261aa2008-02-23 21:15:27 +0800502 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
503 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400504 buf_tmp_dma = slot->buf_dma;
505
506 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
507
508 buf_tmp += MVS_ATA_CMD_SZ;
509 buf_tmp_dma += MVS_ATA_CMD_SZ;
510
Ke Wei8f261aa2008-02-23 21:15:27 +0800511 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400512 /* used for STP. unused for SATA? */
513 buf_oaf = buf_tmp;
514 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
515
516 buf_tmp += MVS_OAF_SZ;
517 buf_tmp_dma += MVS_OAF_SZ;
518
Ke Wei8f261aa2008-02-23 21:15:27 +0800519 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400520 buf_prd = buf_tmp;
Andy Yan20b09c22009-05-08 17:46:40 -0400521
Ke Wei8f261aa2008-02-23 21:15:27 +0800522 if (tei->n_elem)
523 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
524 else
525 hdr->prd_tbl = 0;
Andy Yan20b09c22009-05-08 17:46:40 -0400526 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
Jeff Garzikb5762942007-10-25 20:58:22 -0400527
Jeff Garzikb5762942007-10-25 20:58:22 -0400528 buf_tmp += i;
529 buf_tmp_dma += i;
530
Ke Wei8f261aa2008-02-23 21:15:27 +0800531 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400532 slot->response = buf_tmp;
533 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400534 if (mvi->flags & MVF_FLAG_SOC)
535 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400536
Ke Wei8f261aa2008-02-23 21:15:27 +0800537 req_len = sizeof(struct host_to_dev_fis);
Jeff Garzikb5762942007-10-25 20:58:22 -0400538 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800539 sizeof(struct mvs_err_info) - i;
Jeff Garzikb5762942007-10-25 20:58:22 -0400540
541 /* request, response lengths */
Ke Wei8f261aa2008-02-23 21:15:27 +0800542 resp_len = min(resp_len, max_resp_len);
Jeff Garzikb5762942007-10-25 20:58:22 -0400543 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
544
Andy Yan20b09c22009-05-08 17:46:40 -0400545 if (likely(!task->ata_task.device_control_reg_update))
546 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
Jeff Garzikb5762942007-10-25 20:58:22 -0400547 /* fill in command FIS and ATAPI CDB */
Ke Wei8f261aa2008-02-23 21:15:27 +0800548 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
Hannes Reinecke1cbd7722014-11-05 13:08:20 +0100549 if (dev->sata_dev.class == ATA_DEV_ATAPI)
Ke Wei8f261aa2008-02-23 21:15:27 +0800550 memcpy(buf_cmd + STP_ATAPI_CMD,
551 task->ata_task.atapi_packet, 16);
552
553 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400554 /* initiator, STP, ftype 1h */
555 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
Xiangliang Yu6ceae7c2011-09-29 00:32:37 -0700556 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
Andy Yan20b09c22009-05-08 17:46:40 -0400557 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
558 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400559
560 /* fill in PRD (scatter/gather) table, if any */
Andy Yan20b09c22009-05-08 17:46:40 -0400561 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800562
Andy Yan20b09c22009-05-08 17:46:40 -0400563 if (task->data_dir == DMA_FROM_DEVICE)
Xiangliang Yu8882f082011-05-24 22:33:11 +0800564 MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
Andy Yan20b09c22009-05-08 17:46:40 -0400565 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
Xiangliang Yu8882f082011-05-24 22:33:11 +0800566
Jeff Garzikb5762942007-10-25 20:58:22 -0400567 return 0;
568}
569
570static int mvs_task_prep_ssp(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -0400571 struct mvs_task_exec_info *tei, int is_tmf,
572 struct mvs_tmf_task *tmf)
Jeff Garzikb5762942007-10-25 20:58:22 -0400573{
574 struct sas_task *task = tei->task;
Jeff Garzikb5762942007-10-25 20:58:22 -0400575 struct mvs_cmd_hdr *hdr = tei->hdr;
Ke Wei8f261aa2008-02-23 21:15:27 +0800576 struct mvs_port *port = tei->port;
Andy Yan20b09c22009-05-08 17:46:40 -0400577 struct domain_device *dev = task->dev;
Andy Yanf9da3be2009-05-14 20:41:21 -0400578 struct mvs_device *mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -0400579 struct asd_sas_port *sas_port = dev->port;
Jeff Garzikb5762942007-10-25 20:58:22 -0400580 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -0400581 void *buf_prd;
Jeff Garzikb5762942007-10-25 20:58:22 -0400582 struct ssp_frame_hdr *ssp_hdr;
583 void *buf_tmp;
584 u8 *buf_cmd, *buf_oaf, fburst = 0;
585 dma_addr_t buf_tmp_dma;
586 u32 flags;
Ke Wei8f261aa2008-02-23 21:15:27 +0800587 u32 resp_len, req_len, i, tag = tei->tag;
588 const u32 max_resp_len = SB_RFB_MAX;
Andy Yan20b09c22009-05-08 17:46:40 -0400589 u32 phy_mask;
Jeff Garzikb5762942007-10-25 20:58:22 -0400590
591 slot = &mvi->slot_info[tag];
592
Andy Yan20b09c22009-05-08 17:46:40 -0400593 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
594 sas_port->phy_mask) & TXQ_PHY_MASK;
595
Ke Wei8f261aa2008-02-23 21:15:27 +0800596 slot->tx = mvi->tx_prod;
597 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
598 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
Ke Wei4e52fc02008-03-27 14:54:50 +0800599 (phy_mask << TXQ_PHY_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400600
601 flags = MCH_RETRY;
602 if (task->ssp_task.enable_first_burst) {
603 flags |= MCH_FBURST;
604 fburst = (1 << 7);
605 }
Andy Yan2b288132009-05-11 20:01:55 +0800606 if (is_tmf)
607 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800608 else
609 flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
610
Andy Yan2b288132009-05-11 20:01:55 +0800611 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
Jeff Garzikb5762942007-10-25 20:58:22 -0400612 hdr->tags = cpu_to_le32(tag);
613 hdr->data_len = cpu_to_le32(task->total_xfer_len);
614
615 /*
616 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
617 */
Jeff Garzikb5762942007-10-25 20:58:22 -0400618
Ke Wei8f261aa2008-02-23 21:15:27 +0800619 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
620 buf_cmd = buf_tmp = slot->buf;
Jeff Garzikb5762942007-10-25 20:58:22 -0400621 buf_tmp_dma = slot->buf_dma;
622
623 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
624
625 buf_tmp += MVS_SSP_CMD_SZ;
626 buf_tmp_dma += MVS_SSP_CMD_SZ;
627
Ke Wei8f261aa2008-02-23 21:15:27 +0800628 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400629 buf_oaf = buf_tmp;
630 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
631
632 buf_tmp += MVS_OAF_SZ;
633 buf_tmp_dma += MVS_OAF_SZ;
634
Ke Wei8f261aa2008-02-23 21:15:27 +0800635 /* region 3: PRD table ********************************************* */
Jeff Garzikb5762942007-10-25 20:58:22 -0400636 buf_prd = buf_tmp;
Ke Wei8f261aa2008-02-23 21:15:27 +0800637 if (tei->n_elem)
638 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
639 else
640 hdr->prd_tbl = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400641
Andy Yan20b09c22009-05-08 17:46:40 -0400642 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
Jeff Garzikb5762942007-10-25 20:58:22 -0400643 buf_tmp += i;
644 buf_tmp_dma += i;
645
Ke Wei8f261aa2008-02-23 21:15:27 +0800646 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
Jeff Garzikb5762942007-10-25 20:58:22 -0400647 slot->response = buf_tmp;
648 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
Andy Yan20b09c22009-05-08 17:46:40 -0400649 if (mvi->flags & MVF_FLAG_SOC)
650 hdr->reserved[0] = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400651
Jeff Garzikb5762942007-10-25 20:58:22 -0400652 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
Ke Wei8f261aa2008-02-23 21:15:27 +0800653 sizeof(struct mvs_err_info) - i;
654 resp_len = min(resp_len, max_resp_len);
655
656 req_len = sizeof(struct ssp_frame_hdr) + 28;
Jeff Garzikb5762942007-10-25 20:58:22 -0400657
658 /* request, response lengths */
659 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
660
661 /* generate open address frame hdr (first 12 bytes) */
Andy Yan20b09c22009-05-08 17:46:40 -0400662 /* initiator, SSP, ftype 1h */
663 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
Xiangliang Yu6ceae7c2011-09-29 00:32:37 -0700664 buf_oaf[1] = min(sas_port->linkrate, dev->linkrate) & 0xf;
Andy Yan20b09c22009-05-08 17:46:40 -0400665 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
666 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400667
Ke Wei8f261aa2008-02-23 21:15:27 +0800668 /* fill in SSP frame header (Command Table.SSP frame header) */
669 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
Andy Yan20b09c22009-05-08 17:46:40 -0400670
671 if (is_tmf)
672 ssp_hdr->frame_type = SSP_TASK;
673 else
674 ssp_hdr->frame_type = SSP_COMMAND;
675
676 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
Jeff Garzikb5762942007-10-25 20:58:22 -0400677 HASHED_SAS_ADDR_SIZE);
678 memcpy(ssp_hdr->hashed_src_addr,
Andy Yan20b09c22009-05-08 17:46:40 -0400679 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
Jeff Garzikb5762942007-10-25 20:58:22 -0400680 ssp_hdr->tag = cpu_to_be16(tag);
681
Andy Yan20b09c22009-05-08 17:46:40 -0400682 /* fill in IU for TASK and Command Frame */
Jeff Garzikb5762942007-10-25 20:58:22 -0400683 buf_cmd += sizeof(*ssp_hdr);
684 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
Jeff Garzikb5762942007-10-25 20:58:22 -0400685
Andy Yan20b09c22009-05-08 17:46:40 -0400686 if (ssp_hdr->frame_type != SSP_TASK) {
687 buf_cmd[9] = fburst | task->ssp_task.task_attr |
688 (task->ssp_task.task_prio << 3);
James Bottomleye73823f2013-05-07 15:38:18 -0700689 memcpy(buf_cmd + 12, task->ssp_task.cmd->cmnd,
690 task->ssp_task.cmd->cmd_len);
Andy Yan20b09c22009-05-08 17:46:40 -0400691 } else{
692 buf_cmd[10] = tmf->tmf;
693 switch (tmf->tmf) {
694 case TMF_ABORT_TASK:
695 case TMF_QUERY_TASK:
696 buf_cmd[12] =
697 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
698 buf_cmd[13] =
699 tmf->tag_of_task_to_be_managed & 0xff;
700 break;
701 default:
702 break;
703 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400704 }
Andy Yan20b09c22009-05-08 17:46:40 -0400705 /* fill in PRD (scatter/gather) table, if any */
706 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
Jeff Garzikb5762942007-10-25 20:58:22 -0400707 return 0;
708}
709
James Bottomleyaa9f8322013-05-07 14:44:06 -0700710#define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == SAS_PHY_UNUSED)))
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700711static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
712 struct mvs_tmf_task *tmf, int *pass)
Jeff Garzikb5762942007-10-25 20:58:22 -0400713{
Ke Wei8f261aa2008-02-23 21:15:27 +0800714 struct domain_device *dev = task->dev;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700715 struct mvs_device *mvi_dev = dev->lldd_dev;
Jeff Garzikb5762942007-10-25 20:58:22 -0400716 struct mvs_task_exec_info tei;
Ke Wei4e52fc02008-03-27 14:54:50 +0800717 struct mvs_slot_info *slot;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700718 u32 tag = 0xdeadbeef, n_elem = 0;
719 int rc = 0;
Jeff Garzikb5762942007-10-25 20:58:22 -0400720
Andy Yan20b09c22009-05-08 17:46:40 -0400721 if (!dev->port) {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700722 struct task_status_struct *tsm = &task->task_status;
Andy Yan20b09c22009-05-08 17:46:40 -0400723
724 tsm->resp = SAS_TASK_UNDELIVERED;
725 tsm->stat = SAS_PHY_DOWN;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700726 /*
727 * libsas will use dev->port, should
728 * not call task_done for sata
729 */
James Bottomleyaa9f8322013-05-07 14:44:06 -0700730 if (dev->dev_type != SAS_SATA_DEV)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700731 task->task_done(task);
732 return rc;
Andy Yan20b09c22009-05-08 17:46:40 -0400733 }
734
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700735 if (DEV_IS_GONE(mvi_dev)) {
736 if (mvi_dev)
737 mv_dprintk("device %d not ready.\n",
738 mvi_dev->device_id);
739 else
740 mv_dprintk("device %016llx not ready.\n",
741 SAS_ADDR(dev->sas_addr));
Andy Yan20b09c22009-05-08 17:46:40 -0400742
Luis de Bethencourt7789cd32015-11-30 14:32:17 +0000743 rc = SAS_PHY_DOWN;
744 return rc;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700745 }
746 tei.port = dev->port->lldd_port;
747 if (tei.port && !tei.port->port_attached && !tmf) {
748 if (sas_protocol_ata(task->task_proto)) {
749 struct task_status_struct *ts = &task->task_status;
750 mv_dprintk("SATA/STP port %d does not attach"
751 "device.\n", dev->port->id);
752 ts->resp = SAS_TASK_COMPLETE;
753 ts->stat = SAS_PHY_DOWN;
Andy Yan20b09c22009-05-08 17:46:40 -0400754
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700755 task->task_done(task);
Jeff Garzikb5762942007-10-25 20:58:22 -0400756
Ke Wei8f261aa2008-02-23 21:15:27 +0800757 } else {
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700758 struct task_status_struct *ts = &task->task_status;
759 mv_dprintk("SAS port %d does not attach"
760 "device.\n", dev->port->id);
761 ts->resp = SAS_TASK_UNDELIVERED;
762 ts->stat = SAS_PHY_DOWN;
763 task->task_done(task);
Ke Wei8f261aa2008-02-23 21:15:27 +0800764 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700765 return rc;
766 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400767
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700768 if (!sas_protocol_ata(task->task_proto)) {
769 if (task->num_scatter) {
770 n_elem = dma_map_sg(mvi->dev,
771 task->scatter,
772 task->num_scatter,
773 task->data_dir);
774 if (!n_elem) {
775 rc = -ENOMEM;
776 goto prep_out;
777 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800778 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700779 } else {
780 n_elem = task->num_scatter;
781 }
Jeff Garzikb5762942007-10-25 20:58:22 -0400782
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700783 rc = mvs_tag_alloc(mvi, &tag);
784 if (rc)
785 goto err_out;
Jeff Garzikb5762942007-10-25 20:58:22 -0400786
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700787 slot = &mvi->slot_info[tag];
Srinivas9dc9fd92010-02-15 00:00:00 -0600788
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700789 task->lldd_task = NULL;
790 slot->n_elem = n_elem;
791 slot->slot_tag = tag;
Jeff Garzikb5762942007-10-25 20:58:22 -0400792
Romain Perier4dbd6712017-07-06 10:13:08 +0200793 slot->buf = dma_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
Wei Yongjun18eddae2016-10-31 15:04:10 +0000794 if (!slot->buf) {
795 rc = -ENOMEM;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700796 goto err_out_tag;
Wei Yongjun18eddae2016-10-31 15:04:10 +0000797 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700798 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
799
800 tei.task = task;
801 tei.hdr = &mvi->slot[tag];
802 tei.tag = tag;
803 tei.n_elem = n_elem;
804 switch (task->task_proto) {
805 case SAS_PROTOCOL_SMP:
806 rc = mvs_task_prep_smp(mvi, &tei);
807 break;
808 case SAS_PROTOCOL_SSP:
809 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
810 break;
811 case SAS_PROTOCOL_SATA:
812 case SAS_PROTOCOL_STP:
813 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
814 rc = mvs_task_prep_ata(mvi, &tei);
815 break;
816 default:
817 dev_printk(KERN_ERR, mvi->dev,
818 "unknown sas_task proto: 0x%x\n",
819 task->task_proto);
820 rc = -EINVAL;
821 break;
822 }
823
824 if (rc) {
825 mv_dprintk("rc is %x\n", rc);
826 goto err_out_slot_buf;
827 }
828 slot->task = task;
829 slot->port = tei.port;
830 task->lldd_task = slot;
831 list_add_tail(&slot->entry, &tei.port->list);
832 spin_lock(&task->task_state_lock);
833 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
834 spin_unlock(&task->task_state_lock);
835
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700836 mvi_dev->running_req++;
837 ++(*pass);
838 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
839
840 return rc;
841
842err_out_slot_buf:
Romain Perier4dbd6712017-07-06 10:13:08 +0200843 dma_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
Jeff Garzikb5762942007-10-25 20:58:22 -0400844err_out_tag:
Ke Wei8f261aa2008-02-23 21:15:27 +0800845 mvs_tag_free(mvi, tag);
Jeff Garzikb5762942007-10-25 20:58:22 -0400846err_out:
Andy Yan20b09c22009-05-08 17:46:40 -0400847
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700848 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
849 if (!sas_protocol_ata(task->task_proto))
Ke Wei8f261aa2008-02-23 21:15:27 +0800850 if (n_elem)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700851 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
852 task->data_dir);
853prep_out:
854 return rc;
855}
856
Christoph Hellwig79855d12014-11-05 10:36:28 +0100857static int mvs_task_exec(struct sas_task *task, gfp_t gfp_flags,
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700858 struct completion *completion, int is_tmf,
859 struct mvs_tmf_task *tmf)
860{
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700861 struct mvs_info *mvi = NULL;
862 u32 rc = 0;
863 u32 pass = 0;
864 unsigned long flags = 0;
865
866 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
867
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700868 spin_lock_irqsave(&mvi->lock, flags);
869 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
870 if (rc)
871 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
872
873 if (likely(pass))
874 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
875 (MVS_CHIP_SLOT_SZ - 1));
Andy Yan0b84b702009-05-11 20:05:26 +0800876 spin_unlock_irqrestore(&mvi->lock, flags);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700877
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700878 return rc;
879}
880
Christoph Hellwig79855d12014-11-05 10:36:28 +0100881int mvs_queue_command(struct sas_task *task, gfp_t gfp_flags)
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700882{
Christoph Hellwig79855d12014-11-05 10:36:28 +0100883 return mvs_task_exec(task, gfp_flags, NULL, 0, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -0400884}
885
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400886static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
Ke Wei8f261aa2008-02-23 21:15:27 +0800887{
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400888 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
889 mvs_tag_clear(mvi, slot_idx);
890}
Ke Wei8f261aa2008-02-23 21:15:27 +0800891
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400892static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
893 struct mvs_slot_info *slot, u32 slot_idx)
894{
Dāvis Mosāns22805212015-08-21 07:29:22 +0300895 if (!slot)
896 return;
Andy Yan20b09c22009-05-08 17:46:40 -0400897 if (!slot->task)
898 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400899 if (!sas_protocol_ata(task->task_proto))
900 if (slot->n_elem)
Andy Yan20b09c22009-05-08 17:46:40 -0400901 dma_unmap_sg(mvi->dev, task->scatter,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400902 slot->n_elem, task->data_dir);
Ke Wei8f261aa2008-02-23 21:15:27 +0800903
Ke Wei8f261aa2008-02-23 21:15:27 +0800904 switch (task->task_proto) {
905 case SAS_PROTOCOL_SMP:
Andy Yan20b09c22009-05-08 17:46:40 -0400906 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400907 PCI_DMA_FROMDEVICE);
Andy Yan20b09c22009-05-08 17:46:40 -0400908 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400909 PCI_DMA_TODEVICE);
Ke Wei8f261aa2008-02-23 21:15:27 +0800910 break;
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400911
Ke Wei8f261aa2008-02-23 21:15:27 +0800912 case SAS_PROTOCOL_SATA:
913 case SAS_PROTOCOL_STP:
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400914 case SAS_PROTOCOL_SSP:
Ke Wei8f261aa2008-02-23 21:15:27 +0800915 default:
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400916 /* do nothing */
Ke Wei8f261aa2008-02-23 21:15:27 +0800917 break;
918 }
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700919
920 if (slot->buf) {
Romain Perier4dbd6712017-07-06 10:13:08 +0200921 dma_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
Xiangliang Yu0b15fb12011-04-26 06:36:51 -0700922 slot->buf = NULL;
923 }
Andy Yan20b09c22009-05-08 17:46:40 -0400924 list_del_init(&slot->entry);
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400925 task->lldd_task = NULL;
926 slot->task = NULL;
927 slot->port = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -0400928 slot->slot_tag = 0xFFFFFFFF;
929 mvs_slot_free(mvi, slot_idx);
Ke Wei8f261aa2008-02-23 21:15:27 +0800930}
931
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800932static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
Ke Wei8f261aa2008-02-23 21:15:27 +0800933{
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800934 struct mvs_phy *phy = &mvi->phy[phy_no];
Ke Wei8f261aa2008-02-23 21:15:27 +0800935 struct mvs_port *port = phy->port;
936 int j, no;
937
Andy Yan20b09c22009-05-08 17:46:40 -0400938 for_each_phy(port->wide_port_phymap, j, no) {
939 if (j & 1) {
940 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
941 PHYR_WIDE_PORT);
942 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
Ke Wei8f261aa2008-02-23 21:15:27 +0800943 port->wide_port_phymap);
944 } else {
Andy Yan20b09c22009-05-08 17:46:40 -0400945 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
946 PHYR_WIDE_PORT);
947 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
948 0);
Ke Wei8f261aa2008-02-23 21:15:27 +0800949 }
Andy Yan20b09c22009-05-08 17:46:40 -0400950 }
Ke Wei8f261aa2008-02-23 21:15:27 +0800951}
952
953static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
954{
955 u32 tmp;
956 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -0400957 struct mvs_port *port = phy->port;
Ke Wei8f261aa2008-02-23 21:15:27 +0800958
Andy Yan20b09c22009-05-08 17:46:40 -0400959 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +0800960 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
Ke Wei963829e2008-03-27 14:55:23 +0800961 if (!port)
Ke Wei8f261aa2008-02-23 21:15:27 +0800962 phy->phy_attached = 1;
963 return tmp;
964 }
965
Ke Wei8f261aa2008-02-23 21:15:27 +0800966 if (port) {
967 if (phy->phy_type & PORT_TYPE_SAS) {
968 port->wide_port_phymap &= ~(1U << i);
969 if (!port->wide_port_phymap)
970 port->port_attached = 0;
971 mvs_update_wideport(mvi, i);
972 } else if (phy->phy_type & PORT_TYPE_SATA)
973 port->port_attached = 0;
Ke Wei8f261aa2008-02-23 21:15:27 +0800974 phy->port = NULL;
975 phy->phy_attached = 0;
976 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
977 }
978 return 0;
979}
980
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400981static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
982{
983 u32 *s = (u32 *) buf;
984
985 if (!s)
986 return NULL;
987
Andy Yan20b09c22009-05-08 17:46:40 -0400988 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800989 s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400990
Andy Yan20b09c22009-05-08 17:46:40 -0400991 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800992 s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400993
Andy Yan20b09c22009-05-08 17:46:40 -0400994 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800995 s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Jeff Garzikdd4969a2009-05-08 17:44:01 -0400996
Andy Yan20b09c22009-05-08 17:46:40 -0400997 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +0800998 s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
Andy Yan20b09c22009-05-08 17:46:40 -0400999
Andy Yan20b09c22009-05-08 17:46:40 -04001000 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1001 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001002
Andy Yanf9da3be2009-05-14 20:41:21 -04001003 return s;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001004}
1005
1006static u32 mvs_is_sig_fis_received(u32 irq_status)
1007{
1008 return irq_status & PHYEV_SIG_FIS;
1009}
1010
Xiangliang Yu8882f082011-05-24 22:33:11 +08001011static void mvs_sig_remove_timer(struct mvs_phy *phy)
1012{
1013 if (phy->timer.function)
1014 del_timer(&phy->timer);
1015 phy->timer.function = NULL;
1016}
1017
Andy Yan20b09c22009-05-08 17:46:40 -04001018void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
Ke Wei8f261aa2008-02-23 21:15:27 +08001019{
1020 struct mvs_phy *phy = &mvi->phy[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001021 struct sas_identify_frame *id;
Ke Wei8f261aa2008-02-23 21:15:27 +08001022
Andy Yan20b09c22009-05-08 17:46:40 -04001023 id = (struct sas_identify_frame *)phy->frame_rcvd;
Ke Wei8f261aa2008-02-23 21:15:27 +08001024
1025 if (get_st) {
Andy Yan20b09c22009-05-08 17:46:40 -04001026 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001027 phy->phy_status = mvs_is_phy_ready(mvi, i);
1028 }
1029
1030 if (phy->phy_status) {
Andy Yan20b09c22009-05-08 17:46:40 -04001031 int oob_done = 0;
1032 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
Ke Wei8f261aa2008-02-23 21:15:27 +08001033
Andy Yan20b09c22009-05-08 17:46:40 -04001034 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001035
Andy Yan20b09c22009-05-08 17:46:40 -04001036 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1037 if (phy->phy_type & PORT_TYPE_SATA) {
1038 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1039 if (mvs_is_sig_fis_received(phy->irq_status)) {
Xiangliang Yu8882f082011-05-24 22:33:11 +08001040 mvs_sig_remove_timer(phy);
Andy Yan20b09c22009-05-08 17:46:40 -04001041 phy->phy_attached = 1;
1042 phy->att_dev_sas_addr =
1043 i + mvi->id * mvi->chip->n_phy;
1044 if (oob_done)
1045 sas_phy->oob_mode = SATA_OOB_MODE;
1046 phy->frame_rcvd_size =
1047 sizeof(struct dev_to_host_fis);
Andy Yanf9da3be2009-05-14 20:41:21 -04001048 mvs_get_d2h_reg(mvi, i, id);
Andy Yan20b09c22009-05-08 17:46:40 -04001049 } else {
1050 u32 tmp;
1051 dev_printk(KERN_DEBUG, mvi->dev,
1052 "Phy%d : No sig fis\n", i);
1053 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1054 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1055 tmp | PHYEV_SIG_FIS);
1056 phy->phy_attached = 0;
1057 phy->phy_type &= ~PORT_TYPE_SATA;
Andy Yan20b09c22009-05-08 17:46:40 -04001058 goto out_done;
1059 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001060 } else if (phy->phy_type & PORT_TYPE_SAS
Andy Yan20b09c22009-05-08 17:46:40 -04001061 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1062 phy->phy_attached = 1;
Ke Wei8f261aa2008-02-23 21:15:27 +08001063 phy->identify.device_type =
Andy Yan20b09c22009-05-08 17:46:40 -04001064 phy->att_dev_info & PORT_DEV_TYPE_MASK;
Ke Wei8f261aa2008-02-23 21:15:27 +08001065
James Bottomleyaa9f8322013-05-07 14:44:06 -07001066 if (phy->identify.device_type == SAS_END_DEVICE)
Ke Wei8f261aa2008-02-23 21:15:27 +08001067 phy->identify.target_port_protocols =
1068 SAS_PROTOCOL_SSP;
James Bottomleyaa9f8322013-05-07 14:44:06 -07001069 else if (phy->identify.device_type != SAS_PHY_UNUSED)
Ke Wei8f261aa2008-02-23 21:15:27 +08001070 phy->identify.target_port_protocols =
1071 SAS_PROTOCOL_SMP;
Andy Yan20b09c22009-05-08 17:46:40 -04001072 if (oob_done)
Ke Wei8f261aa2008-02-23 21:15:27 +08001073 sas_phy->oob_mode = SAS_OOB_MODE;
1074 phy->frame_rcvd_size =
1075 sizeof(struct sas_identify_frame);
Ke Wei8f261aa2008-02-23 21:15:27 +08001076 }
Andy Yan20b09c22009-05-08 17:46:40 -04001077 memcpy(sas_phy->attached_sas_addr,
1078 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
Ke Weie9ff91b2008-03-27 14:55:33 +08001079
Andy Yan20b09c22009-05-08 17:46:40 -04001080 if (MVS_CHIP_DISP->phy_work_around)
1081 MVS_CHIP_DISP->phy_work_around(mvi, i);
Ke Wei8f261aa2008-02-23 21:15:27 +08001082 }
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001083 mv_dprintk("phy %d attach dev info is %x\n",
Andy Yan20b09c22009-05-08 17:46:40 -04001084 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001085 mv_dprintk("phy %d attach sas addr is %llx\n",
Andy Yan20b09c22009-05-08 17:46:40 -04001086 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
Ke Weie9ff91b2008-03-27 14:55:33 +08001087out_done:
Ke Wei8f261aa2008-02-23 21:15:27 +08001088 if (get_st)
Andy Yan20b09c22009-05-08 17:46:40 -04001089 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
Ke Wei8f261aa2008-02-23 21:15:27 +08001090}
1091
Andy Yan20b09c22009-05-08 17:46:40 -04001092static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1093{
1094 struct sas_ha_struct *sas_ha = sas_phy->ha;
1095 struct mvs_info *mvi = NULL; int i = 0, hi;
1096 struct mvs_phy *phy = sas_phy->lldd_phy;
1097 struct asd_sas_port *sas_port = sas_phy->port;
1098 struct mvs_port *port;
1099 unsigned long flags = 0;
1100 if (!sas_port)
1101 return;
1102
1103 while (sas_ha->sas_phy[i]) {
1104 if (sas_ha->sas_phy[i] == sas_phy)
1105 break;
1106 i++;
1107 }
1108 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1109 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001110 if (i >= mvi->chip->n_phy)
1111 port = &mvi->port[i - mvi->chip->n_phy];
Andy Yan20b09c22009-05-08 17:46:40 -04001112 else
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001113 port = &mvi->port[i];
Andy Yan20b09c22009-05-08 17:46:40 -04001114 if (lock)
1115 spin_lock_irqsave(&mvi->lock, flags);
1116 port->port_attached = 1;
1117 phy->port = port;
Xiangliang Yu0b15fb12011-04-26 06:36:51 -07001118 sas_port->lldd_port = port;
Andy Yan20b09c22009-05-08 17:46:40 -04001119 if (phy->phy_type & PORT_TYPE_SAS) {
1120 port->wide_port_phymap = sas_port->phy_mask;
1121 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1122 mvs_update_wideport(mvi, sas_phy->id);
Xiangliang Yu477f6d12011-09-29 00:33:49 -07001123
1124 /* direct attached SAS device */
1125 if (phy->att_dev_info & PORT_SSP_TRGT_MASK) {
1126 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_PHY_STAT);
1127 MVS_CHIP_DISP->write_port_cfg_data(mvi, i, 0x04);
1128 }
Andy Yan20b09c22009-05-08 17:46:40 -04001129 }
1130 if (lock)
1131 spin_unlock_irqrestore(&mvi->lock, flags);
1132}
1133
1134static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1135{
Srinivas9dc9fd92010-02-15 00:00:00 -06001136 struct domain_device *dev;
1137 struct mvs_phy *phy = sas_phy->lldd_phy;
1138 struct mvs_info *mvi = phy->mvi;
1139 struct asd_sas_port *port = sas_phy->port;
1140 int phy_no = 0;
1141
1142 while (phy != &mvi->phy[phy_no]) {
1143 phy_no++;
1144 if (phy_no >= MVS_MAX_PHYS)
1145 return;
1146 }
1147 list_for_each_entry(dev, &port->dev_list, dev_list_node)
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001148 mvs_do_release_task(phy->mvi, phy_no, dev);
Srinivas9dc9fd92010-02-15 00:00:00 -06001149
Andy Yan20b09c22009-05-08 17:46:40 -04001150}
1151
1152
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001153void mvs_port_formed(struct asd_sas_phy *sas_phy)
Ke Wei8f261aa2008-02-23 21:15:27 +08001154{
Andy Yan20b09c22009-05-08 17:46:40 -04001155 mvs_port_notify_formed(sas_phy, 1);
1156}
Ke Wei8f261aa2008-02-23 21:15:27 +08001157
Andy Yan20b09c22009-05-08 17:46:40 -04001158void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1159{
1160 mvs_port_notify_deformed(sas_phy, 1);
1161}
1162
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001163static struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
Andy Yan20b09c22009-05-08 17:46:40 -04001164{
1165 u32 dev;
1166 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
James Bottomleyaa9f8322013-05-07 14:44:06 -07001167 if (mvi->devices[dev].dev_type == SAS_PHY_UNUSED) {
Andy Yan20b09c22009-05-08 17:46:40 -04001168 mvi->devices[dev].device_id = dev;
1169 return &mvi->devices[dev];
1170 }
Ke Wei8f261aa2008-02-23 21:15:27 +08001171 }
Andy Yan20b09c22009-05-08 17:46:40 -04001172
1173 if (dev == MVS_MAX_DEVICES)
1174 mv_printk("max support %d devices, ignore ..\n",
1175 MVS_MAX_DEVICES);
1176
1177 return NULL;
1178}
1179
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001180static void mvs_free_dev(struct mvs_device *mvi_dev)
Andy Yan20b09c22009-05-08 17:46:40 -04001181{
1182 u32 id = mvi_dev->device_id;
1183 memset(mvi_dev, 0, sizeof(*mvi_dev));
1184 mvi_dev->device_id = id;
James Bottomleyaa9f8322013-05-07 14:44:06 -07001185 mvi_dev->dev_type = SAS_PHY_UNUSED;
Andy Yan20b09c22009-05-08 17:46:40 -04001186 mvi_dev->dev_status = MVS_DEV_NORMAL;
1187 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1188}
1189
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001190static int mvs_dev_found_notify(struct domain_device *dev, int lock)
Andy Yan20b09c22009-05-08 17:46:40 -04001191{
1192 unsigned long flags = 0;
1193 int res = 0;
1194 struct mvs_info *mvi = NULL;
1195 struct domain_device *parent_dev = dev->parent;
1196 struct mvs_device *mvi_device;
1197
1198 mvi = mvs_find_dev_mvi(dev);
1199
1200 if (lock)
1201 spin_lock_irqsave(&mvi->lock, flags);
1202
1203 mvi_device = mvs_alloc_dev(mvi);
1204 if (!mvi_device) {
1205 res = -1;
1206 goto found_out;
1207 }
Andy Yanf9da3be2009-05-14 20:41:21 -04001208 dev->lldd_dev = mvi_device;
Srinivas9dc9fd92010-02-15 00:00:00 -06001209 mvi_device->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001210 mvi_device->dev_type = dev->dev_type;
Andy Yan9870d9a2009-05-11 22:19:25 +08001211 mvi_device->mvi_info = mvi;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001212 mvi_device->sas_device = dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001213 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1214 int phy_id;
1215 u8 phy_num = parent_dev->ex_dev.num_phys;
1216 struct ex_phy *phy;
1217 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1218 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1219 if (SAS_ADDR(phy->attached_sas_addr) ==
1220 SAS_ADDR(dev->sas_addr)) {
1221 mvi_device->attached_phy = phy_id;
1222 break;
1223 }
1224 }
1225
1226 if (phy_id == phy_num) {
1227 mv_printk("Error: no attached dev:%016llx"
1228 "at ex:%016llx.\n",
1229 SAS_ADDR(dev->sas_addr),
1230 SAS_ADDR(parent_dev->sas_addr));
1231 res = -1;
1232 }
1233 }
1234
1235found_out:
1236 if (lock)
1237 spin_unlock_irqrestore(&mvi->lock, flags);
1238 return res;
1239}
1240
1241int mvs_dev_found(struct domain_device *dev)
1242{
1243 return mvs_dev_found_notify(dev, 1);
1244}
1245
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001246static void mvs_dev_gone_notify(struct domain_device *dev)
Andy Yan20b09c22009-05-08 17:46:40 -04001247{
1248 unsigned long flags = 0;
Andy Yanf9da3be2009-05-14 20:41:21 -04001249 struct mvs_device *mvi_dev = dev->lldd_dev;
Rickard Strandqvisteaa015d2014-05-18 18:12:26 +02001250 struct mvs_info *mvi;
1251
1252 if (!mvi_dev) {
1253 mv_dprintk("found dev has gone.\n");
1254 return;
1255 }
1256
1257 mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001258
Srinivas9dc9fd92010-02-15 00:00:00 -06001259 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001260
Rickard Strandqvisteaa015d2014-05-18 18:12:26 +02001261 mv_dprintk("found dev[%d:%x] is gone.\n",
1262 mvi_dev->device_id, mvi_dev->dev_type);
1263 mvs_release_task(mvi, dev);
1264 mvs_free_reg_set(mvi, mvi_dev);
1265 mvs_free_dev(mvi_dev);
1266
Andy Yan20b09c22009-05-08 17:46:40 -04001267 dev->lldd_dev = NULL;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001268 mvi_dev->sas_device = NULL;
Andy Yan20b09c22009-05-08 17:46:40 -04001269
Srinivas9dc9fd92010-02-15 00:00:00 -06001270 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001271}
1272
1273
1274void mvs_dev_gone(struct domain_device *dev)
1275{
Srinivas9dc9fd92010-02-15 00:00:00 -06001276 mvs_dev_gone_notify(dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001277}
1278
Andy Yan20b09c22009-05-08 17:46:40 -04001279static void mvs_task_done(struct sas_task *task)
1280{
Dan Williamsf0bf7502012-06-21 23:36:30 -07001281 if (!del_timer(&task->slow_task->timer))
Andy Yan20b09c22009-05-08 17:46:40 -04001282 return;
Dan Williamsf0bf7502012-06-21 23:36:30 -07001283 complete(&task->slow_task->completion);
Andy Yan20b09c22009-05-08 17:46:40 -04001284}
1285
1286static void mvs_tmf_timedout(unsigned long data)
1287{
1288 struct sas_task *task = (struct sas_task *)data;
1289
1290 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Dan Williamsf0bf7502012-06-21 23:36:30 -07001291 complete(&task->slow_task->completion);
Andy Yan20b09c22009-05-08 17:46:40 -04001292}
1293
Andy Yan20b09c22009-05-08 17:46:40 -04001294#define MVS_TASK_TIMEOUT 20
1295static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1296 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1297{
1298 int res, retry;
1299 struct sas_task *task = NULL;
1300
1301 for (retry = 0; retry < 3; retry++) {
Dan Williamsf0bf7502012-06-21 23:36:30 -07001302 task = sas_alloc_slow_task(GFP_KERNEL);
Andy Yan20b09c22009-05-08 17:46:40 -04001303 if (!task)
1304 return -ENOMEM;
1305
1306 task->dev = dev;
1307 task->task_proto = dev->tproto;
1308
1309 memcpy(&task->ssp_task, parameter, para_len);
1310 task->task_done = mvs_task_done;
1311
Dan Williamsf0bf7502012-06-21 23:36:30 -07001312 task->slow_task->timer.data = (unsigned long) task;
1313 task->slow_task->timer.function = mvs_tmf_timedout;
1314 task->slow_task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1315 add_timer(&task->slow_task->timer);
Andy Yan20b09c22009-05-08 17:46:40 -04001316
Christoph Hellwig79855d12014-11-05 10:36:28 +01001317 res = mvs_task_exec(task, GFP_KERNEL, NULL, 1, tmf);
Andy Yan20b09c22009-05-08 17:46:40 -04001318
1319 if (res) {
Dan Williamsf0bf7502012-06-21 23:36:30 -07001320 del_timer(&task->slow_task->timer);
Masanari Iida6d3be302013-09-30 23:19:09 +09001321 mv_printk("executing internal task failed:%d\n", res);
Andy Yan20b09c22009-05-08 17:46:40 -04001322 goto ex_err;
1323 }
1324
Dan Williamsf0bf7502012-06-21 23:36:30 -07001325 wait_for_completion(&task->slow_task->completion);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001326 res = TMF_RESP_FUNC_FAILED;
Andy Yan20b09c22009-05-08 17:46:40 -04001327 /* Even TMF timed out, return direct. */
1328 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1329 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1330 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1331 goto ex_err;
1332 }
1333 }
1334
1335 if (task->task_status.resp == SAS_TASK_COMPLETE &&
James Bottomleydf64d3c2010-07-27 15:51:13 -05001336 task->task_status.stat == SAM_STAT_GOOD) {
Andy Yan20b09c22009-05-08 17:46:40 -04001337 res = TMF_RESP_FUNC_COMPLETE;
1338 break;
1339 }
1340
1341 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1342 task->task_status.stat == SAS_DATA_UNDERRUN) {
1343 /* no error, but return the number of bytes of
1344 * underrun */
1345 res = task->task_status.residual;
1346 break;
1347 }
1348
1349 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1350 task->task_status.stat == SAS_DATA_OVERRUN) {
1351 mv_dprintk("blocked task error.\n");
1352 res = -EMSGSIZE;
1353 break;
1354 } else {
1355 mv_dprintk(" task to dev %016llx response: 0x%x "
1356 "status 0x%x\n",
1357 SAS_ADDR(dev->sas_addr),
1358 task->task_status.resp,
1359 task->task_status.stat);
Dan Williams4fcf8122011-07-29 17:26:39 -07001360 sas_free_task(task);
Andy Yan20b09c22009-05-08 17:46:40 -04001361 task = NULL;
1362
1363 }
1364 }
1365ex_err:
1366 BUG_ON(retry == 3 && task != NULL);
Dan Williams4fcf8122011-07-29 17:26:39 -07001367 sas_free_task(task);
Andy Yan20b09c22009-05-08 17:46:40 -04001368 return res;
1369}
1370
1371static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1372 u8 *lun, struct mvs_tmf_task *tmf)
1373{
1374 struct sas_ssp_task ssp_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001375 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1376 return TMF_RESP_FUNC_ESUPP;
1377
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001378 memcpy(ssp_task.LUN, lun, 8);
Andy Yan20b09c22009-05-08 17:46:40 -04001379
1380 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1381 sizeof(ssp_task), tmf);
1382}
1383
1384
1385/* Standard mandates link reset for ATA (type 0)
1386 and hard reset for SSP (type 1) , only for RECOVERY */
1387static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1388{
1389 int rc;
Dan Williamsf41a0c42011-12-21 21:33:17 -08001390 struct sas_phy *phy = sas_get_local_phy(dev);
James Bottomleyaa9f8322013-05-07 14:44:06 -07001391 int reset_type = (dev->dev_type == SAS_SATA_DEV ||
Andy Yan20b09c22009-05-08 17:46:40 -04001392 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1393 rc = sas_phy_reset(phy, reset_type);
Dan Williamsf41a0c42011-12-21 21:33:17 -08001394 sas_put_local_phy(phy);
Andy Yan20b09c22009-05-08 17:46:40 -04001395 msleep(2000);
1396 return rc;
1397}
1398
1399/* mandatory SAM-3 */
1400int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1401{
1402 unsigned long flags;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001403 int rc = TMF_RESP_FUNC_FAILED;
Andy Yan20b09c22009-05-08 17:46:40 -04001404 struct mvs_tmf_task tmf_task;
Andy Yanf9da3be2009-05-14 20:41:21 -04001405 struct mvs_device * mvi_dev = dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001406 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001407
1408 tmf_task.tmf = TMF_LU_RESET;
1409 mvi_dev->dev_status = MVS_DEV_EH;
1410 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1411 if (rc == TMF_RESP_FUNC_COMPLETE) {
Andy Yan20b09c22009-05-08 17:46:40 -04001412 spin_lock_irqsave(&mvi->lock, flags);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001413 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001414 spin_unlock_irqrestore(&mvi->lock, flags);
1415 }
1416 /* If failed, fall-through I_T_Nexus reset */
1417 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1418 mvi_dev->device_id, rc);
1419 return rc;
Jeff Garzikb5762942007-10-25 20:58:22 -04001420}
1421
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001422int mvs_I_T_nexus_reset(struct domain_device *dev)
Ke Weie9ff91b2008-03-27 14:55:33 +08001423{
Andy Yan20b09c22009-05-08 17:46:40 -04001424 unsigned long flags;
Srinivas9dc9fd92010-02-15 00:00:00 -06001425 int rc = TMF_RESP_FUNC_FAILED;
1426 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001427 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001428
1429 if (mvi_dev->dev_status != MVS_DEV_EH)
1430 return TMF_RESP_FUNC_COMPLETE;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001431 else
1432 mvi_dev->dev_status = MVS_DEV_NORMAL;
Andy Yan20b09c22009-05-08 17:46:40 -04001433 rc = mvs_debug_I_T_nexus_reset(dev);
1434 mv_printk("%s for device[%x]:rc= %d\n",
1435 __func__, mvi_dev->device_id, rc);
1436
Andy Yan20b09c22009-05-08 17:46:40 -04001437 spin_lock_irqsave(&mvi->lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001438 mvs_release_task(mvi, dev);
Andy Yan20b09c22009-05-08 17:46:40 -04001439 spin_unlock_irqrestore(&mvi->lock, flags);
1440
1441 return rc;
1442}
1443/* optional SAM-3 */
1444int mvs_query_task(struct sas_task *task)
1445{
1446 u32 tag;
1447 struct scsi_lun lun;
1448 struct mvs_tmf_task tmf_task;
1449 int rc = TMF_RESP_FUNC_FAILED;
1450
1451 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1452 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1453 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001454 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1455 struct mvs_info *mvi = mvi_dev->mvi_info;
Andy Yan20b09c22009-05-08 17:46:40 -04001456
1457 int_to_scsilun(cmnd->device->lun, &lun);
1458 rc = mvs_find_tag(mvi, task, &tag);
1459 if (rc == 0) {
1460 rc = TMF_RESP_FUNC_FAILED;
1461 return rc;
1462 }
1463
1464 tmf_task.tmf = TMF_QUERY_TASK;
1465 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1466
1467 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1468 switch (rc) {
1469 /* The task is still in Lun, release it then */
1470 case TMF_RESP_FUNC_SUCC:
1471 /* The task is not in Lun or failed, reset the phy */
1472 case TMF_RESP_FUNC_FAILED:
1473 case TMF_RESP_FUNC_COMPLETE:
1474 break;
1475 }
1476 }
1477 mv_printk("%s:rc= %d\n", __func__, rc);
1478 return rc;
1479}
1480
1481/* mandatory SAM-3, still need free task/slot info */
1482int mvs_abort_task(struct sas_task *task)
1483{
1484 struct scsi_lun lun;
1485 struct mvs_tmf_task tmf_task;
1486 struct domain_device *dev = task->dev;
Andy Yan9870d9a2009-05-11 22:19:25 +08001487 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
Jiri Slaby24ae1632010-06-22 13:42:02 +02001488 struct mvs_info *mvi;
Andy Yan20b09c22009-05-08 17:46:40 -04001489 int rc = TMF_RESP_FUNC_FAILED;
1490 unsigned long flags;
1491 u32 tag;
Andy Yan9870d9a2009-05-11 22:19:25 +08001492
Srinivas9dc9fd92010-02-15 00:00:00 -06001493 if (!mvi_dev) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001494 mv_printk("Device has removed\n");
1495 return TMF_RESP_FUNC_FAILED;
Srinivas9dc9fd92010-02-15 00:00:00 -06001496 }
1497
Jiri Slaby24ae1632010-06-22 13:42:02 +02001498 mvi = mvi_dev->mvi_info;
1499
Andy Yan20b09c22009-05-08 17:46:40 -04001500 spin_lock_irqsave(&task->task_state_lock, flags);
1501 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1502 spin_unlock_irqrestore(&task->task_state_lock, flags);
1503 rc = TMF_RESP_FUNC_COMPLETE;
1504 goto out;
1505 }
1506 spin_unlock_irqrestore(&task->task_state_lock, flags);
Srinivas9dc9fd92010-02-15 00:00:00 -06001507 mvi_dev->dev_status = MVS_DEV_EH;
Andy Yan20b09c22009-05-08 17:46:40 -04001508 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1509 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1510
1511 int_to_scsilun(cmnd->device->lun, &lun);
1512 rc = mvs_find_tag(mvi, task, &tag);
1513 if (rc == 0) {
1514 mv_printk("No such tag in %s\n", __func__);
1515 rc = TMF_RESP_FUNC_FAILED;
1516 return rc;
1517 }
1518
1519 tmf_task.tmf = TMF_ABORT_TASK;
1520 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1521
1522 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1523
1524 /* if successful, clear the task and callback forwards.*/
1525 if (rc == TMF_RESP_FUNC_COMPLETE) {
1526 u32 slot_no;
1527 struct mvs_slot_info *slot;
Andy Yan20b09c22009-05-08 17:46:40 -04001528
1529 if (task->lldd_task) {
Andy Yanf9da3be2009-05-14 20:41:21 -04001530 slot = task->lldd_task;
Andy Yan20b09c22009-05-08 17:46:40 -04001531 slot_no = (u32) (slot - mvi->slot_info);
Srinivas9dc9fd92010-02-15 00:00:00 -06001532 spin_lock_irqsave(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001533 mvs_slot_complete(mvi, slot_no, 1);
Srinivas9dc9fd92010-02-15 00:00:00 -06001534 spin_unlock_irqrestore(&mvi->lock, flags);
Andy Yan20b09c22009-05-08 17:46:40 -04001535 }
1536 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001537
Andy Yan20b09c22009-05-08 17:46:40 -04001538 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1539 task->task_proto & SAS_PROTOCOL_STP) {
James Bottomleyaa9f8322013-05-07 14:44:06 -07001540 if (SAS_SATA_DEV == dev->dev_type) {
Srinivas9dc9fd92010-02-15 00:00:00 -06001541 struct mvs_slot_info *slot = task->lldd_task;
Srinivas9dc9fd92010-02-15 00:00:00 -06001542 u32 slot_idx = (u32)(slot - mvi->slot_info);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001543 mv_dprintk("mvs_abort_task() mvi=%p task=%p "
Srinivas9dc9fd92010-02-15 00:00:00 -06001544 "slot=%p slot_idx=x%x\n",
1545 mvi, task, slot, slot_idx);
Jianpeng Ma95ab0002012-08-04 10:34:14 +08001546 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
Srinivas9dc9fd92010-02-15 00:00:00 -06001547 mvs_slot_task_free(mvi, task, slot, slot_idx);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001548 rc = TMF_RESP_FUNC_COMPLETE;
1549 goto out;
Srinivas9dc9fd92010-02-15 00:00:00 -06001550 }
Andy Yan20b09c22009-05-08 17:46:40 -04001551
1552 }
1553out:
1554 if (rc != TMF_RESP_FUNC_COMPLETE)
1555 mv_printk("%s:rc= %d\n", __func__, rc);
1556 return rc;
1557}
1558
1559int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1560{
1561 int rc = TMF_RESP_FUNC_FAILED;
1562 struct mvs_tmf_task tmf_task;
1563
1564 tmf_task.tmf = TMF_ABORT_TASK_SET;
1565 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1566
1567 return rc;
1568}
1569
1570int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1571{
1572 int rc = TMF_RESP_FUNC_FAILED;
1573 struct mvs_tmf_task tmf_task;
1574
1575 tmf_task.tmf = TMF_CLEAR_ACA;
1576 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1577
1578 return rc;
1579}
1580
1581int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1582{
1583 int rc = TMF_RESP_FUNC_FAILED;
1584 struct mvs_tmf_task tmf_task;
1585
1586 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1587 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1588
1589 return rc;
Ke Weie9ff91b2008-03-27 14:55:33 +08001590}
1591
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001592static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1593 u32 slot_idx, int err)
1594{
Andy Yanf9da3be2009-05-14 20:41:21 -04001595 struct mvs_device *mvi_dev = task->dev->lldd_dev;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001596 struct task_status_struct *tstat = &task->task_status;
1597 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001598 int stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001599
Andy Yan20b09c22009-05-08 17:46:40 -04001600
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001601 resp->frame_len = sizeof(struct dev_to_host_fis);
1602 memcpy(&resp->ending_fis[0],
Andy Yan20b09c22009-05-08 17:46:40 -04001603 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001604 sizeof(struct dev_to_host_fis));
1605 tstat->buf_valid_size = sizeof(*resp);
Srinivas9dc9fd92010-02-15 00:00:00 -06001606 if (unlikely(err)) {
1607 if (unlikely(err & CMD_ISS_STPD))
1608 stat = SAS_OPEN_REJECT;
1609 else
1610 stat = SAS_PROTO_RESPONSE;
1611 }
1612
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001613 return stat;
1614}
1615
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001616static void mvs_set_sense(u8 *buffer, int len, int d_sense,
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001617 int key, int asc, int ascq)
1618{
1619 memset(buffer, 0, len);
1620
1621 if (d_sense) {
1622 /* Descriptor format */
1623 if (len < 4) {
1624 mv_printk("Length %d of sense buffer too small to "
1625 "fit sense %x:%x:%x", len, key, asc, ascq);
1626 }
1627
1628 buffer[0] = 0x72; /* Response Code */
1629 if (len > 1)
1630 buffer[1] = key; /* Sense Key */
1631 if (len > 2)
1632 buffer[2] = asc; /* ASC */
1633 if (len > 3)
1634 buffer[3] = ascq; /* ASCQ */
1635 } else {
1636 if (len < 14) {
1637 mv_printk("Length %d of sense buffer too small to "
1638 "fit sense %x:%x:%x", len, key, asc, ascq);
1639 }
1640
1641 buffer[0] = 0x70; /* Response Code */
1642 if (len > 2)
1643 buffer[2] = key; /* Sense Key */
1644 if (len > 7)
1645 buffer[7] = 0x0a; /* Additional Sense Length */
1646 if (len > 12)
1647 buffer[12] = asc; /* ASC */
1648 if (len > 13)
1649 buffer[13] = ascq; /* ASCQ */
1650 }
1651
1652 return;
1653}
1654
Baoyou Xie14bf41d2016-09-25 13:48:50 +08001655static void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001656 u8 key, u8 asc, u8 asc_q)
1657{
1658 iu->datapres = 2;
1659 iu->response_data_len = 0;
1660 iu->sense_data_len = 17;
1661 iu->status = 02;
1662 mvs_set_sense(iu->sense_data, 17, 0,
1663 key, asc, asc_q);
1664}
1665
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001666static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1667 u32 slot_idx)
1668{
1669 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
Andy Yan20b09c22009-05-08 17:46:40 -04001670 int stat;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001671 u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001672 u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
Andy Yan20b09c22009-05-08 17:46:40 -04001673 u32 tfs = 0;
1674 enum mvs_port_type type = PORT_TYPE_SAS;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001675
Andy Yan20b09c22009-05-08 17:46:40 -04001676 if (err_dw0 & CMD_ISS_STPD)
1677 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1678
1679 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1680
James Bottomleydf64d3c2010-07-27 15:51:13 -05001681 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001682 switch (task->task_proto) {
1683 case SAS_PROTOCOL_SSP:
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001684 {
Andy Yan20b09c22009-05-08 17:46:40 -04001685 stat = SAS_ABORTED_TASK;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001686 if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
1687 struct ssp_response_iu *iu = slot->response +
1688 sizeof(struct mvs_err_info);
1689 mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
1690 sas_ssp_task_response(mvi->dev, task, iu);
1691 stat = SAM_STAT_CHECK_CONDITION;
1692 }
1693 if (err_dw1 & bit(31))
1694 mv_printk("reuse same slot, retry command.\n");
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001695 break;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001696 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001697 case SAS_PROTOCOL_SMP:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001698 stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001699 break;
Andy Yan20b09c22009-05-08 17:46:40 -04001700
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001701 case SAS_PROTOCOL_SATA:
1702 case SAS_PROTOCOL_STP:
1703 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
Andy Yan20b09c22009-05-08 17:46:40 -04001704 {
Andy Yan20b09c22009-05-08 17:46:40 -04001705 task->ata_task.use_ncq = 0;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001706 stat = SAS_PROTO_RESPONSE;
Srinivas9dc9fd92010-02-15 00:00:00 -06001707 mvs_sata_done(mvi, task, slot_idx, err_dw0);
Andy Yan20b09c22009-05-08 17:46:40 -04001708 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001709 break;
1710 default:
1711 break;
1712 }
1713
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001714 return stat;
1715}
1716
Andy Yan20b09c22009-05-08 17:46:40 -04001717int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001718{
1719 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1720 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1721 struct sas_task *task = slot->task;
Andy Yan20b09c22009-05-08 17:46:40 -04001722 struct mvs_device *mvi_dev = NULL;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001723 struct task_status_struct *tstat;
Srinivas9dc9fd92010-02-15 00:00:00 -06001724 struct domain_device *dev;
1725 u32 aborted;
Andy Yan20b09c22009-05-08 17:46:40 -04001726
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001727 void *to;
Andy Yan20b09c22009-05-08 17:46:40 -04001728 enum exec_status sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001729
Srinivas9dc9fd92010-02-15 00:00:00 -06001730 if (unlikely(!task || !task->lldd_task || !task->dev))
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001731 return -1;
1732
Andy Yan20b09c22009-05-08 17:46:40 -04001733 tstat = &task->task_status;
Srinivas9dc9fd92010-02-15 00:00:00 -06001734 dev = task->dev;
1735 mvi_dev = dev->lldd_dev;
Andy Yan20b09c22009-05-08 17:46:40 -04001736
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001737 spin_lock(&task->task_state_lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001738 task->task_state_flags &=
1739 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1740 task->task_state_flags |= SAS_TASK_STATE_DONE;
1741 /* race condition*/
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001742 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001743 spin_unlock(&task->task_state_lock);
1744
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001745 memset(tstat, 0, sizeof(*tstat));
1746 tstat->resp = SAS_TASK_COMPLETE;
1747
Andy Yan20b09c22009-05-08 17:46:40 -04001748 if (unlikely(aborted)) {
1749 tstat->stat = SAS_ABORTED_TASK;
Srinivas9dc9fd92010-02-15 00:00:00 -06001750 if (mvi_dev && mvi_dev->running_req)
1751 mvi_dev->running_req--;
Andy Yan20b09c22009-05-08 17:46:40 -04001752 if (sas_protocol_ata(task->task_proto))
1753 mvs_free_reg_set(mvi, mvi_dev);
1754
1755 mvs_slot_task_free(mvi, task, slot, slot_idx);
1756 return -1;
1757 }
1758
Xiangliang Yue144f7e2011-05-24 22:38:10 +08001759 /* when no device attaching, go ahead and complete by error handling*/
Srinivas9dc9fd92010-02-15 00:00:00 -06001760 if (unlikely(!mvi_dev || flags)) {
1761 if (!mvi_dev)
1762 mv_dprintk("port has not device.\n");
Andy Yan20b09c22009-05-08 17:46:40 -04001763 tstat->stat = SAS_PHY_DOWN;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001764 goto out;
1765 }
1766
James Bottomley53a983c2013-06-09 09:23:16 -07001767 /*
1768 * error info record present; slot->response is 32 bit aligned but may
1769 * not be 64 bit aligned, so check for zero in two 32 bit reads
1770 */
1771 if (unlikely((rx_desc & RXQ_ERR)
1772 && (*((u32 *)slot->response)
1773 || *(((u32 *)slot->response) + 1)))) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001774 mv_dprintk("port %d slot %d rx_desc %X has error info"
1775 "%016llX.\n", slot->port->sas_port.id, slot_idx,
James Bottomley53a983c2013-06-09 09:23:16 -07001776 rx_desc, get_unaligned_le64(slot->response));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001777 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
Srinivas9dc9fd92010-02-15 00:00:00 -06001778 tstat->resp = SAS_TASK_COMPLETE;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001779 goto out;
1780 }
1781
1782 switch (task->task_proto) {
1783 case SAS_PROTOCOL_SSP:
1784 /* hw says status == 0, datapres == 0 */
1785 if (rx_desc & RXQ_GOOD) {
James Bottomleydf64d3c2010-07-27 15:51:13 -05001786 tstat->stat = SAM_STAT_GOOD;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001787 tstat->resp = SAS_TASK_COMPLETE;
1788 }
1789 /* response frame present */
1790 else if (rx_desc & RXQ_RSP) {
Andy Yan20b09c22009-05-08 17:46:40 -04001791 struct ssp_response_iu *iu = slot->response +
1792 sizeof(struct mvs_err_info);
1793 sas_ssp_task_response(mvi->dev, task, iu);
1794 } else
James Bottomleydf64d3c2010-07-27 15:51:13 -05001795 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001796 break;
1797
1798 case SAS_PROTOCOL_SMP: {
1799 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
James Bottomleydf64d3c2010-07-27 15:51:13 -05001800 tstat->stat = SAM_STAT_GOOD;
Cong Wang77dfce02011-11-25 23:14:23 +08001801 to = kmap_atomic(sg_page(sg_resp));
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001802 memcpy(to + sg_resp->offset,
1803 slot->response + sizeof(struct mvs_err_info),
1804 sg_dma_len(sg_resp));
Cong Wang77dfce02011-11-25 23:14:23 +08001805 kunmap_atomic(to);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001806 break;
1807 }
1808
1809 case SAS_PROTOCOL_SATA:
1810 case SAS_PROTOCOL_STP:
1811 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1812 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1813 break;
1814 }
1815
1816 default:
James Bottomleydf64d3c2010-07-27 15:51:13 -05001817 tstat->stat = SAM_STAT_CHECK_CONDITION;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001818 break;
1819 }
Srinivas9dc9fd92010-02-15 00:00:00 -06001820 if (!slot->port->port_attached) {
1821 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1822 tstat->stat = SAS_PHY_DOWN;
1823 }
1824
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001825
1826out:
Srinivas9dc9fd92010-02-15 00:00:00 -06001827 if (mvi_dev && mvi_dev->running_req) {
1828 mvi_dev->running_req--;
1829 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
Andy Yan0f980a82009-05-11 21:49:52 +08001830 mvs_free_reg_set(mvi, mvi_dev);
1831 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001832 mvs_slot_task_free(mvi, task, slot, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001833 sts = tstat->stat;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001834
1835 spin_unlock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001836 if (task->task_done)
1837 task->task_done(task);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001838
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001839 spin_lock(&mvi->lock);
Andy Yan20b09c22009-05-08 17:46:40 -04001840
1841 return sts;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001842}
1843
Srinivas9dc9fd92010-02-15 00:00:00 -06001844void mvs_do_release_task(struct mvs_info *mvi,
Andy Yan20b09c22009-05-08 17:46:40 -04001845 int phy_no, struct domain_device *dev)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001846{
Srinivas9dc9fd92010-02-15 00:00:00 -06001847 u32 slot_idx;
Andy Yan20b09c22009-05-08 17:46:40 -04001848 struct mvs_phy *phy;
1849 struct mvs_port *port;
1850 struct mvs_slot_info *slot, *slot2;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001851
Andy Yan20b09c22009-05-08 17:46:40 -04001852 phy = &mvi->phy[phy_no];
1853 port = phy->port;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001854 if (!port)
1855 return;
Srinivas9dc9fd92010-02-15 00:00:00 -06001856 /* clean cmpl queue in case request is already finished */
1857 mvs_int_rx(mvi, false);
1858
1859
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001860
Andy Yan20b09c22009-05-08 17:46:40 -04001861 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1862 struct sas_task *task;
1863 slot_idx = (u32) (slot - mvi->slot_info);
1864 task = slot->task;
1865
1866 if (dev && task->dev != dev)
1867 continue;
1868
1869 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1870 slot_idx, slot->slot_tag, task);
Srinivas9dc9fd92010-02-15 00:00:00 -06001871 MVS_CHIP_DISP->command_active(mvi, slot_idx);
Andy Yan20b09c22009-05-08 17:46:40 -04001872
1873 mvs_slot_complete(mvi, slot_idx, 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001874 }
1875}
1876
Srinivas9dc9fd92010-02-15 00:00:00 -06001877void mvs_release_task(struct mvs_info *mvi,
1878 struct domain_device *dev)
1879{
1880 int i, phyno[WIDE_PORT_MAX_PHY], num;
Srinivas9dc9fd92010-02-15 00:00:00 -06001881 num = mvs_find_dev_phyno(dev, phyno);
1882 for (i = 0; i < num; i++)
1883 mvs_do_release_task(mvi, phyno[i], dev);
1884}
1885
Andy Yan20b09c22009-05-08 17:46:40 -04001886static void mvs_phy_disconnected(struct mvs_phy *phy)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001887{
Andy Yan20b09c22009-05-08 17:46:40 -04001888 phy->phy_attached = 0;
1889 phy->att_dev_info = 0;
1890 phy->att_dev_sas_addr = 0;
1891}
1892
1893static void mvs_work_queue(struct work_struct *work)
1894{
1895 struct delayed_work *dw = container_of(work, struct delayed_work, work);
1896 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
1897 struct mvs_info *mvi = mwq->mvi;
1898 unsigned long flags;
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001899 u32 phy_no = (unsigned long) mwq->data;
1900 struct sas_ha_struct *sas_ha = mvi->sas;
1901 struct mvs_phy *phy = &mvi->phy[phy_no];
1902 struct asd_sas_phy *sas_phy = &phy->sas_phy;
Andy Yan20b09c22009-05-08 17:46:40 -04001903
1904 spin_lock_irqsave(&mvi->lock, flags);
1905 if (mwq->handler & PHY_PLUG_EVENT) {
Andy Yan20b09c22009-05-08 17:46:40 -04001906
1907 if (phy->phy_event & PHY_PLUG_OUT) {
1908 u32 tmp;
1909 struct sas_identify_frame *id;
1910 id = (struct sas_identify_frame *)phy->frame_rcvd;
1911 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
1912 phy->phy_event &= ~PHY_PLUG_OUT;
1913 if (!(tmp & PHY_READY_MASK)) {
1914 sas_phy_disconnected(sas_phy);
1915 mvs_phy_disconnected(phy);
1916 sas_ha->notify_phy_event(sas_phy,
1917 PHYE_LOSS_OF_SIGNAL);
1918 mv_dprintk("phy%d Removed Device\n", phy_no);
1919 } else {
1920 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
1921 mvs_update_phyinfo(mvi, phy_no, 1);
1922 mvs_bytes_dmaed(mvi, phy_no);
1923 mvs_port_notify_formed(sas_phy, 0);
1924 mv_dprintk("phy%d Attached Device\n", phy_no);
1925 }
1926 }
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001927 } else if (mwq->handler & EXP_BRCT_CHG) {
1928 phy->phy_event &= ~EXP_BRCT_CHG;
1929 sas_ha->notify_port_event(sas_phy,
1930 PORTE_BROADCAST_RCVD);
1931 mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04001932 }
1933 list_del(&mwq->entry);
1934 spin_unlock_irqrestore(&mvi->lock, flags);
1935 kfree(mwq);
1936}
1937
1938static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
1939{
1940 struct mvs_wq *mwq;
1941 int ret = 0;
1942
1943 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
1944 if (mwq) {
1945 mwq->mvi = mvi;
1946 mwq->data = data;
1947 mwq->handler = handler;
1948 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
1949 list_add_tail(&mwq->entry, &mvi->wq_list);
1950 schedule_delayed_work(&mwq->work_q, HZ * 2);
1951 } else
1952 ret = -ENOMEM;
1953
1954 return ret;
1955}
1956
1957static void mvs_sig_time_out(unsigned long tphy)
1958{
1959 struct mvs_phy *phy = (struct mvs_phy *)tphy;
1960 struct mvs_info *mvi = phy->mvi;
1961 u8 phy_no;
1962
1963 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
1964 if (&mvi->phy[phy_no] == phy) {
1965 mv_dprintk("Get signature time out, reset phy %d\n",
1966 phy_no+mvi->id*mvi->chip->n_phy);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08001967 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
Andy Yan20b09c22009-05-08 17:46:40 -04001968 }
1969 }
1970}
1971
Andy Yan20b09c22009-05-08 17:46:40 -04001972void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
1973{
1974 u32 tmp;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001975 struct mvs_phy *phy = &mvi->phy[phy_no];
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001976
Andy Yan20b09c22009-05-08 17:46:40 -04001977 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001978 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
1979 mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
Andy Yan20b09c22009-05-08 17:46:40 -04001980 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001981 mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
Andy Yan20b09c22009-05-08 17:46:40 -04001982 phy->irq_status);
1983
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001984 /*
1985 * events is port event now ,
1986 * we need check the interrupt status which belongs to per port.
1987 */
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001988
Srinivas9dc9fd92010-02-15 00:00:00 -06001989 if (phy->irq_status & PHYEV_DCDR_ERR) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001990 mv_dprintk("phy %d STP decoding error.\n",
Srinivas9dc9fd92010-02-15 00:00:00 -06001991 phy_no + mvi->id*mvi->chip->n_phy);
1992 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04001993
Andy Yan20b09c22009-05-08 17:46:40 -04001994 if (phy->irq_status & PHYEV_POOF) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08001995 mdelay(500);
Andy Yan20b09c22009-05-08 17:46:40 -04001996 if (!(phy->phy_event & PHY_PLUG_OUT)) {
1997 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
1998 int ready;
Srinivas9dc9fd92010-02-15 00:00:00 -06001999 mvs_do_release_task(mvi, phy_no, NULL);
Andy Yan20b09c22009-05-08 17:46:40 -04002000 phy->phy_event |= PHY_PLUG_OUT;
Srinivas9dc9fd92010-02-15 00:00:00 -06002001 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
Andy Yan20b09c22009-05-08 17:46:40 -04002002 mvs_handle_event(mvi,
2003 (void *)(unsigned long)phy_no,
2004 PHY_PLUG_EVENT);
2005 ready = mvs_is_phy_ready(mvi, phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002006 if (ready || dev_sata) {
2007 if (MVS_CHIP_DISP->stp_reset)
2008 MVS_CHIP_DISP->stp_reset(mvi,
2009 phy_no);
2010 else
2011 MVS_CHIP_DISP->phy_reset(mvi,
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002012 phy_no, MVS_SOFT_RESET);
Andy Yan20b09c22009-05-08 17:46:40 -04002013 return;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002014 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002015 }
2016 }
Andy Yan20b09c22009-05-08 17:46:40 -04002017
2018 if (phy->irq_status & PHYEV_COMWAKE) {
2019 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2020 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2021 tmp | PHYEV_SIG_FIS);
2022 if (phy->timer.function == NULL) {
2023 phy->timer.data = (unsigned long)phy;
2024 phy->timer.function = mvs_sig_time_out;
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002025 phy->timer.expires = jiffies + 5*HZ;
Andy Yan20b09c22009-05-08 17:46:40 -04002026 add_timer(&phy->timer);
2027 }
2028 }
2029 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2030 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
Andy Yan20b09c22009-05-08 17:46:40 -04002031 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2032 if (phy->phy_status) {
2033 mdelay(10);
2034 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2035 if (phy->phy_type & PORT_TYPE_SATA) {
2036 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2037 mvi, phy_no);
2038 tmp &= ~PHYEV_SIG_FIS;
2039 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2040 phy_no, tmp);
2041 }
2042 mvs_update_phyinfo(mvi, phy_no, 0);
Srinivas9dc9fd92010-02-15 00:00:00 -06002043 if (phy->phy_type & PORT_TYPE_SAS) {
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002044 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
Srinivas9dc9fd92010-02-15 00:00:00 -06002045 mdelay(10);
2046 }
2047
Andy Yan20b09c22009-05-08 17:46:40 -04002048 mvs_bytes_dmaed(mvi, phy_no);
2049 /* whether driver is going to handle hot plug */
2050 if (phy->phy_event & PHY_PLUG_OUT) {
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002051 mvs_port_notify_formed(&phy->sas_phy, 0);
Andy Yan20b09c22009-05-08 17:46:40 -04002052 phy->phy_event &= ~PHY_PLUG_OUT;
2053 }
2054 } else {
2055 mv_dprintk("plugin interrupt but phy%d is gone\n",
2056 phy_no + mvi->id*mvi->chip->n_phy);
2057 }
2058 } else if (phy->irq_status & PHYEV_BROAD_CH) {
Xiangliang Yu84fbd0c2011-05-24 22:37:25 +08002059 mv_dprintk("phy %d broadcast change.\n",
Andy Yan20b09c22009-05-08 17:46:40 -04002060 phy_no + mvi->id*mvi->chip->n_phy);
Xiangliang Yua4632aa2011-05-24 22:36:02 +08002061 mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
2062 EXP_BRCT_CHG);
Andy Yan20b09c22009-05-08 17:46:40 -04002063 }
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002064}
2065
Andy Yan20b09c22009-05-08 17:46:40 -04002066int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002067{
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002068 u32 rx_prod_idx, rx_desc;
2069 bool attn = false;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002070
2071 /* the first dword in the RX ring is special: it contains
2072 * a mirror of the hardware's RX producer index, so that
2073 * we don't have to stall the CPU reading that register.
2074 * The actual RX ring is offset by one dword, due to this.
2075 */
2076 rx_prod_idx = mvi->rx_cons;
2077 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2078 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2079 return 0;
2080
2081 /* The CMPL_Q may come late, read from register and try again
2082 * note: if coalescing is enabled,
2083 * it will need to read from register every time for sure
2084 */
Andy Yan20b09c22009-05-08 17:46:40 -04002085 if (unlikely(mvi->rx_cons == rx_prod_idx))
2086 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002087
2088 if (mvi->rx_cons == rx_prod_idx)
2089 return 0;
2090
2091 while (mvi->rx_cons != rx_prod_idx) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002092 /* increment our internal RX consumer pointer */
2093 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002094 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2095
2096 if (likely(rx_desc & RXQ_DONE))
2097 mvs_slot_complete(mvi, rx_desc, 0);
2098 if (rx_desc & RXQ_ATTN) {
2099 attn = true;
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002100 } else if (rx_desc & RXQ_ERR) {
2101 if (!(rx_desc & RXQ_DONE))
2102 mvs_slot_complete(mvi, rx_desc, 0);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002103 } else if (rx_desc & RXQ_SLOT_RESET) {
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002104 mvs_slot_free(mvi, rx_desc);
2105 }
2106 }
2107
2108 if (attn && self_clear)
Andy Yan20b09c22009-05-08 17:46:40 -04002109 MVS_CHIP_DISP->int_full(mvi);
Jeff Garzikdd4969a2009-05-08 17:44:01 -04002110 return 0;
2111}
2112
Wilfried Weissmannc56f5f12015-12-27 20:21:19 +01002113int mvs_gpio_write(struct sas_ha_struct *sha, u8 reg_type, u8 reg_index,
2114 u8 reg_count, u8 *write_data)
2115{
2116 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
2117 struct mvs_info *mvi = mvs_prv->mvi[0];
2118
2119 if (MVS_CHIP_DISP->gpio_write) {
2120 return MVS_CHIP_DISP->gpio_write(mvs_prv, reg_type,
2121 reg_index, reg_count, write_data);
2122 }
2123
2124 return -ENOSYS;
2125}