xref: /freebsd/sys/dev/nfsmb/nfsmb.c (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * Copyright (c) 2005 Ruslan Ermilov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/param.h>
28 #include <sys/bus.h>
29 #include <sys/kernel.h>
30 #include <sys/lock.h>
31 #include <sys/module.h>
32 #include <sys/mutex.h>
33 #include <sys/systm.h>
34 
35 #include <machine/bus.h>
36 #include <machine/resource.h>
37 #include <sys/rman.h>
38 
39 #include <dev/pci/pcivar.h>
40 #include <dev/pci/pcireg.h>
41 
42 #include <dev/smbus/smbconf.h>
43 #include "smbus_if.h"
44 
45 #define	NFSMB_DEBUG(x)	if (nfsmb_debug) (x)
46 
47 #ifdef DEBUG
48 static int nfsmb_debug = 1;
49 #else
50 static int nfsmb_debug = 0;
51 #endif
52 
53 /* NVIDIA nForce2/3/4 MCP */
54 #define	NFSMB_VENDORID_NVIDIA		0x10de
55 #define	NFSMB_DEVICEID_NF2_SMB		0x0064
56 #define	NFSMB_DEVICEID_NF2_ULTRA_SMB	0x0084
57 #define	NFSMB_DEVICEID_NF3_PRO150_SMB	0x00d4
58 #define	NFSMB_DEVICEID_NF3_250GB_SMB	0x00e4
59 #define	NFSMB_DEVICEID_NF4_SMB		0x0052
60 #define	NFSMB_DEVICEID_NF4_04_SMB	0x0034
61 #define	NFSMB_DEVICEID_NF4_51_SMB	0x0264
62 #define	NFSMB_DEVICEID_NF4_55_SMB	0x0368
63 #define	NFSMB_DEVICEID_NF4_61_SMB	0x03eb
64 #define	NFSMB_DEVICEID_NF4_65_SMB	0x0446
65 #define	NFSMB_DEVICEID_NF4_67_SMB	0x0542
66 #define	NFSMB_DEVICEID_NF4_73_SMB	0x07d8
67 #define	NFSMB_DEVICEID_NF4_78S_SMB	0x0752
68 #define	NFSMB_DEVICEID_NF4_79_SMB	0x0aa2
69 
70 /* PCI Configuration space registers */
71 #define	NF2PCI_SMBASE_1		PCIR_BAR(4)
72 #define	NF2PCI_SMBASE_2		PCIR_BAR(5)
73 
74 /*
75  * ACPI 3.0, Chapter 12, SMBus Host Controller Interface.
76  */
77 #define	SMB_PRTCL		0x00	/* protocol */
78 #define	SMB_STS			0x01	/* status */
79 #define	SMB_ADDR		0x02	/* address */
80 #define	SMB_CMD			0x03	/* command */
81 #define	SMB_DATA		0x04	/* 32 data registers */
82 #define	SMB_BCNT		0x24	/* number of data bytes */
83 #define	SMB_ALRM_A		0x25	/* alarm address */
84 #define	SMB_ALRM_D		0x26	/* 2 bytes alarm data */
85 
86 #define	SMB_STS_DONE		0x80
87 #define	SMB_STS_ALRM		0x40
88 #define	SMB_STS_RES		0x20
89 #define	SMB_STS_STATUS		0x1f
90 #define	SMB_STS_OK		0x00	/* OK */
91 #define	SMB_STS_UF		0x07	/* Unknown Failure */
92 #define	SMB_STS_DANA		0x10	/* Device Address Not Acknowledged */
93 #define	SMB_STS_DED		0x11	/* Device Error Detected */
94 #define	SMB_STS_DCAD		0x12	/* Device Command Access Denied */
95 #define	SMB_STS_UE		0x13	/* Unknown Error */
96 #define	SMB_STS_DAD		0x17	/* Device Access Denied */
97 #define	SMB_STS_T		0x18	/* Timeout */
98 #define	SMB_STS_HUP		0x19	/* Host Unsupported Protocol */
99 #define	SMB_STS_B		0x1A	/* Busy */
100 #define	SMB_STS_PEC		0x1F	/* PEC (CRC-8) Error */
101 
102 #define	SMB_PRTCL_WRITE		0x00
103 #define	SMB_PRTCL_READ		0x01
104 #define	SMB_PRTCL_QUICK		0x02
105 #define	SMB_PRTCL_BYTE		0x04
106 #define	SMB_PRTCL_BYTE_DATA	0x06
107 #define	SMB_PRTCL_WORD_DATA	0x08
108 #define	SMB_PRTCL_BLOCK_DATA	0x0a
109 #define	SMB_PRTCL_PROC_CALL	0x0c
110 #define	SMB_PRTCL_BLOCK_PROC_CALL 0x0d
111 #define	SMB_PRTCL_PEC		0x80
112 
113 struct nfsmb_softc {
114 	int rid;
115 	struct resource *res;
116 	device_t smbus;
117 	device_t subdev;
118 	struct mtx lock;
119 };
120 
121 #define	NFSMB_LOCK(nfsmb)		mtx_lock(&(nfsmb)->lock)
122 #define	NFSMB_UNLOCK(nfsmb)		mtx_unlock(&(nfsmb)->lock)
123 #define	NFSMB_LOCK_ASSERT(nfsmb)	mtx_assert(&(nfsmb)->lock, MA_OWNED)
124 
125 #define	NFSMB_SMBINB(nfsmb, register)					\
126 	(bus_read_1(nfsmb->res, register))
127 #define	NFSMB_SMBOUTB(nfsmb, register, value) \
128 	(bus_write_1(nfsmb->res, register, value))
129 
130 static int	nfsmb_detach(device_t dev);
131 static int	nfsmbsub_detach(device_t dev);
132 
133 static int
134 nfsmbsub_probe(device_t dev)
135 {
136 
137 	device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
138 	return (BUS_PROBE_DEFAULT);
139 }
140 
141 static int
142 nfsmb_probe(device_t dev)
143 {
144 	u_int16_t vid;
145 	u_int16_t did;
146 
147 	vid = pci_get_vendor(dev);
148 	did = pci_get_device(dev);
149 
150 	if (vid == NFSMB_VENDORID_NVIDIA) {
151 		switch(did) {
152 		case NFSMB_DEVICEID_NF2_SMB:
153 		case NFSMB_DEVICEID_NF2_ULTRA_SMB:
154 		case NFSMB_DEVICEID_NF3_PRO150_SMB:
155 		case NFSMB_DEVICEID_NF3_250GB_SMB:
156 		case NFSMB_DEVICEID_NF4_SMB:
157 		case NFSMB_DEVICEID_NF4_04_SMB:
158 		case NFSMB_DEVICEID_NF4_51_SMB:
159 		case NFSMB_DEVICEID_NF4_55_SMB:
160 		case NFSMB_DEVICEID_NF4_61_SMB:
161 		case NFSMB_DEVICEID_NF4_65_SMB:
162 		case NFSMB_DEVICEID_NF4_67_SMB:
163 		case NFSMB_DEVICEID_NF4_73_SMB:
164 		case NFSMB_DEVICEID_NF4_78S_SMB:
165 		case NFSMB_DEVICEID_NF4_79_SMB:
166 			device_set_desc(dev, "nForce2/3/4 MCP SMBus Controller");
167 			return (BUS_PROBE_DEFAULT);
168 		}
169 	}
170 
171 	return (ENXIO);
172 }
173 
174 static int
175 nfsmbsub_attach(device_t dev)
176 {
177 	device_t parent;
178 	struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
179 
180 	parent = device_get_parent(dev);
181 
182 	nfsmbsub_sc->rid = NF2PCI_SMBASE_2;
183 
184 	nfsmbsub_sc->res = bus_alloc_resource_any(parent, SYS_RES_IOPORT,
185 	    &nfsmbsub_sc->rid, RF_ACTIVE);
186 	if (nfsmbsub_sc->res == NULL) {
187 		/* Older incarnations of the device used non-standard BARs. */
188 		nfsmbsub_sc->rid = 0x54;
189 		nfsmbsub_sc->res = bus_alloc_resource_any(parent,
190 		    SYS_RES_IOPORT, &nfsmbsub_sc->rid, RF_ACTIVE);
191 		if (nfsmbsub_sc->res == NULL) {
192 			device_printf(dev, "could not map i/o space\n");
193 			return (ENXIO);
194 		}
195 	}
196 	mtx_init(&nfsmbsub_sc->lock, device_get_nameunit(dev), "nfsmb",
197 	    MTX_DEF);
198 
199 	nfsmbsub_sc->smbus = device_add_child(dev, "smbus", -1);
200 	if (nfsmbsub_sc->smbus == NULL) {
201 		nfsmbsub_detach(dev);
202 		return (EINVAL);
203 	}
204 
205 	bus_generic_attach(dev);
206 
207 	return (0);
208 }
209 
210 static int
211 nfsmb_attach(device_t dev)
212 {
213 	struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
214 
215 	/* Allocate I/O space */
216 	nfsmb_sc->rid = NF2PCI_SMBASE_1;
217 
218 	nfsmb_sc->res = bus_alloc_resource_any(dev, SYS_RES_IOPORT,
219 		&nfsmb_sc->rid, RF_ACTIVE);
220 
221 	if (nfsmb_sc->res == NULL) {
222 		/* Older incarnations of the device used non-standard BARs. */
223 		nfsmb_sc->rid = 0x50;
224 		nfsmb_sc->res = bus_alloc_resource_any(dev,
225 		    SYS_RES_IOPORT, &nfsmb_sc->rid, RF_ACTIVE);
226 		if (nfsmb_sc->res == NULL) {
227 			device_printf(dev, "could not map i/o space\n");
228 			return (ENXIO);
229 		}
230 	}
231 
232 	mtx_init(&nfsmb_sc->lock, device_get_nameunit(dev), "nfsmb", MTX_DEF);
233 
234 	/* Allocate a new smbus device */
235 	nfsmb_sc->smbus = device_add_child(dev, "smbus", -1);
236 	if (!nfsmb_sc->smbus) {
237 		nfsmb_detach(dev);
238 		return (EINVAL);
239 	}
240 
241 	nfsmb_sc->subdev = NULL;
242 	switch (pci_get_device(dev)) {
243 	case NFSMB_DEVICEID_NF2_SMB:
244 	case NFSMB_DEVICEID_NF2_ULTRA_SMB:
245 	case NFSMB_DEVICEID_NF3_PRO150_SMB:
246 	case NFSMB_DEVICEID_NF3_250GB_SMB:
247 	case NFSMB_DEVICEID_NF4_SMB:
248 	case NFSMB_DEVICEID_NF4_04_SMB:
249 	case NFSMB_DEVICEID_NF4_51_SMB:
250 	case NFSMB_DEVICEID_NF4_55_SMB:
251 	case NFSMB_DEVICEID_NF4_61_SMB:
252 	case NFSMB_DEVICEID_NF4_65_SMB:
253 	case NFSMB_DEVICEID_NF4_67_SMB:
254 	case NFSMB_DEVICEID_NF4_73_SMB:
255 	case NFSMB_DEVICEID_NF4_78S_SMB:
256 	case NFSMB_DEVICEID_NF4_79_SMB:
257 		/* Trying to add secondary device as slave */
258 		nfsmb_sc->subdev = device_add_child(dev, "nfsmb", -1);
259 		if (!nfsmb_sc->subdev) {
260 			nfsmb_detach(dev);
261 			return (EINVAL);
262 		}
263 		break;
264 	default:
265 		break;
266 	}
267 
268 	bus_generic_attach(dev);
269 
270 	return (0);
271 }
272 
273 static int
274 nfsmbsub_detach(device_t dev)
275 {
276 	device_t parent;
277 	struct nfsmb_softc *nfsmbsub_sc = device_get_softc(dev);
278 
279 	parent = device_get_parent(dev);
280 
281 	if (nfsmbsub_sc->smbus) {
282 		device_delete_child(dev, nfsmbsub_sc->smbus);
283 		nfsmbsub_sc->smbus = NULL;
284 	}
285 	mtx_destroy(&nfsmbsub_sc->lock);
286 	if (nfsmbsub_sc->res) {
287 		bus_release_resource(parent, SYS_RES_IOPORT, nfsmbsub_sc->rid,
288 		    nfsmbsub_sc->res);
289 		nfsmbsub_sc->res = NULL;
290 	}
291 	return (0);
292 }
293 
294 static int
295 nfsmb_detach(device_t dev)
296 {
297 	struct nfsmb_softc *nfsmb_sc = device_get_softc(dev);
298 
299 	if (nfsmb_sc->subdev) {
300 		device_delete_child(dev, nfsmb_sc->subdev);
301 		nfsmb_sc->subdev = NULL;
302 	}
303 
304 	if (nfsmb_sc->smbus) {
305 		device_delete_child(dev, nfsmb_sc->smbus);
306 		nfsmb_sc->smbus = NULL;
307 	}
308 
309 	mtx_destroy(&nfsmb_sc->lock);
310 	if (nfsmb_sc->res) {
311 		bus_release_resource(dev, SYS_RES_IOPORT, nfsmb_sc->rid,
312 		    nfsmb_sc->res);
313 		nfsmb_sc->res = NULL;
314 	}
315 
316 	return (0);
317 }
318 
319 static int
320 nfsmb_callback(device_t dev, int index, void *data)
321 {
322 	int error = 0;
323 
324 	switch (index) {
325 	case SMB_REQUEST_BUS:
326 	case SMB_RELEASE_BUS:
327 		break;
328 	default:
329 		error = EINVAL;
330 	}
331 
332 	return (error);
333 }
334 
335 static int
336 nfsmb_wait(struct nfsmb_softc *sc)
337 {
338 	u_char sts;
339 	int error, count;
340 
341 	NFSMB_LOCK_ASSERT(sc);
342 	if (NFSMB_SMBINB(sc, SMB_PRTCL) != 0)
343 	{
344 		count = 10000;
345 		do {
346 			DELAY(500);
347 		} while (NFSMB_SMBINB(sc, SMB_PRTCL) != 0 && count--);
348 		if (count == 0)
349 			return (SMB_ETIMEOUT);
350 	}
351 
352 	sts = NFSMB_SMBINB(sc, SMB_STS) & SMB_STS_STATUS;
353 	NFSMB_DEBUG(printf("nfsmb: STS=0x%x\n", sts));
354 
355 	switch (sts) {
356 	case SMB_STS_OK:
357 		error = SMB_ENOERR;
358 		break;
359 	case SMB_STS_DANA:
360 		error = SMB_ENOACK;
361 		break;
362 	case SMB_STS_B:
363 		error = SMB_EBUSY;
364 		break;
365 	case SMB_STS_T:
366 		error = SMB_ETIMEOUT;
367 		break;
368 	case SMB_STS_DCAD:
369 	case SMB_STS_DAD:
370 	case SMB_STS_HUP:
371 		error = SMB_ENOTSUPP;
372 		break;
373 	default:
374 		error = SMB_EBUSERR;
375 		break;
376 	}
377 
378 	return (error);
379 }
380 
381 static int
382 nfsmb_quick(device_t dev, u_char slave, int how)
383 {
384 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
385 	u_char protocol;
386 	int error;
387 
388 	protocol = SMB_PRTCL_QUICK;
389 
390 	switch (how) {
391 	case SMB_QWRITE:
392 		protocol |= SMB_PRTCL_WRITE;
393 		NFSMB_DEBUG(printf("nfsmb: QWRITE to 0x%x", slave));
394 		break;
395 	case SMB_QREAD:
396 		protocol |= SMB_PRTCL_READ;
397 		NFSMB_DEBUG(printf("nfsmb: QREAD to 0x%x", slave));
398 		break;
399 	default:
400 		panic("%s: unknown QUICK command (%x)!", __func__, how);
401 	}
402 
403 	NFSMB_LOCK(sc);
404 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
405 	NFSMB_SMBOUTB(sc, SMB_PRTCL, protocol);
406 
407 	error = nfsmb_wait(sc);
408 
409 	NFSMB_DEBUG(printf(", error=0x%x\n", error));
410 	NFSMB_UNLOCK(sc);
411 
412 	return (error);
413 }
414 
415 static int
416 nfsmb_sendb(device_t dev, u_char slave, char byte)
417 {
418 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
419 	int error;
420 
421 	NFSMB_LOCK(sc);
422 	NFSMB_SMBOUTB(sc, SMB_CMD, byte);
423 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
424 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE);
425 
426 	error = nfsmb_wait(sc);
427 
428 	NFSMB_DEBUG(printf("nfsmb: SENDB to 0x%x, byte=0x%x, error=0x%x\n", slave, byte, error));
429 	NFSMB_UNLOCK(sc);
430 
431 	return (error);
432 }
433 
434 static int
435 nfsmb_recvb(device_t dev, u_char slave, char *byte)
436 {
437 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
438 	int error;
439 
440 	NFSMB_LOCK(sc);
441 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
442 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE);
443 
444 	if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
445 		*byte = NFSMB_SMBINB(sc, SMB_DATA);
446 
447 	NFSMB_DEBUG(printf("nfsmb: RECVB from 0x%x, byte=0x%x, error=0x%x\n", slave, *byte, error));
448 	NFSMB_UNLOCK(sc);
449 
450 	return (error);
451 }
452 
453 static int
454 nfsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
455 {
456 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
457 	int error;
458 
459 	NFSMB_LOCK(sc);
460 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
461 	NFSMB_SMBOUTB(sc, SMB_DATA, byte);
462 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
463 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BYTE_DATA);
464 
465 	error = nfsmb_wait(sc);
466 
467 	NFSMB_DEBUG(printf("nfsmb: WRITEB to 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, byte, error));
468 	NFSMB_UNLOCK(sc);
469 
470 	return (error);
471 }
472 
473 static int
474 nfsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
475 {
476 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
477 	int error;
478 
479 	NFSMB_LOCK(sc);
480 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
481 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
482 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BYTE_DATA);
483 
484 	if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
485 		*byte = NFSMB_SMBINB(sc, SMB_DATA);
486 
487 	NFSMB_DEBUG(printf("nfsmb: READB from 0x%x, cmd=0x%x, byte=0x%x, error=0x%x\n", slave, cmd, (unsigned char)*byte, error));
488 	NFSMB_UNLOCK(sc);
489 
490 	return (error);
491 }
492 
493 static int
494 nfsmb_writew(device_t dev, u_char slave, char cmd, short word)
495 {
496 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
497 	int error;
498 
499 	NFSMB_LOCK(sc);
500 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
501 	NFSMB_SMBOUTB(sc, SMB_DATA, word);
502 	NFSMB_SMBOUTB(sc, SMB_DATA + 1, word >> 8);
503 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
504 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_WORD_DATA);
505 
506 	error = nfsmb_wait(sc);
507 
508 	NFSMB_DEBUG(printf("nfsmb: WRITEW to 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, word, error));
509 	NFSMB_UNLOCK(sc);
510 
511 	return (error);
512 }
513 
514 static int
515 nfsmb_readw(device_t dev, u_char slave, char cmd, short *word)
516 {
517 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
518 	int error;
519 
520 	NFSMB_LOCK(sc);
521 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
522 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
523 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_WORD_DATA);
524 
525 	if ((error = nfsmb_wait(sc)) == SMB_ENOERR)
526 		*word = NFSMB_SMBINB(sc, SMB_DATA) |
527 		    (NFSMB_SMBINB(sc, SMB_DATA + 1) << 8);
528 
529 	NFSMB_DEBUG(printf("nfsmb: READW from 0x%x, cmd=0x%x, word=0x%x, error=0x%x\n", slave, cmd, (unsigned short)*word, error));
530 	NFSMB_UNLOCK(sc);
531 
532 	return (error);
533 }
534 
535 static int
536 nfsmb_bwrite(device_t dev, u_char slave, char cmd, u_char count, char *buf)
537 {
538 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
539 	u_char i;
540 	int error;
541 
542 	if (count < 1 || count > 32)
543 		return (SMB_EINVAL);
544 
545 	NFSMB_LOCK(sc);
546 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
547 	NFSMB_SMBOUTB(sc, SMB_BCNT, count);
548 	for (i = 0; i < count; i++)
549 		NFSMB_SMBOUTB(sc, SMB_DATA + i, buf[i]);
550 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
551 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_WRITE | SMB_PRTCL_BLOCK_DATA);
552 
553 	error = nfsmb_wait(sc);
554 
555 	NFSMB_DEBUG(printf("nfsmb: WRITEBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, count, cmd, error));
556 	NFSMB_UNLOCK(sc);
557 
558 	return (error);
559 }
560 
561 static int
562 nfsmb_bread(device_t dev, u_char slave, char cmd, u_char *count, char *buf)
563 {
564 	struct nfsmb_softc *sc = (struct nfsmb_softc *)device_get_softc(dev);
565 	u_char data, len, i;
566 	int error;
567 
568 	if (*count < 1 || *count > 32)
569 		return (SMB_EINVAL);
570 
571 	NFSMB_LOCK(sc);
572 	NFSMB_SMBOUTB(sc, SMB_CMD, cmd);
573 	NFSMB_SMBOUTB(sc, SMB_ADDR, slave);
574 	NFSMB_SMBOUTB(sc, SMB_PRTCL, SMB_PRTCL_READ | SMB_PRTCL_BLOCK_DATA);
575 
576 	if ((error = nfsmb_wait(sc)) == SMB_ENOERR) {
577 		len = NFSMB_SMBINB(sc, SMB_BCNT);
578 		for (i = 0; i < len; i++) {
579 			data = NFSMB_SMBINB(sc, SMB_DATA + i);
580 			if (i < *count)
581 				buf[i] = data;
582 		}
583 		*count = len;
584 	}
585 
586 	NFSMB_DEBUG(printf("nfsmb: READBLK to 0x%x, count=0x%x, cmd=0x%x, error=0x%x", slave, *count, cmd, error));
587 	NFSMB_UNLOCK(sc);
588 
589 	return (error);
590 }
591 
592 static device_method_t nfsmb_methods[] = {
593 	/* Device interface */
594 	DEVMETHOD(device_probe,		nfsmb_probe),
595 	DEVMETHOD(device_attach,	nfsmb_attach),
596 	DEVMETHOD(device_detach,	nfsmb_detach),
597 
598 	/* SMBus interface */
599 	DEVMETHOD(smbus_callback,	nfsmb_callback),
600 	DEVMETHOD(smbus_quick,		nfsmb_quick),
601 	DEVMETHOD(smbus_sendb,		nfsmb_sendb),
602 	DEVMETHOD(smbus_recvb,		nfsmb_recvb),
603 	DEVMETHOD(smbus_writeb,		nfsmb_writeb),
604 	DEVMETHOD(smbus_readb,		nfsmb_readb),
605 	DEVMETHOD(smbus_writew,		nfsmb_writew),
606 	DEVMETHOD(smbus_readw,		nfsmb_readw),
607 	DEVMETHOD(smbus_bwrite,		nfsmb_bwrite),
608 	DEVMETHOD(smbus_bread,		nfsmb_bread),
609 	{ 0, 0 }
610 };
611 
612 static device_method_t nfsmbsub_methods[] = {
613 	/* Device interface */
614 	DEVMETHOD(device_probe,		nfsmbsub_probe),
615 	DEVMETHOD(device_attach,	nfsmbsub_attach),
616 	DEVMETHOD(device_detach,	nfsmbsub_detach),
617 
618 	/* SMBus interface */
619 	DEVMETHOD(smbus_callback,	nfsmb_callback),
620 	DEVMETHOD(smbus_quick,		nfsmb_quick),
621 	DEVMETHOD(smbus_sendb,		nfsmb_sendb),
622 	DEVMETHOD(smbus_recvb,		nfsmb_recvb),
623 	DEVMETHOD(smbus_writeb,		nfsmb_writeb),
624 	DEVMETHOD(smbus_readb,		nfsmb_readb),
625 	DEVMETHOD(smbus_writew,		nfsmb_writew),
626 	DEVMETHOD(smbus_readw,		nfsmb_readw),
627 	DEVMETHOD(smbus_bwrite,		nfsmb_bwrite),
628 	DEVMETHOD(smbus_bread,		nfsmb_bread),
629 	{ 0, 0 }
630 };
631 
632 static driver_t nfsmb_driver = {
633 	"nfsmb",
634 	nfsmb_methods,
635 	sizeof(struct nfsmb_softc),
636 };
637 
638 static driver_t nfsmbsub_driver = {
639 	"nfsmb",
640 	nfsmbsub_methods,
641 	sizeof(struct nfsmb_softc),
642 };
643 
644 DRIVER_MODULE(nfsmb, pci, nfsmb_driver, 0, 0);
645 DRIVER_MODULE(nfsmb, nfsmb, nfsmbsub_driver, 0, 0);
646 DRIVER_MODULE(smbus, nfsmb, smbus_driver, 0, 0);
647 
648 MODULE_DEPEND(nfsmb, pci, 1, 1, 1);
649 MODULE_DEPEND(nfsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
650 MODULE_VERSION(nfsmb, 1);
651