1 // SPDX-License-Identifier: GPL-2.0-only
3 * Aic94xx SAS/SATA driver SCB management.
5 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
6 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
10 #include <scsi/scsi_host.h>
13 #include "aic94xx_reg.h"
14 #include "aic94xx_hwi.h"
15 #include "aic94xx_seq.h"
17 #include "aic94xx_dump.h"
19 /* ---------- EMPTY SCB ---------- */
23 #define PRIMITIVE_RECVD 0x08
24 #define PHY_EVENT 0x10
25 #define LINK_RESET_ERROR 0x18
26 #define TIMER_EVENT 0x20
27 #define REQ_TASK_ABORT 0xF0
28 #define REQ_DEVICE_RESET 0xF1
29 #define SIGNAL_NCQ_ERROR 0xF2
30 #define CLEAR_NCQ_ERROR 0xF3
32 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
33 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
36 static void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
38 struct sas_phy *sas_phy = phy->sas_phy.phy;
40 switch (oob_mode & 7) {
42 /* FIXME: sas transport class doesn't have this */
43 phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
44 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
47 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
48 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
51 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
52 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
55 sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
56 sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
57 sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
58 sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
59 sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
61 if (oob_mode & SAS_MODE)
62 phy->sas_phy.oob_mode = SAS_OOB_MODE;
63 else if (oob_mode & SATA_MODE)
64 phy->sas_phy.oob_mode = SATA_OOB_MODE;
67 static void asd_phy_event_tasklet(struct asd_ascb *ascb,
68 struct done_list_struct *dl)
70 struct asd_ha_struct *asd_ha = ascb->ha;
71 int phy_id = dl->status_block[0] & DL_PHY_MASK;
72 struct asd_phy *phy = &asd_ha->phys[phy_id];
74 u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
75 u8 oob_mode = dl->status_block[2];
78 case CURRENT_LOSS_OF_SIGNAL:
79 /* directly attached device was removed */
80 ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
81 asd_turn_led(asd_ha, phy_id, 0);
82 sas_phy_disconnected(&phy->sas_phy);
83 sas_notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
85 case CURRENT_OOB_DONE:
86 /* hot plugged device */
87 asd_turn_led(asd_ha, phy_id, 1);
88 get_lrate_mode(phy, oob_mode);
89 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
90 phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
91 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
93 case CURRENT_SPINUP_HOLD:
94 /* hot plug SATA, no COMWAKE sent */
95 asd_turn_led(asd_ha, phy_id, 1);
96 sas_notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
98 case CURRENT_GTO_TIMEOUT:
99 case CURRENT_OOB_ERROR:
100 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
101 dl->status_block[1]);
102 asd_turn_led(asd_ha, phy_id, 0);
103 sas_phy_disconnected(&phy->sas_phy);
104 sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
109 /* If phys are enabled sparsely, this will do the right thing. */
110 static unsigned ord_phy(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
112 u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
115 for_each_phy(enabled_mask, enabled_mask, i) {
116 if (&asd_ha->phys[i] == phy)
124 * asd_get_attached_sas_addr -- extract/generate attached SAS address
125 * @phy: pointer to asd_phy
126 * @sas_addr: pointer to buffer where the SAS address is to be written
128 * This function extracts the SAS address from an IDENTIFY frame
129 * received. If OOB is SATA, then a SAS address is generated from the
132 * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
135 static void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
137 if (phy->sas_phy.frame_rcvd[0] == 0x34
138 && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
139 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
140 /* FIS device-to-host */
141 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
143 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
144 *(__be64 *)sas_addr = cpu_to_be64(addr);
146 struct sas_identify_frame *idframe =
147 (void *) phy->sas_phy.frame_rcvd;
148 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
152 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
155 struct asd_port *free_port = NULL;
156 struct asd_port *port;
157 struct asd_sas_phy *sas_phy = &phy->sas_phy;
160 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
161 if (!phy->asd_port) {
162 for (i = 0; i < ASD_MAX_PHYS; i++) {
163 port = &asd_ha->asd_ports[i];
165 /* Check for wide port */
166 if (port->num_phys > 0 &&
167 memcmp(port->sas_addr, sas_phy->sas_addr,
168 SAS_ADDR_SIZE) == 0 &&
169 memcmp(port->attached_sas_addr,
170 sas_phy->attached_sas_addr,
171 SAS_ADDR_SIZE) == 0) {
175 /* Find a free port */
176 if (port->num_phys == 0 && free_port == NULL) {
181 /* Use a free port if this doesn't form a wide port */
182 if (i >= ASD_MAX_PHYS) {
185 memcpy(port->sas_addr, sas_phy->sas_addr,
187 memcpy(port->attached_sas_addr,
188 sas_phy->attached_sas_addr,
192 port->phy_mask |= (1U << sas_phy->id);
193 phy->asd_port = port;
195 ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
196 __func__, phy->asd_port->phy_mask, sas_phy->id);
197 asd_update_port_links(asd_ha, phy);
198 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
201 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
203 struct asd_port *port = phy->asd_port;
204 struct asd_sas_phy *sas_phy = &phy->sas_phy;
207 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
210 port->phy_mask &= ~(1U << sas_phy->id);
211 phy->asd_port = NULL;
213 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
216 static void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
217 struct done_list_struct *dl,
218 int edb_id, int phy_id)
221 int edb_el = edb_id + ascb->edb_index;
222 struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
223 struct asd_phy *phy = &ascb->ha->phys[phy_id];
224 u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
226 size = min(size, (u16) sizeof(phy->frame_rcvd));
228 spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
229 memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
230 phy->sas_phy.frame_rcvd_size = size;
231 asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
232 spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
233 asd_dump_frame_rcvd(phy, dl);
234 asd_form_port(ascb->ha, phy);
235 sas_notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
238 static void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
239 struct done_list_struct *dl,
242 struct asd_ha_struct *asd_ha = ascb->ha;
243 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
244 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
245 struct asd_phy *phy = &asd_ha->phys[phy_id];
246 u8 lr_error = dl->status_block[1];
247 u8 retries_left = dl->status_block[2];
251 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
254 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
257 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
260 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
263 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
268 asd_turn_led(asd_ha, phy_id, 0);
269 sas_phy_disconnected(sas_phy);
270 asd_deform_port(asd_ha, phy);
271 sas_notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
273 if (retries_left == 0) {
275 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
278 asd_printk("%s: out of memory\n", __func__);
281 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
283 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
284 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
291 static void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
292 struct done_list_struct *dl,
296 struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
297 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
298 struct asd_ha_struct *asd_ha = ascb->ha;
299 struct asd_phy *phy = &asd_ha->phys[phy_id];
300 u8 reg = dl->status_block[1];
301 u32 cont = dl->status_block[2] << ((reg & 3)*8);
305 case LmPRMSTAT0BYTE0:
311 ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
313 spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
314 sas_phy->sas_prim = ffs(cont);
315 spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
316 sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD);
320 ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
324 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
329 case LmPRMSTAT1BYTE0:
332 ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
334 /* The sequencer disables all phys on that port.
335 * We have to re-enable the phys ourselves. */
336 asd_deform_port(asd_ha, phy);
337 sas_notify_port_event(sas_phy, PORTE_HARD_RESET);
341 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
347 ASD_DPRINTK("unknown primitive register:0x%x\n",
348 dl->status_block[1]);
354 * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
355 * @ascb: pointer to Empty SCB
356 * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
358 * After an EDB has been invalidated, if all EDBs in this ESCB have been
359 * invalidated, the ESCB is posted back to the sequencer.
360 * Context is tasklet/IRQ.
362 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
364 struct asd_seq_data *seq = &ascb->ha->seq;
365 struct empty_scb *escb = &ascb->scb->escb;
366 struct sg_el *eb = &escb->eb[edb_id];
367 struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
369 memset(edb->vaddr, 0, ASD_EDB_SIZE);
370 eb->flags |= ELEMENT_NOT_VALID;
373 if (escb->num_valid == 0) {
375 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
376 "dma_handle: 0x%08llx, next: 0x%08llx, "
377 "index:%d, opcode:0x%02x\n",
379 (u64)ascb->dma_scb.dma_handle,
380 le64_to_cpu(ascb->scb->header.next_scb),
381 le16_to_cpu(ascb->scb->header.index),
382 ascb->scb->header.opcode);
384 escb->num_valid = ASD_EDBS_PER_SCB;
385 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
386 escb->eb[i].flags = 0;
387 if (!list_empty(&ascb->list))
388 list_del_init(&ascb->list);
389 i = asd_post_escb_list(ascb->ha, ascb, 1);
391 asd_printk("couldn't post escb, err:%d\n", i);
395 static void escb_tasklet_complete(struct asd_ascb *ascb,
396 struct done_list_struct *dl)
398 struct asd_ha_struct *asd_ha = ascb->ha;
399 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
400 int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
401 u8 sb_opcode = dl->status_block[0];
402 int phy_id = sb_opcode & DL_PHY_MASK;
403 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
404 struct asd_phy *phy = &asd_ha->phys[phy_id];
406 if (edb > 6 || edb < 0) {
407 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
409 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
411 ASD_DPRINTK("escb: vaddr: 0x%p, "
412 "dma_handle: 0x%llx, next: 0x%llx, "
413 "index:%d, opcode:0x%02x\n",
415 (unsigned long long)ascb->dma_scb.dma_handle,
417 le64_to_cpu(ascb->scb->header.next_scb),
418 le16_to_cpu(ascb->scb->header.index),
419 ascb->scb->header.opcode);
422 /* Catch these before we mask off the sb_opcode bits */
424 case REQ_TASK_ABORT: {
425 struct asd_ascb *a, *b;
427 struct domain_device *failed_dev = NULL;
429 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
430 __func__, dl->status_block[3]);
433 * Find the task that caused the abort and abort it first.
434 * The sequencer won't put anything on the done list until
437 tc_abort = *((u16*)(&dl->status_block[1]));
438 tc_abort = le16_to_cpu(tc_abort);
440 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
441 struct sas_task *task = a->uldd_task;
443 if (a->tc_index != tc_abort)
447 failed_dev = task->dev;
448 sas_task_abort(task);
450 ASD_DPRINTK("R_T_A for non TASK scb 0x%x\n",
451 a->scb->header.opcode);
457 ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
463 * Now abort everything else for that device (hba?) so
464 * that the EH will wake up and do something.
466 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
467 struct sas_task *task = a->uldd_task;
470 task->dev == failed_dev &&
471 a->tc_index != tc_abort)
472 sas_task_abort(task);
477 case REQ_DEVICE_RESET: {
481 struct sas_task *last_dev_task = NULL;
483 conn_handle = *((u16*)(&dl->status_block[1]));
484 conn_handle = le16_to_cpu(conn_handle);
486 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __func__,
487 dl->status_block[3]);
489 /* Find the last pending task for the device... */
490 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
492 struct domain_device *dev;
493 struct sas_task *task = a->uldd_task;
499 x = (unsigned long)dev->lldd_dev;
500 if (x == conn_handle)
501 last_dev_task = task;
504 if (!last_dev_task) {
505 ASD_DPRINTK("%s: Device reset for idle device %d?\n",
506 __func__, conn_handle);
510 /* ...and set the reset flag */
511 spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
512 last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
513 spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
515 /* Kill all pending tasks for the device */
516 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
518 struct domain_device *dev;
519 struct sas_task *task = a->uldd_task;
525 x = (unsigned long)dev->lldd_dev;
526 if (x == conn_handle)
527 sas_task_abort(task);
532 case SIGNAL_NCQ_ERROR:
533 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __func__);
535 case CLEAR_NCQ_ERROR:
536 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __func__);
540 sb_opcode &= ~DL_PHY_MASK;
544 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __func__, phy_id);
545 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
547 case PRIMITIVE_RECVD:
548 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __func__,
550 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
553 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __func__, phy_id);
554 asd_phy_event_tasklet(ascb, dl);
556 case LINK_RESET_ERROR:
557 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __func__,
559 asd_link_reset_err_tasklet(ascb, dl, phy_id);
562 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
564 asd_turn_led(asd_ha, phy_id, 0);
565 /* the device is gone */
566 sas_phy_disconnected(sas_phy);
567 asd_deform_port(asd_ha, phy);
568 sas_notify_port_event(sas_phy, PORTE_TIMER_EVENT);
571 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __func__,
573 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
575 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
577 ASD_DPRINTK("escb: vaddr: 0x%p, "
578 "dma_handle: 0x%llx, next: 0x%llx, "
579 "index:%d, opcode:0x%02x\n",
581 (unsigned long long)ascb->dma_scb.dma_handle,
583 le64_to_cpu(ascb->scb->header.next_scb),
584 le16_to_cpu(ascb->scb->header.index),
585 ascb->scb->header.opcode);
590 asd_invalidate_edb(ascb, edb);
593 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
595 struct asd_seq_data *seq = &asd_ha->seq;
598 for (i = 0; i < seq->num_escbs; i++)
599 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
601 ASD_DPRINTK("posting %d escbs\n", i);
602 return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
605 /* ---------- CONTROL PHY ---------- */
607 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
608 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
612 * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
613 * @ascb: pointer to an ascb
614 * @dl: pointer to the done list entry
616 * This function completes a CONTROL PHY scb and frees the ascb.
618 * - an LED blinks if there is IO though it,
619 * - if a device is connected to the LED, it is lit,
620 * - if no device is connected to the LED, is is dimmed (off).
622 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
623 struct done_list_struct *dl)
625 struct asd_ha_struct *asd_ha = ascb->ha;
626 struct scb *scb = ascb->scb;
627 struct control_phy *control_phy = &scb->control_phy;
628 u8 phy_id = control_phy->phy_id;
629 struct asd_phy *phy = &ascb->ha->phys[phy_id];
631 u8 status = dl->status_block[0];
632 u8 oob_status = dl->status_block[1];
633 u8 oob_mode = dl->status_block[2];
634 /* u8 oob_signals= dl->status_block[3]; */
637 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
638 __func__, phy_id, status);
642 switch (control_phy->sub_func) {
644 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
645 asd_turn_led(asd_ha, phy_id, 0);
646 asd_control_led(asd_ha, phy_id, 0);
647 ASD_DPRINTK("%s: disable phy%d\n", __func__, phy_id);
651 asd_control_led(asd_ha, phy_id, 1);
652 if (oob_status & CURRENT_OOB_DONE) {
653 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
654 get_lrate_mode(phy, oob_mode);
655 asd_turn_led(asd_ha, phy_id, 1);
656 ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
657 __func__, phy_id,phy->sas_phy.linkrate,
658 phy->sas_phy.iproto);
659 } else if (oob_status & CURRENT_SPINUP_HOLD) {
660 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
661 asd_turn_led(asd_ha, phy_id, 1);
662 ASD_DPRINTK("%s: phy%d, spinup hold\n", __func__,
664 } else if (oob_status & CURRENT_ERR_MASK) {
665 asd_turn_led(asd_ha, phy_id, 0);
666 ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
667 __func__, phy_id, oob_status);
668 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
669 | CURRENT_DEVICE_PRESENT)) {
670 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
671 asd_turn_led(asd_ha, phy_id, 1);
672 ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
675 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
676 asd_turn_led(asd_ha, phy_id, 0);
677 ASD_DPRINTK("%s: phy%d: no device present: "
679 __func__, phy_id, oob_status);
682 case RELEASE_SPINUP_HOLD:
684 case EXECUTE_HARD_RESET:
685 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __func__,
686 phy_id, control_phy->sub_func);
690 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __func__,
691 phy_id, control_phy->sub_func);
698 static void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
700 /* disable all speeds, then enable defaults */
701 *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
702 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
704 switch (pd->max_sas_lrate) {
705 case SAS_LINK_RATE_6_0_GBPS:
706 *speed_mask &= ~SAS_SPEED_60_DIS;
709 case SAS_LINK_RATE_3_0_GBPS:
710 *speed_mask &= ~SAS_SPEED_30_DIS;
712 case SAS_LINK_RATE_1_5_GBPS:
713 *speed_mask &= ~SAS_SPEED_15_DIS;
716 switch (pd->min_sas_lrate) {
717 case SAS_LINK_RATE_6_0_GBPS:
718 *speed_mask |= SAS_SPEED_30_DIS;
720 case SAS_LINK_RATE_3_0_GBPS:
721 *speed_mask |= SAS_SPEED_15_DIS;
723 case SAS_LINK_RATE_1_5_GBPS:
728 switch (pd->max_sata_lrate) {
729 case SAS_LINK_RATE_3_0_GBPS:
730 *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_PROTOCOL_ALL << 4;
782 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
783 control_phy->port_type = SAS_PROTOCOL_ALL;
785 control_phy->port_type =
786 (SAS_PROTOCOL_ALL << 4) | SAS_PROTOCOL_ALL;
788 /* link reset retries, this should be nominal */
789 control_phy->link_reset_retries = 10;
792 case RELEASE_SPINUP_HOLD: /* 0x02 */
793 /* decide the func_mask */
794 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
795 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
796 control_phy->func_mask &= ~SPINUP_HOLD_DIS;
798 control_phy->func_mask |= SPINUP_HOLD_DIS;
801 control_phy->conn_handle = cpu_to_le16(0xFFFF);
803 ascb->tasklet_complete = control_phy_tasklet_complete;
806 /* ---------- INITIATE LINK ADM TASK ---------- */
810 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
811 struct done_list_struct *dl)
813 u8 opcode = dl->opcode;
814 struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
815 u8 phy_id = link_adm->phy_id;
817 if (opcode != TC_NO_ERROR) {
818 asd_printk("phy%d: link adm task 0x%x completed with error "
819 "0x%x\n", phy_id, link_adm->sub_func, opcode);
821 ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
822 phy_id, link_adm->sub_func, opcode);
827 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
830 struct scb *scb = ascb->scb;
831 struct initiate_link_adm *link_adm = &scb->link_adm;
833 scb->header.opcode = INITIATE_LINK_ADM_TASK;
835 link_adm->phy_id = phy_id;
836 link_adm->sub_func = subfunc;
837 link_adm->conn_handle = cpu_to_le16(0xFFFF);
839 ascb->tasklet_complete = link_adm_tasklet_complete;
844 /* ---------- SCB timer ---------- */
847 * asd_ascb_timedout -- called when a pending SCB's timer has expired
848 * @t: Timer context used to fetch the SCB
850 * This is the default timeout function which does the most necessary.
851 * Upper layers can implement their own timeout function, say to free
852 * resources they have with this SCB, and then call this one at the
853 * end of their timeout function. To do this, one should initialize
854 * the ascb->timer.{function, expires} prior to calling the post
855 * function. The timer is started by the post function.
857 void asd_ascb_timedout(struct timer_list *t)
859 struct asd_ascb *ascb = from_timer(ascb, t, timer);
860 struct asd_seq_data *seq = &ascb->ha->seq;
863 ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
865 spin_lock_irqsave(&seq->pend_q_lock, flags);
867 list_del_init(&ascb->list);
868 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
873 /* ---------- CONTROL PHY ---------- */
875 /* Given the spec value, return a driver value. */
876 static const int phy_func_table[] = {
877 [PHY_FUNC_NOP] = PHY_NO_OP,
878 [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
879 [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
880 [PHY_FUNC_DISABLE] = DISABLE_PHY,
881 [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
884 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
886 struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
887 struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
888 struct asd_ascb *ascb;
889 struct sas_phy_linkrates *rates;
893 case PHY_FUNC_CLEAR_ERROR_LOG:
894 case PHY_FUNC_GET_EVENTS:
896 case PHY_FUNC_SET_LINK_RATE:
898 if (rates->minimum_linkrate) {
899 pd->min_sas_lrate = rates->minimum_linkrate;
900 pd->min_sata_lrate = rates->minimum_linkrate;
902 if (rates->maximum_linkrate) {
903 pd->max_sas_lrate = rates->maximum_linkrate;
904 pd->max_sata_lrate = rates->maximum_linkrate;
906 func = PHY_FUNC_LINK_RESET;
912 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
916 asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
917 res = asd_post_ascb_list(asd_ha, ascb , 1);