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