GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / scsi / aic94xx / aic94xx_scb.c
blobf48b00e8e514db58362f711d0cd5ddb31c19d852
1 /*
2 * Aic94xx SAS/SATA driver SCB management.
4 * Copyright (C) 2005 Adaptec, Inc. All rights reserved.
5 * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7 * This file is licensed under GPLv2.
9 * This file is part of the aic94xx driver.
11 * The aic94xx driver is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; version 2 of the
14 * License.
16 * The aic94xx driver is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with the aic94xx driver; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <linux/gfp.h>
28 #include <scsi/scsi_host.h>
30 #include "aic94xx.h"
31 #include "aic94xx_reg.h"
32 #include "aic94xx_hwi.h"
33 #include "aic94xx_seq.h"
35 #include "aic94xx_dump.h"
37 /* ---------- EMPTY SCB ---------- */
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
50 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE \
51 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
52 | CURRENT_OOB_ERROR)
54 static void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
56 struct sas_phy *sas_phy = phy->sas_phy.phy;
58 switch (oob_mode & 7) {
59 case PHY_SPEED_60:
60 phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
61 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
62 break;
63 case PHY_SPEED_30:
64 phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
65 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
66 break;
67 case PHY_SPEED_15:
68 phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
69 phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
70 break;
72 sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
73 sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
74 sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
75 sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
76 sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
78 if (oob_mode & SAS_MODE)
79 phy->sas_phy.oob_mode = SAS_OOB_MODE;
80 else if (oob_mode & SATA_MODE)
81 phy->sas_phy.oob_mode = SATA_OOB_MODE;
84 static void asd_phy_event_tasklet(struct asd_ascb *ascb,
85 struct done_list_struct *dl)
87 struct asd_ha_struct *asd_ha = ascb->ha;
88 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
89 int phy_id = dl->status_block[0] & DL_PHY_MASK;
90 struct asd_phy *phy = &asd_ha->phys[phy_id];
92 u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
93 u8 oob_mode = dl->status_block[2];
95 switch (oob_status) {
96 case CURRENT_LOSS_OF_SIGNAL:
97 /* directly attached device was removed */
98 ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
99 asd_turn_led(asd_ha, phy_id, 0);
100 sas_phy_disconnected(&phy->sas_phy);
101 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
102 break;
103 case CURRENT_OOB_DONE:
104 /* hot plugged device */
105 asd_turn_led(asd_ha, phy_id, 1);
106 get_lrate_mode(phy, oob_mode);
107 ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
108 phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
109 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
110 break;
111 case CURRENT_SPINUP_HOLD:
112 /* hot plug SATA, no COMWAKE sent */
113 asd_turn_led(asd_ha, phy_id, 1);
114 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
115 break;
116 case CURRENT_GTO_TIMEOUT:
117 case CURRENT_OOB_ERROR:
118 ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
119 dl->status_block[1]);
120 asd_turn_led(asd_ha, phy_id, 0);
121 sas_phy_disconnected(&phy->sas_phy);
122 sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
123 break;
127 /* If phys are enabled sparsely, this will do the right thing. */
128 static unsigned ord_phy(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
130 u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
131 int i, k = 0;
133 for_each_phy(enabled_mask, enabled_mask, i) {
134 if (&asd_ha->phys[i] == phy)
135 return k;
136 k++;
138 return 0;
142 * asd_get_attached_sas_addr -- extract/generate attached SAS address
143 * phy: pointer to asd_phy
144 * sas_addr: pointer to buffer where the SAS address is to be written
146 * This function extracts the SAS address from an IDENTIFY frame
147 * received. If OOB is SATA, then a SAS address is generated from the
148 * HA tables.
150 * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
151 * buffer.
153 static void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
155 if (phy->sas_phy.frame_rcvd[0] == 0x34
156 && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
157 struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
158 /* FIS device-to-host */
159 u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
161 addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
162 *(__be64 *)sas_addr = cpu_to_be64(addr);
163 } else {
164 struct sas_identify_frame *idframe =
165 (void *) phy->sas_phy.frame_rcvd;
166 memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
170 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
172 int i;
173 struct asd_port *free_port = NULL;
174 struct asd_port *port;
175 struct asd_sas_phy *sas_phy = &phy->sas_phy;
176 unsigned long flags;
178 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
179 if (!phy->asd_port) {
180 for (i = 0; i < ASD_MAX_PHYS; i++) {
181 port = &asd_ha->asd_ports[i];
183 /* Check for wide port */
184 if (port->num_phys > 0 &&
185 memcmp(port->sas_addr, sas_phy->sas_addr,
186 SAS_ADDR_SIZE) == 0 &&
187 memcmp(port->attached_sas_addr,
188 sas_phy->attached_sas_addr,
189 SAS_ADDR_SIZE) == 0) {
190 break;
193 /* Find a free port */
194 if (port->num_phys == 0 && free_port == NULL) {
195 free_port = port;
199 /* Use a free port if this doesn't form a wide port */
200 if (i >= ASD_MAX_PHYS) {
201 port = free_port;
202 BUG_ON(!port);
203 memcpy(port->sas_addr, sas_phy->sas_addr,
204 SAS_ADDR_SIZE);
205 memcpy(port->attached_sas_addr,
206 sas_phy->attached_sas_addr,
207 SAS_ADDR_SIZE);
209 port->num_phys++;
210 port->phy_mask |= (1U << sas_phy->id);
211 phy->asd_port = port;
213 ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
214 __func__, phy->asd_port->phy_mask, sas_phy->id);
215 asd_update_port_links(asd_ha, phy);
216 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
219 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
221 struct asd_port *port = phy->asd_port;
222 struct asd_sas_phy *sas_phy = &phy->sas_phy;
223 unsigned long flags;
225 spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
226 if (port) {
227 port->num_phys--;
228 port->phy_mask &= ~(1U << sas_phy->id);
229 phy->asd_port = NULL;
231 spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
234 static void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
235 struct done_list_struct *dl,
236 int edb_id, int phy_id)
238 unsigned long flags;
239 int edb_el = edb_id + ascb->edb_index;
240 struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
241 struct asd_phy *phy = &ascb->ha->phys[phy_id];
242 struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
243 u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
245 size = min(size, (u16) sizeof(phy->frame_rcvd));
247 spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
248 memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
249 phy->sas_phy.frame_rcvd_size = size;
250 asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
251 spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
252 asd_dump_frame_rcvd(phy, dl);
253 asd_form_port(ascb->ha, phy);
254 sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
257 static void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
258 struct done_list_struct *dl,
259 int phy_id)
261 struct asd_ha_struct *asd_ha = ascb->ha;
262 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
263 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
264 struct asd_phy *phy = &asd_ha->phys[phy_id];
265 u8 lr_error = dl->status_block[1];
266 u8 retries_left = dl->status_block[2];
268 switch (lr_error) {
269 case 0:
270 ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
271 break;
272 case 1:
273 ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
274 break;
275 case 2:
276 ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
277 break;
278 case 3:
279 ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
280 break;
281 default:
282 ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
283 phy_id, lr_error);
284 break;
287 asd_turn_led(asd_ha, phy_id, 0);
288 sas_phy_disconnected(sas_phy);
289 asd_deform_port(asd_ha, phy);
290 sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
292 if (retries_left == 0) {
293 int num = 1;
294 struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
295 GFP_ATOMIC);
296 if (!cp) {
297 asd_printk("%s: out of memory\n", __func__);
298 goto out;
300 ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
301 phy_id);
302 asd_build_control_phy(cp, phy_id, ENABLE_PHY);
303 if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
304 asd_ascb_free(cp);
306 out:
310 static void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
311 struct done_list_struct *dl,
312 int phy_id)
314 unsigned long flags;
315 struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
316 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
317 struct asd_ha_struct *asd_ha = ascb->ha;
318 struct asd_phy *phy = &asd_ha->phys[phy_id];
319 u8 reg = dl->status_block[1];
320 u32 cont = dl->status_block[2] << ((reg & 3)*8);
322 reg &= ~3;
323 switch (reg) {
324 case LmPRMSTAT0BYTE0:
325 switch (cont) {
326 case LmBROADCH:
327 case LmBROADRVCH0:
328 case LmBROADRVCH1:
329 case LmBROADSES:
330 ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
331 phy_id, cont);
332 spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
333 sas_phy->sas_prim = ffs(cont);
334 spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
335 sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
336 break;
338 case LmUNKNOWNP:
339 ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
340 break;
342 default:
343 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
344 phy_id, reg, cont);
345 break;
347 break;
348 case LmPRMSTAT1BYTE0:
349 switch (cont) {
350 case LmHARDRST:
351 ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
352 phy_id);
353 /* The sequencer disables all phys on that port.
354 * We have to re-enable the phys ourselves. */
355 asd_deform_port(asd_ha, phy);
356 sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
357 break;
359 default:
360 ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
361 phy_id, reg, cont);
362 break;
364 break;
365 default:
366 ASD_DPRINTK("unknown primitive register:0x%x\n",
367 dl->status_block[1]);
368 break;
373 * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
374 * @ascb: pointer to Empty SCB
375 * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
377 * After an EDB has been invalidated, if all EDBs in this ESCB have been
378 * invalidated, the ESCB is posted back to the sequencer.
379 * Context is tasklet/IRQ.
381 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
383 struct asd_seq_data *seq = &ascb->ha->seq;
384 struct empty_scb *escb = &ascb->scb->escb;
385 struct sg_el *eb = &escb->eb[edb_id];
386 struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
388 memset(edb->vaddr, 0, ASD_EDB_SIZE);
389 eb->flags |= ELEMENT_NOT_VALID;
390 escb->num_valid--;
392 if (escb->num_valid == 0) {
393 int i;
394 /* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
395 "dma_handle: 0x%08llx, next: 0x%08llx, "
396 "index:%d, opcode:0x%02x\n",
397 ascb->dma_scb.vaddr,
398 (u64)ascb->dma_scb.dma_handle,
399 le64_to_cpu(ascb->scb->header.next_scb),
400 le16_to_cpu(ascb->scb->header.index),
401 ascb->scb->header.opcode);
403 escb->num_valid = ASD_EDBS_PER_SCB;
404 for (i = 0; i < ASD_EDBS_PER_SCB; i++)
405 escb->eb[i].flags = 0;
406 if (!list_empty(&ascb->list))
407 list_del_init(&ascb->list);
408 i = asd_post_escb_list(ascb->ha, ascb, 1);
409 if (i)
410 asd_printk("couldn't post escb, err:%d\n", i);
414 static void escb_tasklet_complete(struct asd_ascb *ascb,
415 struct done_list_struct *dl)
417 struct asd_ha_struct *asd_ha = ascb->ha;
418 struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
419 int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
420 u8 sb_opcode = dl->status_block[0];
421 int phy_id = sb_opcode & DL_PHY_MASK;
422 struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
423 struct asd_phy *phy = &asd_ha->phys[phy_id];
425 if (edb > 6 || edb < 0) {
426 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
427 edb, dl->opcode);
428 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
429 sb_opcode, phy_id);
430 ASD_DPRINTK("escb: vaddr: 0x%p, "
431 "dma_handle: 0x%llx, next: 0x%llx, "
432 "index:%d, opcode:0x%02x\n",
433 ascb->dma_scb.vaddr,
434 (unsigned long long)ascb->dma_scb.dma_handle,
435 (unsigned long long)
436 le64_to_cpu(ascb->scb->header.next_scb),
437 le16_to_cpu(ascb->scb->header.index),
438 ascb->scb->header.opcode);
441 /* Catch these before we mask off the sb_opcode bits */
442 switch (sb_opcode) {
443 case REQ_TASK_ABORT: {
444 struct asd_ascb *a, *b;
445 u16 tc_abort;
446 struct domain_device *failed_dev = NULL;
448 ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
449 __func__, dl->status_block[3]);
452 * Find the task that caused the abort and abort it first.
453 * The sequencer won't put anything on the done list until
454 * that happens.
456 tc_abort = *((u16*)(&dl->status_block[1]));
457 tc_abort = le16_to_cpu(tc_abort);
459 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
460 struct sas_task *task = a->uldd_task;
462 if (a->tc_index != tc_abort)
463 continue;
465 if (task) {
466 failed_dev = task->dev;
467 sas_task_abort(task);
468 } else {
469 ASD_DPRINTK("R_T_A for non TASK scb 0x%x\n",
470 a->scb->header.opcode);
472 break;
475 if (!failed_dev) {
476 ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
477 __func__, tc_abort);
478 goto out;
482 * Now abort everything else for that device (hba?) so
483 * that the EH will wake up and do something.
485 list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
486 struct sas_task *task = a->uldd_task;
488 if (task &&
489 task->dev == failed_dev &&
490 a->tc_index != tc_abort)
491 sas_task_abort(task);
494 goto out;
496 case REQ_DEVICE_RESET: {
497 struct asd_ascb *a;
498 u16 conn_handle;
499 unsigned long flags;
500 struct sas_task *last_dev_task = NULL;
502 conn_handle = *((u16*)(&dl->status_block[1]));
503 conn_handle = le16_to_cpu(conn_handle);
505 ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __func__,
506 dl->status_block[3]);
508 /* Find the last pending task for the device... */
509 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
510 u16 x;
511 struct domain_device *dev;
512 struct sas_task *task = a->uldd_task;
514 if (!task)
515 continue;
516 dev = task->dev;
518 x = (unsigned long)dev->lldd_dev;
519 if (x == conn_handle)
520 last_dev_task = task;
523 if (!last_dev_task) {
524 ASD_DPRINTK("%s: Device reset for idle device %d?\n",
525 __func__, conn_handle);
526 goto out;
529 /* ...and set the reset flag */
530 spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
531 last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
532 spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
534 /* Kill all pending tasks for the device */
535 list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
536 u16 x;
537 struct domain_device *dev;
538 struct sas_task *task = a->uldd_task;
540 if (!task)
541 continue;
542 dev = task->dev;
544 x = (unsigned long)dev->lldd_dev;
545 if (x == conn_handle)
546 sas_task_abort(task);
549 goto out;
551 case SIGNAL_NCQ_ERROR:
552 ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __func__);
553 goto out;
554 case CLEAR_NCQ_ERROR:
555 ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __func__);
556 goto out;
559 sb_opcode &= ~DL_PHY_MASK;
561 switch (sb_opcode) {
562 case BYTES_DMAED:
563 ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __func__, phy_id);
564 asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
565 break;
566 case PRIMITIVE_RECVD:
567 ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __func__,
568 phy_id);
569 asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
570 break;
571 case PHY_EVENT:
572 ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __func__, phy_id);
573 asd_phy_event_tasklet(ascb, dl);
574 break;
575 case LINK_RESET_ERROR:
576 ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __func__,
577 phy_id);
578 asd_link_reset_err_tasklet(ascb, dl, phy_id);
579 break;
580 case TIMER_EVENT:
581 ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
582 __func__, phy_id);
583 asd_turn_led(asd_ha, phy_id, 0);
584 /* the device is gone */
585 sas_phy_disconnected(sas_phy);
586 asd_deform_port(asd_ha, phy);
587 sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
588 break;
589 default:
590 ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __func__,
591 phy_id, sb_opcode);
592 ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
593 edb, dl->opcode);
594 ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
595 sb_opcode, phy_id);
596 ASD_DPRINTK("escb: vaddr: 0x%p, "
597 "dma_handle: 0x%llx, next: 0x%llx, "
598 "index:%d, opcode:0x%02x\n",
599 ascb->dma_scb.vaddr,
600 (unsigned long long)ascb->dma_scb.dma_handle,
601 (unsigned long long)
602 le64_to_cpu(ascb->scb->header.next_scb),
603 le16_to_cpu(ascb->scb->header.index),
604 ascb->scb->header.opcode);
606 break;
608 out:
609 asd_invalidate_edb(ascb, edb);
612 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
614 struct asd_seq_data *seq = &asd_ha->seq;
615 int i;
617 for (i = 0; i < seq->num_escbs; i++)
618 seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
620 ASD_DPRINTK("posting %d escbs\n", i);
621 return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
624 /* ---------- CONTROL PHY ---------- */
626 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE \
627 | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
628 | CURRENT_OOB_ERROR)
631 * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
632 * @ascb: pointer to an ascb
633 * @dl: pointer to the done list entry
635 * This function completes a CONTROL PHY scb and frees the ascb.
636 * A note on LEDs:
637 * - an LED blinks if there is IO though it,
638 * - if a device is connected to the LED, it is lit,
639 * - if no device is connected to the LED, is is dimmed (off).
641 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
642 struct done_list_struct *dl)
644 struct asd_ha_struct *asd_ha = ascb->ha;
645 struct scb *scb = ascb->scb;
646 struct control_phy *control_phy = &scb->control_phy;
647 u8 phy_id = control_phy->phy_id;
648 struct asd_phy *phy = &ascb->ha->phys[phy_id];
650 u8 status = dl->status_block[0];
651 u8 oob_status = dl->status_block[1];
652 u8 oob_mode = dl->status_block[2];
653 /* u8 oob_signals= dl->status_block[3]; */
655 if (status != 0) {
656 ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
657 __func__, phy_id, status);
658 goto out;
661 switch (control_phy->sub_func) {
662 case DISABLE_PHY:
663 asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
664 asd_turn_led(asd_ha, phy_id, 0);
665 asd_control_led(asd_ha, phy_id, 0);
666 ASD_DPRINTK("%s: disable phy%d\n", __func__, phy_id);
667 break;
669 case ENABLE_PHY:
670 asd_control_led(asd_ha, phy_id, 1);
671 if (oob_status & CURRENT_OOB_DONE) {
672 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
673 get_lrate_mode(phy, oob_mode);
674 asd_turn_led(asd_ha, phy_id, 1);
675 ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
676 __func__, phy_id,phy->sas_phy.linkrate,
677 phy->sas_phy.iproto);
678 } else if (oob_status & CURRENT_SPINUP_HOLD) {
679 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
680 asd_turn_led(asd_ha, phy_id, 1);
681 ASD_DPRINTK("%s: phy%d, spinup hold\n", __func__,
682 phy_id);
683 } else if (oob_status & CURRENT_ERR_MASK) {
684 asd_turn_led(asd_ha, phy_id, 0);
685 ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
686 __func__, phy_id, oob_status);
687 } else if (oob_status & (CURRENT_HOT_PLUG_CNCT
688 | CURRENT_DEVICE_PRESENT)) {
689 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
690 asd_turn_led(asd_ha, phy_id, 1);
691 ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
692 __func__, phy_id);
693 } else {
694 asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
695 asd_turn_led(asd_ha, phy_id, 0);
696 ASD_DPRINTK("%s: phy%d: no device present: "
697 "oob_status:0x%x\n",
698 __func__, phy_id, oob_status);
700 break;
701 case RELEASE_SPINUP_HOLD:
702 case PHY_NO_OP:
703 case EXECUTE_HARD_RESET:
704 ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __func__,
705 phy_id, control_phy->sub_func);
706 break;
707 default:
708 ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __func__,
709 phy_id, control_phy->sub_func);
710 break;
712 out:
713 asd_ascb_free(ascb);
716 static void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
718 /* disable all speeds, then enable defaults */
719 *speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
720 | SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
722 switch (pd->max_sas_lrate) {
723 case SAS_LINK_RATE_6_0_GBPS:
724 *speed_mask &= ~SAS_SPEED_60_DIS;
725 default:
726 case SAS_LINK_RATE_3_0_GBPS:
727 *speed_mask &= ~SAS_SPEED_30_DIS;
728 case SAS_LINK_RATE_1_5_GBPS:
729 *speed_mask &= ~SAS_SPEED_15_DIS;
732 switch (pd->min_sas_lrate) {
733 case SAS_LINK_RATE_6_0_GBPS:
734 *speed_mask |= SAS_SPEED_30_DIS;
735 case SAS_LINK_RATE_3_0_GBPS:
736 *speed_mask |= SAS_SPEED_15_DIS;
737 default:
738 case SAS_LINK_RATE_1_5_GBPS:
739 /* nothing to do */
743 switch (pd->max_sata_lrate) {
744 case SAS_LINK_RATE_3_0_GBPS:
745 *speed_mask &= ~SATA_SPEED_30_DIS;
746 default:
747 case SAS_LINK_RATE_1_5_GBPS:
748 *speed_mask &= ~SATA_SPEED_15_DIS;
751 switch (pd->min_sata_lrate) {
752 case SAS_LINK_RATE_3_0_GBPS:
753 *speed_mask |= SATA_SPEED_15_DIS;
754 default:
755 case SAS_LINK_RATE_1_5_GBPS:
756 /* nothing to do */
762 * asd_build_control_phy -- build a CONTROL PHY SCB
763 * @ascb: pointer to an ascb
764 * @phy_id: phy id to control, integer
765 * @subfunc: subfunction, what to actually to do the phy
767 * This function builds a CONTROL PHY scb. No allocation of any kind
768 * is performed. @ascb is allocated with the list function.
769 * The caller can override the ascb->tasklet_complete to point
770 * to its own callback function. It must call asd_ascb_free()
771 * at its tasklet complete function.
772 * See the default implementation.
774 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
776 struct asd_phy *phy = &ascb->ha->phys[phy_id];
777 struct scb *scb = ascb->scb;
778 struct control_phy *control_phy = &scb->control_phy;
780 scb->header.opcode = CONTROL_PHY;
781 control_phy->phy_id = (u8) phy_id;
782 control_phy->sub_func = subfunc;
784 switch (subfunc) {
785 case EXECUTE_HARD_RESET: /* 0x81 */
786 case ENABLE_PHY: /* 0x01 */
787 /* decide hot plug delay */
788 control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
790 /* decide speed mask */
791 set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
793 /* initiator port settings are in the hi nibble */
794 if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
795 control_phy->port_type = SAS_PROTOCOL_ALL << 4;
796 else if (phy->sas_phy.role == PHY_ROLE_TARGET)
797 control_phy->port_type = SAS_PROTOCOL_ALL;
798 else
799 control_phy->port_type =
800 (SAS_PROTOCOL_ALL << 4) | SAS_PROTOCOL_ALL;
802 /* link reset retries, this should be nominal */
803 control_phy->link_reset_retries = 10;
805 case RELEASE_SPINUP_HOLD: /* 0x02 */
806 /* decide the func_mask */
807 control_phy->func_mask = FUNCTION_MASK_DEFAULT;
808 if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
809 control_phy->func_mask &= ~SPINUP_HOLD_DIS;
810 else
811 control_phy->func_mask |= SPINUP_HOLD_DIS;
814 control_phy->conn_handle = cpu_to_le16(0xFFFF);
816 ascb->tasklet_complete = control_phy_tasklet_complete;
819 /* ---------- INITIATE LINK ADM TASK ---------- */
822 /* ---------- SCB timer ---------- */
825 * asd_ascb_timedout -- called when a pending SCB's timer has expired
826 * @data: unsigned long, a pointer to the ascb in question
828 * This is the default timeout function which does the most necessary.
829 * Upper layers can implement their own timeout function, say to free
830 * resources they have with this SCB, and then call this one at the
831 * end of their timeout function. To do this, one should initialize
832 * the ascb->timer.{function, data, expires} prior to calling the post
833 * funcion. The timer is started by the post function.
835 void asd_ascb_timedout(unsigned long data)
837 struct asd_ascb *ascb = (void *) data;
838 struct asd_seq_data *seq = &ascb->ha->seq;
839 unsigned long flags;
841 ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
843 spin_lock_irqsave(&seq->pend_q_lock, flags);
844 seq->pending--;
845 list_del_init(&ascb->list);
846 spin_unlock_irqrestore(&seq->pend_q_lock, flags);
848 asd_ascb_free(ascb);
851 /* ---------- CONTROL PHY ---------- */
853 /* Given the spec value, return a driver value. */
854 static const int phy_func_table[] = {
855 [PHY_FUNC_NOP] = PHY_NO_OP,
856 [PHY_FUNC_LINK_RESET] = ENABLE_PHY,
857 [PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
858 [PHY_FUNC_DISABLE] = DISABLE_PHY,
859 [PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
862 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
864 struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
865 struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
866 struct asd_ascb *ascb;
867 struct sas_phy_linkrates *rates;
868 int res = 1;
870 switch (func) {
871 case PHY_FUNC_CLEAR_ERROR_LOG:
872 return -ENOSYS;
873 case PHY_FUNC_SET_LINK_RATE:
874 rates = arg;
875 if (rates->minimum_linkrate) {
876 pd->min_sas_lrate = rates->minimum_linkrate;
877 pd->min_sata_lrate = rates->minimum_linkrate;
879 if (rates->maximum_linkrate) {
880 pd->max_sas_lrate = rates->maximum_linkrate;
881 pd->max_sata_lrate = rates->maximum_linkrate;
883 func = PHY_FUNC_LINK_RESET;
884 break;
885 default:
886 break;
889 ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
890 if (!ascb)
891 return -ENOMEM;
893 asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
894 res = asd_post_ascb_list(asd_ha, ascb , 1);
895 if (res)
896 asd_ascb_free(ascb);
898 return res;