]> err.no Git - linux-2.6/blob - drivers/scsi/aic94xx/aic94xx_scb.c
[SCSI] aic94xx: handle REQ_TASK_ABORT
[linux-2.6] / 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 #include <scsi/scsi_host.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         struct sas_phy *sas_phy = phy->sas_phy.phy;
57
58         switch (oob_mode & 7) {
59         case PHY_SPEED_60:
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;
63                 break;
64         case PHY_SPEED_30:
65                 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
66                 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
67                 break;
68         case PHY_SPEED_15:
69                 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
70                 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
71                 break;
72         }
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;
78
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;
83 }
84
85 static inline void asd_phy_event_tasklet(struct asd_ascb *ascb,
86                                          struct done_list_struct *dl)
87 {
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];
92
93         u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
94         u8 oob_mode   = dl->status_block[2];
95
96         switch (oob_status) {
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);
103                 break;
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);
111                 break;
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);
116                 break;
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);
124                 break;
125         }
126 }
127
128 /* If phys are enabled sparsely, this will do the right thing. */
129 static inline unsigned ord_phy(struct asd_ha_struct *asd_ha,
130                                struct asd_phy *phy)
131 {
132         u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
133         int i, k = 0;
134
135         for_each_phy(enabled_mask, enabled_mask, i) {
136                 if (&asd_ha->phys[i] == phy)
137                         return k;
138                 k++;
139         }
140         return 0;
141 }
142
143 /**
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
147  *
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
150  * HA tables.
151  *
152  * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
153  * buffer.
154  */
155 static inline void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
156 {
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);
162
163                 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
164                 *(__be64 *)sas_addr = cpu_to_be64(addr);
165         } else {
166                 struct sas_identify_frame *idframe =
167                         (void *) phy->sas_phy.frame_rcvd;
168                 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
169         }
170 }
171
172 static inline void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
173                                            struct done_list_struct *dl,
174                                            int edb_id, int phy_id)
175 {
176         unsigned long flags;
177         int edb_el = edb_id + ascb->edb_index;
178         struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
179         struct asd_phy *phy = &ascb->ha->phys[phy_id];
180         struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
181         u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
182
183         size = min(size, (u16) sizeof(phy->frame_rcvd));
184
185         spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
186         memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
187         phy->sas_phy.frame_rcvd_size = size;
188         asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
189         spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
190         asd_dump_frame_rcvd(phy, dl);
191         sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
192 }
193
194 static inline void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
195                                               struct done_list_struct *dl,
196                                               int phy_id)
197 {
198         struct asd_ha_struct *asd_ha = ascb->ha;
199         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
200         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
201         u8 lr_error = dl->status_block[1];
202         u8 retries_left = dl->status_block[2];
203
204         switch (lr_error) {
205         case 0:
206                 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
207                 break;
208         case 1:
209                 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
210                 break;
211         case 2:
212                 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
213                 break;
214         case 3:
215                 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
216                 break;
217         default:
218                 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
219                             phy_id, lr_error);
220                 break;
221         }
222
223         asd_turn_led(asd_ha, phy_id, 0);
224         sas_phy_disconnected(sas_phy);
225         sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
226
227         if (retries_left == 0) {
228                 int num = 1;
229                 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
230                                                           GFP_ATOMIC);
231                 if (!cp) {
232                         asd_printk("%s: out of memory\n", __FUNCTION__);
233                         goto out;
234                 }
235                 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
236                             phy_id);
237                 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
238                 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
239                         asd_ascb_free(cp);
240         }
241 out:
242         ;
243 }
244
245 static inline void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
246                                               struct done_list_struct *dl,
247                                               int phy_id)
248 {
249         unsigned long flags;
250         struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
251         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
252         u8  reg  = dl->status_block[1];
253         u32 cont = dl->status_block[2] << ((reg & 3)*8);
254
255         reg &= ~3;
256         switch (reg) {
257         case LmPRMSTAT0BYTE0:
258                 switch (cont) {
259                 case LmBROADCH:
260                 case LmBROADRVCH0:
261                 case LmBROADRVCH1:
262                 case LmBROADSES:
263                         ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
264                                     phy_id, cont);
265                         spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
266                         sas_phy->sas_prim = ffs(cont);
267                         spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
268                         sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
269                         break;
270
271                 case LmUNKNOWNP:
272                         ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
273                         break;
274
275                 default:
276                         ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
277                                     phy_id, reg, cont);
278                         break;
279                 }
280                 break;
281         case LmPRMSTAT1BYTE0:
282                 switch (cont) {
283                 case LmHARDRST:
284                         ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
285                                     phy_id);
286                         /* The sequencer disables all phys on that port.
287                          * We have to re-enable the phys ourselves. */
288                         sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
289                         break;
290
291                 default:
292                         ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
293                                     phy_id, reg, cont);
294                         break;
295                 }
296                 break;
297         default:
298                 ASD_DPRINTK("unknown primitive register:0x%x\n",
299                             dl->status_block[1]);
300                 break;
301         }
302 }
303
304 /**
305  * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
306  * @ascb: pointer to Empty SCB
307  * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
308  *
309  * After an EDB has been invalidated, if all EDBs in this ESCB have been
310  * invalidated, the ESCB is posted back to the sequencer.
311  * Context is tasklet/IRQ.
312  */
313 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
314 {
315         struct asd_seq_data *seq = &ascb->ha->seq;
316         struct empty_scb *escb = &ascb->scb->escb;
317         struct sg_el     *eb   = &escb->eb[edb_id];
318         struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
319
320         memset(edb->vaddr, 0, ASD_EDB_SIZE);
321         eb->flags |= ELEMENT_NOT_VALID;
322         escb->num_valid--;
323
324         if (escb->num_valid == 0) {
325                 int i;
326                 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
327                             "dma_handle: 0x%08llx, next: 0x%08llx, "
328                             "index:%d, opcode:0x%02x\n",
329                             ascb->dma_scb.vaddr,
330                             (u64)ascb->dma_scb.dma_handle,
331                             le64_to_cpu(ascb->scb->header.next_scb),
332                             le16_to_cpu(ascb->scb->header.index),
333                             ascb->scb->header.opcode);
334                 */
335                 escb->num_valid = ASD_EDBS_PER_SCB;
336                 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
337                         escb->eb[i].flags = 0;
338                 if (!list_empty(&ascb->list))
339                         list_del_init(&ascb->list);
340                 i = asd_post_escb_list(ascb->ha, ascb, 1);
341                 if (i)
342                         asd_printk("couldn't post escb, err:%d\n", i);
343         }
344 }
345
346 /* start up the ABORT TASK tmf... */
347 static void task_kill_later(struct asd_ascb *ascb)
348 {
349         struct asd_ha_struct *asd_ha = ascb->ha;
350         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
351         struct Scsi_Host *shost = sas_ha->core.shost;
352         struct sas_task *task = ascb->uldd_task;
353
354         INIT_WORK(&task->abort_work, (void (*)(void *))sas_task_abort, task);
355         queue_work(shost->work_q, &task->abort_work);
356 }
357
358 static void escb_tasklet_complete(struct asd_ascb *ascb,
359                                   struct done_list_struct *dl)
360 {
361         struct asd_ha_struct *asd_ha = ascb->ha;
362         struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
363         int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
364         u8  sb_opcode = dl->status_block[0];
365         int phy_id = sb_opcode & DL_PHY_MASK;
366         struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
367
368         if (edb > 6 || edb < 0) {
369                 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
370                             edb, dl->opcode);
371                 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
372                             sb_opcode, phy_id);
373                 ASD_DPRINTK("escb: vaddr: 0x%p, "
374                             "dma_handle: 0x%llx, next: 0x%llx, "
375                             "index:%d, opcode:0x%02x\n",
376                             ascb->dma_scb.vaddr,
377                             (unsigned long long)ascb->dma_scb.dma_handle,
378                             (unsigned long long)
379                             le64_to_cpu(ascb->scb->header.next_scb),
380                             le16_to_cpu(ascb->scb->header.index),
381                             ascb->scb->header.opcode);
382         }
383
384         /* Catch these before we mask off the sb_opcode bits */
385         switch (sb_opcode) {
386         case REQ_TASK_ABORT: {
387                 struct asd_ascb *a, *b;
388                 u16 tc_abort;
389
390                 tc_abort = *((u16*)(&dl->status_block[1]));
391                 tc_abort = le16_to_cpu(tc_abort);
392
393                 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
394                             __FUNCTION__, dl->status_block[3]);
395
396                 /* Find the pending task and abort it. */
397                 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list)
398                         if (a->tc_index == tc_abort) {
399                                 task_kill_later(a);
400                                 break;
401                         }
402                 goto out;
403         }
404         case REQ_DEVICE_RESET: {
405                 struct asd_ascb *a, *b;
406                 u16 conn_handle;
407
408                 conn_handle = *((u16*)(&dl->status_block[1]));
409                 conn_handle = le16_to_cpu(conn_handle);
410
411                 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __FUNCTION__,
412                             dl->status_block[3]);
413
414                 /* Kill all pending tasks and reset the device */
415                 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
416                         struct sas_task *task = a->uldd_task;
417                         struct domain_device *dev = task->dev;
418                         u16 x;
419
420                         x = *((u16*)(&dev->lldd_dev));
421                         if (x == conn_handle)
422                                 task_kill_later(a);
423                 }
424
425                 /* FIXME: Reset device port (huh?) */
426                 goto out;
427         }
428         case SIGNAL_NCQ_ERROR:
429                 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __FUNCTION__);
430                 goto out;
431         case CLEAR_NCQ_ERROR:
432                 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __FUNCTION__);
433                 goto out;
434         }
435
436         sb_opcode &= ~DL_PHY_MASK;
437
438         switch (sb_opcode) {
439         case BYTES_DMAED:
440                 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __FUNCTION__, phy_id);
441                 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
442                 break;
443         case PRIMITIVE_RECVD:
444                 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __FUNCTION__,
445                             phy_id);
446                 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
447                 break;
448         case PHY_EVENT:
449                 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __FUNCTION__, phy_id);
450                 asd_phy_event_tasklet(ascb, dl);
451                 break;
452         case LINK_RESET_ERROR:
453                 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __FUNCTION__,
454                             phy_id);
455                 asd_link_reset_err_tasklet(ascb, dl, phy_id);
456                 break;
457         case TIMER_EVENT:
458                 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
459                             __FUNCTION__, phy_id);
460                 asd_turn_led(asd_ha, phy_id, 0);
461                 /* the device is gone */
462                 sas_phy_disconnected(sas_phy);
463                 sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
464                 break;
465         default:
466                 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __FUNCTION__,
467                             phy_id, sb_opcode);
468                 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
469                             edb, dl->opcode);
470                 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
471                             sb_opcode, phy_id);
472                 ASD_DPRINTK("escb: vaddr: 0x%p, "
473                             "dma_handle: 0x%llx, next: 0x%llx, "
474                             "index:%d, opcode:0x%02x\n",
475                             ascb->dma_scb.vaddr,
476                             (unsigned long long)ascb->dma_scb.dma_handle,
477                             (unsigned long long)
478                             le64_to_cpu(ascb->scb->header.next_scb),
479                             le16_to_cpu(ascb->scb->header.index),
480                             ascb->scb->header.opcode);
481
482                 break;
483         }
484 out:
485         asd_invalidate_edb(ascb, edb);
486 }
487
488 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
489 {
490         struct asd_seq_data *seq = &asd_ha->seq;
491         int i;
492
493         for (i = 0; i < seq->num_escbs; i++)
494                 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
495
496         ASD_DPRINTK("posting %d escbs\n", i);
497         return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
498 }
499
500 /* ---------- CONTROL PHY ---------- */
501
502 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE   \
503                             | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
504                             | CURRENT_OOB_ERROR)
505
506 /**
507  * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
508  * @ascb: pointer to an ascb
509  * @dl: pointer to the done list entry
510  *
511  * This function completes a CONTROL PHY scb and frees the ascb.
512  * A note on LEDs:
513  *  - an LED blinks if there is IO though it,
514  *  - if a device is connected to the LED, it is lit,
515  *  - if no device is connected to the LED, is is dimmed (off).
516  */
517 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
518                                          struct done_list_struct *dl)
519 {
520         struct asd_ha_struct *asd_ha = ascb->ha;
521         struct scb *scb = ascb->scb;
522         struct control_phy *control_phy = &scb->control_phy;
523         u8 phy_id = control_phy->phy_id;
524         struct asd_phy *phy = &ascb->ha->phys[phy_id];
525
526         u8 status     = dl->status_block[0];
527         u8 oob_status = dl->status_block[1];
528         u8 oob_mode   = dl->status_block[2];
529         /* u8 oob_signals= dl->status_block[3]; */
530
531         if (status != 0) {
532                 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
533                             __FUNCTION__, phy_id, status);
534                 goto out;
535         }
536
537         switch (control_phy->sub_func) {
538         case DISABLE_PHY:
539                 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
540                 asd_turn_led(asd_ha, phy_id, 0);
541                 asd_control_led(asd_ha, phy_id, 0);
542                 ASD_DPRINTK("%s: disable phy%d\n", __FUNCTION__, phy_id);
543                 break;
544
545         case ENABLE_PHY:
546                 asd_control_led(asd_ha, phy_id, 1);
547                 if (oob_status & CURRENT_OOB_DONE) {
548                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
549                         get_lrate_mode(phy, oob_mode);
550                         asd_turn_led(asd_ha, phy_id, 1);
551                         ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
552                                     __FUNCTION__, phy_id,phy->sas_phy.linkrate,
553                                     phy->sas_phy.iproto);
554                 } else if (oob_status & CURRENT_SPINUP_HOLD) {
555                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
556                         asd_turn_led(asd_ha, phy_id, 1);
557                         ASD_DPRINTK("%s: phy%d, spinup hold\n", __FUNCTION__,
558                                     phy_id);
559                 } else if (oob_status & CURRENT_ERR_MASK) {
560                         asd_turn_led(asd_ha, phy_id, 0);
561                         ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
562                                     __FUNCTION__, phy_id, oob_status);
563                 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
564                                          | CURRENT_DEVICE_PRESENT))  {
565                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
566                         asd_turn_led(asd_ha, phy_id, 1);
567                         ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
568                                     __FUNCTION__, phy_id);
569                 } else {
570                         asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
571                         asd_turn_led(asd_ha, phy_id, 0);
572                         ASD_DPRINTK("%s: phy%d: no device present: "
573                                     "oob_status:0x%x\n",
574                                     __FUNCTION__, phy_id, oob_status);
575                 }
576                 break;
577         case RELEASE_SPINUP_HOLD:
578         case PHY_NO_OP:
579         case EXECUTE_HARD_RESET:
580                 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __FUNCTION__,
581                             phy_id, control_phy->sub_func);
582                 /* XXX finish */
583                 break;
584         default:
585                 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __FUNCTION__,
586                             phy_id, control_phy->sub_func);
587                 break;
588         }
589 out:
590         asd_ascb_free(ascb);
591 }
592
593 static inline void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
594 {
595         /* disable all speeds, then enable defaults */
596         *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
597                 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
598
599         switch (pd->max_sas_lrate) {
600         case SAS_LINK_RATE_6_0_GBPS:
601                 *speed_mask &= ~SAS_SPEED_60_DIS;
602         default:
603         case SAS_LINK_RATE_3_0_GBPS:
604                 *speed_mask &= ~SAS_SPEED_30_DIS;
605         case SAS_LINK_RATE_1_5_GBPS:
606                 *speed_mask &= ~SAS_SPEED_15_DIS;
607         }
608
609         switch (pd->min_sas_lrate) {
610         case SAS_LINK_RATE_6_0_GBPS:
611                 *speed_mask |= SAS_SPEED_30_DIS;
612         case SAS_LINK_RATE_3_0_GBPS:
613                 *speed_mask |= SAS_SPEED_15_DIS;
614         default:
615         case SAS_LINK_RATE_1_5_GBPS:
616                 /* nothing to do */
617                 ;
618         }
619
620         switch (pd->max_sata_lrate) {
621         case SAS_LINK_RATE_3_0_GBPS:
622                 *speed_mask &= ~SATA_SPEED_30_DIS;
623         default:
624         case SAS_LINK_RATE_1_5_GBPS:
625                 *speed_mask &= ~SATA_SPEED_15_DIS;
626         }
627
628         switch (pd->min_sata_lrate) {
629         case SAS_LINK_RATE_3_0_GBPS:
630                 *speed_mask |= SATA_SPEED_15_DIS;
631         default:
632         case SAS_LINK_RATE_1_5_GBPS:
633                 /* nothing to do */
634                 ;
635         }
636 }
637
638 /**
639  * asd_build_control_phy -- build a CONTROL PHY SCB
640  * @ascb: pointer to an ascb
641  * @phy_id: phy id to control, integer
642  * @subfunc: subfunction, what to actually to do the phy
643  *
644  * This function builds a CONTROL PHY scb.  No allocation of any kind
645  * is performed. @ascb is allocated with the list function.
646  * The caller can override the ascb->tasklet_complete to point
647  * to its own callback function.  It must call asd_ascb_free()
648  * at its tasklet complete function.
649  * See the default implementation.
650  */
651 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
652 {
653         struct asd_phy *phy = &ascb->ha->phys[phy_id];
654         struct scb *scb = ascb->scb;
655         struct control_phy *control_phy = &scb->control_phy;
656
657         scb->header.opcode = CONTROL_PHY;
658         control_phy->phy_id = (u8) phy_id;
659         control_phy->sub_func = subfunc;
660
661         switch (subfunc) {
662         case EXECUTE_HARD_RESET:  /* 0x81 */
663         case ENABLE_PHY:          /* 0x01 */
664                 /* decide hot plug delay */
665                 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
666
667                 /* decide speed mask */
668                 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
669
670                 /* initiator port settings are in the hi nibble */
671                 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
672                         control_phy->port_type = SAS_PROTO_ALL << 4;
673                 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
674                         control_phy->port_type = SAS_PROTO_ALL;
675                 else
676                         control_phy->port_type =
677                                 (SAS_PROTO_ALL << 4) | SAS_PROTO_ALL;
678
679                 /* link reset retries, this should be nominal */
680                 control_phy->link_reset_retries = 10;
681
682         case RELEASE_SPINUP_HOLD: /* 0x02 */
683                 /* decide the func_mask */
684                 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
685                 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
686                         control_phy->func_mask &= ~SPINUP_HOLD_DIS;
687                 else
688                         control_phy->func_mask |= SPINUP_HOLD_DIS;
689         }
690
691         control_phy->conn_handle = cpu_to_le16(0xFFFF);
692
693         ascb->tasklet_complete = control_phy_tasklet_complete;
694 }
695
696 /* ---------- INITIATE LINK ADM TASK ---------- */
697
698 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
699                                       struct done_list_struct *dl)
700 {
701         u8 opcode = dl->opcode;
702         struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
703         u8 phy_id = link_adm->phy_id;
704
705         if (opcode != TC_NO_ERROR) {
706                 asd_printk("phy%d: link adm task 0x%x completed with error "
707                            "0x%x\n", phy_id, link_adm->sub_func, opcode);
708         }
709         ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
710                     phy_id, link_adm->sub_func, opcode);
711
712         asd_ascb_free(ascb);
713 }
714
715 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
716                                       u8 subfunc)
717 {
718         struct scb *scb = ascb->scb;
719         struct initiate_link_adm *link_adm = &scb->link_adm;
720
721         scb->header.opcode = INITIATE_LINK_ADM_TASK;
722
723         link_adm->phy_id = phy_id;
724         link_adm->sub_func = subfunc;
725         link_adm->conn_handle = cpu_to_le16(0xFFFF);
726
727         ascb->tasklet_complete = link_adm_tasklet_complete;
728 }
729
730 /* ---------- SCB timer ---------- */
731
732 /**
733  * asd_ascb_timedout -- called when a pending SCB's timer has expired
734  * @data: unsigned long, a pointer to the ascb in question
735  *
736  * This is the default timeout function which does the most necessary.
737  * Upper layers can implement their own timeout function, say to free
738  * resources they have with this SCB, and then call this one at the
739  * end of their timeout function.  To do this, one should initialize
740  * the ascb->timer.{function, data, expires} prior to calling the post
741  * funcion.  The timer is started by the post function.
742  */
743 void asd_ascb_timedout(unsigned long data)
744 {
745         struct asd_ascb *ascb = (void *) data;
746         struct asd_seq_data *seq = &ascb->ha->seq;
747         unsigned long flags;
748
749         ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
750
751         spin_lock_irqsave(&seq->pend_q_lock, flags);
752         seq->pending--;
753         list_del_init(&ascb->list);
754         spin_unlock_irqrestore(&seq->pend_q_lock, flags);
755
756         asd_ascb_free(ascb);
757 }
758
759 /* ---------- CONTROL PHY ---------- */
760
761 /* Given the spec value, return a driver value. */
762 static const int phy_func_table[] = {
763         [PHY_FUNC_NOP]        = PHY_NO_OP,
764         [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
765         [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
766         [PHY_FUNC_DISABLE]    = DISABLE_PHY,
767         [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
768 };
769
770 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
771 {
772         struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
773         struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
774         struct asd_ascb *ascb;
775         struct sas_phy_linkrates *rates;
776         int res = 1;
777
778         switch (func) {
779         case PHY_FUNC_CLEAR_ERROR_LOG:
780                 return -ENOSYS;
781         case PHY_FUNC_SET_LINK_RATE:
782                 rates = arg;
783                 if (rates->minimum_linkrate) {
784                         pd->min_sas_lrate = rates->minimum_linkrate;
785                         pd->min_sata_lrate = rates->minimum_linkrate;
786                 }
787                 if (rates->maximum_linkrate) {
788                         pd->max_sas_lrate = rates->maximum_linkrate;
789                         pd->max_sata_lrate = rates->maximum_linkrate;
790                 }
791                 func = PHY_FUNC_LINK_RESET;
792                 break;
793         default:
794                 break;
795         }
796
797         ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
798         if (!ascb)
799                 return -ENOMEM;
800
801         asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
802         res = asd_post_ascb_list(asd_ha, ascb , 1);
803         if (res)
804                 asd_ascb_free(ascb);
805
806         return res;
807 }