xref: /haiku/src/add-ons/kernel/busses/scsi/ahci/ahci_controller.cpp (revision 9a6a20d4689307142a7ed26a1437ba47e244e73f)
1 /*
2  * Copyright 2007-2009, Marcus Overhagen. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 #include "ahci_controller.h"
7 #include "util.h"
8 
9 #include <algorithm>
10 #include <KernelExport.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <new>
14 
15 #define TRACE(a...) dprintf("ahci: " a)
16 #define FLOW(a...)	dprintf("ahci: " a)
17 
18 
19 AHCIController::AHCIController(device_node *node,
20 		pci_device_module_info *pciModule, pci_device *device)
21 	:
22 	fNode(node),
23 	fPCI(pciModule),
24 	fPCIDevice(device),
25 	fPCIVendorID(0xffff),
26 	fPCIDeviceID(0xffff),
27 	fFlags(0),
28 	fCommandSlotCount(0),
29 	fPortCount(0),
30 	fPortImplementedMask(0),
31 	fIRQ(0),
32 	fUseMSI(false),
33 	fInstanceCheck(-1)
34 {
35 	memset(fPort, 0, sizeof(fPort));
36 
37 	ASSERT(sizeof(ahci_port) == 128);
38 	ASSERT(sizeof(ahci_hba) == 4352);
39 	ASSERT(sizeof(fis) == 256);
40 	ASSERT(sizeof(command_list_entry) == 32);
41 	ASSERT(sizeof(command_table) == 128);
42 	ASSERT(sizeof(prd) == 16);
43 }
44 
45 
46 AHCIController::~AHCIController()
47 {
48 }
49 
50 
51 status_t
52 AHCIController::Init()
53 {
54 	pci_info pciInfo;
55 	fPCI->get_pci_info(fPCIDevice, &pciInfo);
56 
57 	fPCIVendorID = pciInfo.vendor_id;
58 	fPCIDeviceID = pciInfo.device_id;
59 
60 	TRACE("AHCIController::Init %u:%u:%u vendor %04x, device %04x\n",
61 		pciInfo.bus, pciInfo.device, pciInfo.function, fPCIVendorID, fPCIDeviceID);
62 
63 // --- Instance check workaround begin
64 	char sName[32];
65 	snprintf(sName, sizeof(sName), "ahci-inst-%u-%u-%u", pciInfo.bus, pciInfo.device, pciInfo.function);
66 	if (find_port(sName) >= 0) {
67 		dprintf("AHCIController::Init ERROR: an instance for object %u:%u:%u already exists\n",
68 			pciInfo.bus, pciInfo.device, pciInfo.function);
69 		return B_ERROR;
70 	}
71 	fInstanceCheck = create_port(1, sName);
72 // --- Instance check workaround end
73 
74 	get_device_info(fPCIVendorID, fPCIDeviceID, NULL, &fFlags);
75 
76 	uchar capabilityOffset;
77 	status_t res = fPCI->find_pci_capability(fPCIDevice, PCI_cap_id_sata, &capabilityOffset);
78 	if (res == B_OK) {
79 		uint32 satacr0;
80 		uint32 satacr1;
81 		TRACE("PCI SATA capability found at offset 0x%x\n", capabilityOffset);
82 		satacr0 = fPCI->read_pci_config(fPCIDevice, capabilityOffset, 4);
83 		satacr1 = fPCI->read_pci_config(fPCIDevice, capabilityOffset + 4, 4);
84 		TRACE("satacr0 = 0x%08" B_PRIx32 ", satacr1 = 0x%08" B_PRIx32 "\n",
85 			satacr0, satacr1);
86 	}
87 
88 	uint16 pcicmd = fPCI->read_pci_config(fPCIDevice, PCI_command, 2);
89 	TRACE("pcicmd old 0x%04x\n", pcicmd);
90 	pcicmd &= ~(PCI_command_io | PCI_command_int_disable);
91 	pcicmd |= PCI_command_master | PCI_command_memory;
92 	TRACE("pcicmd new 0x%04x\n", pcicmd);
93 	fPCI->write_pci_config(fPCIDevice, PCI_command, 2, pcicmd);
94 
95 	if (fPCIVendorID == PCI_VENDOR_JMICRON) {
96 		uint32 ctrl = fPCI->read_pci_config(fPCIDevice, PCI_JMICRON_CONTROLLER_CONTROL_1, 4);
97 		TRACE("Jmicron controller control 1 old 0x%08" B_PRIx32 "\n", ctrl);
98 		ctrl &= ~((1 << 9) | (1 << 12) | (1 << 14));	// disable SFF 8038i emulation
99 		ctrl |= (1 << 8) | (1 << 13) | (1 << 15);		// enable AHCI controller
100 		TRACE("Jmicron controller control 1 new 0x%08" B_PRIx32 "\n", ctrl);
101 		fPCI->write_pci_config(fPCIDevice, PCI_JMICRON_CONTROLLER_CONTROL_1, 4, ctrl);
102 	}
103 
104 	fIRQ = pciInfo.u.h0.interrupt_line;
105 	if (fIRQ == 0xff)
106 		fIRQ = 0;
107 
108 	if (fPCI->get_msi_count(fPCIDevice) >= 1) {
109 		uint32 vector;
110 		if (fPCI->configure_msi(fPCIDevice, 1, &vector) == B_OK
111 			&& fPCI->enable_msi(fPCIDevice) == B_OK) {
112 			TRACE("using MSI vector %" B_PRIu32 "\n", vector);
113 			fIRQ = vector;
114 			fUseMSI = true;
115 		} else {
116 			TRACE("couldn't use MSI\n");
117 		}
118 	}
119 	if (fIRQ == 0) {
120 		TRACE("Error: PCI IRQ not assigned\n");
121 		return B_ERROR;
122 	}
123 
124 	phys_addr_t addr = pciInfo.u.h0.base_registers[5];
125 	size_t size = pciInfo.u.h0.base_register_sizes[5];
126 
127 	TRACE("registers at %#" B_PRIxPHYSADDR ", size %#" B_PRIxSIZE "\n", addr,
128 		size);
129 	if (addr == 0) {
130 		TRACE("PCI base address register 5 not assigned\n");
131 		return B_ERROR;
132 	}
133 
134 	fRegsArea = map_mem((void **)&fRegs, addr, size, B_KERNEL_READ_AREA | B_KERNEL_WRITE_AREA,
135 		"AHCI HBA regs");
136 	if (fRegsArea < B_OK) {
137 		TRACE("mapping registers failed\n");
138 		return B_ERROR;
139 	}
140 
141 	// make sure interrupts are disabled
142 	fRegs->ghc &= ~GHC_IE;
143 	FlushPostedWrites();
144 
145 	if (ResetController() < B_OK) {
146 		TRACE("controller reset failed\n");
147 		goto err;
148 	}
149 
150 	fCommandSlotCount = 1 + ((fRegs->cap >> CAP_NCS_SHIFT) & CAP_NCS_MASK);
151 	fPortCount = 1 + ((fRegs->cap >> CAP_NP_SHIFT) & CAP_NP_MASK);
152 
153 	fPortImplementedMask = fRegs->pi;
154 
155 	// reported mask of implemented ports is sometimes empty
156 	if (fPortImplementedMask == 0) {
157 		fPortImplementedMask = 0xffffffff >> (32 - fPortCount);
158 		TRACE("ports-implemented mask is zero, using 0x%" B_PRIx32 " instead.\n",
159 			fPortImplementedMask);
160 	}
161 
162 	// reported number of ports is sometimes too small
163 	int highestPort;
164 	highestPort = fls(fPortImplementedMask); // 1-based, 1 to 32
165 	if (fPortCount < highestPort) {
166 		TRACE("reported number of ports is wrong, using %d instead.\n", highestPort);
167 		fPortCount = highestPort;
168 	}
169 
170 	TRACE("cap: Interface Speed Support: generation %" B_PRIu32 "\n",
171 		(fRegs->cap >> CAP_ISS_SHIFT) & CAP_ISS_MASK);
172 	TRACE("cap: Number of Command Slots: %d (raw %#" B_PRIx32 ")\n",
173 		fCommandSlotCount, (fRegs->cap >> CAP_NCS_SHIFT) & CAP_NCS_MASK);
174 	TRACE("cap: Number of Ports: %d (raw %#" B_PRIx32 ")\n", fPortCount,
175 		(fRegs->cap >> CAP_NP_SHIFT) & CAP_NP_MASK);
176 	TRACE("cap: Supports Port Multiplier: %s\n",
177 		(fRegs->cap & CAP_SPM) ? "yes" : "no");
178 	TRACE("cap: Supports External SATA: %s\n",
179 		(fRegs->cap & CAP_SXS) ? "yes" : "no");
180 	TRACE("cap: Enclosure Management Supported: %s\n",
181 		(fRegs->cap & CAP_EMS) ? "yes" : "no");
182 
183 	TRACE("cap: FIS-based Switching Control: %s\n",
184 		(fRegs->cap & CAP_FBSS) ? "yes" : "no");
185 
186 	TRACE("cap: Supports Command List Override: %s\n",
187 		(fRegs->cap & CAP_SCLO) ? "yes" : "no");
188 	TRACE("cap: Supports Staggered Spin-up: %s\n",
189 		(fRegs->cap & CAP_SSS) ? "yes" : "no");
190 	TRACE("cap: Supports Mechanical Presence Switch: %s\n",
191 		(fRegs->cap & CAP_SMPS) ? "yes" : "no");
192 
193 	TRACE("cap: Supports 64-bit Addressing: %s\n",
194 		(fRegs->cap & CAP_S64A) ? "yes" : "no");
195 	TRACE("cap: Supports Native Command Queuing: %s\n",
196 		(fRegs->cap & CAP_SNCQ) ? "yes" : "no");
197 	TRACE("cap: Supports SNotification Register: %s\n",
198 		(fRegs->cap & CAP_SSNTF) ? "yes" : "no");
199 	TRACE("cap: Supports Command List Override: %s\n",
200 		(fRegs->cap & CAP_SCLO) ? "yes" : "no");
201 
202 	TRACE("cap: Supports AHCI mode only: %s\n",			(fRegs->cap & CAP_SAM) ? "yes" : "no");
203 
204 	if (fRegs->vs >= 0x00010200) {
205 		TRACE("cap2: DevSleep Entrance from Slumber Only: %s\n",
206 			(fRegs->cap2 & CAP2_DESO) ? "yes" : "no");
207 		TRACE("cap2: Supports Aggressive Device Sleep Management: %s\n",
208 			(fRegs->cap2 & CAP2_SADM) ? "yes" : "no");
209 		TRACE("cap2: Supports Device Sleep: %s\n",
210 			(fRegs->cap2 & CAP2_SDS) ? "yes" : "no");
211 		TRACE("cap2: Automatic Partial to Slumber Transitions: %s\n",
212 			(fRegs->cap2 & CAP2_APST) ? "yes" : "no");
213 		TRACE("cap2: NVMHCI Present: %s\n",
214 			(fRegs->cap2 & CAP2_NVMP) ? "yes" : "no");
215 		TRACE("cap2: BIOS/OS Handoff: %s\n",
216 			(fRegs->cap2 & CAP2_BOH) ? "yes" : "no");
217 	}
218 	TRACE("ghc: AHCI Enable: %s\n",	(fRegs->ghc & GHC_AE) ? "yes" : "no");
219 	TRACE("Ports Implemented Mask: %#08" B_PRIx32 " Number of Available Ports:"
220 		" %d\n", fPortImplementedMask, count_bits_set(fPortImplementedMask));
221 	TRACE("AHCI Version %02" B_PRIx32 "%02" B_PRIx32 ".%02" B_PRIx32 ".%02"
222 		B_PRIx32 " Interrupt %" B_PRIu32 "\n", fRegs->vs >> 24, (fRegs->vs >> 16) & 0xff,
223 		(fRegs->vs >> 8) & 0xff, fRegs->vs & 0xff, fIRQ);
224 
225 	// setup interrupt handler
226 	if (install_io_interrupt_handler(fIRQ, Interrupt, this, 0) < B_OK) {
227 		TRACE("can't install interrupt handler\n");
228 		goto err;
229 	}
230 
231 	for (int i = 0; i < fPortCount; i++) {
232 		if (fPortImplementedMask & (1 << i)) {
233 			fPort[i] = new (std::nothrow)AHCIPort(this, i);
234 			if (!fPort[i]) {
235 				TRACE("out of memory creating port %d\n", i);
236 				break;
237 			}
238 			status_t status = fPort[i]->Init1();
239 			if (status < B_OK) {
240 				TRACE("init-1 port %d failed\n", i);
241 				delete fPort[i];
242 				fPort[i] = NULL;
243 			}
244 		}
245 	}
246 
247 	// clear any pending interrupts
248 	uint32 interruptsPending;
249 	interruptsPending = fRegs->is;
250 	fRegs->is = interruptsPending;
251 	FlushPostedWrites();
252 
253 	// enable interrupts
254 	fRegs->ghc |= GHC_IE;
255 	FlushPostedWrites();
256 
257 	for (int i = 0; i < fPortCount; i++) {
258 		if (fPort[i]) {
259 			status_t status = fPort[i]->Init2();
260 			if (status < B_OK) {
261 				TRACE("init-2 port %d failed\n", i);
262 				fPort[i]->Uninit();
263 				delete fPort[i];
264 				fPort[i] = NULL;
265 			}
266 		}
267 	}
268 
269 
270 	return B_OK;
271 
272 err:
273 	delete_area(fRegsArea);
274 	return B_ERROR;
275 }
276 
277 
278 void
279 AHCIController::Uninit()
280 {
281 	TRACE("AHCIController::Uninit\n");
282 
283 	for (int i = 0; i < fPortCount; i++) {
284 		if (fPort[i]) {
285 			fPort[i]->Uninit();
286 			delete fPort[i];
287 		}
288 	}
289 
290 	// disable interrupts
291 	fRegs->ghc &= ~GHC_IE;
292 	FlushPostedWrites();
293 
294 	// clear pending interrupts
295 	fRegs->is = 0xffffffff;
296 	FlushPostedWrites();
297 
298 	// well...
299 	remove_io_interrupt_handler(fIRQ, Interrupt, this);
300 
301 	if (fUseMSI) {
302 		fPCI->disable_msi(fPCIDevice);
303 		fPCI->unconfigure_msi(fPCIDevice);
304 	}
305 
306 	delete_area(fRegsArea);
307 
308 // --- Instance check workaround begin
309 	delete_port(fInstanceCheck);
310 // --- Instance check workaround end
311 }
312 
313 
314 status_t
315 AHCIController::ResetController()
316 {
317 	uint32 saveCaps = fRegs->cap & (CAP_SMPS | CAP_SSS | CAP_SPM | CAP_EMS | CAP_SXS);
318 	uint32 savePI = fRegs->pi;
319 
320 	// AHCI 1.3: Software may perform an HBA reset prior to initializing the controller
321 	//           by setting GHC.AE to ‘1’ and then setting GHC.HR to ‘1’ if desired.
322 	fRegs->ghc |= GHC_AE;
323 	FlushPostedWrites();
324 	fRegs->ghc |= GHC_HR;
325 	FlushPostedWrites();
326 	if (wait_until_clear(&fRegs->ghc, GHC_HR, 1000000) < B_OK)
327 		return B_TIMED_OUT;
328 
329 	fRegs->ghc |= GHC_AE;
330 	FlushPostedWrites();
331 	fRegs->cap |= saveCaps;
332 	fRegs->pi = savePI;
333 	FlushPostedWrites();
334 
335 	if (fPCIVendorID == PCI_VENDOR_INTEL) {
336 		// Intel PCS—Port Control and Status
337 		// SATA port enable bits must be set
338 		int portCount = std::max(fls(fRegs->pi), 1 + (int)((fRegs->cap >> CAP_NP_SHIFT) & CAP_NP_MASK));
339 		if (portCount > 8) {
340 			// TODO: fix this when specification available
341 			TRACE("don't know how to enable SATA ports 9 to %d\n", portCount);
342 			portCount = 8;
343 		}
344 		// If not all ports are enabled, try to enable them. If they are already enabled, don't
345 		// rewrite the register.
346 		uint16 mask = 0xff >> (8 - portCount);
347 		uint16 pcs = fPCI->read_pci_config(fPCIDevice, 0x92, 2);
348 		if ((pcs & mask) != mask) {
349 			pcs |= (0xff >> (8 - portCount));
350 			fPCI->write_pci_config(fPCIDevice, 0x92, 2, pcs);
351 		}
352 	}
353 	return B_OK;
354 }
355 
356 
357 int32
358 AHCIController::Interrupt(void *data)
359 {
360 	AHCIController *self = (AHCIController *)data;
361 	uint32 interruptPending = self->fRegs->is & self->fPortImplementedMask;
362 
363 	if (interruptPending == 0)
364 		return B_UNHANDLED_INTERRUPT;
365 
366 	for (int i = 0; i < self->fPortCount; i++) {
367 		if (interruptPending & (1 << i)) {
368 			if (self->fPort[i]) {
369 				self->fPort[i]->Interrupt();
370 			} else {
371 				FLOW("interrupt on non-existent port %d\n", i);
372 			}
373 		}
374 	}
375 
376 	// clear pending interrupts
377 	self->fRegs->is = interruptPending;
378 
379 	return B_INVOKE_SCHEDULER;
380 }
381 
382 
383 void
384 AHCIController::ExecuteRequest(scsi_ccb *request)
385 {
386 	if (request->target_lun || !fPort[request->target_id]) {
387 		request->subsys_status = SCSI_DEV_NOT_THERE;
388 		gSCSI->finished(request, 1);
389 		return;
390 	}
391 
392 	fPort[request->target_id]->ScsiExecuteRequest(request);
393 }
394 
395 
396 uchar
397 AHCIController::AbortRequest(scsi_ccb *request)
398 {
399 	if (request->target_lun || !fPort[request->target_id])
400 		return SCSI_DEV_NOT_THERE;
401 
402 	return fPort[request->target_id]->ScsiAbortRequest(request);
403 }
404 
405 
406 uchar
407 AHCIController::TerminateRequest(scsi_ccb *request)
408 {
409 	if (request->target_lun || !fPort[request->target_id])
410 		return SCSI_DEV_NOT_THERE;
411 
412 	return fPort[request->target_id]->ScsiTerminateRequest(request);
413 }
414 
415 
416 uchar
417 AHCIController::ResetDevice(uchar targetID, uchar targetLUN)
418 {
419 	if (targetLUN || !fPort[targetID])
420 		return SCSI_DEV_NOT_THERE;
421 
422 	return fPort[targetID]->ScsiResetDevice();
423 }
424 
425 
426 void
427 AHCIController::GetRestrictions(uchar targetID, bool *isATAPI,
428 	bool *noAutoSense, uint32 *maxBlocks)
429 {
430 	if (!fPort[targetID])
431 		return;
432 
433 	fPort[targetID]->ScsiGetRestrictions(isATAPI, noAutoSense, maxBlocks);
434 }
435