2 * Aic94xx SAS/SATA driver SCB management.
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This file is part of the aic94xx driver.
11 * The aic94xx driver is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; version 2 of the
16 * The aic94xx driver is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with the aic94xx driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <linux/pci.h>
28 #include <scsi/scsi_host.h>
31 #include "aic94xx_reg.h"
32 #include "aic94xx_hwi.h"
33 #include "aic94xx_seq.h"
35 #include "aic94xx_dump.h"
37 /* ---------- EMPTY SCB ---------- */
41 #define PRIMITIVE_RECVD 0x08
42 #define PHY_EVENT 0x10
43 #define LINK_RESET_ERROR 0x18
44 #define TIMER_EVENT 0x20
45 #define REQ_TASK_ABORT 0xF0
46 #define REQ_DEVICE_RESET 0xF1
47 #define SIGNAL_NCQ_ERROR 0xF2
48 #define CLEAR_NCQ_ERROR 0xF3
50 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
51 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
54 static inline void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
56 struct sas_phy *sas_phy = phy->sas_phy.phy;
58 switch (oob_mode & 7) {
60 /* FIXME: sas transport class doesn't have this */
61 phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
62 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
65 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
66 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
69 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
70 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
73 sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
74 sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
75 sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
76 sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
77 sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
79 if (oob_mode & SAS_MODE)
80 phy->sas_phy.oob_mode = SAS_OOB_MODE;
81 else if (oob_mode & SATA_MODE)
82 phy->sas_phy.oob_mode = SATA_OOB_MODE;
85 static inline void asd_phy_event_tasklet(struct asd_ascb *ascb,
86 struct done_list_struct *dl)
88 struct asd_ha_struct *asd_ha = ascb->ha;
89 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
90 int phy_id = dl->status_block[0] & DL_PHY_MASK;
91 struct asd_phy *phy = &asd_ha->phys[phy_id];
93 u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
94 u8 oob_mode = dl->status_block[2];
97 case CURRENT_LOSS_OF_SIGNAL:
98 /* directly attached device was removed */
99 ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
100 asd_turn_led(asd_ha, phy_id, 0);
101 sas_phy_disconnected(&phy->sas_phy);
102 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
104 case CURRENT_OOB_DONE:
105 /* hot plugged device */
106 asd_turn_led(asd_ha, phy_id, 1);
107 get_lrate_mode(phy, oob_mode);
108 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
109 phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
110 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
112 case CURRENT_SPINUP_HOLD:
113 /* hot plug SATA, no COMWAKE sent */
114 asd_turn_led(asd_ha, phy_id, 1);
115 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
117 case CURRENT_GTO_TIMEOUT:
118 case CURRENT_OOB_ERROR:
119 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
120 dl->status_block[1]);
121 asd_turn_led(asd_ha, phy_id, 0);
122 sas_phy_disconnected(&phy->sas_phy);
123 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
128 /* If phys are enabled sparsely, this will do the right thing. */
129 static inline unsigned ord_phy(struct asd_ha_struct *asd_ha,
132 u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
135 for_each_phy(enabled_mask, enabled_mask, i) {
136 if (&asd_ha->phys[i] == phy)
144 * asd_get_attached_sas_addr -- extract/generate attached SAS address
145 * phy: pointer to asd_phy
146 * sas_addr: pointer to buffer where the SAS address is to be written
148 * This function extracts the SAS address from an IDENTIFY frame
149 * received. If OOB is SATA, then a SAS address is generated from the
152 * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
155 static inline void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
157 if (phy->sas_phy.frame_rcvd[0] == 0x34
158 && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
159 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
160 /* FIS device-to-host */
161 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
163 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
164 *(__be64 *)sas_addr = cpu_to_be64(addr);
166 struct sas_identify_frame *idframe =
167 (void *) phy->sas_phy.frame_rcvd;
168 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
172 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
175 struct asd_port *free_port = NULL;
176 struct asd_port *port;
177 struct asd_sas_phy *sas_phy = &phy->sas_phy;
180 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
181 if (!phy->asd_port) {
182 for (i = 0; i < ASD_MAX_PHYS; i++) {
183 port = &asd_ha->asd_ports[i];
185 /* Check for wide port */
186 if (port->num_phys > 0 &&
187 memcmp(port->sas_addr, sas_phy->sas_addr,
188 SAS_ADDR_SIZE) == 0 &&
189 memcmp(port->attached_sas_addr,
190 sas_phy->attached_sas_addr,
191 SAS_ADDR_SIZE) == 0) {
195 /* Find a free port */
196 if (port->num_phys == 0 && free_port == NULL) {
201 /* Use a free port if this doesn't form a wide port */
202 if (i >= ASD_MAX_PHYS) {
205 memcpy(port->sas_addr, sas_phy->sas_addr,
207 memcpy(port->attached_sas_addr,
208 sas_phy->attached_sas_addr,
212 port->phy_mask |= (1U << sas_phy->id);
213 phy->asd_port = port;
215 ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
216 __FUNCTION__, phy->asd_port->phy_mask, sas_phy->id);
217 asd_update_port_links(asd_ha, phy);
218 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
221 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
223 struct asd_port *port = phy->asd_port;
224 struct asd_sas_phy *sas_phy = &phy->sas_phy;
227 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
230 port->phy_mask &= ~(1U << sas_phy->id);
231 phy->asd_port = NULL;
233 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
236 static inline void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
237 struct done_list_struct *dl,
238 int edb_id, int phy_id)
241 int edb_el = edb_id + ascb->edb_index;
242 struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
243 struct asd_phy *phy = &ascb->ha->phys[phy_id];
244 struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
245 u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
247 size = min(size, (u16) sizeof(phy->frame_rcvd));
249 spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
250 memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
251 phy->sas_phy.frame_rcvd_size = size;
252 asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
253 spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
254 asd_dump_frame_rcvd(phy, dl);
255 asd_form_port(ascb->ha, phy);
256 sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
259 static inline void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
260 struct done_list_struct *dl,
263 struct asd_ha_struct *asd_ha = ascb->ha;
264 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
265 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
266 struct asd_phy *phy = &asd_ha->phys[phy_id];
267 u8 lr_error = dl->status_block[1];
268 u8 retries_left = dl->status_block[2];
272 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
275 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
278 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
281 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
284 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
289 asd_turn_led(asd_ha, phy_id, 0);
290 sas_phy_disconnected(sas_phy);
291 asd_deform_port(asd_ha, phy);
292 sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
294 if (retries_left == 0) {
296 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
299 asd_printk("%s: out of memory\n", __FUNCTION__);
302 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
304 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
305 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
312 static inline void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
313 struct done_list_struct *dl,
317 struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
318 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
319 struct asd_ha_struct *asd_ha = ascb->ha;
320 struct asd_phy *phy = &asd_ha->phys[phy_id];
321 u8 reg = dl->status_block[1];
322 u32 cont = dl->status_block[2] << ((reg & 3)*8);
326 case LmPRMSTAT0BYTE0:
332 ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
334 spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
335 sas_phy->sas_prim = ffs(cont);
336 spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
337 sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
341 ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
345 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
350 case LmPRMSTAT1BYTE0:
353 ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
355 /* The sequencer disables all phys on that port.
356 * We have to re-enable the phys ourselves. */
357 asd_deform_port(asd_ha, phy);
358 sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
362 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
368 ASD_DPRINTK("unknown primitive register:0x%x\n",
369 dl->status_block[1]);
375 * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
376 * @ascb: pointer to Empty SCB
377 * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
379 * After an EDB has been invalidated, if all EDBs in this ESCB have been
380 * invalidated, the ESCB is posted back to the sequencer.
381 * Context is tasklet/IRQ.
383 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
385 struct asd_seq_data *seq = &ascb->ha->seq;
386 struct empty_scb *escb = &ascb->scb->escb;
387 struct sg_el *eb = &escb->eb[edb_id];
388 struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
390 memset(edb->vaddr, 0, ASD_EDB_SIZE);
391 eb->flags |= ELEMENT_NOT_VALID;
394 if (escb->num_valid == 0) {
396 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
397 "dma_handle: 0x%08llx, next: 0x%08llx, "
398 "index:%d, opcode:0x%02x\n",
400 (u64)ascb->dma_scb.dma_handle,
401 le64_to_cpu(ascb->scb->header.next_scb),
402 le16_to_cpu(ascb->scb->header.index),
403 ascb->scb->header.opcode);
405 escb->num_valid = ASD_EDBS_PER_SCB;
406 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
407 escb->eb[i].flags = 0;
408 if (!list_empty(&ascb->list))
409 list_del_init(&ascb->list);
410 i = asd_post_escb_list(ascb->ha, ascb, 1);
412 asd_printk("couldn't post escb, err:%d\n", i);
416 /* hard reset a phy later */
417 static void do_phy_reset_later(void *data)
419 struct sas_phy *sas_phy = data;
422 ASD_DPRINTK("%s: About to hard reset phy %d\n", __FUNCTION__,
423 sas_phy->identify.phy_identifier);
424 /* Reset device port */
425 error = sas_phy_reset(sas_phy, 1);
427 ASD_DPRINTK("%s: Hard reset of phy %d failed (%d).\n",
428 __FUNCTION__, sas_phy->identify.phy_identifier, error);
431 static void phy_reset_later(struct sas_phy *sas_phy, struct Scsi_Host *shost)
433 INIT_WORK(&sas_phy->reset_work, do_phy_reset_later, sas_phy);
434 queue_work(shost->work_q, &sas_phy->reset_work);
437 /* start up the ABORT TASK tmf... */
438 static void task_kill_later(struct asd_ascb *ascb)
440 struct asd_ha_struct *asd_ha = ascb->ha;
441 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
442 struct Scsi_Host *shost = sas_ha->core.shost;
443 struct sas_task *task = ascb->uldd_task;
445 INIT_WORK(&task->abort_work, (void (*)(void *))sas_task_abort, task);
446 queue_work(shost->work_q, &task->abort_work);
449 static void escb_tasklet_complete(struct asd_ascb *ascb,
450 struct done_list_struct *dl)
452 struct asd_ha_struct *asd_ha = ascb->ha;
453 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
454 int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
455 u8 sb_opcode = dl->status_block[0];
456 int phy_id = sb_opcode & DL_PHY_MASK;
457 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
458 struct asd_phy *phy = &asd_ha->phys[phy_id];
460 if (edb > 6 || edb < 0) {
461 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
463 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
465 ASD_DPRINTK("escb: vaddr: 0x%p, "
466 "dma_handle: 0x%llx, next: 0x%llx, "
467 "index:%d, opcode:0x%02x\n",
469 (unsigned long long)ascb->dma_scb.dma_handle,
471 le64_to_cpu(ascb->scb->header.next_scb),
472 le16_to_cpu(ascb->scb->header.index),
473 ascb->scb->header.opcode);
476 /* Catch these before we mask off the sb_opcode bits */
478 case REQ_TASK_ABORT: {
479 struct asd_ascb *a, *b;
482 tc_abort = *((u16*)(&dl->status_block[1]));
483 tc_abort = le16_to_cpu(tc_abort);
485 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
486 __FUNCTION__, dl->status_block[3]);
488 /* Find the pending task and abort it. */
489 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list)
490 if (a->tc_index == tc_abort) {
496 case REQ_DEVICE_RESET: {
497 struct Scsi_Host *shost = sas_ha->core.shost;
498 struct sas_phy *dev_phy;
502 conn_handle = *((u16*)(&dl->status_block[1]));
503 conn_handle = le16_to_cpu(conn_handle);
505 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __FUNCTION__,
506 dl->status_block[3]);
508 /* Kill all pending tasks and reset the device */
510 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
511 struct sas_task *task;
512 struct domain_device *dev;
520 x = (unsigned long)dev->lldd_dev;
521 if (x == conn_handle) {
522 dev_phy = dev->port->phy;
527 /* Reset device port */
529 ASD_DPRINTK("%s: No pending commands; can't reset.\n",
533 phy_reset_later(dev_phy, shost);
536 case SIGNAL_NCQ_ERROR:
537 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __FUNCTION__);
539 case CLEAR_NCQ_ERROR:
540 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __FUNCTION__);
544 sb_opcode &= ~DL_PHY_MASK;
548 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __FUNCTION__, phy_id);
549 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
551 case PRIMITIVE_RECVD:
552 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __FUNCTION__,
554 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
557 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __FUNCTION__, phy_id);
558 asd_phy_event_tasklet(ascb, dl);
560 case LINK_RESET_ERROR:
561 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __FUNCTION__,
563 asd_link_reset_err_tasklet(ascb, dl, phy_id);
566 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
567 __FUNCTION__, phy_id);
568 asd_turn_led(asd_ha, phy_id, 0);
569 /* the device is gone */
570 sas_phy_disconnected(sas_phy);
571 asd_deform_port(asd_ha, phy);
572 sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
575 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __FUNCTION__,
577 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
579 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
581 ASD_DPRINTK("escb: vaddr: 0x%p, "
582 "dma_handle: 0x%llx, next: 0x%llx, "
583 "index:%d, opcode:0x%02x\n",
585 (unsigned long long)ascb->dma_scb.dma_handle,
587 le64_to_cpu(ascb->scb->header.next_scb),
588 le16_to_cpu(ascb->scb->header.index),
589 ascb->scb->header.opcode);
594 asd_invalidate_edb(ascb, edb);
597 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
599 struct asd_seq_data *seq = &asd_ha->seq;
602 for (i = 0; i < seq->num_escbs; i++)
603 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
605 ASD_DPRINTK("posting %d escbs\n", i);
606 return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
609 /* ---------- CONTROL PHY ---------- */
611 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
612 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
616 * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
617 * @ascb: pointer to an ascb
618 * @dl: pointer to the done list entry
620 * This function completes a CONTROL PHY scb and frees the ascb.
622 * - an LED blinks if there is IO though it,
623 * - if a device is connected to the LED, it is lit,
624 * - if no device is connected to the LED, is is dimmed (off).
626 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
627 struct done_list_struct *dl)
629 struct asd_ha_struct *asd_ha = ascb->ha;
630 struct scb *scb = ascb->scb;
631 struct control_phy *control_phy = &scb->control_phy;
632 u8 phy_id = control_phy->phy_id;
633 struct asd_phy *phy = &ascb->ha->phys[phy_id];
635 u8 status = dl->status_block[0];
636 u8 oob_status = dl->status_block[1];
637 u8 oob_mode = dl->status_block[2];
638 /* u8 oob_signals= dl->status_block[3]; */
641 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
642 __FUNCTION__, phy_id, status);
646 switch (control_phy->sub_func) {
648 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
649 asd_turn_led(asd_ha, phy_id, 0);
650 asd_control_led(asd_ha, phy_id, 0);
651 ASD_DPRINTK("%s: disable phy%d\n", __FUNCTION__, phy_id);
655 asd_control_led(asd_ha, phy_id, 1);
656 if (oob_status & CURRENT_OOB_DONE) {
657 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
658 get_lrate_mode(phy, oob_mode);
659 asd_turn_led(asd_ha, phy_id, 1);
660 ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
661 __FUNCTION__, phy_id,phy->sas_phy.linkrate,
662 phy->sas_phy.iproto);
663 } else if (oob_status & CURRENT_SPINUP_HOLD) {
664 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
665 asd_turn_led(asd_ha, phy_id, 1);
666 ASD_DPRINTK("%s: phy%d, spinup hold\n", __FUNCTION__,
668 } else if (oob_status & CURRENT_ERR_MASK) {
669 asd_turn_led(asd_ha, phy_id, 0);
670 ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
671 __FUNCTION__, phy_id, oob_status);
672 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
673 | CURRENT_DEVICE_PRESENT)) {
674 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
675 asd_turn_led(asd_ha, phy_id, 1);
676 ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
677 __FUNCTION__, phy_id);
679 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
680 asd_turn_led(asd_ha, phy_id, 0);
681 ASD_DPRINTK("%s: phy%d: no device present: "
683 __FUNCTION__, phy_id, oob_status);
686 case RELEASE_SPINUP_HOLD:
688 case EXECUTE_HARD_RESET:
689 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __FUNCTION__,
690 phy_id, control_phy->sub_func);
694 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __FUNCTION__,
695 phy_id, control_phy->sub_func);
702 static inline void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
704 /* disable all speeds, then enable defaults */
705 *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
706 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
708 switch (pd->max_sas_lrate) {
709 case SAS_LINK_RATE_6_0_GBPS:
710 *speed_mask &= ~SAS_SPEED_60_DIS;
712 case SAS_LINK_RATE_3_0_GBPS:
713 *speed_mask &= ~SAS_SPEED_30_DIS;
714 case SAS_LINK_RATE_1_5_GBPS:
715 *speed_mask &= ~SAS_SPEED_15_DIS;
718 switch (pd->min_sas_lrate) {
719 case SAS_LINK_RATE_6_0_GBPS:
720 *speed_mask |= SAS_SPEED_30_DIS;
721 case SAS_LINK_RATE_3_0_GBPS:
722 *speed_mask |= SAS_SPEED_15_DIS;
724 case SAS_LINK_RATE_1_5_GBPS:
729 switch (pd->max_sata_lrate) {
730 case SAS_LINK_RATE_3_0_GBPS:
731 *speed_mask &= ~SATA_SPEED_30_DIS;
733 case SAS_LINK_RATE_1_5_GBPS:
734 *speed_mask &= ~SATA_SPEED_15_DIS;
737 switch (pd->min_sata_lrate) {
738 case SAS_LINK_RATE_3_0_GBPS:
739 *speed_mask |= SATA_SPEED_15_DIS;
741 case SAS_LINK_RATE_1_5_GBPS:
748 * asd_build_control_phy -- build a CONTROL PHY SCB
749 * @ascb: pointer to an ascb
750 * @phy_id: phy id to control, integer
751 * @subfunc: subfunction, what to actually to do the phy
753 * This function builds a CONTROL PHY scb. No allocation of any kind
754 * is performed. @ascb is allocated with the list function.
755 * The caller can override the ascb->tasklet_complete to point
756 * to its own callback function. It must call asd_ascb_free()
757 * at its tasklet complete function.
758 * See the default implementation.
760 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
762 struct asd_phy *phy = &ascb->ha->phys[phy_id];
763 struct scb *scb = ascb->scb;
764 struct control_phy *control_phy = &scb->control_phy;
766 scb->header.opcode = CONTROL_PHY;
767 control_phy->phy_id = (u8) phy_id;
768 control_phy->sub_func = subfunc;
771 case EXECUTE_HARD_RESET: /* 0x81 */
772 case ENABLE_PHY: /* 0x01 */
773 /* decide hot plug delay */
774 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
776 /* decide speed mask */
777 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
779 /* initiator port settings are in the hi nibble */
780 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
781 control_phy->port_type = SAS_PROTO_ALL << 4;
782 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
783 control_phy->port_type = SAS_PROTO_ALL;
785 control_phy->port_type =
786 (SAS_PROTO_ALL << 4) | SAS_PROTO_ALL;
788 /* link reset retries, this should be nominal */
789 control_phy->link_reset_retries = 10;
791 case RELEASE_SPINUP_HOLD: /* 0x02 */
792 /* decide the func_mask */
793 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
794 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
795 control_phy->func_mask &= ~SPINUP_HOLD_DIS;
797 control_phy->func_mask |= SPINUP_HOLD_DIS;
800 control_phy->conn_handle = cpu_to_le16(0xFFFF);
802 ascb->tasklet_complete = control_phy_tasklet_complete;
805 /* ---------- INITIATE LINK ADM TASK ---------- */
807 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
808 struct done_list_struct *dl)
810 u8 opcode = dl->opcode;
811 struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
812 u8 phy_id = link_adm->phy_id;
814 if (opcode != TC_NO_ERROR) {
815 asd_printk("phy%d: link adm task 0x%x completed with error "
816 "0x%x\n", phy_id, link_adm->sub_func, opcode);
818 ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
819 phy_id, link_adm->sub_func, opcode);
824 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
827 struct scb *scb = ascb->scb;
828 struct initiate_link_adm *link_adm = &scb->link_adm;
830 scb->header.opcode = INITIATE_LINK_ADM_TASK;
832 link_adm->phy_id = phy_id;
833 link_adm->sub_func = subfunc;
834 link_adm->conn_handle = cpu_to_le16(0xFFFF);
836 ascb->tasklet_complete = link_adm_tasklet_complete;
839 /* ---------- SCB timer ---------- */
842 * asd_ascb_timedout -- called when a pending SCB's timer has expired
843 * @data: unsigned long, a pointer to the ascb in question
845 * This is the default timeout function which does the most necessary.
846 * Upper layers can implement their own timeout function, say to free
847 * resources they have with this SCB, and then call this one at the
848 * end of their timeout function. To do this, one should initialize
849 * the ascb->timer.{function, data, expires} prior to calling the post
850 * funcion. The timer is started by the post function.
852 void asd_ascb_timedout(unsigned long data)
854 struct asd_ascb *ascb = (void *) data;
855 struct asd_seq_data *seq = &ascb->ha->seq;
858 ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
860 spin_lock_irqsave(&seq->pend_q_lock, flags);
862 list_del_init(&ascb->list);
863 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
868 /* ---------- CONTROL PHY ---------- */
870 /* Given the spec value, return a driver value. */
871 static const int phy_func_table[] = {
872 [PHY_FUNC_NOP] = PHY_NO_OP,
873 [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
874 [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
875 [PHY_FUNC_DISABLE] = DISABLE_PHY,
876 [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
879 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
881 struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
882 struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
883 struct asd_ascb *ascb;
884 struct sas_phy_linkrates *rates;
888 case PHY_FUNC_CLEAR_ERROR_LOG:
890 case PHY_FUNC_SET_LINK_RATE:
892 if (rates->minimum_linkrate) {
893 pd->min_sas_lrate = rates->minimum_linkrate;
894 pd->min_sata_lrate = rates->minimum_linkrate;
896 if (rates->maximum_linkrate) {
897 pd->max_sas_lrate = rates->maximum_linkrate;
898 pd->max_sata_lrate = rates->maximum_linkrate;
900 func = PHY_FUNC_LINK_RESET;
906 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
910 asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
911 res = asd_post_ascb_list(asd_ha, ascb , 1);