ibmphp_core.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IBM Hot Plug Controller Driver
  4. *
  5. * Written By: Chuck Cole, Jyoti Shah, Tong Yu, Irene Zubarev, IBM Corporation
  6. *
  7. * Copyright (C) 2001,2003 Greg Kroah-Hartman (greg@kroah.com)
  8. * Copyright (C) 2001-2003 IBM Corp.
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <gregkh@us.ibm.com>
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/slab.h>
  18. #include <linux/pci.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include "../pci.h"
  23. #include <asm/pci_x86.h> /* for struct irq_routing_table */
  24. #include <asm/io_apic.h>
  25. #include "ibmphp.h"
  26. #define attn_on(sl) ibmphp_hpc_writeslot(sl, HPC_SLOT_ATTNON)
  27. #define attn_off(sl) ibmphp_hpc_writeslot(sl, HPC_SLOT_ATTNOFF)
  28. #define attn_LED_blink(sl) ibmphp_hpc_writeslot(sl, HPC_SLOT_BLINKLED)
  29. #define get_ctrl_revision(sl, rev) ibmphp_hpc_readslot(sl, READ_REVLEVEL, rev)
  30. #define get_hpc_options(sl, opt) ibmphp_hpc_readslot(sl, READ_HPCOPTIONS, opt)
  31. #define DRIVER_VERSION "0.6"
  32. #define DRIVER_DESC "IBM Hot Plug PCI Controller Driver"
  33. int ibmphp_debug;
  34. static bool debug;
  35. module_param(debug, bool, S_IRUGO | S_IWUSR);
  36. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
  37. MODULE_LICENSE("GPL");
  38. MODULE_DESCRIPTION(DRIVER_DESC);
  39. struct pci_bus *ibmphp_pci_bus;
  40. static int max_slots;
  41. static int irqs[16]; /* PIC mode IRQs we're using so far (in case MPS
  42. * tables don't provide default info for empty slots */
  43. static int init_flag;
  44. static inline int get_cur_bus_info(struct slot **sl)
  45. {
  46. int rc = 1;
  47. struct slot *slot_cur = *sl;
  48. debug("options = %x\n", slot_cur->ctrl->options);
  49. debug("revision = %x\n", slot_cur->ctrl->revision);
  50. if (READ_BUS_STATUS(slot_cur->ctrl))
  51. rc = ibmphp_hpc_readslot(slot_cur, READ_BUSSTATUS, NULL);
  52. if (rc)
  53. return rc;
  54. slot_cur->bus_on->current_speed = CURRENT_BUS_SPEED(slot_cur->busstatus);
  55. if (READ_BUS_MODE(slot_cur->ctrl))
  56. slot_cur->bus_on->current_bus_mode =
  57. CURRENT_BUS_MODE(slot_cur->busstatus);
  58. else
  59. slot_cur->bus_on->current_bus_mode = 0xFF;
  60. debug("busstatus = %x, bus_speed = %x, bus_mode = %x\n",
  61. slot_cur->busstatus,
  62. slot_cur->bus_on->current_speed,
  63. slot_cur->bus_on->current_bus_mode);
  64. *sl = slot_cur;
  65. return 0;
  66. }
  67. static inline int slot_update(struct slot **sl)
  68. {
  69. int rc;
  70. rc = ibmphp_hpc_readslot(*sl, READ_ALLSTAT, NULL);
  71. if (rc)
  72. return rc;
  73. if (!init_flag)
  74. rc = get_cur_bus_info(sl);
  75. return rc;
  76. }
  77. static int __init get_max_slots(void)
  78. {
  79. struct slot *slot_cur;
  80. u8 slot_count = 0;
  81. list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
  82. /* sometimes the hot-pluggable slots start with 4 (not always from 1) */
  83. slot_count = max(slot_count, slot_cur->number);
  84. }
  85. return slot_count;
  86. }
  87. /* This routine will put the correct slot->device information per slot. It's
  88. * called from initialization of the slot structures. It will also assign
  89. * interrupt numbers per each slot.
  90. * Parameters: struct slot
  91. * Returns 0 or errors
  92. */
  93. int ibmphp_init_devno(struct slot **cur_slot)
  94. {
  95. struct irq_routing_table *rtable;
  96. int len;
  97. int loop;
  98. int i;
  99. rtable = pcibios_get_irq_routing_table();
  100. if (!rtable) {
  101. err("no BIOS routing table...\n");
  102. return -ENOMEM;
  103. }
  104. len = (rtable->size - sizeof(struct irq_routing_table)) /
  105. sizeof(struct irq_info);
  106. if (!len) {
  107. kfree(rtable);
  108. return -1;
  109. }
  110. for (loop = 0; loop < len; loop++) {
  111. if ((*cur_slot)->number == rtable->slots[loop].slot &&
  112. (*cur_slot)->bus == rtable->slots[loop].bus) {
  113. (*cur_slot)->device = PCI_SLOT(rtable->slots[loop].devfn);
  114. for (i = 0; i < 4; i++)
  115. (*cur_slot)->irq[i] = IO_APIC_get_PCI_irq_vector((int) (*cur_slot)->bus,
  116. (int) (*cur_slot)->device, i);
  117. debug("(*cur_slot)->irq[0] = %x\n",
  118. (*cur_slot)->irq[0]);
  119. debug("(*cur_slot)->irq[1] = %x\n",
  120. (*cur_slot)->irq[1]);
  121. debug("(*cur_slot)->irq[2] = %x\n",
  122. (*cur_slot)->irq[2]);
  123. debug("(*cur_slot)->irq[3] = %x\n",
  124. (*cur_slot)->irq[3]);
  125. debug("rtable->exclusive_irqs = %x\n",
  126. rtable->exclusive_irqs);
  127. debug("rtable->slots[loop].irq[0].bitmap = %x\n",
  128. rtable->slots[loop].irq[0].bitmap);
  129. debug("rtable->slots[loop].irq[1].bitmap = %x\n",
  130. rtable->slots[loop].irq[1].bitmap);
  131. debug("rtable->slots[loop].irq[2].bitmap = %x\n",
  132. rtable->slots[loop].irq[2].bitmap);
  133. debug("rtable->slots[loop].irq[3].bitmap = %x\n",
  134. rtable->slots[loop].irq[3].bitmap);
  135. debug("rtable->slots[loop].irq[0].link = %x\n",
  136. rtable->slots[loop].irq[0].link);
  137. debug("rtable->slots[loop].irq[1].link = %x\n",
  138. rtable->slots[loop].irq[1].link);
  139. debug("rtable->slots[loop].irq[2].link = %x\n",
  140. rtable->slots[loop].irq[2].link);
  141. debug("rtable->slots[loop].irq[3].link = %x\n",
  142. rtable->slots[loop].irq[3].link);
  143. debug("end of init_devno\n");
  144. kfree(rtable);
  145. return 0;
  146. }
  147. }
  148. kfree(rtable);
  149. return -1;
  150. }
  151. static inline int power_on(struct slot *slot_cur)
  152. {
  153. u8 cmd = HPC_SLOT_ON;
  154. int retval;
  155. retval = ibmphp_hpc_writeslot(slot_cur, cmd);
  156. if (retval) {
  157. err("power on failed\n");
  158. return retval;
  159. }
  160. if (CTLR_RESULT(slot_cur->ctrl->status)) {
  161. err("command not completed successfully in power_on\n");
  162. return -EIO;
  163. }
  164. msleep(3000); /* For ServeRAID cards, and some 66 PCI */
  165. return 0;
  166. }
  167. static inline int power_off(struct slot *slot_cur)
  168. {
  169. u8 cmd = HPC_SLOT_OFF;
  170. int retval;
  171. retval = ibmphp_hpc_writeslot(slot_cur, cmd);
  172. if (retval) {
  173. err("power off failed\n");
  174. return retval;
  175. }
  176. if (CTLR_RESULT(slot_cur->ctrl->status)) {
  177. err("command not completed successfully in power_off\n");
  178. retval = -EIO;
  179. }
  180. return retval;
  181. }
  182. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 value)
  183. {
  184. int rc = 0;
  185. struct slot *pslot;
  186. u8 cmd = 0x00; /* avoid compiler warning */
  187. debug("set_attention_status - Entry hotplug_slot[%lx] value[%x]\n",
  188. (ulong) hotplug_slot, value);
  189. ibmphp_lock_operations();
  190. if (hotplug_slot) {
  191. switch (value) {
  192. case HPC_SLOT_ATTN_OFF:
  193. cmd = HPC_SLOT_ATTNOFF;
  194. break;
  195. case HPC_SLOT_ATTN_ON:
  196. cmd = HPC_SLOT_ATTNON;
  197. break;
  198. case HPC_SLOT_ATTN_BLINK:
  199. cmd = HPC_SLOT_BLINKLED;
  200. break;
  201. default:
  202. rc = -ENODEV;
  203. err("set_attention_status - Error : invalid input [%x]\n",
  204. value);
  205. break;
  206. }
  207. if (rc == 0) {
  208. pslot = to_slot(hotplug_slot);
  209. rc = ibmphp_hpc_writeslot(pslot, cmd);
  210. }
  211. } else
  212. rc = -ENODEV;
  213. ibmphp_unlock_operations();
  214. debug("set_attention_status - Exit rc[%d]\n", rc);
  215. return rc;
  216. }
  217. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  218. {
  219. int rc = -ENODEV;
  220. struct slot *pslot;
  221. struct slot myslot;
  222. debug("get_attention_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
  223. (ulong) hotplug_slot, (ulong) value);
  224. ibmphp_lock_operations();
  225. if (hotplug_slot) {
  226. pslot = to_slot(hotplug_slot);
  227. memcpy(&myslot, pslot, sizeof(struct slot));
  228. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
  229. &myslot.status);
  230. if (!rc)
  231. rc = ibmphp_hpc_readslot(pslot, READ_EXTSLOTSTATUS,
  232. &myslot.ext_status);
  233. if (!rc)
  234. *value = SLOT_ATTN(myslot.status, myslot.ext_status);
  235. }
  236. ibmphp_unlock_operations();
  237. debug("get_attention_status - Exit rc[%d] value[%x]\n", rc, *value);
  238. return rc;
  239. }
  240. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  241. {
  242. int rc = -ENODEV;
  243. struct slot *pslot;
  244. struct slot myslot;
  245. debug("get_latch_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
  246. (ulong) hotplug_slot, (ulong) value);
  247. ibmphp_lock_operations();
  248. if (hotplug_slot) {
  249. pslot = to_slot(hotplug_slot);
  250. memcpy(&myslot, pslot, sizeof(struct slot));
  251. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
  252. &myslot.status);
  253. if (!rc)
  254. *value = SLOT_LATCH(myslot.status);
  255. }
  256. ibmphp_unlock_operations();
  257. debug("get_latch_status - Exit rc[%d] rc[%x] value[%x]\n",
  258. rc, rc, *value);
  259. return rc;
  260. }
  261. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  262. {
  263. int rc = -ENODEV;
  264. struct slot *pslot;
  265. struct slot myslot;
  266. debug("get_power_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
  267. (ulong) hotplug_slot, (ulong) value);
  268. ibmphp_lock_operations();
  269. if (hotplug_slot) {
  270. pslot = to_slot(hotplug_slot);
  271. memcpy(&myslot, pslot, sizeof(struct slot));
  272. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
  273. &myslot.status);
  274. if (!rc)
  275. *value = SLOT_PWRGD(myslot.status);
  276. }
  277. ibmphp_unlock_operations();
  278. debug("get_power_status - Exit rc[%d] rc[%x] value[%x]\n",
  279. rc, rc, *value);
  280. return rc;
  281. }
  282. static int get_adapter_present(struct hotplug_slot *hotplug_slot, u8 *value)
  283. {
  284. int rc = -ENODEV;
  285. struct slot *pslot;
  286. u8 present;
  287. struct slot myslot;
  288. debug("get_adapter_status - Entry hotplug_slot[%lx] pvalue[%lx]\n",
  289. (ulong) hotplug_slot, (ulong) value);
  290. ibmphp_lock_operations();
  291. if (hotplug_slot) {
  292. pslot = to_slot(hotplug_slot);
  293. memcpy(&myslot, pslot, sizeof(struct slot));
  294. rc = ibmphp_hpc_readslot(pslot, READ_SLOTSTATUS,
  295. &myslot.status);
  296. if (!rc) {
  297. present = SLOT_PRESENT(myslot.status);
  298. if (present == HPC_SLOT_EMPTY)
  299. *value = 0;
  300. else
  301. *value = 1;
  302. }
  303. }
  304. ibmphp_unlock_operations();
  305. debug("get_adapter_present - Exit rc[%d] value[%x]\n", rc, *value);
  306. return rc;
  307. }
  308. static int get_max_bus_speed(struct slot *slot)
  309. {
  310. int rc = 0;
  311. u8 mode = 0;
  312. enum pci_bus_speed speed;
  313. struct pci_bus *bus = slot->hotplug_slot.pci_slot->bus;
  314. debug("%s - Entry slot[%p]\n", __func__, slot);
  315. ibmphp_lock_operations();
  316. mode = slot->supported_bus_mode;
  317. speed = slot->supported_speed;
  318. ibmphp_unlock_operations();
  319. switch (speed) {
  320. case BUS_SPEED_33:
  321. break;
  322. case BUS_SPEED_66:
  323. if (mode == BUS_MODE_PCIX)
  324. speed += 0x01;
  325. break;
  326. case BUS_SPEED_100:
  327. case BUS_SPEED_133:
  328. speed += 0x01;
  329. break;
  330. default:
  331. /* Note (will need to change): there would be soon 256, 512 also */
  332. rc = -ENODEV;
  333. }
  334. if (!rc)
  335. bus->max_bus_speed = speed;
  336. debug("%s - Exit rc[%d] speed[%x]\n", __func__, rc, speed);
  337. return rc;
  338. }
  339. /****************************************************************************
  340. * This routine will initialize the ops data structure used in the validate
  341. * function. It will also power off empty slots that are powered on since BIOS
  342. * leaves those on, albeit disconnected
  343. ****************************************************************************/
  344. static int __init init_ops(void)
  345. {
  346. struct slot *slot_cur;
  347. int retval;
  348. int rc;
  349. list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
  350. debug("BEFORE GETTING SLOT STATUS, slot # %x\n",
  351. slot_cur->number);
  352. if (slot_cur->ctrl->revision == 0xFF)
  353. if (get_ctrl_revision(slot_cur,
  354. &slot_cur->ctrl->revision))
  355. return -1;
  356. if (slot_cur->bus_on->current_speed == 0xFF)
  357. if (get_cur_bus_info(&slot_cur))
  358. return -1;
  359. get_max_bus_speed(slot_cur);
  360. if (slot_cur->ctrl->options == 0xFF)
  361. if (get_hpc_options(slot_cur, &slot_cur->ctrl->options))
  362. return -1;
  363. retval = slot_update(&slot_cur);
  364. if (retval)
  365. return retval;
  366. debug("status = %x\n", slot_cur->status);
  367. debug("ext_status = %x\n", slot_cur->ext_status);
  368. debug("SLOT_POWER = %x\n", SLOT_POWER(slot_cur->status));
  369. debug("SLOT_PRESENT = %x\n", SLOT_PRESENT(slot_cur->status));
  370. debug("SLOT_LATCH = %x\n", SLOT_LATCH(slot_cur->status));
  371. if ((SLOT_PWRGD(slot_cur->status)) &&
  372. !(SLOT_PRESENT(slot_cur->status)) &&
  373. !(SLOT_LATCH(slot_cur->status))) {
  374. debug("BEFORE POWER OFF COMMAND\n");
  375. rc = power_off(slot_cur);
  376. if (rc)
  377. return rc;
  378. /* retval = slot_update(&slot_cur);
  379. * if (retval)
  380. * return retval;
  381. * ibmphp_update_slot_info(slot_cur);
  382. */
  383. }
  384. }
  385. init_flag = 0;
  386. return 0;
  387. }
  388. /* This operation will check whether the slot is within the bounds and
  389. * the operation is valid to perform on that slot
  390. * Parameters: slot, operation
  391. * Returns: 0 or error codes
  392. */
  393. static int validate(struct slot *slot_cur, int opn)
  394. {
  395. int number;
  396. int retval;
  397. if (!slot_cur)
  398. return -ENODEV;
  399. number = slot_cur->number;
  400. if ((number > max_slots) || (number < 0))
  401. return -EBADSLT;
  402. debug("slot_number in validate is %d\n", slot_cur->number);
  403. retval = slot_update(&slot_cur);
  404. if (retval)
  405. return retval;
  406. switch (opn) {
  407. case ENABLE:
  408. if (!(SLOT_PWRGD(slot_cur->status)) &&
  409. (SLOT_PRESENT(slot_cur->status)) &&
  410. !(SLOT_LATCH(slot_cur->status)))
  411. return 0;
  412. break;
  413. case DISABLE:
  414. if ((SLOT_PWRGD(slot_cur->status)) &&
  415. (SLOT_PRESENT(slot_cur->status)) &&
  416. !(SLOT_LATCH(slot_cur->status)))
  417. return 0;
  418. break;
  419. default:
  420. break;
  421. }
  422. err("validate failed....\n");
  423. return -EINVAL;
  424. }
  425. /****************************************************************************
  426. * This routine is for updating the data structures in the hotplug core
  427. * Parameters: struct slot
  428. * Returns: 0 or error
  429. ****************************************************************************/
  430. int ibmphp_update_slot_info(struct slot *slot_cur)
  431. {
  432. struct pci_bus *bus = slot_cur->hotplug_slot.pci_slot->bus;
  433. u8 bus_speed;
  434. u8 mode;
  435. bus_speed = slot_cur->bus_on->current_speed;
  436. mode = slot_cur->bus_on->current_bus_mode;
  437. switch (bus_speed) {
  438. case BUS_SPEED_33:
  439. break;
  440. case BUS_SPEED_66:
  441. if (mode == BUS_MODE_PCIX)
  442. bus_speed += 0x01;
  443. else if (mode == BUS_MODE_PCI)
  444. ;
  445. else
  446. bus_speed = PCI_SPEED_UNKNOWN;
  447. break;
  448. case BUS_SPEED_100:
  449. case BUS_SPEED_133:
  450. bus_speed += 0x01;
  451. break;
  452. default:
  453. bus_speed = PCI_SPEED_UNKNOWN;
  454. }
  455. bus->cur_bus_speed = bus_speed;
  456. // To do: bus_names
  457. return 0;
  458. }
  459. /******************************************************************************
  460. * This function will return the pci_func, given bus and devfunc, or NULL. It
  461. * is called from visit routines
  462. ******************************************************************************/
  463. static struct pci_func *ibm_slot_find(u8 busno, u8 device, u8 function)
  464. {
  465. struct pci_func *func_cur;
  466. struct slot *slot_cur;
  467. list_for_each_entry(slot_cur, &ibmphp_slot_head, ibm_slot_list) {
  468. if (slot_cur->func) {
  469. func_cur = slot_cur->func;
  470. while (func_cur) {
  471. if ((func_cur->busno == busno) &&
  472. (func_cur->device == device) &&
  473. (func_cur->function == function))
  474. return func_cur;
  475. func_cur = func_cur->next;
  476. }
  477. }
  478. }
  479. return NULL;
  480. }
  481. /*************************************************************
  482. * This routine frees up memory used by struct slot, including
  483. * the pointers to pci_func, bus, hotplug_slot, controller,
  484. * and deregistering from the hotplug core
  485. *************************************************************/
  486. static void free_slots(void)
  487. {
  488. struct slot *slot_cur, *next;
  489. debug("%s -- enter\n", __func__);
  490. list_for_each_entry_safe(slot_cur, next, &ibmphp_slot_head,
  491. ibm_slot_list) {
  492. pci_hp_del(&slot_cur->hotplug_slot);
  493. slot_cur->ctrl = NULL;
  494. slot_cur->bus_on = NULL;
  495. /*
  496. * We don't want to actually remove the resources,
  497. * since ibmphp_free_resources() will do just that.
  498. */
  499. ibmphp_unconfigure_card(&slot_cur, -1);
  500. pci_hp_destroy(&slot_cur->hotplug_slot);
  501. kfree(slot_cur);
  502. }
  503. debug("%s -- exit\n", __func__);
  504. }
  505. static void ibm_unconfigure_device(struct pci_func *func)
  506. {
  507. struct pci_dev *temp;
  508. u8 j;
  509. debug("inside %s\n", __func__);
  510. debug("func->device = %x, func->function = %x\n",
  511. func->device, func->function);
  512. debug("func->device << 3 | 0x0 = %x\n", func->device << 3 | 0x0);
  513. pci_lock_rescan_remove();
  514. for (j = 0; j < 0x08; j++) {
  515. temp = pci_get_domain_bus_and_slot(0, func->busno,
  516. (func->device << 3) | j);
  517. if (temp) {
  518. pci_stop_and_remove_bus_device(temp);
  519. pci_dev_put(temp);
  520. }
  521. }
  522. pci_dev_put(func->dev);
  523. pci_unlock_rescan_remove();
  524. }
  525. /*
  526. * The following function is to fix kernel bug regarding
  527. * getting bus entries, here we manually add those primary
  528. * bus entries to kernel bus structure whenever apply
  529. */
  530. static u8 bus_structure_fixup(u8 busno)
  531. {
  532. struct pci_bus *bus, *b;
  533. struct pci_dev *dev;
  534. u16 l;
  535. if (pci_find_bus(0, busno) || !(ibmphp_find_same_bus_num(busno)))
  536. return 1;
  537. bus = kmalloc_obj(*bus);
  538. if (!bus)
  539. return 1;
  540. dev = kmalloc_obj(*dev);
  541. if (!dev) {
  542. kfree(bus);
  543. return 1;
  544. }
  545. bus->number = busno;
  546. bus->ops = ibmphp_pci_bus->ops;
  547. dev->bus = bus;
  548. for (dev->devfn = 0; dev->devfn < 256; dev->devfn += 8) {
  549. if (!pci_read_config_word(dev, PCI_VENDOR_ID, &l) &&
  550. (l != 0x0000) && (l != 0xffff)) {
  551. debug("%s - Inside bus_structure_fixup()\n",
  552. __func__);
  553. b = pci_scan_bus(busno, ibmphp_pci_bus->ops, NULL);
  554. if (!b)
  555. continue;
  556. pci_bus_add_devices(b);
  557. break;
  558. }
  559. }
  560. kfree(dev);
  561. kfree(bus);
  562. return 0;
  563. }
  564. static int ibm_configure_device(struct pci_func *func)
  565. {
  566. struct pci_bus *child;
  567. int num;
  568. int flag = 0; /* this is to make sure we don't double scan the bus,
  569. for bridged devices primarily */
  570. pci_lock_rescan_remove();
  571. if (!(bus_structure_fixup(func->busno)))
  572. flag = 1;
  573. if (func->dev == NULL)
  574. func->dev = pci_get_domain_bus_and_slot(0, func->busno,
  575. PCI_DEVFN(func->device, func->function));
  576. if (func->dev == NULL) {
  577. struct pci_bus *bus = pci_find_bus(0, func->busno);
  578. if (!bus)
  579. goto out;
  580. num = pci_scan_slot(bus,
  581. PCI_DEVFN(func->device, func->function));
  582. if (num)
  583. pci_bus_add_devices(bus);
  584. func->dev = pci_get_domain_bus_and_slot(0, func->busno,
  585. PCI_DEVFN(func->device, func->function));
  586. if (func->dev == NULL) {
  587. err("ERROR... : pci_dev still NULL\n");
  588. goto out;
  589. }
  590. }
  591. if (!(flag) && (func->dev->hdr_type == PCI_HEADER_TYPE_BRIDGE)) {
  592. pci_hp_add_bridge(func->dev);
  593. child = func->dev->subordinate;
  594. if (child)
  595. pci_bus_add_devices(child);
  596. }
  597. out:
  598. pci_unlock_rescan_remove();
  599. return 0;
  600. }
  601. /*******************************************************
  602. * Returns whether the bus is empty or not
  603. *******************************************************/
  604. static int is_bus_empty(struct slot *slot_cur)
  605. {
  606. int rc;
  607. struct slot *tmp_slot;
  608. u8 i = slot_cur->bus_on->slot_min;
  609. while (i <= slot_cur->bus_on->slot_max) {
  610. if (i == slot_cur->number) {
  611. i++;
  612. continue;
  613. }
  614. tmp_slot = ibmphp_get_slot_from_physical_num(i);
  615. if (!tmp_slot)
  616. return 0;
  617. rc = slot_update(&tmp_slot);
  618. if (rc)
  619. return 0;
  620. if (SLOT_PRESENT(tmp_slot->status) &&
  621. SLOT_PWRGD(tmp_slot->status))
  622. return 0;
  623. i++;
  624. }
  625. return 1;
  626. }
  627. /***********************************************************
  628. * If the HPC permits and the bus currently empty, tries to set the
  629. * bus speed and mode at the maximum card and bus capability
  630. * Parameters: slot
  631. * Returns: bus is set (0) or error code
  632. ***********************************************************/
  633. static int set_bus(struct slot *slot_cur)
  634. {
  635. int rc;
  636. u8 speed;
  637. u8 cmd = 0x0;
  638. int retval;
  639. static const struct pci_device_id ciobx[] = {
  640. { PCI_DEVICE(PCI_VENDOR_ID_SERVERWORKS, 0x0101) },
  641. { },
  642. };
  643. debug("%s - entry slot # %d\n", __func__, slot_cur->number);
  644. if (SET_BUS_STATUS(slot_cur->ctrl) && is_bus_empty(slot_cur)) {
  645. rc = slot_update(&slot_cur);
  646. if (rc)
  647. return rc;
  648. speed = SLOT_SPEED(slot_cur->ext_status);
  649. debug("ext_status = %x, speed = %x\n", slot_cur->ext_status, speed);
  650. switch (speed) {
  651. case HPC_SLOT_SPEED_33:
  652. cmd = HPC_BUS_33CONVMODE;
  653. break;
  654. case HPC_SLOT_SPEED_66:
  655. if (SLOT_PCIX(slot_cur->ext_status)) {
  656. if ((slot_cur->supported_speed >= BUS_SPEED_66) &&
  657. (slot_cur->supported_bus_mode == BUS_MODE_PCIX))
  658. cmd = HPC_BUS_66PCIXMODE;
  659. else if (!SLOT_BUS_MODE(slot_cur->ext_status))
  660. /* if max slot/bus capability is 66 pci
  661. and there's no bus mode mismatch, then
  662. the adapter supports 66 pci */
  663. cmd = HPC_BUS_66CONVMODE;
  664. else
  665. cmd = HPC_BUS_33CONVMODE;
  666. } else {
  667. if (slot_cur->supported_speed >= BUS_SPEED_66)
  668. cmd = HPC_BUS_66CONVMODE;
  669. else
  670. cmd = HPC_BUS_33CONVMODE;
  671. }
  672. break;
  673. case HPC_SLOT_SPEED_133:
  674. switch (slot_cur->supported_speed) {
  675. case BUS_SPEED_33:
  676. cmd = HPC_BUS_33CONVMODE;
  677. break;
  678. case BUS_SPEED_66:
  679. if (slot_cur->supported_bus_mode == BUS_MODE_PCIX)
  680. cmd = HPC_BUS_66PCIXMODE;
  681. else
  682. cmd = HPC_BUS_66CONVMODE;
  683. break;
  684. case BUS_SPEED_100:
  685. cmd = HPC_BUS_100PCIXMODE;
  686. break;
  687. case BUS_SPEED_133:
  688. /* This is to take care of the bug in CIOBX chip */
  689. if (pci_dev_present(ciobx))
  690. ibmphp_hpc_writeslot(slot_cur,
  691. HPC_BUS_100PCIXMODE);
  692. cmd = HPC_BUS_133PCIXMODE;
  693. break;
  694. default:
  695. err("Wrong bus speed\n");
  696. return -ENODEV;
  697. }
  698. break;
  699. default:
  700. err("wrong slot speed\n");
  701. return -ENODEV;
  702. }
  703. debug("setting bus speed for slot %d, cmd %x\n",
  704. slot_cur->number, cmd);
  705. retval = ibmphp_hpc_writeslot(slot_cur, cmd);
  706. if (retval) {
  707. err("setting bus speed failed\n");
  708. return retval;
  709. }
  710. if (CTLR_RESULT(slot_cur->ctrl->status)) {
  711. err("command not completed successfully in set_bus\n");
  712. return -EIO;
  713. }
  714. }
  715. /* This is for x440, once Brandon fixes the firmware,
  716. will not need this delay */
  717. msleep(1000);
  718. debug("%s -Exit\n", __func__);
  719. return 0;
  720. }
  721. /* This routine checks the bus limitations that the slot is on from the BIOS.
  722. * This is used in deciding whether or not to power up the slot.
  723. * (electrical/spec limitations. For example, >1 133 MHz or >2 66 PCI cards on
  724. * same bus)
  725. * Parameters: slot
  726. * Returns: 0 = no limitations, -EINVAL = exceeded limitations on the bus
  727. */
  728. static int check_limitations(struct slot *slot_cur)
  729. {
  730. u8 i;
  731. struct slot *tmp_slot;
  732. u8 count = 0;
  733. u8 limitation = 0;
  734. for (i = slot_cur->bus_on->slot_min; i <= slot_cur->bus_on->slot_max; i++) {
  735. tmp_slot = ibmphp_get_slot_from_physical_num(i);
  736. if (!tmp_slot)
  737. return -ENODEV;
  738. if ((SLOT_PWRGD(tmp_slot->status)) &&
  739. !(SLOT_CONNECT(tmp_slot->status)))
  740. count++;
  741. }
  742. get_cur_bus_info(&slot_cur);
  743. switch (slot_cur->bus_on->current_speed) {
  744. case BUS_SPEED_33:
  745. limitation = slot_cur->bus_on->slots_at_33_conv;
  746. break;
  747. case BUS_SPEED_66:
  748. if (slot_cur->bus_on->current_bus_mode == BUS_MODE_PCIX)
  749. limitation = slot_cur->bus_on->slots_at_66_pcix;
  750. else
  751. limitation = slot_cur->bus_on->slots_at_66_conv;
  752. break;
  753. case BUS_SPEED_100:
  754. limitation = slot_cur->bus_on->slots_at_100_pcix;
  755. break;
  756. case BUS_SPEED_133:
  757. limitation = slot_cur->bus_on->slots_at_133_pcix;
  758. break;
  759. }
  760. if ((count + 1) > limitation)
  761. return -EINVAL;
  762. return 0;
  763. }
  764. static inline void print_card_capability(struct slot *slot_cur)
  765. {
  766. info("capability of the card is ");
  767. if ((slot_cur->ext_status & CARD_INFO) == PCIX133)
  768. info(" 133 MHz PCI-X\n");
  769. else if ((slot_cur->ext_status & CARD_INFO) == PCIX66)
  770. info(" 66 MHz PCI-X\n");
  771. else if ((slot_cur->ext_status & CARD_INFO) == PCI66)
  772. info(" 66 MHz PCI\n");
  773. else
  774. info(" 33 MHz PCI\n");
  775. }
  776. /* This routine will power on the slot, configure the device(s) and find the
  777. * drivers for them.
  778. * Parameters: hotplug_slot
  779. * Returns: 0 or failure codes
  780. */
  781. static int enable_slot(struct hotplug_slot *hs)
  782. {
  783. int rc, i, rcpr;
  784. struct slot *slot_cur;
  785. u8 function;
  786. struct pci_func *tmp_func;
  787. ibmphp_lock_operations();
  788. debug("ENABLING SLOT........\n");
  789. slot_cur = to_slot(hs);
  790. rc = validate(slot_cur, ENABLE);
  791. if (rc) {
  792. err("validate function failed\n");
  793. goto error_nopower;
  794. }
  795. attn_LED_blink(slot_cur);
  796. rc = set_bus(slot_cur);
  797. if (rc) {
  798. err("was not able to set the bus\n");
  799. goto error_nopower;
  800. }
  801. /*-----------------debugging------------------------------*/
  802. get_cur_bus_info(&slot_cur);
  803. debug("the current bus speed right after set_bus = %x\n",
  804. slot_cur->bus_on->current_speed);
  805. /*----------------------------------------------------------*/
  806. rc = check_limitations(slot_cur);
  807. if (rc) {
  808. err("Adding this card exceeds the limitations of this bus.\n");
  809. err("(i.e., >1 133MHz cards running on same bus, or >2 66 PCI cards running on same bus.\n");
  810. err("Try hot-adding into another bus\n");
  811. rc = -EINVAL;
  812. goto error_nopower;
  813. }
  814. rc = power_on(slot_cur);
  815. if (rc) {
  816. err("something wrong when powering up... please see below for details\n");
  817. /* need to turn off before on, otherwise, blinking overwrites */
  818. attn_off(slot_cur);
  819. attn_on(slot_cur);
  820. if (slot_update(&slot_cur)) {
  821. attn_off(slot_cur);
  822. attn_on(slot_cur);
  823. rc = -ENODEV;
  824. goto exit;
  825. }
  826. /* Check to see the error of why it failed */
  827. if ((SLOT_POWER(slot_cur->status)) &&
  828. !(SLOT_PWRGD(slot_cur->status)))
  829. err("power fault occurred trying to power up\n");
  830. else if (SLOT_BUS_SPEED(slot_cur->status)) {
  831. err("bus speed mismatch occurred. please check current bus speed and card capability\n");
  832. print_card_capability(slot_cur);
  833. } else if (SLOT_BUS_MODE(slot_cur->ext_status)) {
  834. err("bus mode mismatch occurred. please check current bus mode and card capability\n");
  835. print_card_capability(slot_cur);
  836. }
  837. ibmphp_update_slot_info(slot_cur);
  838. goto exit;
  839. }
  840. debug("after power_on\n");
  841. /*-----------------------debugging---------------------------*/
  842. get_cur_bus_info(&slot_cur);
  843. debug("the current bus speed right after power_on = %x\n",
  844. slot_cur->bus_on->current_speed);
  845. /*----------------------------------------------------------*/
  846. rc = slot_update(&slot_cur);
  847. if (rc)
  848. goto error_power;
  849. rc = -EINVAL;
  850. if (SLOT_POWER(slot_cur->status) && !(SLOT_PWRGD(slot_cur->status))) {
  851. err("power fault occurred trying to power up...\n");
  852. goto error_power;
  853. }
  854. if (SLOT_POWER(slot_cur->status) && (SLOT_BUS_SPEED(slot_cur->status))) {
  855. err("bus speed mismatch occurred. please check current bus speed and card capability\n");
  856. print_card_capability(slot_cur);
  857. goto error_power;
  858. }
  859. /* Don't think this case will happen after above checks...
  860. * but just in case, for paranoia sake */
  861. if (!(SLOT_POWER(slot_cur->status))) {
  862. err("power on failed...\n");
  863. goto error_power;
  864. }
  865. slot_cur->func = kzalloc_obj(struct pci_func);
  866. if (!slot_cur->func) {
  867. /* do update_slot_info here? */
  868. rc = -ENOMEM;
  869. goto error_power;
  870. }
  871. slot_cur->func->busno = slot_cur->bus;
  872. slot_cur->func->device = slot_cur->device;
  873. for (i = 0; i < 4; i++)
  874. slot_cur->func->irq[i] = slot_cur->irq[i];
  875. debug("b4 configure_card, slot_cur->bus = %x, slot_cur->device = %x\n",
  876. slot_cur->bus, slot_cur->device);
  877. if (ibmphp_configure_card(slot_cur->func, slot_cur->number)) {
  878. err("configure_card was unsuccessful...\n");
  879. /* true because don't need to actually deallocate resources,
  880. * just remove references */
  881. ibmphp_unconfigure_card(&slot_cur, 1);
  882. debug("after unconfigure_card\n");
  883. slot_cur->func = NULL;
  884. rc = -ENOMEM;
  885. goto error_power;
  886. }
  887. function = 0x00;
  888. do {
  889. tmp_func = ibm_slot_find(slot_cur->bus, slot_cur->func->device,
  890. function++);
  891. if (tmp_func && !(tmp_func->dev))
  892. ibm_configure_device(tmp_func);
  893. } while (tmp_func);
  894. attn_off(slot_cur);
  895. if (slot_update(&slot_cur)) {
  896. rc = -EFAULT;
  897. goto exit;
  898. }
  899. ibmphp_print_test();
  900. rc = ibmphp_update_slot_info(slot_cur);
  901. exit:
  902. ibmphp_unlock_operations();
  903. return rc;
  904. error_nopower:
  905. attn_off(slot_cur); /* need to turn off if was blinking b4 */
  906. attn_on(slot_cur);
  907. error_cont:
  908. rcpr = slot_update(&slot_cur);
  909. if (rcpr) {
  910. rc = rcpr;
  911. goto exit;
  912. }
  913. ibmphp_update_slot_info(slot_cur);
  914. goto exit;
  915. error_power:
  916. attn_off(slot_cur); /* need to turn off if was blinking b4 */
  917. attn_on(slot_cur);
  918. rcpr = power_off(slot_cur);
  919. if (rcpr) {
  920. rc = rcpr;
  921. goto exit;
  922. }
  923. goto error_cont;
  924. }
  925. /**************************************************************
  926. * HOT REMOVING ADAPTER CARD *
  927. * INPUT: POINTER TO THE HOTPLUG SLOT STRUCTURE *
  928. * OUTPUT: SUCCESS 0 ; FAILURE: UNCONFIGURE , VALIDATE *
  929. * DISABLE POWER , *
  930. **************************************************************/
  931. static int ibmphp_disable_slot(struct hotplug_slot *hotplug_slot)
  932. {
  933. struct slot *slot = to_slot(hotplug_slot);
  934. int rc;
  935. ibmphp_lock_operations();
  936. rc = ibmphp_do_disable_slot(slot);
  937. ibmphp_unlock_operations();
  938. return rc;
  939. }
  940. int ibmphp_do_disable_slot(struct slot *slot_cur)
  941. {
  942. int rc;
  943. u8 flag;
  944. debug("DISABLING SLOT...\n");
  945. if ((slot_cur == NULL) || (slot_cur->ctrl == NULL))
  946. return -ENODEV;
  947. flag = slot_cur->flag;
  948. slot_cur->flag = 1;
  949. if (flag == 1) {
  950. rc = validate(slot_cur, DISABLE);
  951. /* checking if powered off already & valid slot # */
  952. if (rc)
  953. goto error;
  954. }
  955. attn_LED_blink(slot_cur);
  956. if (slot_cur->func == NULL) {
  957. /* We need this for functions that were there on bootup */
  958. slot_cur->func = kzalloc_obj(struct pci_func);
  959. if (!slot_cur->func) {
  960. rc = -ENOMEM;
  961. goto error;
  962. }
  963. slot_cur->func->busno = slot_cur->bus;
  964. slot_cur->func->device = slot_cur->device;
  965. }
  966. ibm_unconfigure_device(slot_cur->func);
  967. /*
  968. * If we got here from latch suddenly opening on operating card or
  969. * a power fault, there's no power to the card, so cannot
  970. * read from it to determine what resources it occupied. This operation
  971. * is forbidden anyhow. The best we can do is remove it from kernel
  972. * lists at least */
  973. if (!flag) {
  974. attn_off(slot_cur);
  975. return 0;
  976. }
  977. rc = ibmphp_unconfigure_card(&slot_cur, 0);
  978. slot_cur->func = NULL;
  979. debug("in disable_slot. after unconfigure_card\n");
  980. if (rc) {
  981. err("could not unconfigure card.\n");
  982. goto error;
  983. }
  984. rc = ibmphp_hpc_writeslot(slot_cur, HPC_SLOT_OFF);
  985. if (rc)
  986. goto error;
  987. attn_off(slot_cur);
  988. rc = slot_update(&slot_cur);
  989. if (rc)
  990. goto exit;
  991. rc = ibmphp_update_slot_info(slot_cur);
  992. ibmphp_print_test();
  993. exit:
  994. return rc;
  995. error:
  996. /* Need to turn off if was blinking b4 */
  997. attn_off(slot_cur);
  998. attn_on(slot_cur);
  999. if (slot_update(&slot_cur)) {
  1000. rc = -EFAULT;
  1001. goto exit;
  1002. }
  1003. if (flag)
  1004. ibmphp_update_slot_info(slot_cur);
  1005. goto exit;
  1006. }
  1007. const struct hotplug_slot_ops ibmphp_hotplug_slot_ops = {
  1008. .set_attention_status = set_attention_status,
  1009. .enable_slot = enable_slot,
  1010. .disable_slot = ibmphp_disable_slot,
  1011. .hardware_test = NULL,
  1012. .get_power_status = get_power_status,
  1013. .get_attention_status = get_attention_status,
  1014. .get_latch_status = get_latch_status,
  1015. .get_adapter_status = get_adapter_present,
  1016. };
  1017. static void ibmphp_unload(void)
  1018. {
  1019. free_slots();
  1020. debug("after slots\n");
  1021. ibmphp_free_resources();
  1022. debug("after resources\n");
  1023. ibmphp_free_bus_info_queue();
  1024. debug("after bus info\n");
  1025. ibmphp_free_ebda_hpc_queue();
  1026. debug("after ebda hpc\n");
  1027. ibmphp_free_ebda_pci_rsrc_queue();
  1028. debug("after ebda pci rsrc\n");
  1029. kfree(ibmphp_pci_bus);
  1030. }
  1031. static int __init ibmphp_init(void)
  1032. {
  1033. struct pci_bus *bus;
  1034. int i = 0;
  1035. int rc = 0;
  1036. init_flag = 1;
  1037. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  1038. ibmphp_pci_bus = kmalloc_obj(*ibmphp_pci_bus);
  1039. if (!ibmphp_pci_bus) {
  1040. rc = -ENOMEM;
  1041. goto exit;
  1042. }
  1043. bus = pci_find_bus(0, 0);
  1044. if (!bus) {
  1045. err("Can't find the root pci bus, can not continue\n");
  1046. rc = -ENODEV;
  1047. goto error;
  1048. }
  1049. memcpy(ibmphp_pci_bus, bus, sizeof(*ibmphp_pci_bus));
  1050. ibmphp_debug = debug;
  1051. for (i = 0; i < 16; i++)
  1052. irqs[i] = 0;
  1053. rc = ibmphp_access_ebda();
  1054. if (rc)
  1055. goto error;
  1056. debug("after ibmphp_access_ebda()\n");
  1057. rc = ibmphp_rsrc_init();
  1058. if (rc)
  1059. goto error;
  1060. debug("AFTER Resource & EBDA INITIALIZATIONS\n");
  1061. max_slots = get_max_slots();
  1062. rc = ibmphp_register_pci();
  1063. if (rc)
  1064. goto error;
  1065. if (init_ops()) {
  1066. rc = -ENODEV;
  1067. goto error;
  1068. }
  1069. ibmphp_print_test();
  1070. rc = ibmphp_hpc_start_poll_thread();
  1071. if (rc)
  1072. goto error;
  1073. exit:
  1074. return rc;
  1075. error:
  1076. ibmphp_unload();
  1077. goto exit;
  1078. }
  1079. static void __exit ibmphp_exit(void)
  1080. {
  1081. ibmphp_hpc_stop_poll_thread();
  1082. debug("after polling\n");
  1083. ibmphp_unload();
  1084. debug("done\n");
  1085. }
  1086. module_init(ibmphp_init);
  1087. module_exit(ibmphp_exit);