Add linux-next specific files for 20110831
[linux-2.6/next.git] / drivers / scsi / mvsas / mv_sas.c
blob4196eee33dc9b7e0433dd5206d2966aec49cba29
1 /*
2 * Marvell 88SE64xx/88SE94xx main function
4 * Copyright 2007 Red Hat, Inc.
5 * Copyright 2008 Marvell. <kewei@marvell.com>
6 * Copyright 2009-2011 Marvell. <yuxiangl@marvell.com>
8 * This file is licensed under GPLv2.
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.
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.
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
26 #include "mv_sas.h"
28 static int mvs_find_tag(struct mvs_info *mvi, struct sas_task *task, u32 *tag)
30 if (task->lldd_task) {
31 struct mvs_slot_info *slot;
32 slot = task->lldd_task;
33 *tag = slot->slot_tag;
34 return 1;
36 return 0;
39 void mvs_tag_clear(struct mvs_info *mvi, u32 tag)
41 void *bitmap = mvi->tags;
42 clear_bit(tag, bitmap);
45 void mvs_tag_free(struct mvs_info *mvi, u32 tag)
47 mvs_tag_clear(mvi, tag);
50 void mvs_tag_set(struct mvs_info *mvi, unsigned int tag)
52 void *bitmap = mvi->tags;
53 set_bit(tag, bitmap);
56 inline int mvs_tag_alloc(struct mvs_info *mvi, u32 *tag_out)
58 unsigned int index, tag;
59 void *bitmap = mvi->tags;
61 index = find_first_zero_bit(bitmap, mvi->tags_num);
62 tag = index;
63 if (tag >= mvi->tags_num)
64 return -SAS_QUEUE_FULL;
65 mvs_tag_set(mvi, tag);
66 *tag_out = tag;
67 return 0;
70 void mvs_tag_init(struct mvs_info *mvi)
72 int i;
73 for (i = 0; i < mvi->tags_num; ++i)
74 mvs_tag_clear(mvi, i);
77 struct mvs_info *mvs_find_dev_mvi(struct domain_device *dev)
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;
84 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++;
94 break;
96 i++;
98 hi = j/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
99 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
101 return mvi;
105 int mvs_find_dev_phyno(struct domain_device *dev, int *phyno)
107 unsigned long i = 0, j = 0, n = 0, num = 0;
108 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
109 struct mvs_info *mvi = mvi_dev->mvi_info;
110 struct sas_ha_struct *sha = dev->port->ha;
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++;
123 phyno[n] = (j >= mvi->chip->n_phy) ?
124 (j - mvi->chip->n_phy) : j;
125 num++;
126 n++;
128 break;
130 i++;
132 return num;
135 struct mvs_device *mvs_find_dev_by_reg_set(struct mvs_info *mvi,
136 u8 reg_set)
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;
143 if (mvi->devices[dev_no].taskfileset == reg_set)
144 return &mvi->devices[dev_no];
146 return NULL;
149 static inline void mvs_free_reg_set(struct mvs_info *mvi,
150 struct mvs_device *dev)
152 if (!dev) {
153 mv_printk("device has been free.\n");
154 return;
156 if (dev->taskfileset == MVS_ID_NOT_MAPPED)
157 return;
158 MVS_CHIP_DISP->free_reg_set(mvi, &dev->taskfileset);
161 static inline u8 mvs_assign_reg_set(struct mvs_info *mvi,
162 struct mvs_device *dev)
164 if (dev->taskfileset != MVS_ID_NOT_MAPPED)
165 return 0;
166 return MVS_CHIP_DISP->assign_reg_set(mvi, &dev->taskfileset);
169 void mvs_phys_reset(struct mvs_info *mvi, u32 phy_mask, int hard)
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);
179 int mvs_phy_control(struct asd_sas_phy *sas_phy, enum phy_func func,
180 void *funcdata)
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;
187 while (sha->sas_phy[i]) {
188 if (sha->sas_phy[i] == sas_phy)
189 break;
190 i++;
192 hi = i/((struct mvs_prv_info *)sha->lldd_ha)->n_phy;
193 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[hi];
195 switch (func) {
196 case PHY_FUNC_SET_LINK_RATE:
197 MVS_CHIP_DISP->phy_set_link_rate(mvi, phy_id, funcdata);
198 break;
200 case PHY_FUNC_HARD_RESET:
201 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_id);
202 if (tmp & PHY_RST_HARD)
203 break;
204 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_HARD_RESET);
205 break;
207 case PHY_FUNC_LINK_RESET:
208 MVS_CHIP_DISP->phy_enable(mvi, phy_id);
209 MVS_CHIP_DISP->phy_reset(mvi, phy_id, MVS_SOFT_RESET);
210 break;
212 case PHY_FUNC_DISABLE:
213 MVS_CHIP_DISP->phy_disable(mvi, phy_id);
214 break;
215 case PHY_FUNC_RELEASE_SPINUP_HOLD:
216 default:
217 rc = -EOPNOTSUPP;
219 msleep(200);
220 return rc;
223 void __devinit mvs_set_sas_addr(struct mvs_info *mvi, int port_id,
224 u32 off_lo, u32 off_hi, u64 sas_addr)
226 u32 lo = (u32)sas_addr;
227 u32 hi = (u32)(sas_addr>>32);
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);
235 static void mvs_bytes_dmaed(struct mvs_info *mvi, int i)
237 struct mvs_phy *phy = &mvi->phy[i];
238 struct asd_sas_phy *sas_phy = &phy->sas_phy;
239 struct sas_ha_struct *sas_ha;
240 if (!phy->phy_attached)
241 return;
243 if (!(phy->att_dev_info & PORT_DEV_TRGT_MASK)
244 && phy->phy_type & PORT_TYPE_SAS) {
245 return;
248 sas_ha = mvi->sas;
249 sas_ha->notify_phy_event(sas_phy, PHYE_OOB_DONE);
251 if (sas_phy->phy) {
252 struct sas_phy *sphy = sas_phy->phy;
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;
258 sphy->maximum_linkrate_hw = MVS_CHIP_DISP->phy_max_link_rate();
261 if (phy->phy_type & PORT_TYPE_SAS) {
262 struct sas_identify_frame *id;
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;
268 } else if (phy->phy_type & PORT_TYPE_SATA) {
269 /*Nothing*/
271 mv_dprintk("phy %d byte dmaded.\n", i + mvi->id * mvi->chip->n_phy);
273 sas_phy->frame_rcvd_size = phy->frame_rcvd_size;
275 mvi->sas->notify_port_event(sas_phy,
276 PORTE_BYTES_DMAED);
279 int mvs_slave_alloc(struct scsi_device *scsi_dev)
281 struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
282 if (dev_is_sata(dev)) {
283 /* We don't need to rescan targets
284 * if REPORT_LUNS request is failed
286 if (scsi_dev->lun > 0)
287 return -ENXIO;
288 scsi_dev->tagged_supported = 1;
291 return sas_slave_alloc(scsi_dev);
294 int mvs_slave_configure(struct scsi_device *sdev)
296 struct domain_device *dev = sdev_to_domain_dev(sdev);
297 int ret = sas_slave_configure(sdev);
299 if (ret)
300 return ret;
301 if (!dev_is_sata(dev)) {
302 sas_change_queue_depth(sdev,
303 MVS_QUEUE_SIZE,
304 SCSI_QDEPTH_DEFAULT);
306 return 0;
309 void mvs_scan_start(struct Scsi_Host *shost)
311 int i, j;
312 unsigned short core_nr;
313 struct mvs_info *mvi;
314 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
315 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
317 core_nr = ((struct mvs_prv_info *)sha->lldd_ha)->n_host;
319 for (j = 0; j < core_nr; j++) {
320 mvi = ((struct mvs_prv_info *)sha->lldd_ha)->mvi[j];
321 for (i = 0; i < mvi->chip->n_phy; ++i)
322 mvs_bytes_dmaed(mvi, i);
324 mvs_prv->scan_finished = 1;
327 int mvs_scan_finished(struct Scsi_Host *shost, unsigned long time)
329 struct sas_ha_struct *sha = SHOST_TO_SAS_HA(shost);
330 struct mvs_prv_info *mvs_prv = sha->lldd_ha;
332 if (mvs_prv->scan_finished == 0)
333 return 0;
335 scsi_flush_work(shost);
336 return 1;
339 static int mvs_task_prep_smp(struct mvs_info *mvi,
340 struct mvs_task_exec_info *tei)
342 int elem, rc, i;
343 struct sas_task *task = tei->task;
344 struct mvs_cmd_hdr *hdr = tei->hdr;
345 struct domain_device *dev = task->dev;
346 struct asd_sas_port *sas_port = dev->port;
347 struct scatterlist *sg_req, *sg_resp;
348 u32 req_len, resp_len, tag = tei->tag;
349 void *buf_tmp;
350 u8 *buf_oaf;
351 dma_addr_t buf_tmp_dma;
352 void *buf_prd;
353 struct mvs_slot_info *slot = &mvi->slot_info[tag];
354 u32 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
357 * DMA-map SMP request, response buffers
359 sg_req = &task->smp_task.smp_req;
360 elem = dma_map_sg(mvi->dev, sg_req, 1, PCI_DMA_TODEVICE);
361 if (!elem)
362 return -ENOMEM;
363 req_len = sg_dma_len(sg_req);
365 sg_resp = &task->smp_task.smp_resp;
366 elem = dma_map_sg(mvi->dev, sg_resp, 1, PCI_DMA_FROMDEVICE);
367 if (!elem) {
368 rc = -ENOMEM;
369 goto err_out;
371 resp_len = SB_RFB_MAX;
373 /* must be in dwords */
374 if ((req_len & 0x3) || (resp_len & 0x3)) {
375 rc = -EINVAL;
376 goto err_out_2;
380 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
383 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ***** */
384 buf_tmp = slot->buf;
385 buf_tmp_dma = slot->buf_dma;
387 hdr->cmd_tbl = cpu_to_le64(sg_dma_address(sg_req));
389 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
390 buf_oaf = buf_tmp;
391 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
393 buf_tmp += MVS_OAF_SZ;
394 buf_tmp_dma += MVS_OAF_SZ;
396 /* region 3: PRD table *********************************** */
397 buf_prd = buf_tmp;
398 if (tei->n_elem)
399 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
400 else
401 hdr->prd_tbl = 0;
403 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
404 buf_tmp += i;
405 buf_tmp_dma += i;
407 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
408 slot->response = buf_tmp;
409 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
410 if (mvi->flags & MVF_FLAG_SOC)
411 hdr->reserved[0] = 0;
414 * Fill in TX ring and command slot header
416 slot->tx = mvi->tx_prod;
417 mvi->tx[mvi->tx_prod] = cpu_to_le32((TXQ_CMD_SMP << TXQ_CMD_SHIFT) |
418 TXQ_MODE_I | tag |
419 (sas_port->phy_mask << TXQ_PHY_SHIFT));
421 hdr->flags |= flags;
422 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | ((req_len - 4) / 4));
423 hdr->tags = cpu_to_le32(tag);
424 hdr->data_len = 0;
426 /* generate open address frame hdr (first 12 bytes) */
427 /* initiator, SMP, ftype 1h */
428 buf_oaf[0] = (1 << 7) | (PROTOCOL_SMP << 4) | 0x01;
429 buf_oaf[1] = dev->linkrate & 0xf;
430 *(u16 *)(buf_oaf + 2) = 0xFFFF; /* SAS SPEC */
431 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
433 /* fill in PRD (scatter/gather) table, if any */
434 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
436 return 0;
438 err_out_2:
439 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_resp, 1,
440 PCI_DMA_FROMDEVICE);
441 err_out:
442 dma_unmap_sg(mvi->dev, &tei->task->smp_task.smp_req, 1,
443 PCI_DMA_TODEVICE);
444 return rc;
447 static u32 mvs_get_ncq_tag(struct sas_task *task, u32 *tag)
449 struct ata_queued_cmd *qc = task->uldd_task;
451 if (qc) {
452 if (qc->tf.command == ATA_CMD_FPDMA_WRITE ||
453 qc->tf.command == ATA_CMD_FPDMA_READ) {
454 *tag = qc->tag;
455 return 1;
459 return 0;
462 static int mvs_task_prep_ata(struct mvs_info *mvi,
463 struct mvs_task_exec_info *tei)
465 struct sas_task *task = tei->task;
466 struct domain_device *dev = task->dev;
467 struct mvs_device *mvi_dev = dev->lldd_dev;
468 struct mvs_cmd_hdr *hdr = tei->hdr;
469 struct asd_sas_port *sas_port = dev->port;
470 struct mvs_slot_info *slot;
471 void *buf_prd;
472 u32 tag = tei->tag, hdr_tag;
473 u32 flags, del_q;
474 void *buf_tmp;
475 u8 *buf_cmd, *buf_oaf;
476 dma_addr_t buf_tmp_dma;
477 u32 i, req_len, resp_len;
478 const u32 max_resp_len = SB_RFB_MAX;
480 if (mvs_assign_reg_set(mvi, mvi_dev) == MVS_ID_NOT_MAPPED) {
481 mv_dprintk("Have not enough regiset for dev %d.\n",
482 mvi_dev->device_id);
483 return -EBUSY;
485 slot = &mvi->slot_info[tag];
486 slot->tx = mvi->tx_prod;
487 del_q = TXQ_MODE_I | tag |
488 (TXQ_CMD_STP << TXQ_CMD_SHIFT) |
489 (sas_port->phy_mask << TXQ_PHY_SHIFT) |
490 (mvi_dev->taskfileset << TXQ_SRS_SHIFT);
491 mvi->tx[mvi->tx_prod] = cpu_to_le32(del_q);
493 if (task->data_dir == DMA_FROM_DEVICE)
494 flags = (MVS_CHIP_DISP->prd_count() << MCH_PRD_LEN_SHIFT);
495 else
496 flags = (tei->n_elem << MCH_PRD_LEN_SHIFT);
498 if (task->ata_task.use_ncq)
499 flags |= MCH_FPDMA;
500 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET) {
501 if (task->ata_task.fis.command != ATA_CMD_ID_ATAPI)
502 flags |= MCH_ATAPI;
505 hdr->flags = cpu_to_le32(flags);
507 if (task->ata_task.use_ncq && mvs_get_ncq_tag(task, &hdr_tag))
508 task->ata_task.fis.sector_count |= (u8) (hdr_tag << 3);
509 else
510 hdr_tag = tag;
512 hdr->tags = cpu_to_le32(hdr_tag);
514 hdr->data_len = cpu_to_le32(task->total_xfer_len);
517 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
520 /* region 1: command table area (MVS_ATA_CMD_SZ bytes) ************** */
521 buf_cmd = buf_tmp = slot->buf;
522 buf_tmp_dma = slot->buf_dma;
524 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
526 buf_tmp += MVS_ATA_CMD_SZ;
527 buf_tmp_dma += MVS_ATA_CMD_SZ;
529 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
530 /* used for STP. unused for SATA? */
531 buf_oaf = buf_tmp;
532 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
534 buf_tmp += MVS_OAF_SZ;
535 buf_tmp_dma += MVS_OAF_SZ;
537 /* region 3: PRD table ********************************************* */
538 buf_prd = buf_tmp;
540 if (tei->n_elem)
541 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
542 else
543 hdr->prd_tbl = 0;
544 i = MVS_CHIP_DISP->prd_size() * MVS_CHIP_DISP->prd_count();
546 buf_tmp += i;
547 buf_tmp_dma += i;
549 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
550 slot->response = buf_tmp;
551 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
552 if (mvi->flags & MVF_FLAG_SOC)
553 hdr->reserved[0] = 0;
555 req_len = sizeof(struct host_to_dev_fis);
556 resp_len = MVS_SLOT_BUF_SZ - MVS_ATA_CMD_SZ -
557 sizeof(struct mvs_err_info) - i;
559 /* request, response lengths */
560 resp_len = min(resp_len, max_resp_len);
561 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
563 if (likely(!task->ata_task.device_control_reg_update))
564 task->ata_task.fis.flags |= 0x80; /* C=1: update ATA cmd reg */
565 /* fill in command FIS and ATAPI CDB */
566 memcpy(buf_cmd, &task->ata_task.fis, sizeof(struct host_to_dev_fis));
567 if (dev->sata_dev.command_set == ATAPI_COMMAND_SET)
568 memcpy(buf_cmd + STP_ATAPI_CMD,
569 task->ata_task.atapi_packet, 16);
571 /* generate open address frame hdr (first 12 bytes) */
572 /* initiator, STP, ftype 1h */
573 buf_oaf[0] = (1 << 7) | (PROTOCOL_STP << 4) | 0x1;
574 buf_oaf[1] = dev->linkrate & 0xf;
575 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
576 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
578 /* fill in PRD (scatter/gather) table, if any */
579 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
581 if (task->data_dir == DMA_FROM_DEVICE)
582 MVS_CHIP_DISP->dma_fix(mvi, sas_port->phy_mask,
583 TRASH_BUCKET_SIZE, tei->n_elem, buf_prd);
585 return 0;
588 static int mvs_task_prep_ssp(struct mvs_info *mvi,
589 struct mvs_task_exec_info *tei, int is_tmf,
590 struct mvs_tmf_task *tmf)
592 struct sas_task *task = tei->task;
593 struct mvs_cmd_hdr *hdr = tei->hdr;
594 struct mvs_port *port = tei->port;
595 struct domain_device *dev = task->dev;
596 struct mvs_device *mvi_dev = dev->lldd_dev;
597 struct asd_sas_port *sas_port = dev->port;
598 struct mvs_slot_info *slot;
599 void *buf_prd;
600 struct ssp_frame_hdr *ssp_hdr;
601 void *buf_tmp;
602 u8 *buf_cmd, *buf_oaf, fburst = 0;
603 dma_addr_t buf_tmp_dma;
604 u32 flags;
605 u32 resp_len, req_len, i, tag = tei->tag;
606 const u32 max_resp_len = SB_RFB_MAX;
607 u32 phy_mask;
609 slot = &mvi->slot_info[tag];
611 phy_mask = ((port->wide_port_phymap) ? port->wide_port_phymap :
612 sas_port->phy_mask) & TXQ_PHY_MASK;
614 slot->tx = mvi->tx_prod;
615 mvi->tx[mvi->tx_prod] = cpu_to_le32(TXQ_MODE_I | tag |
616 (TXQ_CMD_SSP << TXQ_CMD_SHIFT) |
617 (phy_mask << TXQ_PHY_SHIFT));
619 flags = MCH_RETRY;
620 if (task->ssp_task.enable_first_burst) {
621 flags |= MCH_FBURST;
622 fburst = (1 << 7);
624 if (is_tmf)
625 flags |= (MCH_SSP_FR_TASK << MCH_SSP_FR_TYPE_SHIFT);
626 else
627 flags |= (MCH_SSP_FR_CMD << MCH_SSP_FR_TYPE_SHIFT);
629 hdr->flags = cpu_to_le32(flags | (tei->n_elem << MCH_PRD_LEN_SHIFT));
630 hdr->tags = cpu_to_le32(tag);
631 hdr->data_len = cpu_to_le32(task->total_xfer_len);
634 * arrange MVS_SLOT_BUF_SZ-sized DMA buffer according to our needs
637 /* region 1: command table area (MVS_SSP_CMD_SZ bytes) ************** */
638 buf_cmd = buf_tmp = slot->buf;
639 buf_tmp_dma = slot->buf_dma;
641 hdr->cmd_tbl = cpu_to_le64(buf_tmp_dma);
643 buf_tmp += MVS_SSP_CMD_SZ;
644 buf_tmp_dma += MVS_SSP_CMD_SZ;
646 /* region 2: open address frame area (MVS_OAF_SZ bytes) ********* */
647 buf_oaf = buf_tmp;
648 hdr->open_frame = cpu_to_le64(buf_tmp_dma);
650 buf_tmp += MVS_OAF_SZ;
651 buf_tmp_dma += MVS_OAF_SZ;
653 /* region 3: PRD table ********************************************* */
654 buf_prd = buf_tmp;
655 if (tei->n_elem)
656 hdr->prd_tbl = cpu_to_le64(buf_tmp_dma);
657 else
658 hdr->prd_tbl = 0;
660 i = MVS_CHIP_DISP->prd_size() * tei->n_elem;
661 buf_tmp += i;
662 buf_tmp_dma += i;
664 /* region 4: status buffer (larger the PRD, smaller this buf) ****** */
665 slot->response = buf_tmp;
666 hdr->status_buf = cpu_to_le64(buf_tmp_dma);
667 if (mvi->flags & MVF_FLAG_SOC)
668 hdr->reserved[0] = 0;
670 resp_len = MVS_SLOT_BUF_SZ - MVS_SSP_CMD_SZ - MVS_OAF_SZ -
671 sizeof(struct mvs_err_info) - i;
672 resp_len = min(resp_len, max_resp_len);
674 req_len = sizeof(struct ssp_frame_hdr) + 28;
676 /* request, response lengths */
677 hdr->lens = cpu_to_le32(((resp_len / 4) << 16) | (req_len / 4));
679 /* generate open address frame hdr (first 12 bytes) */
680 /* initiator, SSP, ftype 1h */
681 buf_oaf[0] = (1 << 7) | (PROTOCOL_SSP << 4) | 0x1;
682 buf_oaf[1] = dev->linkrate & 0xf;
683 *(u16 *)(buf_oaf + 2) = cpu_to_be16(mvi_dev->device_id + 1);
684 memcpy(buf_oaf + 4, dev->sas_addr, SAS_ADDR_SIZE);
686 /* fill in SSP frame header (Command Table.SSP frame header) */
687 ssp_hdr = (struct ssp_frame_hdr *)buf_cmd;
689 if (is_tmf)
690 ssp_hdr->frame_type = SSP_TASK;
691 else
692 ssp_hdr->frame_type = SSP_COMMAND;
694 memcpy(ssp_hdr->hashed_dest_addr, dev->hashed_sas_addr,
695 HASHED_SAS_ADDR_SIZE);
696 memcpy(ssp_hdr->hashed_src_addr,
697 dev->hashed_sas_addr, HASHED_SAS_ADDR_SIZE);
698 ssp_hdr->tag = cpu_to_be16(tag);
700 /* fill in IU for TASK and Command Frame */
701 buf_cmd += sizeof(*ssp_hdr);
702 memcpy(buf_cmd, &task->ssp_task.LUN, 8);
704 if (ssp_hdr->frame_type != SSP_TASK) {
705 buf_cmd[9] = fburst | task->ssp_task.task_attr |
706 (task->ssp_task.task_prio << 3);
707 memcpy(buf_cmd + 12, &task->ssp_task.cdb, 16);
708 } else{
709 buf_cmd[10] = tmf->tmf;
710 switch (tmf->tmf) {
711 case TMF_ABORT_TASK:
712 case TMF_QUERY_TASK:
713 buf_cmd[12] =
714 (tmf->tag_of_task_to_be_managed >> 8) & 0xff;
715 buf_cmd[13] =
716 tmf->tag_of_task_to_be_managed & 0xff;
717 break;
718 default:
719 break;
722 /* fill in PRD (scatter/gather) table, if any */
723 MVS_CHIP_DISP->make_prd(task->scatter, tei->n_elem, buf_prd);
724 return 0;
727 #define DEV_IS_GONE(mvi_dev) ((!mvi_dev || (mvi_dev->dev_type == NO_DEVICE)))
728 static int mvs_task_prep(struct sas_task *task, struct mvs_info *mvi, int is_tmf,
729 struct mvs_tmf_task *tmf, int *pass)
731 struct domain_device *dev = task->dev;
732 struct mvs_device *mvi_dev = dev->lldd_dev;
733 struct mvs_task_exec_info tei;
734 struct mvs_slot_info *slot;
735 u32 tag = 0xdeadbeef, n_elem = 0;
736 int rc = 0;
738 if (!dev->port) {
739 struct task_status_struct *tsm = &task->task_status;
741 tsm->resp = SAS_TASK_UNDELIVERED;
742 tsm->stat = SAS_PHY_DOWN;
744 * libsas will use dev->port, should
745 * not call task_done for sata
747 if (dev->dev_type != SATA_DEV)
748 task->task_done(task);
749 return rc;
752 if (DEV_IS_GONE(mvi_dev)) {
753 if (mvi_dev)
754 mv_dprintk("device %d not ready.\n",
755 mvi_dev->device_id);
756 else
757 mv_dprintk("device %016llx not ready.\n",
758 SAS_ADDR(dev->sas_addr));
760 rc = SAS_PHY_DOWN;
761 return rc;
763 tei.port = dev->port->lldd_port;
764 if (tei.port && !tei.port->port_attached && !tmf) {
765 if (sas_protocol_ata(task->task_proto)) {
766 struct task_status_struct *ts = &task->task_status;
767 mv_dprintk("SATA/STP port %d does not attach"
768 "device.\n", dev->port->id);
769 ts->resp = SAS_TASK_COMPLETE;
770 ts->stat = SAS_PHY_DOWN;
772 task->task_done(task);
774 } else {
775 struct task_status_struct *ts = &task->task_status;
776 mv_dprintk("SAS port %d does not attach"
777 "device.\n", dev->port->id);
778 ts->resp = SAS_TASK_UNDELIVERED;
779 ts->stat = SAS_PHY_DOWN;
780 task->task_done(task);
782 return rc;
785 if (!sas_protocol_ata(task->task_proto)) {
786 if (task->num_scatter) {
787 n_elem = dma_map_sg(mvi->dev,
788 task->scatter,
789 task->num_scatter,
790 task->data_dir);
791 if (!n_elem) {
792 rc = -ENOMEM;
793 goto prep_out;
796 } else {
797 n_elem = task->num_scatter;
800 rc = mvs_tag_alloc(mvi, &tag);
801 if (rc)
802 goto err_out;
804 slot = &mvi->slot_info[tag];
806 task->lldd_task = NULL;
807 slot->n_elem = n_elem;
808 slot->slot_tag = tag;
810 slot->buf = pci_pool_alloc(mvi->dma_pool, GFP_ATOMIC, &slot->buf_dma);
811 if (!slot->buf)
812 goto err_out_tag;
813 memset(slot->buf, 0, MVS_SLOT_BUF_SZ);
815 tei.task = task;
816 tei.hdr = &mvi->slot[tag];
817 tei.tag = tag;
818 tei.n_elem = n_elem;
819 switch (task->task_proto) {
820 case SAS_PROTOCOL_SMP:
821 rc = mvs_task_prep_smp(mvi, &tei);
822 break;
823 case SAS_PROTOCOL_SSP:
824 rc = mvs_task_prep_ssp(mvi, &tei, is_tmf, tmf);
825 break;
826 case SAS_PROTOCOL_SATA:
827 case SAS_PROTOCOL_STP:
828 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
829 rc = mvs_task_prep_ata(mvi, &tei);
830 break;
831 default:
832 dev_printk(KERN_ERR, mvi->dev,
833 "unknown sas_task proto: 0x%x\n",
834 task->task_proto);
835 rc = -EINVAL;
836 break;
839 if (rc) {
840 mv_dprintk("rc is %x\n", rc);
841 goto err_out_slot_buf;
843 slot->task = task;
844 slot->port = tei.port;
845 task->lldd_task = slot;
846 list_add_tail(&slot->entry, &tei.port->list);
847 spin_lock(&task->task_state_lock);
848 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
849 spin_unlock(&task->task_state_lock);
851 mvi_dev->running_req++;
852 ++(*pass);
853 mvi->tx_prod = (mvi->tx_prod + 1) & (MVS_CHIP_SLOT_SZ - 1);
855 return rc;
857 err_out_slot_buf:
858 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
859 err_out_tag:
860 mvs_tag_free(mvi, tag);
861 err_out:
863 dev_printk(KERN_ERR, mvi->dev, "mvsas prep failed[%d]!\n", rc);
864 if (!sas_protocol_ata(task->task_proto))
865 if (n_elem)
866 dma_unmap_sg(mvi->dev, task->scatter, n_elem,
867 task->data_dir);
868 prep_out:
869 return rc;
872 static struct mvs_task_list *mvs_task_alloc_list(int *num, gfp_t gfp_flags)
874 struct mvs_task_list *first = NULL;
876 for (; *num > 0; --*num) {
877 struct mvs_task_list *mvs_list = kmem_cache_zalloc(mvs_task_list_cache, gfp_flags);
879 if (!mvs_list)
880 break;
882 INIT_LIST_HEAD(&mvs_list->list);
883 if (!first)
884 first = mvs_list;
885 else
886 list_add_tail(&mvs_list->list, &first->list);
890 return first;
893 static inline void mvs_task_free_list(struct mvs_task_list *mvs_list)
895 LIST_HEAD(list);
896 struct list_head *pos, *a;
897 struct mvs_task_list *mlist = NULL;
899 __list_add(&list, mvs_list->list.prev, &mvs_list->list);
901 list_for_each_safe(pos, a, &list) {
902 list_del_init(pos);
903 mlist = list_entry(pos, struct mvs_task_list, list);
904 kmem_cache_free(mvs_task_list_cache, mlist);
908 static int mvs_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
909 struct completion *completion, int is_tmf,
910 struct mvs_tmf_task *tmf)
912 struct domain_device *dev = task->dev;
913 struct mvs_info *mvi = NULL;
914 u32 rc = 0;
915 u32 pass = 0;
916 unsigned long flags = 0;
918 mvi = ((struct mvs_device *)task->dev->lldd_dev)->mvi_info;
920 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
921 spin_unlock_irq(dev->sata_dev.ap->lock);
923 spin_lock_irqsave(&mvi->lock, flags);
924 rc = mvs_task_prep(task, mvi, is_tmf, tmf, &pass);
925 if (rc)
926 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
928 if (likely(pass))
929 MVS_CHIP_DISP->start_delivery(mvi, (mvi->tx_prod - 1) &
930 (MVS_CHIP_SLOT_SZ - 1));
931 spin_unlock_irqrestore(&mvi->lock, flags);
933 if ((dev->dev_type == SATA_DEV) && (dev->sata_dev.ap != NULL))
934 spin_lock_irq(dev->sata_dev.ap->lock);
936 return rc;
939 static int mvs_collector_task_exec(struct sas_task *task, const int num, gfp_t gfp_flags,
940 struct completion *completion, int is_tmf,
941 struct mvs_tmf_task *tmf)
943 struct domain_device *dev = task->dev;
944 struct mvs_prv_info *mpi = dev->port->ha->lldd_ha;
945 struct mvs_info *mvi = NULL;
946 struct sas_task *t = task;
947 struct mvs_task_list *mvs_list = NULL, *a;
948 LIST_HEAD(q);
949 int pass[2] = {0};
950 u32 rc = 0;
951 u32 n = num;
952 unsigned long flags = 0;
954 mvs_list = mvs_task_alloc_list(&n, gfp_flags);
955 if (n) {
956 printk(KERN_ERR "%s: mvs alloc list failed.\n", __func__);
957 rc = -ENOMEM;
958 goto free_list;
961 __list_add(&q, mvs_list->list.prev, &mvs_list->list);
963 list_for_each_entry(a, &q, list) {
964 a->task = t;
965 t = list_entry(t->list.next, struct sas_task, list);
968 list_for_each_entry(a, &q , list) {
970 t = a->task;
971 mvi = ((struct mvs_device *)t->dev->lldd_dev)->mvi_info;
973 spin_lock_irqsave(&mvi->lock, flags);
974 rc = mvs_task_prep(t, mvi, is_tmf, tmf, &pass[mvi->id]);
975 if (rc)
976 dev_printk(KERN_ERR, mvi->dev, "mvsas exec failed[%d]!\n", rc);
977 spin_unlock_irqrestore(&mvi->lock, flags);
980 if (likely(pass[0]))
981 MVS_CHIP_DISP->start_delivery(mpi->mvi[0],
982 (mpi->mvi[0]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
984 if (likely(pass[1]))
985 MVS_CHIP_DISP->start_delivery(mpi->mvi[1],
986 (mpi->mvi[1]->tx_prod - 1) & (MVS_CHIP_SLOT_SZ - 1));
988 list_del_init(&q);
990 free_list:
991 if (mvs_list)
992 mvs_task_free_list(mvs_list);
994 return rc;
997 int mvs_queue_command(struct sas_task *task, const int num,
998 gfp_t gfp_flags)
1000 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1001 struct sas_ha_struct *sas = mvi_dev->mvi_info->sas;
1003 if (sas->lldd_max_execute_num < 2)
1004 return mvs_task_exec(task, num, gfp_flags, NULL, 0, NULL);
1005 else
1006 return mvs_collector_task_exec(task, num, gfp_flags, NULL, 0, NULL);
1009 static void mvs_slot_free(struct mvs_info *mvi, u32 rx_desc)
1011 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1012 mvs_tag_clear(mvi, slot_idx);
1015 static void mvs_slot_task_free(struct mvs_info *mvi, struct sas_task *task,
1016 struct mvs_slot_info *slot, u32 slot_idx)
1018 if (!slot->task)
1019 return;
1020 if (!sas_protocol_ata(task->task_proto))
1021 if (slot->n_elem)
1022 dma_unmap_sg(mvi->dev, task->scatter,
1023 slot->n_elem, task->data_dir);
1025 switch (task->task_proto) {
1026 case SAS_PROTOCOL_SMP:
1027 dma_unmap_sg(mvi->dev, &task->smp_task.smp_resp, 1,
1028 PCI_DMA_FROMDEVICE);
1029 dma_unmap_sg(mvi->dev, &task->smp_task.smp_req, 1,
1030 PCI_DMA_TODEVICE);
1031 break;
1033 case SAS_PROTOCOL_SATA:
1034 case SAS_PROTOCOL_STP:
1035 case SAS_PROTOCOL_SSP:
1036 default:
1037 /* do nothing */
1038 break;
1041 if (slot->buf) {
1042 pci_pool_free(mvi->dma_pool, slot->buf, slot->buf_dma);
1043 slot->buf = NULL;
1045 list_del_init(&slot->entry);
1046 task->lldd_task = NULL;
1047 slot->task = NULL;
1048 slot->port = NULL;
1049 slot->slot_tag = 0xFFFFFFFF;
1050 mvs_slot_free(mvi, slot_idx);
1053 static void mvs_update_wideport(struct mvs_info *mvi, int phy_no)
1055 struct mvs_phy *phy = &mvi->phy[phy_no];
1056 struct mvs_port *port = phy->port;
1057 int j, no;
1059 for_each_phy(port->wide_port_phymap, j, no) {
1060 if (j & 1) {
1061 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1062 PHYR_WIDE_PORT);
1063 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1064 port->wide_port_phymap);
1065 } else {
1066 MVS_CHIP_DISP->write_port_cfg_addr(mvi, no,
1067 PHYR_WIDE_PORT);
1068 MVS_CHIP_DISP->write_port_cfg_data(mvi, no,
1074 static u32 mvs_is_phy_ready(struct mvs_info *mvi, int i)
1076 u32 tmp;
1077 struct mvs_phy *phy = &mvi->phy[i];
1078 struct mvs_port *port = phy->port;
1080 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, i);
1081 if ((tmp & PHY_READY_MASK) && !(phy->irq_status & PHYEV_POOF)) {
1082 if (!port)
1083 phy->phy_attached = 1;
1084 return tmp;
1087 if (port) {
1088 if (phy->phy_type & PORT_TYPE_SAS) {
1089 port->wide_port_phymap &= ~(1U << i);
1090 if (!port->wide_port_phymap)
1091 port->port_attached = 0;
1092 mvs_update_wideport(mvi, i);
1093 } else if (phy->phy_type & PORT_TYPE_SATA)
1094 port->port_attached = 0;
1095 phy->port = NULL;
1096 phy->phy_attached = 0;
1097 phy->phy_type &= ~(PORT_TYPE_SAS | PORT_TYPE_SATA);
1099 return 0;
1102 static void *mvs_get_d2h_reg(struct mvs_info *mvi, int i, void *buf)
1104 u32 *s = (u32 *) buf;
1106 if (!s)
1107 return NULL;
1109 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG3);
1110 s[3] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1112 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG2);
1113 s[2] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1115 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG1);
1116 s[1] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1118 MVS_CHIP_DISP->write_port_cfg_addr(mvi, i, PHYR_SATA_SIG0);
1119 s[0] = cpu_to_le32(MVS_CHIP_DISP->read_port_cfg_data(mvi, i));
1121 if (((s[1] & 0x00FFFFFF) == 0x00EB1401) && (*(u8 *)&s[3] == 0x01))
1122 s[1] = 0x00EB1401 | (*((u8 *)&s[1] + 3) & 0x10);
1124 return s;
1127 static u32 mvs_is_sig_fis_received(u32 irq_status)
1129 return irq_status & PHYEV_SIG_FIS;
1132 static void mvs_sig_remove_timer(struct mvs_phy *phy)
1134 if (phy->timer.function)
1135 del_timer(&phy->timer);
1136 phy->timer.function = NULL;
1139 void mvs_update_phyinfo(struct mvs_info *mvi, int i, int get_st)
1141 struct mvs_phy *phy = &mvi->phy[i];
1142 struct sas_identify_frame *id;
1144 id = (struct sas_identify_frame *)phy->frame_rcvd;
1146 if (get_st) {
1147 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, i);
1148 phy->phy_status = mvs_is_phy_ready(mvi, i);
1151 if (phy->phy_status) {
1152 int oob_done = 0;
1153 struct asd_sas_phy *sas_phy = &mvi->phy[i].sas_phy;
1155 oob_done = MVS_CHIP_DISP->oob_done(mvi, i);
1157 MVS_CHIP_DISP->fix_phy_info(mvi, i, id);
1158 if (phy->phy_type & PORT_TYPE_SATA) {
1159 phy->identify.target_port_protocols = SAS_PROTOCOL_STP;
1160 if (mvs_is_sig_fis_received(phy->irq_status)) {
1161 mvs_sig_remove_timer(phy);
1162 phy->phy_attached = 1;
1163 phy->att_dev_sas_addr =
1164 i + mvi->id * mvi->chip->n_phy;
1165 if (oob_done)
1166 sas_phy->oob_mode = SATA_OOB_MODE;
1167 phy->frame_rcvd_size =
1168 sizeof(struct dev_to_host_fis);
1169 mvs_get_d2h_reg(mvi, i, id);
1170 } else {
1171 u32 tmp;
1172 dev_printk(KERN_DEBUG, mvi->dev,
1173 "Phy%d : No sig fis\n", i);
1174 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, i);
1175 MVS_CHIP_DISP->write_port_irq_mask(mvi, i,
1176 tmp | PHYEV_SIG_FIS);
1177 phy->phy_attached = 0;
1178 phy->phy_type &= ~PORT_TYPE_SATA;
1179 goto out_done;
1181 } else if (phy->phy_type & PORT_TYPE_SAS
1182 || phy->att_dev_info & PORT_SSP_INIT_MASK) {
1183 phy->phy_attached = 1;
1184 phy->identify.device_type =
1185 phy->att_dev_info & PORT_DEV_TYPE_MASK;
1187 if (phy->identify.device_type == SAS_END_DEV)
1188 phy->identify.target_port_protocols =
1189 SAS_PROTOCOL_SSP;
1190 else if (phy->identify.device_type != NO_DEVICE)
1191 phy->identify.target_port_protocols =
1192 SAS_PROTOCOL_SMP;
1193 if (oob_done)
1194 sas_phy->oob_mode = SAS_OOB_MODE;
1195 phy->frame_rcvd_size =
1196 sizeof(struct sas_identify_frame);
1198 memcpy(sas_phy->attached_sas_addr,
1199 &phy->att_dev_sas_addr, SAS_ADDR_SIZE);
1201 if (MVS_CHIP_DISP->phy_work_around)
1202 MVS_CHIP_DISP->phy_work_around(mvi, i);
1204 mv_dprintk("phy %d attach dev info is %x\n",
1205 i + mvi->id * mvi->chip->n_phy, phy->att_dev_info);
1206 mv_dprintk("phy %d attach sas addr is %llx\n",
1207 i + mvi->id * mvi->chip->n_phy, phy->att_dev_sas_addr);
1208 out_done:
1209 if (get_st)
1210 MVS_CHIP_DISP->write_port_irq_stat(mvi, i, phy->irq_status);
1213 static void mvs_port_notify_formed(struct asd_sas_phy *sas_phy, int lock)
1215 struct sas_ha_struct *sas_ha = sas_phy->ha;
1216 struct mvs_info *mvi = NULL; int i = 0, hi;
1217 struct mvs_phy *phy = sas_phy->lldd_phy;
1218 struct asd_sas_port *sas_port = sas_phy->port;
1219 struct mvs_port *port;
1220 unsigned long flags = 0;
1221 if (!sas_port)
1222 return;
1224 while (sas_ha->sas_phy[i]) {
1225 if (sas_ha->sas_phy[i] == sas_phy)
1226 break;
1227 i++;
1229 hi = i/((struct mvs_prv_info *)sas_ha->lldd_ha)->n_phy;
1230 mvi = ((struct mvs_prv_info *)sas_ha->lldd_ha)->mvi[hi];
1231 if (i >= mvi->chip->n_phy)
1232 port = &mvi->port[i - mvi->chip->n_phy];
1233 else
1234 port = &mvi->port[i];
1235 if (lock)
1236 spin_lock_irqsave(&mvi->lock, flags);
1237 port->port_attached = 1;
1238 phy->port = port;
1239 sas_port->lldd_port = port;
1240 if (phy->phy_type & PORT_TYPE_SAS) {
1241 port->wide_port_phymap = sas_port->phy_mask;
1242 mv_printk("set wide port phy map %x\n", sas_port->phy_mask);
1243 mvs_update_wideport(mvi, sas_phy->id);
1245 if (lock)
1246 spin_unlock_irqrestore(&mvi->lock, flags);
1249 static void mvs_port_notify_deformed(struct asd_sas_phy *sas_phy, int lock)
1251 struct domain_device *dev;
1252 struct mvs_phy *phy = sas_phy->lldd_phy;
1253 struct mvs_info *mvi = phy->mvi;
1254 struct asd_sas_port *port = sas_phy->port;
1255 int phy_no = 0;
1257 while (phy != &mvi->phy[phy_no]) {
1258 phy_no++;
1259 if (phy_no >= MVS_MAX_PHYS)
1260 return;
1262 list_for_each_entry(dev, &port->dev_list, dev_list_node)
1263 mvs_do_release_task(phy->mvi, phy_no, dev);
1268 void mvs_port_formed(struct asd_sas_phy *sas_phy)
1270 mvs_port_notify_formed(sas_phy, 1);
1273 void mvs_port_deformed(struct asd_sas_phy *sas_phy)
1275 mvs_port_notify_deformed(sas_phy, 1);
1278 struct mvs_device *mvs_alloc_dev(struct mvs_info *mvi)
1280 u32 dev;
1281 for (dev = 0; dev < MVS_MAX_DEVICES; dev++) {
1282 if (mvi->devices[dev].dev_type == NO_DEVICE) {
1283 mvi->devices[dev].device_id = dev;
1284 return &mvi->devices[dev];
1288 if (dev == MVS_MAX_DEVICES)
1289 mv_printk("max support %d devices, ignore ..\n",
1290 MVS_MAX_DEVICES);
1292 return NULL;
1295 void mvs_free_dev(struct mvs_device *mvi_dev)
1297 u32 id = mvi_dev->device_id;
1298 memset(mvi_dev, 0, sizeof(*mvi_dev));
1299 mvi_dev->device_id = id;
1300 mvi_dev->dev_type = NO_DEVICE;
1301 mvi_dev->dev_status = MVS_DEV_NORMAL;
1302 mvi_dev->taskfileset = MVS_ID_NOT_MAPPED;
1305 int mvs_dev_found_notify(struct domain_device *dev, int lock)
1307 unsigned long flags = 0;
1308 int res = 0;
1309 struct mvs_info *mvi = NULL;
1310 struct domain_device *parent_dev = dev->parent;
1311 struct mvs_device *mvi_device;
1313 mvi = mvs_find_dev_mvi(dev);
1315 if (lock)
1316 spin_lock_irqsave(&mvi->lock, flags);
1318 mvi_device = mvs_alloc_dev(mvi);
1319 if (!mvi_device) {
1320 res = -1;
1321 goto found_out;
1323 dev->lldd_dev = mvi_device;
1324 mvi_device->dev_status = MVS_DEV_NORMAL;
1325 mvi_device->dev_type = dev->dev_type;
1326 mvi_device->mvi_info = mvi;
1327 mvi_device->sas_device = dev;
1328 if (parent_dev && DEV_IS_EXPANDER(parent_dev->dev_type)) {
1329 int phy_id;
1330 u8 phy_num = parent_dev->ex_dev.num_phys;
1331 struct ex_phy *phy;
1332 for (phy_id = 0; phy_id < phy_num; phy_id++) {
1333 phy = &parent_dev->ex_dev.ex_phy[phy_id];
1334 if (SAS_ADDR(phy->attached_sas_addr) ==
1335 SAS_ADDR(dev->sas_addr)) {
1336 mvi_device->attached_phy = phy_id;
1337 break;
1341 if (phy_id == phy_num) {
1342 mv_printk("Error: no attached dev:%016llx"
1343 "at ex:%016llx.\n",
1344 SAS_ADDR(dev->sas_addr),
1345 SAS_ADDR(parent_dev->sas_addr));
1346 res = -1;
1350 found_out:
1351 if (lock)
1352 spin_unlock_irqrestore(&mvi->lock, flags);
1353 return res;
1356 int mvs_dev_found(struct domain_device *dev)
1358 return mvs_dev_found_notify(dev, 1);
1361 void mvs_dev_gone_notify(struct domain_device *dev)
1363 unsigned long flags = 0;
1364 struct mvs_device *mvi_dev = dev->lldd_dev;
1365 struct mvs_info *mvi = mvi_dev->mvi_info;
1367 spin_lock_irqsave(&mvi->lock, flags);
1369 if (mvi_dev) {
1370 mv_dprintk("found dev[%d:%x] is gone.\n",
1371 mvi_dev->device_id, mvi_dev->dev_type);
1372 mvs_release_task(mvi, dev);
1373 mvs_free_reg_set(mvi, mvi_dev);
1374 mvs_free_dev(mvi_dev);
1375 } else {
1376 mv_dprintk("found dev has gone.\n");
1378 dev->lldd_dev = NULL;
1379 mvi_dev->sas_device = NULL;
1381 spin_unlock_irqrestore(&mvi->lock, flags);
1385 void mvs_dev_gone(struct domain_device *dev)
1387 mvs_dev_gone_notify(dev);
1390 static void mvs_task_done(struct sas_task *task)
1392 if (!del_timer(&task->timer))
1393 return;
1394 complete(&task->completion);
1397 static void mvs_tmf_timedout(unsigned long data)
1399 struct sas_task *task = (struct sas_task *)data;
1401 task->task_state_flags |= SAS_TASK_STATE_ABORTED;
1402 complete(&task->completion);
1405 #define MVS_TASK_TIMEOUT 20
1406 static int mvs_exec_internal_tmf_task(struct domain_device *dev,
1407 void *parameter, u32 para_len, struct mvs_tmf_task *tmf)
1409 int res, retry;
1410 struct sas_task *task = NULL;
1412 for (retry = 0; retry < 3; retry++) {
1413 task = sas_alloc_task(GFP_KERNEL);
1414 if (!task)
1415 return -ENOMEM;
1417 task->dev = dev;
1418 task->task_proto = dev->tproto;
1420 memcpy(&task->ssp_task, parameter, para_len);
1421 task->task_done = mvs_task_done;
1423 task->timer.data = (unsigned long) task;
1424 task->timer.function = mvs_tmf_timedout;
1425 task->timer.expires = jiffies + MVS_TASK_TIMEOUT*HZ;
1426 add_timer(&task->timer);
1428 res = mvs_task_exec(task, 1, GFP_KERNEL, NULL, 1, tmf);
1430 if (res) {
1431 del_timer(&task->timer);
1432 mv_printk("executing internel task failed:%d\n", res);
1433 goto ex_err;
1436 wait_for_completion(&task->completion);
1437 res = TMF_RESP_FUNC_FAILED;
1438 /* Even TMF timed out, return direct. */
1439 if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1440 if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1441 mv_printk("TMF task[%x] timeout.\n", tmf->tmf);
1442 goto ex_err;
1446 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1447 task->task_status.stat == SAM_STAT_GOOD) {
1448 res = TMF_RESP_FUNC_COMPLETE;
1449 break;
1452 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1453 task->task_status.stat == SAS_DATA_UNDERRUN) {
1454 /* no error, but return the number of bytes of
1455 * underrun */
1456 res = task->task_status.residual;
1457 break;
1460 if (task->task_status.resp == SAS_TASK_COMPLETE &&
1461 task->task_status.stat == SAS_DATA_OVERRUN) {
1462 mv_dprintk("blocked task error.\n");
1463 res = -EMSGSIZE;
1464 break;
1465 } else {
1466 mv_dprintk(" task to dev %016llx response: 0x%x "
1467 "status 0x%x\n",
1468 SAS_ADDR(dev->sas_addr),
1469 task->task_status.resp,
1470 task->task_status.stat);
1471 sas_free_task(task);
1472 task = NULL;
1476 ex_err:
1477 BUG_ON(retry == 3 && task != NULL);
1478 sas_free_task(task);
1479 return res;
1482 static int mvs_debug_issue_ssp_tmf(struct domain_device *dev,
1483 u8 *lun, struct mvs_tmf_task *tmf)
1485 struct sas_ssp_task ssp_task;
1486 if (!(dev->tproto & SAS_PROTOCOL_SSP))
1487 return TMF_RESP_FUNC_ESUPP;
1489 memcpy(ssp_task.LUN, lun, 8);
1491 return mvs_exec_internal_tmf_task(dev, &ssp_task,
1492 sizeof(ssp_task), tmf);
1496 /* Standard mandates link reset for ATA (type 0)
1497 and hard reset for SSP (type 1) , only for RECOVERY */
1498 static int mvs_debug_I_T_nexus_reset(struct domain_device *dev)
1500 int rc;
1501 struct sas_phy *phy = sas_find_local_phy(dev);
1502 int reset_type = (dev->dev_type == SATA_DEV ||
1503 (dev->tproto & SAS_PROTOCOL_STP)) ? 0 : 1;
1504 rc = sas_phy_reset(phy, reset_type);
1505 msleep(2000);
1506 return rc;
1509 /* mandatory SAM-3 */
1510 int mvs_lu_reset(struct domain_device *dev, u8 *lun)
1512 unsigned long flags;
1513 int rc = TMF_RESP_FUNC_FAILED;
1514 struct mvs_tmf_task tmf_task;
1515 struct mvs_device * mvi_dev = dev->lldd_dev;
1516 struct mvs_info *mvi = mvi_dev->mvi_info;
1518 tmf_task.tmf = TMF_LU_RESET;
1519 mvi_dev->dev_status = MVS_DEV_EH;
1520 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1521 if (rc == TMF_RESP_FUNC_COMPLETE) {
1522 spin_lock_irqsave(&mvi->lock, flags);
1523 mvs_release_task(mvi, dev);
1524 spin_unlock_irqrestore(&mvi->lock, flags);
1526 /* If failed, fall-through I_T_Nexus reset */
1527 mv_printk("%s for device[%x]:rc= %d\n", __func__,
1528 mvi_dev->device_id, rc);
1529 return rc;
1532 int mvs_I_T_nexus_reset(struct domain_device *dev)
1534 unsigned long flags;
1535 int rc = TMF_RESP_FUNC_FAILED;
1536 struct mvs_device * mvi_dev = (struct mvs_device *)dev->lldd_dev;
1537 struct mvs_info *mvi = mvi_dev->mvi_info;
1539 if (mvi_dev->dev_status != MVS_DEV_EH)
1540 return TMF_RESP_FUNC_COMPLETE;
1541 else
1542 mvi_dev->dev_status = MVS_DEV_NORMAL;
1543 rc = mvs_debug_I_T_nexus_reset(dev);
1544 mv_printk("%s for device[%x]:rc= %d\n",
1545 __func__, mvi_dev->device_id, rc);
1547 spin_lock_irqsave(&mvi->lock, flags);
1548 mvs_release_task(mvi, dev);
1549 spin_unlock_irqrestore(&mvi->lock, flags);
1551 return rc;
1553 /* optional SAM-3 */
1554 int mvs_query_task(struct sas_task *task)
1556 u32 tag;
1557 struct scsi_lun lun;
1558 struct mvs_tmf_task tmf_task;
1559 int rc = TMF_RESP_FUNC_FAILED;
1561 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1562 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1563 struct domain_device *dev = task->dev;
1564 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1565 struct mvs_info *mvi = mvi_dev->mvi_info;
1567 int_to_scsilun(cmnd->device->lun, &lun);
1568 rc = mvs_find_tag(mvi, task, &tag);
1569 if (rc == 0) {
1570 rc = TMF_RESP_FUNC_FAILED;
1571 return rc;
1574 tmf_task.tmf = TMF_QUERY_TASK;
1575 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1577 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1578 switch (rc) {
1579 /* The task is still in Lun, release it then */
1580 case TMF_RESP_FUNC_SUCC:
1581 /* The task is not in Lun or failed, reset the phy */
1582 case TMF_RESP_FUNC_FAILED:
1583 case TMF_RESP_FUNC_COMPLETE:
1584 break;
1587 mv_printk("%s:rc= %d\n", __func__, rc);
1588 return rc;
1591 /* mandatory SAM-3, still need free task/slot info */
1592 int mvs_abort_task(struct sas_task *task)
1594 struct scsi_lun lun;
1595 struct mvs_tmf_task tmf_task;
1596 struct domain_device *dev = task->dev;
1597 struct mvs_device *mvi_dev = (struct mvs_device *)dev->lldd_dev;
1598 struct mvs_info *mvi;
1599 int rc = TMF_RESP_FUNC_FAILED;
1600 unsigned long flags;
1601 u32 tag;
1603 if (!mvi_dev) {
1604 mv_printk("Device has removed\n");
1605 return TMF_RESP_FUNC_FAILED;
1608 mvi = mvi_dev->mvi_info;
1610 spin_lock_irqsave(&task->task_state_lock, flags);
1611 if (task->task_state_flags & SAS_TASK_STATE_DONE) {
1612 spin_unlock_irqrestore(&task->task_state_lock, flags);
1613 rc = TMF_RESP_FUNC_COMPLETE;
1614 goto out;
1616 spin_unlock_irqrestore(&task->task_state_lock, flags);
1617 mvi_dev->dev_status = MVS_DEV_EH;
1618 if (task->lldd_task && task->task_proto & SAS_PROTOCOL_SSP) {
1619 struct scsi_cmnd * cmnd = (struct scsi_cmnd *)task->uldd_task;
1621 int_to_scsilun(cmnd->device->lun, &lun);
1622 rc = mvs_find_tag(mvi, task, &tag);
1623 if (rc == 0) {
1624 mv_printk("No such tag in %s\n", __func__);
1625 rc = TMF_RESP_FUNC_FAILED;
1626 return rc;
1629 tmf_task.tmf = TMF_ABORT_TASK;
1630 tmf_task.tag_of_task_to_be_managed = cpu_to_le16(tag);
1632 rc = mvs_debug_issue_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1634 /* if successful, clear the task and callback forwards.*/
1635 if (rc == TMF_RESP_FUNC_COMPLETE) {
1636 u32 slot_no;
1637 struct mvs_slot_info *slot;
1639 if (task->lldd_task) {
1640 slot = task->lldd_task;
1641 slot_no = (u32) (slot - mvi->slot_info);
1642 spin_lock_irqsave(&mvi->lock, flags);
1643 mvs_slot_complete(mvi, slot_no, 1);
1644 spin_unlock_irqrestore(&mvi->lock, flags);
1648 } else if (task->task_proto & SAS_PROTOCOL_SATA ||
1649 task->task_proto & SAS_PROTOCOL_STP) {
1650 if (SATA_DEV == dev->dev_type) {
1651 struct mvs_slot_info *slot = task->lldd_task;
1652 u32 slot_idx = (u32)(slot - mvi->slot_info);
1653 mv_dprintk("mvs_abort_task() mvi=%p task=%p "
1654 "slot=%p slot_idx=x%x\n",
1655 mvi, task, slot, slot_idx);
1656 mvs_tmf_timedout((unsigned long)task);
1657 mvs_slot_task_free(mvi, task, slot, slot_idx);
1658 rc = TMF_RESP_FUNC_COMPLETE;
1659 goto out;
1663 out:
1664 if (rc != TMF_RESP_FUNC_COMPLETE)
1665 mv_printk("%s:rc= %d\n", __func__, rc);
1666 return rc;
1669 int mvs_abort_task_set(struct domain_device *dev, u8 *lun)
1671 int rc = TMF_RESP_FUNC_FAILED;
1672 struct mvs_tmf_task tmf_task;
1674 tmf_task.tmf = TMF_ABORT_TASK_SET;
1675 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1677 return rc;
1680 int mvs_clear_aca(struct domain_device *dev, u8 *lun)
1682 int rc = TMF_RESP_FUNC_FAILED;
1683 struct mvs_tmf_task tmf_task;
1685 tmf_task.tmf = TMF_CLEAR_ACA;
1686 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1688 return rc;
1691 int mvs_clear_task_set(struct domain_device *dev, u8 *lun)
1693 int rc = TMF_RESP_FUNC_FAILED;
1694 struct mvs_tmf_task tmf_task;
1696 tmf_task.tmf = TMF_CLEAR_TASK_SET;
1697 rc = mvs_debug_issue_ssp_tmf(dev, lun, &tmf_task);
1699 return rc;
1702 static int mvs_sata_done(struct mvs_info *mvi, struct sas_task *task,
1703 u32 slot_idx, int err)
1705 struct mvs_device *mvi_dev = task->dev->lldd_dev;
1706 struct task_status_struct *tstat = &task->task_status;
1707 struct ata_task_resp *resp = (struct ata_task_resp *)tstat->buf;
1708 int stat = SAM_STAT_GOOD;
1711 resp->frame_len = sizeof(struct dev_to_host_fis);
1712 memcpy(&resp->ending_fis[0],
1713 SATA_RECEIVED_D2H_FIS(mvi_dev->taskfileset),
1714 sizeof(struct dev_to_host_fis));
1715 tstat->buf_valid_size = sizeof(*resp);
1716 if (unlikely(err)) {
1717 if (unlikely(err & CMD_ISS_STPD))
1718 stat = SAS_OPEN_REJECT;
1719 else
1720 stat = SAS_PROTO_RESPONSE;
1723 return stat;
1726 void mvs_set_sense(u8 *buffer, int len, int d_sense,
1727 int key, int asc, int ascq)
1729 memset(buffer, 0, len);
1731 if (d_sense) {
1732 /* Descriptor format */
1733 if (len < 4) {
1734 mv_printk("Length %d of sense buffer too small to "
1735 "fit sense %x:%x:%x", len, key, asc, ascq);
1738 buffer[0] = 0x72; /* Response Code */
1739 if (len > 1)
1740 buffer[1] = key; /* Sense Key */
1741 if (len > 2)
1742 buffer[2] = asc; /* ASC */
1743 if (len > 3)
1744 buffer[3] = ascq; /* ASCQ */
1745 } else {
1746 if (len < 14) {
1747 mv_printk("Length %d of sense buffer too small to "
1748 "fit sense %x:%x:%x", len, key, asc, ascq);
1751 buffer[0] = 0x70; /* Response Code */
1752 if (len > 2)
1753 buffer[2] = key; /* Sense Key */
1754 if (len > 7)
1755 buffer[7] = 0x0a; /* Additional Sense Length */
1756 if (len > 12)
1757 buffer[12] = asc; /* ASC */
1758 if (len > 13)
1759 buffer[13] = ascq; /* ASCQ */
1762 return;
1765 void mvs_fill_ssp_resp_iu(struct ssp_response_iu *iu,
1766 u8 key, u8 asc, u8 asc_q)
1768 iu->datapres = 2;
1769 iu->response_data_len = 0;
1770 iu->sense_data_len = 17;
1771 iu->status = 02;
1772 mvs_set_sense(iu->sense_data, 17, 0,
1773 key, asc, asc_q);
1776 static int mvs_slot_err(struct mvs_info *mvi, struct sas_task *task,
1777 u32 slot_idx)
1779 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1780 int stat;
1781 u32 err_dw0 = le32_to_cpu(*(u32 *)slot->response);
1782 u32 err_dw1 = le32_to_cpu(*((u32 *)slot->response + 1));
1783 u32 tfs = 0;
1784 enum mvs_port_type type = PORT_TYPE_SAS;
1786 if (err_dw0 & CMD_ISS_STPD)
1787 MVS_CHIP_DISP->issue_stop(mvi, type, tfs);
1789 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1791 stat = SAM_STAT_CHECK_CONDITION;
1792 switch (task->task_proto) {
1793 case SAS_PROTOCOL_SSP:
1795 stat = SAS_ABORTED_TASK;
1796 if ((err_dw0 & NO_DEST) || err_dw1 & bit(31)) {
1797 struct ssp_response_iu *iu = slot->response +
1798 sizeof(struct mvs_err_info);
1799 mvs_fill_ssp_resp_iu(iu, NOT_READY, 0x04, 01);
1800 sas_ssp_task_response(mvi->dev, task, iu);
1801 stat = SAM_STAT_CHECK_CONDITION;
1803 if (err_dw1 & bit(31))
1804 mv_printk("reuse same slot, retry command.\n");
1805 break;
1807 case SAS_PROTOCOL_SMP:
1808 stat = SAM_STAT_CHECK_CONDITION;
1809 break;
1811 case SAS_PROTOCOL_SATA:
1812 case SAS_PROTOCOL_STP:
1813 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP:
1815 task->ata_task.use_ncq = 0;
1816 stat = SAS_PROTO_RESPONSE;
1817 mvs_sata_done(mvi, task, slot_idx, err_dw0);
1819 break;
1820 default:
1821 break;
1824 return stat;
1827 int mvs_slot_complete(struct mvs_info *mvi, u32 rx_desc, u32 flags)
1829 u32 slot_idx = rx_desc & RXQ_SLOT_MASK;
1830 struct mvs_slot_info *slot = &mvi->slot_info[slot_idx];
1831 struct sas_task *task = slot->task;
1832 struct mvs_device *mvi_dev = NULL;
1833 struct task_status_struct *tstat;
1834 struct domain_device *dev;
1835 u32 aborted;
1837 void *to;
1838 enum exec_status sts;
1840 if (unlikely(!task || !task->lldd_task || !task->dev))
1841 return -1;
1843 tstat = &task->task_status;
1844 dev = task->dev;
1845 mvi_dev = dev->lldd_dev;
1847 spin_lock(&task->task_state_lock);
1848 task->task_state_flags &=
1849 ~(SAS_TASK_STATE_PENDING | SAS_TASK_AT_INITIATOR);
1850 task->task_state_flags |= SAS_TASK_STATE_DONE;
1851 /* race condition*/
1852 aborted = task->task_state_flags & SAS_TASK_STATE_ABORTED;
1853 spin_unlock(&task->task_state_lock);
1855 memset(tstat, 0, sizeof(*tstat));
1856 tstat->resp = SAS_TASK_COMPLETE;
1858 if (unlikely(aborted)) {
1859 tstat->stat = SAS_ABORTED_TASK;
1860 if (mvi_dev && mvi_dev->running_req)
1861 mvi_dev->running_req--;
1862 if (sas_protocol_ata(task->task_proto))
1863 mvs_free_reg_set(mvi, mvi_dev);
1865 mvs_slot_task_free(mvi, task, slot, slot_idx);
1866 return -1;
1869 /* when no device attaching, go ahead and complete by error handling*/
1870 if (unlikely(!mvi_dev || flags)) {
1871 if (!mvi_dev)
1872 mv_dprintk("port has not device.\n");
1873 tstat->stat = SAS_PHY_DOWN;
1874 goto out;
1877 /* error info record present */
1878 if (unlikely((rx_desc & RXQ_ERR) && (*(u64 *) slot->response))) {
1879 mv_dprintk("port %d slot %d rx_desc %X has error info"
1880 "%016llX.\n", slot->port->sas_port.id, slot_idx,
1881 rx_desc, (u64)(*(u64 *)slot->response));
1882 tstat->stat = mvs_slot_err(mvi, task, slot_idx);
1883 tstat->resp = SAS_TASK_COMPLETE;
1884 goto out;
1887 switch (task->task_proto) {
1888 case SAS_PROTOCOL_SSP:
1889 /* hw says status == 0, datapres == 0 */
1890 if (rx_desc & RXQ_GOOD) {
1891 tstat->stat = SAM_STAT_GOOD;
1892 tstat->resp = SAS_TASK_COMPLETE;
1894 /* response frame present */
1895 else if (rx_desc & RXQ_RSP) {
1896 struct ssp_response_iu *iu = slot->response +
1897 sizeof(struct mvs_err_info);
1898 sas_ssp_task_response(mvi->dev, task, iu);
1899 } else
1900 tstat->stat = SAM_STAT_CHECK_CONDITION;
1901 break;
1903 case SAS_PROTOCOL_SMP: {
1904 struct scatterlist *sg_resp = &task->smp_task.smp_resp;
1905 tstat->stat = SAM_STAT_GOOD;
1906 to = kmap_atomic(sg_page(sg_resp), KM_IRQ0);
1907 memcpy(to + sg_resp->offset,
1908 slot->response + sizeof(struct mvs_err_info),
1909 sg_dma_len(sg_resp));
1910 kunmap_atomic(to, KM_IRQ0);
1911 break;
1914 case SAS_PROTOCOL_SATA:
1915 case SAS_PROTOCOL_STP:
1916 case SAS_PROTOCOL_SATA | SAS_PROTOCOL_STP: {
1917 tstat->stat = mvs_sata_done(mvi, task, slot_idx, 0);
1918 break;
1921 default:
1922 tstat->stat = SAM_STAT_CHECK_CONDITION;
1923 break;
1925 if (!slot->port->port_attached) {
1926 mv_dprintk("port %d has removed.\n", slot->port->sas_port.id);
1927 tstat->stat = SAS_PHY_DOWN;
1931 out:
1932 if (mvi_dev && mvi_dev->running_req) {
1933 mvi_dev->running_req--;
1934 if (sas_protocol_ata(task->task_proto) && !mvi_dev->running_req)
1935 mvs_free_reg_set(mvi, mvi_dev);
1937 mvs_slot_task_free(mvi, task, slot, slot_idx);
1938 sts = tstat->stat;
1940 spin_unlock(&mvi->lock);
1941 if (task->task_done)
1942 task->task_done(task);
1944 spin_lock(&mvi->lock);
1946 return sts;
1949 void mvs_do_release_task(struct mvs_info *mvi,
1950 int phy_no, struct domain_device *dev)
1952 u32 slot_idx;
1953 struct mvs_phy *phy;
1954 struct mvs_port *port;
1955 struct mvs_slot_info *slot, *slot2;
1957 phy = &mvi->phy[phy_no];
1958 port = phy->port;
1959 if (!port)
1960 return;
1961 /* clean cmpl queue in case request is already finished */
1962 mvs_int_rx(mvi, false);
1966 list_for_each_entry_safe(slot, slot2, &port->list, entry) {
1967 struct sas_task *task;
1968 slot_idx = (u32) (slot - mvi->slot_info);
1969 task = slot->task;
1971 if (dev && task->dev != dev)
1972 continue;
1974 mv_printk("Release slot [%x] tag[%x], task [%p]:\n",
1975 slot_idx, slot->slot_tag, task);
1976 MVS_CHIP_DISP->command_active(mvi, slot_idx);
1978 mvs_slot_complete(mvi, slot_idx, 1);
1982 void mvs_release_task(struct mvs_info *mvi,
1983 struct domain_device *dev)
1985 int i, phyno[WIDE_PORT_MAX_PHY], num;
1986 num = mvs_find_dev_phyno(dev, phyno);
1987 for (i = 0; i < num; i++)
1988 mvs_do_release_task(mvi, phyno[i], dev);
1991 static void mvs_phy_disconnected(struct mvs_phy *phy)
1993 phy->phy_attached = 0;
1994 phy->att_dev_info = 0;
1995 phy->att_dev_sas_addr = 0;
1998 static void mvs_work_queue(struct work_struct *work)
2000 struct delayed_work *dw = container_of(work, struct delayed_work, work);
2001 struct mvs_wq *mwq = container_of(dw, struct mvs_wq, work_q);
2002 struct mvs_info *mvi = mwq->mvi;
2003 unsigned long flags;
2004 u32 phy_no = (unsigned long) mwq->data;
2005 struct sas_ha_struct *sas_ha = mvi->sas;
2006 struct mvs_phy *phy = &mvi->phy[phy_no];
2007 struct asd_sas_phy *sas_phy = &phy->sas_phy;
2009 spin_lock_irqsave(&mvi->lock, flags);
2010 if (mwq->handler & PHY_PLUG_EVENT) {
2012 if (phy->phy_event & PHY_PLUG_OUT) {
2013 u32 tmp;
2014 struct sas_identify_frame *id;
2015 id = (struct sas_identify_frame *)phy->frame_rcvd;
2016 tmp = MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no);
2017 phy->phy_event &= ~PHY_PLUG_OUT;
2018 if (!(tmp & PHY_READY_MASK)) {
2019 sas_phy_disconnected(sas_phy);
2020 mvs_phy_disconnected(phy);
2021 sas_ha->notify_phy_event(sas_phy,
2022 PHYE_LOSS_OF_SIGNAL);
2023 mv_dprintk("phy%d Removed Device\n", phy_no);
2024 } else {
2025 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2026 mvs_update_phyinfo(mvi, phy_no, 1);
2027 mvs_bytes_dmaed(mvi, phy_no);
2028 mvs_port_notify_formed(sas_phy, 0);
2029 mv_dprintk("phy%d Attached Device\n", phy_no);
2032 } else if (mwq->handler & EXP_BRCT_CHG) {
2033 phy->phy_event &= ~EXP_BRCT_CHG;
2034 sas_ha->notify_port_event(sas_phy,
2035 PORTE_BROADCAST_RCVD);
2036 mv_dprintk("phy%d Got Broadcast Change\n", phy_no);
2038 list_del(&mwq->entry);
2039 spin_unlock_irqrestore(&mvi->lock, flags);
2040 kfree(mwq);
2043 static int mvs_handle_event(struct mvs_info *mvi, void *data, int handler)
2045 struct mvs_wq *mwq;
2046 int ret = 0;
2048 mwq = kmalloc(sizeof(struct mvs_wq), GFP_ATOMIC);
2049 if (mwq) {
2050 mwq->mvi = mvi;
2051 mwq->data = data;
2052 mwq->handler = handler;
2053 MV_INIT_DELAYED_WORK(&mwq->work_q, mvs_work_queue, mwq);
2054 list_add_tail(&mwq->entry, &mvi->wq_list);
2055 schedule_delayed_work(&mwq->work_q, HZ * 2);
2056 } else
2057 ret = -ENOMEM;
2059 return ret;
2062 static void mvs_sig_time_out(unsigned long tphy)
2064 struct mvs_phy *phy = (struct mvs_phy *)tphy;
2065 struct mvs_info *mvi = phy->mvi;
2066 u8 phy_no;
2068 for (phy_no = 0; phy_no < mvi->chip->n_phy; phy_no++) {
2069 if (&mvi->phy[phy_no] == phy) {
2070 mv_dprintk("Get signature time out, reset phy %d\n",
2071 phy_no+mvi->id*mvi->chip->n_phy);
2072 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_HARD_RESET);
2077 void mvs_int_port(struct mvs_info *mvi, int phy_no, u32 events)
2079 u32 tmp;
2080 struct mvs_phy *phy = &mvi->phy[phy_no];
2082 phy->irq_status = MVS_CHIP_DISP->read_port_irq_stat(mvi, phy_no);
2083 MVS_CHIP_DISP->write_port_irq_stat(mvi, phy_no, phy->irq_status);
2084 mv_dprintk("phy %d ctrl sts=0x%08X.\n", phy_no+mvi->id*mvi->chip->n_phy,
2085 MVS_CHIP_DISP->read_phy_ctl(mvi, phy_no));
2086 mv_dprintk("phy %d irq sts = 0x%08X\n", phy_no+mvi->id*mvi->chip->n_phy,
2087 phy->irq_status);
2090 * events is port event now ,
2091 * we need check the interrupt status which belongs to per port.
2094 if (phy->irq_status & PHYEV_DCDR_ERR) {
2095 mv_dprintk("phy %d STP decoding error.\n",
2096 phy_no + mvi->id*mvi->chip->n_phy);
2099 if (phy->irq_status & PHYEV_POOF) {
2100 mdelay(500);
2101 if (!(phy->phy_event & PHY_PLUG_OUT)) {
2102 int dev_sata = phy->phy_type & PORT_TYPE_SATA;
2103 int ready;
2104 mvs_do_release_task(mvi, phy_no, NULL);
2105 phy->phy_event |= PHY_PLUG_OUT;
2106 MVS_CHIP_DISP->clear_srs_irq(mvi, 0, 1);
2107 mvs_handle_event(mvi,
2108 (void *)(unsigned long)phy_no,
2109 PHY_PLUG_EVENT);
2110 ready = mvs_is_phy_ready(mvi, phy_no);
2111 if (ready || dev_sata) {
2112 if (MVS_CHIP_DISP->stp_reset)
2113 MVS_CHIP_DISP->stp_reset(mvi,
2114 phy_no);
2115 else
2116 MVS_CHIP_DISP->phy_reset(mvi,
2117 phy_no, MVS_SOFT_RESET);
2118 return;
2123 if (phy->irq_status & PHYEV_COMWAKE) {
2124 tmp = MVS_CHIP_DISP->read_port_irq_mask(mvi, phy_no);
2125 MVS_CHIP_DISP->write_port_irq_mask(mvi, phy_no,
2126 tmp | PHYEV_SIG_FIS);
2127 if (phy->timer.function == NULL) {
2128 phy->timer.data = (unsigned long)phy;
2129 phy->timer.function = mvs_sig_time_out;
2130 phy->timer.expires = jiffies + 5*HZ;
2131 add_timer(&phy->timer);
2134 if (phy->irq_status & (PHYEV_SIG_FIS | PHYEV_ID_DONE)) {
2135 phy->phy_status = mvs_is_phy_ready(mvi, phy_no);
2136 mv_dprintk("notify plug in on phy[%d]\n", phy_no);
2137 if (phy->phy_status) {
2138 mdelay(10);
2139 MVS_CHIP_DISP->detect_porttype(mvi, phy_no);
2140 if (phy->phy_type & PORT_TYPE_SATA) {
2141 tmp = MVS_CHIP_DISP->read_port_irq_mask(
2142 mvi, phy_no);
2143 tmp &= ~PHYEV_SIG_FIS;
2144 MVS_CHIP_DISP->write_port_irq_mask(mvi,
2145 phy_no, tmp);
2147 mvs_update_phyinfo(mvi, phy_no, 0);
2148 if (phy->phy_type & PORT_TYPE_SAS) {
2149 MVS_CHIP_DISP->phy_reset(mvi, phy_no, MVS_PHY_TUNE);
2150 mdelay(10);
2153 mvs_bytes_dmaed(mvi, phy_no);
2154 /* whether driver is going to handle hot plug */
2155 if (phy->phy_event & PHY_PLUG_OUT) {
2156 mvs_port_notify_formed(&phy->sas_phy, 0);
2157 phy->phy_event &= ~PHY_PLUG_OUT;
2159 } else {
2160 mv_dprintk("plugin interrupt but phy%d is gone\n",
2161 phy_no + mvi->id*mvi->chip->n_phy);
2163 } else if (phy->irq_status & PHYEV_BROAD_CH) {
2164 mv_dprintk("phy %d broadcast change.\n",
2165 phy_no + mvi->id*mvi->chip->n_phy);
2166 mvs_handle_event(mvi, (void *)(unsigned long)phy_no,
2167 EXP_BRCT_CHG);
2171 int mvs_int_rx(struct mvs_info *mvi, bool self_clear)
2173 u32 rx_prod_idx, rx_desc;
2174 bool attn = false;
2176 /* the first dword in the RX ring is special: it contains
2177 * a mirror of the hardware's RX producer index, so that
2178 * we don't have to stall the CPU reading that register.
2179 * The actual RX ring is offset by one dword, due to this.
2181 rx_prod_idx = mvi->rx_cons;
2182 mvi->rx_cons = le32_to_cpu(mvi->rx[0]);
2183 if (mvi->rx_cons == 0xfff) /* h/w hasn't touched RX ring yet */
2184 return 0;
2186 /* The CMPL_Q may come late, read from register and try again
2187 * note: if coalescing is enabled,
2188 * it will need to read from register every time for sure
2190 if (unlikely(mvi->rx_cons == rx_prod_idx))
2191 mvi->rx_cons = MVS_CHIP_DISP->rx_update(mvi) & RX_RING_SZ_MASK;
2193 if (mvi->rx_cons == rx_prod_idx)
2194 return 0;
2196 while (mvi->rx_cons != rx_prod_idx) {
2197 /* increment our internal RX consumer pointer */
2198 rx_prod_idx = (rx_prod_idx + 1) & (MVS_RX_RING_SZ - 1);
2199 rx_desc = le32_to_cpu(mvi->rx[rx_prod_idx + 1]);
2201 if (likely(rx_desc & RXQ_DONE))
2202 mvs_slot_complete(mvi, rx_desc, 0);
2203 if (rx_desc & RXQ_ATTN) {
2204 attn = true;
2205 } else if (rx_desc & RXQ_ERR) {
2206 if (!(rx_desc & RXQ_DONE))
2207 mvs_slot_complete(mvi, rx_desc, 0);
2208 } else if (rx_desc & RXQ_SLOT_RESET) {
2209 mvs_slot_free(mvi, rx_desc);
2213 if (attn && self_clear)
2214 MVS_CHIP_DISP->int_full(mvi);
2215 return 0;