1 /* 2 * usbgem.h: General USB to Ethernet MAC driver framework 3 * @(#)usbgem.h 1.4 12/02/09 4 * (C) Copyright 2003-2009 Masayuki Murayama KHF04453@nifty.ne.jp 5 */ 6 7 #ifndef __USBGEM_H__ 8 #define __USBGEM_H__ 9 10 #pragma ident "@(#)usbgem.h 1.4 12/02/09" 11 12 #include <sys/mac.h> 13 #ifndef MAC_VERSION 14 #include <sys/mac_provider.h> 15 #endif 16 #include <sys/mac_ether.h> 17 18 /* 19 * Useful macros and typedefs 20 */ 21 #define USBGEM_NAME_LEN 32 22 23 #define USBGEM_TX_TIMEOUT (drv_usectohz(3*1000000)) 24 #define USBGEM_TX_TIMEOUT_INTERVAL (drv_usectohz(1*1000000)) 25 #define USBGEM_LINK_WATCH_INTERVAL (drv_usectohz(1*1000000)) 26 27 /* general return code */ 28 #define USBGEM_SUCCESS 0 29 #define USBGEM_FAILURE 1 30 31 /* return code of usbgem_tx_done */ 32 #define INTR_RESTART_TX 0x80000000U 33 34 struct usbgem_stats { 35 uint32_t intr; 36 37 uint32_t crc; 38 uint32_t errrcv; 39 uint32_t overflow; 40 uint32_t frame; 41 uint32_t missed; 42 uint32_t runt; 43 uint32_t frame_too_long; 44 uint32_t norcvbuf; 45 uint32_t sqe; 46 47 uint32_t collisions; 48 uint32_t first_coll; 49 uint32_t multi_coll; 50 uint32_t excoll; 51 uint32_t xmit_internal_err; 52 uint32_t nocarrier; 53 uint32_t defer; 54 uint32_t errxmt; 55 uint32_t underflow; 56 uint32_t xmtlatecoll; 57 uint32_t noxmtbuf; 58 uint32_t jabber; 59 60 61 uint64_t rbytes; 62 uint64_t obytes; 63 uint64_t rpackets; 64 uint64_t opackets; 65 uint32_t rbcast; 66 uint32_t obcast; 67 uint32_t rmcast; 68 uint32_t omcast; 69 uint32_t rcv_internal_err; 70 }; 71 72 struct mcast_addr { 73 struct ether_addr addr; 74 uint32_t hash; 75 }; 76 77 #define USBGEM_MAXMC 64 78 #define USBGEM_MCALLOC (sizeof (struct mcast_addr) * USBGEM_MAXMC) 79 80 #define SLOT(dp, n) ((n) % (dp)->ugc.usbgc_tx_list_max) 81 82 /* 83 * mac soft state 84 */ 85 struct usbgem_dev { 86 dev_info_t *dip; 87 mac_handle_t mh; 88 char name[USBGEM_NAME_LEN]; 89 90 /* pointer to usb private data */ 91 usb_client_dev_data_t *reg_data; 92 93 /* usb handles */ 94 usb_pipe_handle_t default_pipe; 95 usb_pipe_handle_t bulkin_pipe; 96 usb_pipe_handle_t bulkout_pipe; 97 usb_pipe_handle_t intr_pipe; 98 99 /* usb endpoints */ 100 usb_ep_descr_t *ep_default; 101 usb_ep_descr_t *ep_bulkin; 102 usb_ep_descr_t *ep_bulkout; 103 usb_ep_descr_t *ep_intr; 104 105 /* usb policies */ 106 usb_pipe_policy_t policy_default; 107 usb_pipe_policy_t policy_bulkin; 108 usb_pipe_policy_t policy_bulkout; 109 usb_pipe_policy_t policy_interrupt; 110 111 /* MAC address information */ 112 struct ether_addr cur_addr; 113 struct ether_addr dev_addr; 114 115 /* RX state and resource management */ 116 kmutex_t rxlock; 117 int rx_busy_cnt; 118 boolean_t rx_active; 119 kcondvar_t rx_drain_cv; 120 121 /* RX buffer management */ 122 int rx_buf_len; 123 124 /* TX state and resource management */ 125 kmutex_t txlock; 126 int tx_busy_cnt; 127 usb_bulk_req_t *tx_free_list; 128 kcondvar_t tx_drain_cv; 129 clock_t tx_start_time; 130 int bulkout_timeout; /* in second */ 131 int tx_max_packets; 132 int tx_seq_num; 133 int tx_intr_pended; 134 135 /* NIC state from OS view */ 136 int nic_state; 137 #define NIC_STATE_UNKNOWN 0 138 #define NIC_STATE_STOPPED 1 139 #define NIC_STATE_INITIALIZED 2 140 #define NIC_STATE_ONLINE 3 141 142 /* MAC state from hardware view */ 143 int mac_state; 144 #define MAC_STATE_DISCONNECTED 0 /* it includes suspended state too */ 145 #define MAC_STATE_STOPPED 1 /* powered up / buf not initialized */ 146 #define MAC_STATE_INITIALIZED 2 /* initialized */ 147 #define MAC_STATE_ONLINE 3 /* working correctly */ 148 #define MAC_STATE_ERROR 4 /* need to restart nic */ 149 150 clock_t fatal_error; 151 152 /* robustness: timer and watchdog */ 153 uint_t tx_watcher_stop; 154 kt_did_t tx_watcher_did; 155 kcondvar_t tx_watcher_cv; 156 kmutex_t tx_watcher_lock; 157 clock_t tx_watcher_timeout; 158 clock_t tx_watcher_interval; 159 160 /* MII mamagement */ 161 boolean_t anadv_autoneg:1; 162 boolean_t anadv_1000fdx:1; 163 boolean_t anadv_1000hdx:1; 164 boolean_t anadv_100t4:1; 165 boolean_t anadv_100fdx:1; 166 boolean_t anadv_100hdx:1; 167 boolean_t anadv_10fdx:1; 168 boolean_t anadv_10hdx:1; 169 boolean_t anadv_1000t_ms:2; 170 boolean_t anadv_pause:1; 171 boolean_t anadv_asmpause:1; 172 boolean_t mii_advert_ro:1; 173 174 boolean_t full_duplex:1; 175 int speed:3; 176 #define USBGEM_SPD_10 0 177 #define USBGEM_SPD_100 1 178 #define USBGEM_SPD_1000 2 179 #define USBGEM_SPD_NUM 3 180 unsigned int flow_control:2; 181 #define FLOW_CONTROL_NONE 0 182 #define FLOW_CONTROL_SYMMETRIC 1 183 #define FLOW_CONTROL_TX_PAUSE 2 184 #define FLOW_CONTROL_RX_PAUSE 3 185 186 boolean_t mii_supress_msg:1; 187 188 uint32_t mii_phy_id; 189 uint16_t mii_status; 190 uint16_t mii_advert; 191 uint16_t mii_lpable; 192 uint16_t mii_exp; 193 uint16_t mii_ctl1000; 194 uint16_t mii_stat1000; 195 uint16_t mii_xstatus; 196 int8_t mii_phy_addr; /* must be signed */ 197 198 uint16_t mii_status_ro; 199 uint16_t mii_xstatus_ro; 200 201 int mii_state; 202 #define MII_STATE_UNKNOWN 0 203 #define MII_STATE_RESETTING 1 204 #define MII_STATE_AUTONEGOTIATING 2 205 #define MII_STATE_AN_DONE 3 206 #define MII_STATE_MEDIA_SETUP 4 207 #define MII_STATE_LINKUP 5 208 #define MII_STATE_LINKDOWN 6 209 210 clock_t mii_last_check; /* in tick */ 211 clock_t mii_timer; /* in tick */ 212 #define MII_RESET_TIMEOUT drv_usectohz(1000*1000) 213 #define MII_AN_TIMEOUT drv_usectohz(5000*1000) 214 #define MII_LINKDOWN_TIMEOUT drv_usectohz(10000*1000) 215 216 clock_t mii_interval; /* in tick */ 217 clock_t linkup_delay; /* in tick */ 218 219 uint_t link_watcher_stop; 220 kt_did_t link_watcher_did; 221 kcondvar_t link_watcher_wait_cv; 222 kmutex_t link_watcher_lock; 223 224 krwlock_t dev_state_lock; /* mac_state and nic_state */ 225 ksema_t hal_op_lock; /* serialize hw operations */ 226 ksema_t drv_op_lock; /* hotplug op lock */ 227 228 /* multcast list */ 229 ksema_t rxfilter_lock; 230 int mc_count; 231 int mc_count_req; 232 struct mcast_addr *mc_list; 233 int rxmode; 234 #define RXMODE_PROMISC 0x01 235 #define RXMODE_ALLMULTI_REQ 0x02 236 #define RXMODE_MULTI_OVF 0x04 237 #define RXMODE_ENABLE 0x08 238 #define RXMODE_ALLMULTI (RXMODE_ALLMULTI_REQ | RXMODE_MULTI_OVF) 239 #define RXMODE_BITS \ 240 "\020" \ 241 "\004ENABLE" \ 242 "\003MULTI_OVF" \ 243 "\002ALLMULTI_REQ" \ 244 "\001PROMISC" 245 246 /* statistcs */ 247 struct usbgem_stats stats; 248 249 /* pointer to local structure */ 250 void *private; 251 int priv_size; 252 253 /* configuration */ 254 struct usbgem_conf { 255 /* name */ 256 char usbgc_name[USBGEM_NAME_LEN]; 257 int usbgc_ppa; 258 259 /* specification on usb */ 260 int usbgc_ifnum; /* interface number */ 261 int usbgc_alt; /* alternate */ 262 263 /* specification on tx engine */ 264 int usbgc_tx_list_max; 265 266 /* specification on rx engine */ 267 int usbgc_rx_header_len; 268 int usbgc_rx_list_max; 269 270 /* time out parameters */ 271 clock_t usbgc_tx_timeout; 272 clock_t usbgc_tx_timeout_interval; 273 274 /* flow control */ 275 int usbgc_flow_control; 276 277 /* MII timeout parameters */ 278 clock_t usbgc_mii_linkdown_timeout; 279 clock_t usbgc_mii_link_watch_interval; 280 clock_t usbgc_mii_reset_timeout; 281 282 clock_t usbgc_mii_an_watch_interval; 283 clock_t usbgc_mii_an_timeout; 284 clock_t usbgc_mii_an_wait; 285 clock_t usbgc_mii_an_delay; 286 287 /* MII configuration */ 288 int usbgc_mii_addr_min; 289 int usbgc_mii_linkdown_action; 290 int usbgc_mii_linkdown_timeout_action; 291 #define MII_ACTION_NONE 0 292 #define MII_ACTION_RESET 1 293 #define MII_ACTION_RSA 2 294 boolean_t usbgc_mii_dont_reset:1; 295 boolean_t usbgc_mii_an_oneshot:1; 296 boolean_t usbgc_mii_hw_link_detection:1; 297 boolean_t usbgc_mii_stop_mac_on_linkdown:1; 298 uint16_t usbgc_mii_an_cmd; 299 300 /* I/O methods */ 301 302 /* mac operation */ 303 int (*usbgc_attach_chip)(struct usbgem_dev *dp); 304 int (*usbgc_reset_chip)(struct usbgem_dev *dp); 305 int (*usbgc_init_chip)(struct usbgem_dev *dp); 306 int (*usbgc_start_chip)(struct usbgem_dev *dp); 307 int (*usbgc_stop_chip)(struct usbgem_dev *dp); 308 uint32_t (*usbgc_multicast_hash)(struct usbgem_dev *dp, 309 const uint8_t *); 310 int (*usbgc_set_rx_filter)(struct usbgem_dev *dp); 311 int (*usbgc_set_media)(struct usbgem_dev *dp); 312 int (*usbgc_get_stats)(struct usbgem_dev *dp); 313 void (*usbgc_interrupt)(struct usbgem_dev *dp, mblk_t *mp); 314 315 /* packet manipulation */ 316 mblk_t *(*usbgc_tx_make_packet)(struct usbgem_dev *dp, 317 mblk_t *mp); 318 mblk_t *(*usbgc_rx_make_packet)(struct usbgem_dev *dp, 319 mblk_t *mp); 320 /* mii operations */ 321 int (*usbgc_mii_probe)(struct usbgem_dev *dp); 322 int (*usbgc_mii_init)(struct usbgem_dev *dp); 323 int (*usbgc_mii_config)(struct usbgem_dev *dp, int *errp); 324 uint16_t (*usbgc_mii_read)(struct usbgem_dev *dp, uint_t reg, 325 int *errp); 326 void (*usbgc_mii_write)(struct usbgem_dev *dp, uint_t reg, 327 uint16_t val, int *errp); 328 329 /* jumbo frame */ 330 int usbgc_max_mtu; 331 int usbgc_default_mtu; 332 int usbgc_min_mtu; 333 } ugc; 334 335 int misc_flag; 336 #define USBGEM_VLAN 0x0001 337 timeout_id_t intr_watcher_id; 338 339 /* buffer size */ 340 uint_t mtu; 341 342 /* performance tuning parameters */ 343 uint_t txthr; /* tx fifo threshoold */ 344 uint_t txmaxdma; /* tx max dma burst size */ 345 uint_t rxthr; /* rx fifo threshoold */ 346 uint_t rxmaxdma; /* tx max dma burst size */ 347 348 /* kstat stuff */ 349 kstat_t *ksp; 350 351 /* ndd stuff */ 352 caddr_t nd_data_p; 353 caddr_t nd_arg_p; 354 355 #ifdef USBGEM_DEBUG_LEVEL 356 int tx_cnt; 357 #endif 358 }; 359 360 /* 361 * Exported functions 362 */ 363 int usbgem_ctrl_out(struct usbgem_dev *dp, 364 uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len, 365 void *bp, int size); 366 367 int usbgem_ctrl_in(struct usbgem_dev *dp, 368 uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len, 369 void *bp, int size); 370 371 int usbgem_ctrl_out_val(struct usbgem_dev *dp, 372 uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len, 373 uint32_t v); 374 375 int usbgem_ctrl_in_val(struct usbgem_dev *dp, 376 uint8_t reqt, uint8_t req, uint16_t val, uint16_t ix, uint16_t len, 377 void *valp); 378 379 void usbgem_generate_macaddr(struct usbgem_dev *, uint8_t *); 380 boolean_t usbgem_get_mac_addr_conf(struct usbgem_dev *); 381 int usbgem_mii_probe_default(struct usbgem_dev *); 382 int usbgem_mii_init_default(struct usbgem_dev *); 383 int usbgem_mii_config_default(struct usbgem_dev *, int *errp); 384 void usbgem_mii_update_link(struct usbgem_dev *); 385 void usbgem_restart_tx(struct usbgem_dev *); 386 boolean_t usbgem_tx_done(struct usbgem_dev *, int); 387 void usbgem_receive(struct usbgem_dev *); 388 struct usbgem_dev *usbgem_do_attach(dev_info_t *, 389 struct usbgem_conf *, void *, int); 390 int usbgem_do_detach(dev_info_t *); 391 392 uint32_t usbgem_ether_crc_le(const uint8_t *addr); 393 uint32_t usbgem_ether_crc_be(const uint8_t *addr); 394 395 int usbgem_resume(dev_info_t *); 396 int usbgem_suspend(dev_info_t *); 397 int usbgem_quiesce(dev_info_t *); 398 399 #define USBGEM_STREAM_OPS(dev_ops, attach, detach) \ 400 DDI_DEFINE_STREAM_OPS(dev_ops, nulldev, nulldev, attach, detach, \ 401 nodev, NULL, D_MP, NULL, usbgem_quiesce) 402 403 int usbgem_mod_init(struct dev_ops *, char *); 404 void usbgem_mod_fini(struct dev_ops *); 405 406 #define USBGEM_GET_DEV(dip) \ 407 ((struct usbgem_dev *)(ddi_get_driver_private(dip))) 408 409 #endif /* __USBGEM_H__ */ 410