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