ioc3.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * SGI IOC3 master driver and IRQ demuxer
  3. *
  4. * Copyright (c) 2005 Stanislaw Skowronek <skylark@linux-mips.org>
  5. * Heavily based on similar work by:
  6. * Brent Casavant <bcasavan@sgi.com> - IOC4 master driver
  7. * Pat Gefre <pfg@sgi.com> - IOC3 serial port IRQ demuxer
  8. */
  9. #include <linux/errno.h>
  10. #include <linux/module.h>
  11. #include <linux/pci.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/spinlock.h>
  15. #include <linux/delay.h>
  16. #include <linux/ioc3.h>
  17. #include <linux/rwsem.h>
  18. #include <linux/slab.h>
  19. #define IOC3_PCI_SIZE 0x100000
  20. static LIST_HEAD(ioc3_devices);
  21. static int ioc3_counter;
  22. static DECLARE_RWSEM(ioc3_devices_rwsem);
  23. static struct ioc3_submodule *ioc3_submodules[IOC3_MAX_SUBMODULES];
  24. static struct ioc3_submodule *ioc3_ethernet;
  25. static DEFINE_RWLOCK(ioc3_submodules_lock);
  26. /* NIC probing code */
  27. #define GPCR_MLAN_EN 0x00200000 /* enable MCR to pin 8 */
  28. static inline unsigned mcr_pack(unsigned pulse, unsigned sample)
  29. {
  30. return (pulse << 10) | (sample << 2);
  31. }
  32. static int nic_wait(struct ioc3_driver_data *idd)
  33. {
  34. unsigned mcr;
  35. do {
  36. mcr = readl(&idd->vma->mcr);
  37. } while (!(mcr & 2));
  38. return mcr & 1;
  39. }
  40. static int nic_reset(struct ioc3_driver_data *idd)
  41. {
  42. int presence;
  43. unsigned long flags;
  44. local_irq_save(flags);
  45. writel(mcr_pack(500, 65), &idd->vma->mcr);
  46. presence = nic_wait(idd);
  47. local_irq_restore(flags);
  48. udelay(500);
  49. return presence;
  50. }
  51. static int nic_read_bit(struct ioc3_driver_data *idd)
  52. {
  53. int result;
  54. unsigned long flags;
  55. local_irq_save(flags);
  56. writel(mcr_pack(6, 13), &idd->vma->mcr);
  57. result = nic_wait(idd);
  58. local_irq_restore(flags);
  59. udelay(500);
  60. return result;
  61. }
  62. static void nic_write_bit(struct ioc3_driver_data *idd, int bit)
  63. {
  64. if (bit)
  65. writel(mcr_pack(6, 110), &idd->vma->mcr);
  66. else
  67. writel(mcr_pack(80, 30), &idd->vma->mcr);
  68. nic_wait(idd);
  69. }
  70. static unsigned nic_read_byte(struct ioc3_driver_data *idd)
  71. {
  72. unsigned result = 0;
  73. int i;
  74. for (i = 0; i < 8; i++)
  75. result = (result >> 1) | (nic_read_bit(idd) << 7);
  76. return result;
  77. }
  78. static void nic_write_byte(struct ioc3_driver_data *idd, int byte)
  79. {
  80. int i, bit;
  81. for (i = 8; i; i--) {
  82. bit = byte & 1;
  83. byte >>= 1;
  84. nic_write_bit(idd, bit);
  85. }
  86. }
  87. static unsigned long
  88. nic_find(struct ioc3_driver_data *idd, int *last, unsigned long addr)
  89. {
  90. int a, b, index, disc;
  91. nic_reset(idd);
  92. /* Search ROM. */
  93. nic_write_byte(idd, 0xF0);
  94. /* Algorithm from ``Book of iButton Standards''. */
  95. for (index = 0, disc = 0; index < 64; index++) {
  96. a = nic_read_bit(idd);
  97. b = nic_read_bit(idd);
  98. if (a && b) {
  99. printk(KERN_WARNING "IOC3 NIC search failed.\n");
  100. *last = 0;
  101. return 0;
  102. }
  103. if (!a && !b) {
  104. if (index == *last) {
  105. addr |= 1UL << index;
  106. } else if (index > *last) {
  107. addr &= ~(1UL << index);
  108. disc = index;
  109. } else if ((addr & (1UL << index)) == 0)
  110. disc = index;
  111. nic_write_bit(idd, (addr>>index)&1);
  112. continue;
  113. } else {
  114. if (a)
  115. addr |= 1UL << index;
  116. else
  117. addr &= ~(1UL << index);
  118. nic_write_bit(idd, a);
  119. continue;
  120. }
  121. }
  122. *last = disc;
  123. return addr;
  124. }
  125. static void nic_addr(struct ioc3_driver_data *idd, unsigned long addr)
  126. {
  127. int index;
  128. nic_reset(idd);
  129. nic_write_byte(idd, 0xF0);
  130. for (index = 0; index < 64; index++) {
  131. nic_read_bit(idd);
  132. nic_read_bit(idd);
  133. nic_write_bit(idd, (addr>>index)&1);
  134. }
  135. }
  136. static void crc16_byte(unsigned int *crc, unsigned char db)
  137. {
  138. int i;
  139. for(i=0;i<8;i++) {
  140. *crc <<= 1;
  141. if((db^(*crc>>16)) & 1)
  142. *crc ^= 0x8005;
  143. db >>= 1;
  144. }
  145. *crc &= 0xFFFF;
  146. }
  147. static unsigned int crc16_area(unsigned char *dbs, int size, unsigned int crc)
  148. {
  149. while(size--)
  150. crc16_byte(&crc, *(dbs++));
  151. return crc;
  152. }
  153. static void crc8_byte(unsigned int *crc, unsigned char db)
  154. {
  155. int i,f;
  156. for(i=0;i<8;i++) {
  157. f = (*crc ^ db) & 1;
  158. *crc >>= 1;
  159. db >>= 1;
  160. if(f)
  161. *crc ^= 0x8c;
  162. }
  163. *crc &= 0xff;
  164. }
  165. static unsigned int crc8_addr(unsigned long addr)
  166. {
  167. int i;
  168. unsigned int crc = 0x00;
  169. for(i=0;i<8;i++)
  170. crc8_byte(&crc, addr>>(i<<3));
  171. return crc;
  172. }
  173. static void
  174. read_redir_page(struct ioc3_driver_data *idd, unsigned long addr, int page,
  175. unsigned char *redir, unsigned char *data)
  176. {
  177. int loops = 16, i;
  178. while(redir[page] != 0xFF) {
  179. page = redir[page]^0xFF;
  180. loops--;
  181. if(loops<0) {
  182. printk(KERN_ERR "IOC3: NIC circular redirection\n");
  183. return;
  184. }
  185. }
  186. loops = 3;
  187. while(loops>0) {
  188. nic_addr(idd, addr);
  189. nic_write_byte(idd, 0xF0);
  190. nic_write_byte(idd, (page << 5) & 0xE0);
  191. nic_write_byte(idd, (page >> 3) & 0x1F);
  192. for(i=0;i<0x20;i++)
  193. data[i] = nic_read_byte(idd);
  194. if(crc16_area(data, 0x20, 0x0000) == 0x800d)
  195. return;
  196. loops--;
  197. }
  198. printk(KERN_ERR "IOC3: CRC error in data page\n");
  199. for(i=0;i<0x20;i++)
  200. data[i] = 0x00;
  201. }
  202. static void
  203. read_redir_map(struct ioc3_driver_data *idd, unsigned long addr,
  204. unsigned char *redir)
  205. {
  206. int i,j,loops = 3,crc_ok;
  207. unsigned int crc;
  208. while(loops>0) {
  209. crc_ok = 1;
  210. nic_addr(idd, addr);
  211. nic_write_byte(idd, 0xAA);
  212. nic_write_byte(idd, 0x00);
  213. nic_write_byte(idd, 0x01);
  214. for(i=0;i<64;i+=8) {
  215. for(j=0;j<8;j++)
  216. redir[i+j] = nic_read_byte(idd);
  217. crc = crc16_area(redir+i, 8, (i==0)?0x8707:0x0000);
  218. crc16_byte(&crc, nic_read_byte(idd));
  219. crc16_byte(&crc, nic_read_byte(idd));
  220. if(crc != 0x800d)
  221. crc_ok = 0;
  222. }
  223. if(crc_ok)
  224. return;
  225. loops--;
  226. }
  227. printk(KERN_ERR "IOC3: CRC error in redirection page\n");
  228. for(i=0;i<64;i++)
  229. redir[i] = 0xFF;
  230. }
  231. static void read_nic(struct ioc3_driver_data *idd, unsigned long addr)
  232. {
  233. unsigned char redir[64];
  234. unsigned char data[64],part[32];
  235. int i,j;
  236. /* read redirections */
  237. read_redir_map(idd, addr, redir);
  238. /* read data pages */
  239. read_redir_page(idd, addr, 0, redir, data);
  240. read_redir_page(idd, addr, 1, redir, data+32);
  241. /* assemble the part # */
  242. j=0;
  243. for(i=0;i<19;i++)
  244. if(data[i+11] != ' ')
  245. part[j++] = data[i+11];
  246. for(i=0;i<6;i++)
  247. if(data[i+32] != ' ')
  248. part[j++] = data[i+32];
  249. part[j] = 0;
  250. /* skip Octane power supplies */
  251. if(!strncmp(part, "060-0035-", 9))
  252. return;
  253. if(!strncmp(part, "060-0038-", 9))
  254. return;
  255. strcpy(idd->nic_part, part);
  256. /* assemble the serial # */
  257. j=0;
  258. for(i=0;i<10;i++)
  259. if(data[i+1] != ' ')
  260. idd->nic_serial[j++] = data[i+1];
  261. idd->nic_serial[j] = 0;
  262. }
  263. static void read_mac(struct ioc3_driver_data *idd, unsigned long addr)
  264. {
  265. int i, loops = 3;
  266. unsigned char data[13];
  267. while(loops>0) {
  268. nic_addr(idd, addr);
  269. nic_write_byte(idd, 0xF0);
  270. nic_write_byte(idd, 0x00);
  271. nic_write_byte(idd, 0x00);
  272. nic_read_byte(idd);
  273. for(i=0;i<13;i++)
  274. data[i] = nic_read_byte(idd);
  275. if(crc16_area(data, 13, 0x0000) == 0x800d) {
  276. for(i=10;i>4;i--)
  277. idd->nic_mac[10-i] = data[i];
  278. return;
  279. }
  280. loops--;
  281. }
  282. printk(KERN_ERR "IOC3: CRC error in MAC address\n");
  283. for(i=0;i<6;i++)
  284. idd->nic_mac[i] = 0x00;
  285. }
  286. static void probe_nic(struct ioc3_driver_data *idd)
  287. {
  288. int save = 0, loops = 3;
  289. unsigned long first, addr;
  290. writel(GPCR_MLAN_EN, &idd->vma->gpcr_s);
  291. while(loops>0) {
  292. idd->nic_part[0] = 0;
  293. idd->nic_serial[0] = 0;
  294. addr = first = nic_find(idd, &save, 0);
  295. if(!first)
  296. return;
  297. while(1) {
  298. if(crc8_addr(addr))
  299. break;
  300. else {
  301. switch(addr & 0xFF) {
  302. case 0x0B:
  303. read_nic(idd, addr);
  304. break;
  305. case 0x09:
  306. case 0x89:
  307. case 0x91:
  308. read_mac(idd, addr);
  309. break;
  310. }
  311. }
  312. addr = nic_find(idd, &save, addr);
  313. if(addr == first)
  314. return;
  315. }
  316. loops--;
  317. }
  318. printk(KERN_ERR "IOC3: CRC error in NIC address\n");
  319. }
  320. /* Interrupts */
  321. static void write_ireg(struct ioc3_driver_data *idd, uint32_t val, int which)
  322. {
  323. unsigned long flags;
  324. spin_lock_irqsave(&idd->ir_lock, flags);
  325. switch (which) {
  326. case IOC3_W_IES:
  327. writel(val, &idd->vma->sio_ies);
  328. break;
  329. case IOC3_W_IEC:
  330. writel(val, &idd->vma->sio_iec);
  331. break;
  332. }
  333. spin_unlock_irqrestore(&idd->ir_lock, flags);
  334. }
  335. static inline uint32_t get_pending_intrs(struct ioc3_driver_data *idd)
  336. {
  337. unsigned long flag;
  338. uint32_t intrs = 0;
  339. spin_lock_irqsave(&idd->ir_lock, flag);
  340. intrs = readl(&idd->vma->sio_ir);
  341. intrs &= readl(&idd->vma->sio_ies);
  342. spin_unlock_irqrestore(&idd->ir_lock, flag);
  343. return intrs;
  344. }
  345. static irqreturn_t ioc3_intr_io(int irq, void *arg)
  346. {
  347. unsigned long flags;
  348. struct ioc3_driver_data *idd = arg;
  349. int handled = 1, id;
  350. unsigned int pending;
  351. read_lock_irqsave(&ioc3_submodules_lock, flags);
  352. if(idd->dual_irq && readb(&idd->vma->eisr)) {
  353. /* send Ethernet IRQ to the driver */
  354. if(ioc3_ethernet && idd->active[ioc3_ethernet->id] &&
  355. ioc3_ethernet->intr) {
  356. handled = handled && !ioc3_ethernet->intr(ioc3_ethernet,
  357. idd, 0);
  358. }
  359. }
  360. pending = get_pending_intrs(idd); /* look at the IO IRQs */
  361. for(id=0;id<IOC3_MAX_SUBMODULES;id++) {
  362. if(idd->active[id] && ioc3_submodules[id]
  363. && (pending & ioc3_submodules[id]->irq_mask)
  364. && ioc3_submodules[id]->intr) {
  365. write_ireg(idd, ioc3_submodules[id]->irq_mask,
  366. IOC3_W_IEC);
  367. if(!ioc3_submodules[id]->intr(ioc3_submodules[id],
  368. idd, pending & ioc3_submodules[id]->irq_mask))
  369. pending &= ~ioc3_submodules[id]->irq_mask;
  370. if (ioc3_submodules[id]->reset_mask)
  371. write_ireg(idd, ioc3_submodules[id]->irq_mask,
  372. IOC3_W_IES);
  373. }
  374. }
  375. read_unlock_irqrestore(&ioc3_submodules_lock, flags);
  376. if(pending) {
  377. printk(KERN_WARNING
  378. "IOC3: Pending IRQs 0x%08x discarded and disabled\n",pending);
  379. write_ireg(idd, pending, IOC3_W_IEC);
  380. handled = 1;
  381. }
  382. return handled?IRQ_HANDLED:IRQ_NONE;
  383. }
  384. static irqreturn_t ioc3_intr_eth(int irq, void *arg)
  385. {
  386. unsigned long flags;
  387. struct ioc3_driver_data *idd = (struct ioc3_driver_data *)arg;
  388. int handled = 1;
  389. if(!idd->dual_irq)
  390. return IRQ_NONE;
  391. read_lock_irqsave(&ioc3_submodules_lock, flags);
  392. if(ioc3_ethernet && idd->active[ioc3_ethernet->id]
  393. && ioc3_ethernet->intr)
  394. handled = handled && !ioc3_ethernet->intr(ioc3_ethernet, idd, 0);
  395. read_unlock_irqrestore(&ioc3_submodules_lock, flags);
  396. return handled?IRQ_HANDLED:IRQ_NONE;
  397. }
  398. void ioc3_enable(struct ioc3_submodule *is,
  399. struct ioc3_driver_data *idd, unsigned int irqs)
  400. {
  401. write_ireg(idd, irqs & is->irq_mask, IOC3_W_IES);
  402. }
  403. void ioc3_ack(struct ioc3_submodule *is, struct ioc3_driver_data *idd,
  404. unsigned int irqs)
  405. {
  406. writel(irqs & is->irq_mask, &idd->vma->sio_ir);
  407. }
  408. void ioc3_disable(struct ioc3_submodule *is,
  409. struct ioc3_driver_data *idd, unsigned int irqs)
  410. {
  411. write_ireg(idd, irqs & is->irq_mask, IOC3_W_IEC);
  412. }
  413. void ioc3_gpcr_set(struct ioc3_driver_data *idd, unsigned int val)
  414. {
  415. unsigned long flags;
  416. spin_lock_irqsave(&idd->gpio_lock, flags);
  417. writel(val, &idd->vma->gpcr_s);
  418. spin_unlock_irqrestore(&idd->gpio_lock, flags);
  419. }
  420. /* Keep it simple, stupid! */
  421. static int find_slot(void **tab, int max)
  422. {
  423. int i;
  424. for(i=0;i<max;i++)
  425. if(!(tab[i]))
  426. return i;
  427. return -1;
  428. }
  429. /* Register an IOC3 submodule */
  430. int ioc3_register_submodule(struct ioc3_submodule *is)
  431. {
  432. struct ioc3_driver_data *idd;
  433. int alloc_id;
  434. unsigned long flags;
  435. write_lock_irqsave(&ioc3_submodules_lock, flags);
  436. alloc_id = find_slot((void **)ioc3_submodules, IOC3_MAX_SUBMODULES);
  437. if(alloc_id != -1) {
  438. ioc3_submodules[alloc_id] = is;
  439. if(is->ethernet) {
  440. if(ioc3_ethernet==NULL)
  441. ioc3_ethernet=is;
  442. else
  443. printk(KERN_WARNING
  444. "IOC3 Ethernet module already registered!\n");
  445. }
  446. }
  447. write_unlock_irqrestore(&ioc3_submodules_lock, flags);
  448. if(alloc_id == -1) {
  449. printk(KERN_WARNING "Increase IOC3_MAX_SUBMODULES!\n");
  450. return -ENOMEM;
  451. }
  452. is->id=alloc_id;
  453. /* Initialize submodule for each IOC3 */
  454. if (!is->probe)
  455. return 0;
  456. down_read(&ioc3_devices_rwsem);
  457. list_for_each_entry(idd, &ioc3_devices, list) {
  458. /* set to 1 for IRQs in probe */
  459. idd->active[alloc_id] = 1;
  460. idd->active[alloc_id] = !is->probe(is, idd);
  461. }
  462. up_read(&ioc3_devices_rwsem);
  463. return 0;
  464. }
  465. /* Unregister an IOC3 submodule */
  466. void ioc3_unregister_submodule(struct ioc3_submodule *is)
  467. {
  468. struct ioc3_driver_data *idd;
  469. unsigned long flags;
  470. write_lock_irqsave(&ioc3_submodules_lock, flags);
  471. if(ioc3_submodules[is->id]==is)
  472. ioc3_submodules[is->id]=NULL;
  473. else
  474. printk(KERN_WARNING
  475. "IOC3 submodule %s has wrong ID.\n",is->name);
  476. if(ioc3_ethernet==is)
  477. ioc3_ethernet = NULL;
  478. write_unlock_irqrestore(&ioc3_submodules_lock, flags);
  479. /* Remove submodule for each IOC3 */
  480. down_read(&ioc3_devices_rwsem);
  481. list_for_each_entry(idd, &ioc3_devices, list)
  482. if(idd->active[is->id]) {
  483. if(is->remove)
  484. if(is->remove(is, idd))
  485. printk(KERN_WARNING
  486. "%s: IOC3 submodule %s remove failed "
  487. "for pci_dev %s.\n",
  488. __func__, module_name(is->owner),
  489. pci_name(idd->pdev));
  490. idd->active[is->id] = 0;
  491. if(is->irq_mask)
  492. write_ireg(idd, is->irq_mask, IOC3_W_IEC);
  493. }
  494. up_read(&ioc3_devices_rwsem);
  495. }
  496. /*********************
  497. * Device management *
  498. *********************/
  499. static char *ioc3_class_names[] = { "unknown", "IP27 BaseIO", "IP30 system",
  500. "MENET 1/2/3", "MENET 4", "CADduo", "Altix Serial" };
  501. static int ioc3_class(struct ioc3_driver_data *idd)
  502. {
  503. int res = IOC3_CLASS_NONE;
  504. /* NIC-based logic */
  505. if(!strncmp(idd->nic_part, "030-0891-", 9))
  506. res = IOC3_CLASS_BASE_IP30;
  507. if(!strncmp(idd->nic_part, "030-1155-", 9))
  508. res = IOC3_CLASS_CADDUO;
  509. if(!strncmp(idd->nic_part, "030-1657-", 9))
  510. res = IOC3_CLASS_SERIAL;
  511. if(!strncmp(idd->nic_part, "030-1664-", 9))
  512. res = IOC3_CLASS_SERIAL;
  513. /* total random heuristics */
  514. #ifdef CONFIG_SGI_IP27
  515. if(!idd->nic_part[0])
  516. res = IOC3_CLASS_BASE_IP27;
  517. #endif
  518. /* print educational message */
  519. printk(KERN_INFO "IOC3 part: [%s], serial: [%s] => class %s\n",
  520. idd->nic_part, idd->nic_serial, ioc3_class_names[res]);
  521. return res;
  522. }
  523. /* Adds a new instance of an IOC3 card */
  524. static int ioc3_probe(struct pci_dev *pdev, const struct pci_device_id *pci_id)
  525. {
  526. struct ioc3_driver_data *idd;
  527. uint32_t pcmd;
  528. int ret, id;
  529. /* Enable IOC3 and take ownership of it */
  530. if ((ret = pci_enable_device(pdev))) {
  531. printk(KERN_WARNING
  532. "%s: Failed to enable IOC3 device for pci_dev %s.\n",
  533. __func__, pci_name(pdev));
  534. goto out;
  535. }
  536. pci_set_master(pdev);
  537. #ifdef USE_64BIT_DMA
  538. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  539. if (!ret) {
  540. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
  541. if (ret < 0) {
  542. printk(KERN_WARNING "%s: Unable to obtain 64 bit DMA "
  543. "for consistent allocations\n",
  544. __func__);
  545. }
  546. }
  547. #endif
  548. /* Set up per-IOC3 data */
  549. idd = kzalloc(sizeof(struct ioc3_driver_data), GFP_KERNEL);
  550. if (!idd) {
  551. printk(KERN_WARNING
  552. "%s: Failed to allocate IOC3 data for pci_dev %s.\n",
  553. __func__, pci_name(pdev));
  554. ret = -ENODEV;
  555. goto out_idd;
  556. }
  557. spin_lock_init(&idd->ir_lock);
  558. spin_lock_init(&idd->gpio_lock);
  559. idd->pdev = pdev;
  560. /* Map all IOC3 registers. These are shared between subdevices
  561. * so the main IOC3 module manages them.
  562. */
  563. idd->pma = pci_resource_start(pdev, 0);
  564. if (!idd->pma) {
  565. printk(KERN_WARNING
  566. "%s: Unable to find IOC3 resource "
  567. "for pci_dev %s.\n",
  568. __func__, pci_name(pdev));
  569. ret = -ENODEV;
  570. goto out_pci;
  571. }
  572. if (!request_mem_region(idd->pma, IOC3_PCI_SIZE, "ioc3")) {
  573. printk(KERN_WARNING
  574. "%s: Unable to request IOC3 region "
  575. "for pci_dev %s.\n",
  576. __func__, pci_name(pdev));
  577. ret = -ENODEV;
  578. goto out_pci;
  579. }
  580. idd->vma = ioremap(idd->pma, IOC3_PCI_SIZE);
  581. if (!idd->vma) {
  582. printk(KERN_WARNING
  583. "%s: Unable to remap IOC3 region "
  584. "for pci_dev %s.\n",
  585. __func__, pci_name(pdev));
  586. ret = -ENODEV;
  587. goto out_misc_region;
  588. }
  589. /* Track PCI-device specific data */
  590. pci_set_drvdata(pdev, idd);
  591. down_write(&ioc3_devices_rwsem);
  592. list_add_tail(&idd->list, &ioc3_devices);
  593. idd->id = ioc3_counter++;
  594. up_write(&ioc3_devices_rwsem);
  595. idd->gpdr_shadow = readl(&idd->vma->gpdr);
  596. /* Read IOC3 NIC contents */
  597. probe_nic(idd);
  598. /* Detect IOC3 class */
  599. idd->class = ioc3_class(idd);
  600. /* Initialize IOC3 */
  601. pci_read_config_dword(pdev, PCI_COMMAND, &pcmd);
  602. pci_write_config_dword(pdev, PCI_COMMAND,
  603. pcmd | PCI_COMMAND_MEMORY |
  604. PCI_COMMAND_PARITY | PCI_COMMAND_SERR |
  605. PCI_SCR_DROP_MODE_EN);
  606. write_ireg(idd, ~0, IOC3_W_IEC);
  607. writel(~0, &idd->vma->sio_ir);
  608. /* Set up IRQs */
  609. if(idd->class == IOC3_CLASS_BASE_IP30
  610. || idd->class == IOC3_CLASS_BASE_IP27) {
  611. writel(0, &idd->vma->eier);
  612. writel(~0, &idd->vma->eisr);
  613. idd->dual_irq = 1;
  614. if (!request_irq(pdev->irq, ioc3_intr_eth, IRQF_SHARED,
  615. "ioc3-eth", (void *)idd)) {
  616. idd->irq_eth = pdev->irq;
  617. } else {
  618. printk(KERN_WARNING
  619. "%s : request_irq fails for IRQ 0x%x\n ",
  620. __func__, pdev->irq);
  621. }
  622. if (!request_irq(pdev->irq+2, ioc3_intr_io, IRQF_SHARED,
  623. "ioc3-io", (void *)idd)) {
  624. idd->irq_io = pdev->irq+2;
  625. } else {
  626. printk(KERN_WARNING
  627. "%s : request_irq fails for IRQ 0x%x\n ",
  628. __func__, pdev->irq+2);
  629. }
  630. } else {
  631. if (!request_irq(pdev->irq, ioc3_intr_io, IRQF_SHARED,
  632. "ioc3", (void *)idd)) {
  633. idd->irq_io = pdev->irq;
  634. } else {
  635. printk(KERN_WARNING
  636. "%s : request_irq fails for IRQ 0x%x\n ",
  637. __func__, pdev->irq);
  638. }
  639. }
  640. /* Add this IOC3 to all submodules */
  641. for(id=0;id<IOC3_MAX_SUBMODULES;id++)
  642. if(ioc3_submodules[id] && ioc3_submodules[id]->probe) {
  643. idd->active[id] = 1;
  644. idd->active[id] = !ioc3_submodules[id]->probe
  645. (ioc3_submodules[id], idd);
  646. }
  647. printk(KERN_INFO "IOC3 Master Driver loaded for %s\n", pci_name(pdev));
  648. return 0;
  649. out_misc_region:
  650. release_mem_region(idd->pma, IOC3_PCI_SIZE);
  651. out_pci:
  652. kfree(idd);
  653. out_idd:
  654. pci_disable_device(pdev);
  655. out:
  656. return ret;
  657. }
  658. /* Removes a particular instance of an IOC3 card. */
  659. static void ioc3_remove(struct pci_dev *pdev)
  660. {
  661. int id;
  662. struct ioc3_driver_data *idd;
  663. idd = pci_get_drvdata(pdev);
  664. /* Remove this IOC3 from all submodules */
  665. for(id=0;id<IOC3_MAX_SUBMODULES;id++)
  666. if(idd->active[id]) {
  667. if(ioc3_submodules[id] && ioc3_submodules[id]->remove)
  668. if(ioc3_submodules[id]->remove(ioc3_submodules[id],
  669. idd))
  670. printk(KERN_WARNING
  671. "%s: IOC3 submodule 0x%s remove failed "
  672. "for pci_dev %s.\n",
  673. __func__,
  674. module_name(ioc3_submodules[id]->owner),
  675. pci_name(pdev));
  676. idd->active[id] = 0;
  677. }
  678. /* Clear and disable all IRQs */
  679. write_ireg(idd, ~0, IOC3_W_IEC);
  680. writel(~0, &idd->vma->sio_ir);
  681. /* Release resources */
  682. free_irq(idd->irq_io, (void *)idd);
  683. if(idd->dual_irq)
  684. free_irq(idd->irq_eth, (void *)idd);
  685. iounmap(idd->vma);
  686. release_mem_region(idd->pma, IOC3_PCI_SIZE);
  687. /* Disable IOC3 and relinquish */
  688. pci_disable_device(pdev);
  689. /* Remove and free driver data */
  690. down_write(&ioc3_devices_rwsem);
  691. list_del(&idd->list);
  692. up_write(&ioc3_devices_rwsem);
  693. kfree(idd);
  694. }
  695. static struct pci_device_id ioc3_id_table[] = {
  696. {PCI_VENDOR_ID_SGI, PCI_DEVICE_ID_SGI_IOC3, PCI_ANY_ID, PCI_ANY_ID},
  697. {0}
  698. };
  699. static struct pci_driver ioc3_driver = {
  700. .name = "IOC3",
  701. .id_table = ioc3_id_table,
  702. .probe = ioc3_probe,
  703. .remove = ioc3_remove,
  704. };
  705. MODULE_DEVICE_TABLE(pci, ioc3_id_table);
  706. /*********************
  707. * Module management *
  708. *********************/
  709. /* Module load */
  710. static int __init ioc3_init(void)
  711. {
  712. if (ia64_platform_is("sn2"))
  713. return pci_register_driver(&ioc3_driver);
  714. return -ENODEV;
  715. }
  716. /* Module unload */
  717. static void __exit ioc3_exit(void)
  718. {
  719. pci_unregister_driver(&ioc3_driver);
  720. }
  721. module_init(ioc3_init);
  722. module_exit(ioc3_exit);
  723. MODULE_AUTHOR("Stanislaw Skowronek <skylark@linux-mips.org>");
  724. MODULE_DESCRIPTION("PCI driver for SGI IOC3");
  725. MODULE_LICENSE("GPL");
  726. EXPORT_SYMBOL_GPL(ioc3_register_submodule);
  727. EXPORT_SYMBOL_GPL(ioc3_unregister_submodule);
  728. EXPORT_SYMBOL_GPL(ioc3_ack);
  729. EXPORT_SYMBOL_GPL(ioc3_gpcr_set);
  730. EXPORT_SYMBOL_GPL(ioc3_disable);
  731. EXPORT_SYMBOL_GPL(ioc3_enable);