[SCSI] aic94xx: new driver
[pandora-kernel.git] / drivers / scsi / aic94xx / aic94xx_scb.c
1 /*
2  * Aic94xx SAS/SATA driver SCB management.
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This file is part of the aic94xx driver.
10  *
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
14  * License.
15  *
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.
20  *
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
24  *
25  */
26
27 #include <linux/pci.h>
28
29 #include "aic94xx.h"
30 #include "aic94xx_reg.h"
31 #include "aic94xx_hwi.h"
32 #include "aic94xx_seq.h"
33
34 #include "aic94xx_dump.h"
35
36 /* ---------- EMPTY SCB ---------- */
37
38 #define DL_PHY_MASK      7
39 #define BYTES_DMAED      0
40 #define PRIMITIVE_RECVD  0x08
41 #define PHY_EVENT        0x10
42 #define LINK_RESET_ERROR 0x18
43 #define TIMER_EVENT      0x20
44 #define REQ_TASK_ABORT   0xF0
45 #define REQ_DEVICE_RESET 0xF1
46 #define SIGNAL_NCQ_ERROR 0xF2
47 #define CLEAR_NCQ_ERROR  0xF3
48
49 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE   \
50                            | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
51                            | CURRENT_OOB_ERROR)
52
53 static inline void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
54 {
55         switch (oob_mode & 7) {
56         case PHY_SPEED_60:
57                 /* FIXME: sas transport class doesn't have this */
58                 phy->sas_phy.linkrate = PHY_LINKRATE_6;
59                 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
60                 break;
61         case PHY_SPEED_30:
62                 phy->sas_phy.linkrate = PHY_LINKRATE_3;
63                 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
64                 break;
65         case PHY_SPEED_15:
66                 phy->sas_phy.linkrate = PHY_LINKRATE_1_5;
67                 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
68                 break;
69         }
70         if (oob_mode & SAS_MODE)
71                 phy->sas_phy.oob_mode = SAS_OOB_MODE;
72         else if (oob_mode & SATA_MODE)
73                 phy->sas_phy.oob_mode = SATA_OOB_MODE;
74 }
75
76 static inline void asd_phy_event_tasklet(struct asd_ascb *ascb,
77                                          struct done_list_struct *dl)
78 {
79         struct asd_ha_struct *asd_ha = ascb->ha;
80         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
81         int phy_id = dl->status_block[0] & DL_PHY_MASK;
82         struct asd_phy *phy = &asd_ha->phys[phy_id];
83
84         u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
85         u8 oob_mode   = dl->status_block[2];
86
87         switch (oob_status) {
88         case CURRENT_LOSS_OF_SIGNAL:
89                 /* directly attached device was removed */
90                 ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
91                 asd_turn_led(asd_ha, phy_id, 0);
92                 sas_phy_disconnected(&phy->sas_phy);
93                 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
94                 break;
95         case CURRENT_OOB_DONE:
96                 /* hot plugged device */
97                 asd_turn_led(asd_ha, phy_id, 1);
98                 get_lrate_mode(phy, oob_mode);
99                 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
100                             phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
101                 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
102                 break;
103         case CURRENT_SPINUP_HOLD:
104                 /* hot plug SATA, no COMWAKE sent */
105                 asd_turn_led(asd_ha, phy_id, 1);
106                 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
107                 break;
108         case CURRENT_GTO_TIMEOUT:
109         case CURRENT_OOB_ERROR:
110                 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
111                             dl->status_block[1]);
112                 asd_turn_led(asd_ha, phy_id, 0);
113                 sas_phy_disconnected(&phy->sas_phy);
114                 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
115                 break;
116         }
117 }
118
119 /* If phys are enabled sparsely, this will do the right thing. */
120 static inline unsigned ord_phy(struct asd_ha_struct *asd_ha,
121                                struct asd_phy *phy)
122 {
123         u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
124         int i, k = 0;
125
126         for_each_phy(enabled_mask, enabled_mask, i) {
127                 if (&asd_ha->phys[i] == phy)
128                         return k;
129                 k++;
130         }
131         return 0;
132 }
133
134 /**
135  * asd_get_attached_sas_addr -- extract/generate attached SAS address
136  * phy: pointer to asd_phy
137  * sas_addr: pointer to buffer where the SAS address is to be written
138  *
139  * This function extracts the SAS address from an IDENTIFY frame
140  * received.  If OOB is SATA, then a SAS address is generated from the
141  * HA tables.
142  *
143  * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
144  * buffer.
145  */
146 static inline void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
147 {
148         if (phy->sas_phy.frame_rcvd[0] == 0x34
149             && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
150                 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
151                 /* FIS device-to-host */
152                 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
153
154                 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
155                 *(__be64 *)sas_addr = cpu_to_be64(addr);
156         } else {
157                 struct sas_identify_frame *idframe =
158                         (void *) phy->sas_phy.frame_rcvd;
159                 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
160         }
161 }
162
163 static inline void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
164                                            struct done_list_struct *dl,
165                                            int edb_id, int phy_id)
166 {
167         unsigned long flags;
168         int edb_el = edb_id + ascb->edb_index;
169         struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
170         struct asd_phy *phy = &ascb->ha->phys[phy_id];
171         struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
172         u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
173
174         size = min(size, (u16) sizeof(phy->frame_rcvd));
175
176         spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
177         memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
178         phy->sas_phy.frame_rcvd_size = size;
179         asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
180         spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
181         asd_dump_frame_rcvd(phy, dl);
182         sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
183 }
184
185 static inline void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
186                                               struct done_list_struct *dl,
187                                               int phy_id)
188 {
189         struct asd_ha_struct *asd_ha = ascb->ha;
190         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
191         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
192         u8 lr_error = dl->status_block[1];
193         u8 retries_left = dl->status_block[2];
194
195         switch (lr_error) {
196         case 0:
197                 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
198                 break;
199         case 1:
200                 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
201                 break;
202         case 2:
203                 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
204                 break;
205         case 3:
206                 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
207                 break;
208         default:
209                 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
210                             phy_id, lr_error);
211                 break;
212         }
213
214         asd_turn_led(asd_ha, phy_id, 0);
215         sas_phy_disconnected(sas_phy);
216         sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
217
218         if (retries_left == 0) {
219                 int num = 1;
220                 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
221                                                           GFP_ATOMIC);
222                 if (!cp) {
223                         asd_printk("%s: out of memory\n", __FUNCTION__);
224                         goto out;
225                 }
226                 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
227                             phy_id);
228                 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
229                 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
230                         asd_ascb_free(cp);
231         }
232 out:
233         ;
234 }
235
236 static inline void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
237                                               struct done_list_struct *dl,
238                                               int phy_id)
239 {
240         unsigned long flags;
241         struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
242         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
243         u8  reg  = dl->status_block[1];
244         u32 cont = dl->status_block[2] << ((reg & 3)*8);
245
246         reg &= ~3;
247         switch (reg) {
248         case LmPRMSTAT0BYTE0:
249                 switch (cont) {
250                 case LmBROADCH:
251                 case LmBROADRVCH0:
252                 case LmBROADRVCH1:
253                 case LmBROADSES:
254                         ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
255                                     phy_id, cont);
256                         spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
257                         sas_phy->sas_prim = ffs(cont);
258                         spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
259                         sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
260                         break;
261
262                 case LmUNKNOWNP:
263                         ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
264                         break;
265
266                 default:
267                         ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
268                                     phy_id, reg, cont);
269                         break;
270                 }
271                 break;
272         case LmPRMSTAT1BYTE0:
273                 switch (cont) {
274                 case LmHARDRST:
275                         ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
276                                     phy_id);
277                         /* The sequencer disables all phys on that port.
278                          * We have to re-enable the phys ourselves. */
279                         sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
280                         break;
281
282                 default:
283                         ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
284                                     phy_id, reg, cont);
285                         break;
286                 }
287                 break;
288         default:
289                 ASD_DPRINTK("unknown primitive register:0x%x\n",
290                             dl->status_block[1]);
291                 break;
292         }
293 }
294
295 /**
296  * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
297  * @ascb: pointer to Empty SCB
298  * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
299  *
300  * After an EDB has been invalidated, if all EDBs in this ESCB have been
301  * invalidated, the ESCB is posted back to the sequencer.
302  * Context is tasklet/IRQ.
303  */
304 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
305 {
306         struct asd_seq_data *seq = &ascb->ha->seq;
307         struct empty_scb *escb = &ascb->scb->escb;
308         struct sg_el     *eb   = &escb->eb[edb_id];
309         struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
310
311         memset(edb->vaddr, 0, ASD_EDB_SIZE);
312         eb->flags |= ELEMENT_NOT_VALID;
313         escb->num_valid--;
314
315         if (escb->num_valid == 0) {
316                 int i;
317                 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
318                             "dma_handle: 0x%08llx, next: 0x%08llx, "
319                             "index:%d, opcode:0x%02x\n",
320                             ascb->dma_scb.vaddr,
321                             (u64)ascb->dma_scb.dma_handle,
322                             le64_to_cpu(ascb->scb->header.next_scb),
323                             le16_to_cpu(ascb->scb->header.index),
324                             ascb->scb->header.opcode);
325                 */
326                 escb->num_valid = ASD_EDBS_PER_SCB;
327                 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
328                         escb->eb[i].flags = 0;
329                 if (!list_empty(&ascb->list))
330                         list_del_init(&ascb->list);
331                 i = asd_post_escb_list(ascb->ha, ascb, 1);
332                 if (i)
333                         asd_printk("couldn't post escb, err:%d\n", i);
334         }
335 }
336
337 static void escb_tasklet_complete(struct asd_ascb *ascb,
338                                   struct done_list_struct *dl)
339 {
340         struct asd_ha_struct *asd_ha = ascb->ha;
341         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
342         int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
343         u8  sb_opcode = dl->status_block[0];
344         int phy_id = sb_opcode & DL_PHY_MASK;
345         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
346
347         if (edb > 6 || edb < 0) {
348                 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
349                             edb, dl->opcode);
350                 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
351                             sb_opcode, phy_id);
352                 ASD_DPRINTK("escb: vaddr: 0x%p, "
353                             "dma_handle: 0x%llx, next: 0x%llx, "
354                             "index:%d, opcode:0x%02x\n",
355                             ascb->dma_scb.vaddr,
356                             (unsigned long long)ascb->dma_scb.dma_handle,
357                             (unsigned long long)
358                             le64_to_cpu(ascb->scb->header.next_scb),
359                             le16_to_cpu(ascb->scb->header.index),
360                             ascb->scb->header.opcode);
361         }
362
363         sb_opcode &= ~DL_PHY_MASK;
364
365         switch (sb_opcode) {
366         case BYTES_DMAED:
367                 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __FUNCTION__, phy_id);
368                 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
369                 break;
370         case PRIMITIVE_RECVD:
371                 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __FUNCTION__,
372                             phy_id);
373                 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
374                 break;
375         case PHY_EVENT:
376                 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __FUNCTION__, phy_id);
377                 asd_phy_event_tasklet(ascb, dl);
378                 break;
379         case LINK_RESET_ERROR:
380                 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __FUNCTION__,
381                             phy_id);
382                 asd_link_reset_err_tasklet(ascb, dl, phy_id);
383                 break;
384         case TIMER_EVENT:
385                 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
386                             __FUNCTION__, phy_id);
387                 asd_turn_led(asd_ha, phy_id, 0);
388                 /* the device is gone */
389                 sas_phy_disconnected(sas_phy);
390                 sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
391                 break;
392         case REQ_TASK_ABORT:
393                 ASD_DPRINTK("%s: phy%d: REQ_TASK_ABORT\n", __FUNCTION__,
394                             phy_id);
395                 break;
396         case REQ_DEVICE_RESET:
397                 ASD_DPRINTK("%s: phy%d: REQ_DEVICE_RESET\n", __FUNCTION__,
398                             phy_id);
399                 break;
400         case SIGNAL_NCQ_ERROR:
401                 ASD_DPRINTK("%s: phy%d: SIGNAL_NCQ_ERROR\n", __FUNCTION__,
402                             phy_id);
403                 break;
404         case CLEAR_NCQ_ERROR:
405                 ASD_DPRINTK("%s: phy%d: CLEAR_NCQ_ERROR\n", __FUNCTION__,
406                             phy_id);
407                 break;
408         default:
409                 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __FUNCTION__,
410                             phy_id, sb_opcode);
411                 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
412                             edb, dl->opcode);
413                 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
414                             sb_opcode, phy_id);
415                 ASD_DPRINTK("escb: vaddr: 0x%p, "
416                             "dma_handle: 0x%llx, next: 0x%llx, "
417                             "index:%d, opcode:0x%02x\n",
418                             ascb->dma_scb.vaddr,
419                             (unsigned long long)ascb->dma_scb.dma_handle,
420                             (unsigned long long)
421                             le64_to_cpu(ascb->scb->header.next_scb),
422                             le16_to_cpu(ascb->scb->header.index),
423                             ascb->scb->header.opcode);
424
425                 break;
426         }
427
428         asd_invalidate_edb(ascb, edb);
429 }
430
431 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
432 {
433         struct asd_seq_data *seq = &asd_ha->seq;
434         int i;
435
436         for (i = 0; i < seq->num_escbs; i++)
437                 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
438
439         ASD_DPRINTK("posting %d escbs\n", i);
440         return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
441 }
442
443 /* ---------- CONTROL PHY ---------- */
444
445 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE   \
446                             | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
447                             | CURRENT_OOB_ERROR)
448
449 /**
450  * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
451  * @ascb: pointer to an ascb
452  * @dl: pointer to the done list entry
453  *
454  * This function completes a CONTROL PHY scb and frees the ascb.
455  * A note on LEDs:
456  *  - an LED blinks if there is IO though it,
457  *  - if a device is connected to the LED, it is lit,
458  *  - if no device is connected to the LED, is is dimmed (off).
459  */
460 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
461                                          struct done_list_struct *dl)
462 {
463         struct asd_ha_struct *asd_ha = ascb->ha;
464         struct scb *scb = ascb->scb;
465         struct control_phy *control_phy = &scb->control_phy;
466         u8 phy_id = control_phy->phy_id;
467         struct asd_phy *phy = &ascb->ha->phys[phy_id];
468
469         u8 status     = dl->status_block[0];
470         u8 oob_status = dl->status_block[1];
471         u8 oob_mode   = dl->status_block[2];
472         /* u8 oob_signals= dl->status_block[3]; */
473
474         if (status != 0) {
475                 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
476                             __FUNCTION__, phy_id, status);
477                 goto out;
478         }
479
480         switch (control_phy->sub_func) {
481         case DISABLE_PHY:
482                 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
483                 asd_turn_led(asd_ha, phy_id, 0);
484                 asd_control_led(asd_ha, phy_id, 0);
485                 ASD_DPRINTK("%s: disable phy%d\n", __FUNCTION__, phy_id);
486                 break;
487
488         case ENABLE_PHY:
489                 asd_control_led(asd_ha, phy_id, 1);
490                 if (oob_status & CURRENT_OOB_DONE) {
491                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
492                         get_lrate_mode(phy, oob_mode);
493                         asd_turn_led(asd_ha, phy_id, 1);
494                         ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
495                                     __FUNCTION__, phy_id,phy->sas_phy.linkrate,
496                                     phy->sas_phy.iproto);
497                 } else if (oob_status & CURRENT_SPINUP_HOLD) {
498                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
499                         asd_turn_led(asd_ha, phy_id, 1);
500                         ASD_DPRINTK("%s: phy%d, spinup hold\n", __FUNCTION__,
501                                     phy_id);
502                 } else if (oob_status & CURRENT_ERR_MASK) {
503                         asd_turn_led(asd_ha, phy_id, 0);
504                         ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
505                                     __FUNCTION__, phy_id, oob_status);
506                 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
507                                          | CURRENT_DEVICE_PRESENT))  {
508                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
509                         asd_turn_led(asd_ha, phy_id, 1);
510                         ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
511                                     __FUNCTION__, phy_id);
512                 } else {
513                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
514                         asd_turn_led(asd_ha, phy_id, 0);
515                         ASD_DPRINTK("%s: phy%d: no device present: "
516                                     "oob_status:0x%x\n",
517                                     __FUNCTION__, phy_id, oob_status);
518                 }
519                 break;
520         case RELEASE_SPINUP_HOLD:
521         case PHY_NO_OP:
522         case EXECUTE_HARD_RESET:
523                 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __FUNCTION__,
524                             phy_id, control_phy->sub_func);
525                 /* XXX finish */
526                 break;
527         default:
528                 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __FUNCTION__,
529                             phy_id, control_phy->sub_func);
530                 break;
531         }
532 out:
533         asd_ascb_free(ascb);
534 }
535
536 static inline void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
537 {
538         /* disable all speeds, then enable defaults */
539         *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
540                 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
541
542         switch (pd->max_sas_lrate) {
543         case PHY_LINKRATE_6:
544                 *speed_mask &= ~SAS_SPEED_60_DIS;
545         default:
546         case PHY_LINKRATE_3:
547                 *speed_mask &= ~SAS_SPEED_30_DIS;
548         case PHY_LINKRATE_1_5:
549                 *speed_mask &= ~SAS_SPEED_15_DIS;
550         }
551
552         switch (pd->min_sas_lrate) {
553         case PHY_LINKRATE_6:
554                 *speed_mask |= SAS_SPEED_30_DIS;
555         case PHY_LINKRATE_3:
556                 *speed_mask |= SAS_SPEED_15_DIS;
557         default:
558         case PHY_LINKRATE_1_5:
559                 /* nothing to do */
560                 ;
561         }
562
563         switch (pd->max_sata_lrate) {
564         case PHY_LINKRATE_3:
565                 *speed_mask &= ~SATA_SPEED_30_DIS;
566         default:
567         case PHY_LINKRATE_1_5:
568                 *speed_mask &= ~SATA_SPEED_15_DIS;
569         }
570
571         switch (pd->min_sata_lrate) {
572         case PHY_LINKRATE_3:
573                 *speed_mask |= SATA_SPEED_15_DIS;
574         default:
575         case PHY_LINKRATE_1_5:
576                 /* nothing to do */
577                 ;
578         }
579 }
580
581 /**
582  * asd_build_control_phy -- build a CONTROL PHY SCB
583  * @ascb: pointer to an ascb
584  * @phy_id: phy id to control, integer
585  * @subfunc: subfunction, what to actually to do the phy
586  *
587  * This function builds a CONTROL PHY scb.  No allocation of any kind
588  * is performed. @ascb is allocated with the list function.
589  * The caller can override the ascb->tasklet_complete to point
590  * to its own callback function.  It must call asd_ascb_free()
591  * at its tasklet complete function.
592  * See the default implementation.
593  */
594 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
595 {
596         struct asd_phy *phy = &ascb->ha->phys[phy_id];
597         struct scb *scb = ascb->scb;
598         struct control_phy *control_phy = &scb->control_phy;
599
600         scb->header.opcode = CONTROL_PHY;
601         control_phy->phy_id = (u8) phy_id;
602         control_phy->sub_func = subfunc;
603
604         switch (subfunc) {
605         case EXECUTE_HARD_RESET:  /* 0x81 */
606         case ENABLE_PHY:          /* 0x01 */
607                 /* decide hot plug delay */
608                 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
609
610                 /* decide speed mask */
611                 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
612
613                 /* initiator port settings are in the hi nibble */
614                 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
615                         control_phy->port_type = SAS_PROTO_ALL << 4;
616                 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
617                         control_phy->port_type = SAS_PROTO_ALL;
618                 else
619                         control_phy->port_type =
620                                 (SAS_PROTO_ALL << 4) | SAS_PROTO_ALL;
621
622                 /* link reset retries, this should be nominal */
623                 control_phy->link_reset_retries = 10;
624
625         case RELEASE_SPINUP_HOLD: /* 0x02 */
626                 /* decide the func_mask */
627                 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
628                 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
629                         control_phy->func_mask &= ~SPINUP_HOLD_DIS;
630                 else
631                         control_phy->func_mask |= SPINUP_HOLD_DIS;
632         }
633
634         control_phy->conn_handle = cpu_to_le16(0xFFFF);
635
636         ascb->tasklet_complete = control_phy_tasklet_complete;
637 }
638
639 /* ---------- INITIATE LINK ADM TASK ---------- */
640
641 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
642                                       struct done_list_struct *dl)
643 {
644         u8 opcode = dl->opcode;
645         struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
646         u8 phy_id = link_adm->phy_id;
647
648         if (opcode != TC_NO_ERROR) {
649                 asd_printk("phy%d: link adm task 0x%x completed with error "
650                            "0x%x\n", phy_id, link_adm->sub_func, opcode);
651         }
652         ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
653                     phy_id, link_adm->sub_func, opcode);
654
655         asd_ascb_free(ascb);
656 }
657
658 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
659                                       u8 subfunc)
660 {
661         struct scb *scb = ascb->scb;
662         struct initiate_link_adm *link_adm = &scb->link_adm;
663
664         scb->header.opcode = INITIATE_LINK_ADM_TASK;
665
666         link_adm->phy_id = phy_id;
667         link_adm->sub_func = subfunc;
668         link_adm->conn_handle = cpu_to_le16(0xFFFF);
669
670         ascb->tasklet_complete = link_adm_tasklet_complete;
671 }
672
673 /* ---------- SCB timer ---------- */
674
675 /**
676  * asd_ascb_timedout -- called when a pending SCB's timer has expired
677  * @data: unsigned long, a pointer to the ascb in question
678  *
679  * This is the default timeout function which does the most necessary.
680  * Upper layers can implement their own timeout function, say to free
681  * resources they have with this SCB, and then call this one at the
682  * end of their timeout function.  To do this, one should initialize
683  * the ascb->timer.{function, data, expires} prior to calling the post
684  * funcion.  The timer is started by the post function.
685  */
686 void asd_ascb_timedout(unsigned long data)
687 {
688         struct asd_ascb *ascb = (void *) data;
689         struct asd_seq_data *seq = &ascb->ha->seq;
690         unsigned long flags;
691
692         ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
693
694         spin_lock_irqsave(&seq->pend_q_lock, flags);
695         seq->pending--;
696         list_del_init(&ascb->list);
697         spin_unlock_irqrestore(&seq->pend_q_lock, flags);
698
699         asd_ascb_free(ascb);
700 }
701
702 /* ---------- CONTROL PHY ---------- */
703
704 /* Given the spec value, return a driver value. */
705 static const int phy_func_table[] = {
706         [PHY_FUNC_NOP]        = PHY_NO_OP,
707         [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
708         [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
709         [PHY_FUNC_DISABLE]    = DISABLE_PHY,
710         [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
711 };
712
713 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func)
714 {
715         struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
716         struct asd_ascb *ascb;
717         int res = 1;
718
719         if (func == PHY_FUNC_CLEAR_ERROR_LOG)
720                 return -ENOSYS;
721
722         ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
723         if (!ascb)
724                 return -ENOMEM;
725
726         asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
727         res = asd_post_ascb_list(asd_ha, ascb , 1);
728         if (res)
729                 asd_ascb_free(ascb);
730
731         return res;
732 }