hpilo.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  1. /*
  2. * Driver for the HP iLO management processor.
  3. *
  4. * Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
  5. * David Altobelli <david.altobelli@hpe.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/module.h>
  14. #include <linux/fs.h>
  15. #include <linux/pci.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/ioport.h>
  18. #include <linux/device.h>
  19. #include <linux/file.h>
  20. #include <linux/cdev.h>
  21. #include <linux/sched.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/delay.h>
  24. #include <linux/uaccess.h>
  25. #include <linux/io.h>
  26. #include <linux/wait.h>
  27. #include <linux/poll.h>
  28. #include <linux/slab.h>
  29. #include "hpilo.h"
  30. static struct class *ilo_class;
  31. static unsigned int ilo_major;
  32. static unsigned int max_ccb = 16;
  33. static char ilo_hwdev[MAX_ILO_DEV];
  34. static inline int get_entry_id(int entry)
  35. {
  36. return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;
  37. }
  38. static inline int get_entry_len(int entry)
  39. {
  40. return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;
  41. }
  42. static inline int mk_entry(int id, int len)
  43. {
  44. int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
  45. return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;
  46. }
  47. static inline int desc_mem_sz(int nr_entry)
  48. {
  49. return nr_entry << L2_QENTRY_SZ;
  50. }
  51. /*
  52. * FIFO queues, shared with hardware.
  53. *
  54. * If a queue has empty slots, an entry is added to the queue tail,
  55. * and that entry is marked as occupied.
  56. * Entries can be dequeued from the head of the list, when the device
  57. * has marked the entry as consumed.
  58. *
  59. * Returns true on successful queue/dequeue, false on failure.
  60. */
  61. static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry)
  62. {
  63. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  64. unsigned long flags;
  65. int ret = 0;
  66. spin_lock_irqsave(&hw->fifo_lock, flags);
  67. if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]
  68. & ENTRY_MASK_O)) {
  69. fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=
  70. (entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;
  71. fifo_q->tail += 1;
  72. ret = 1;
  73. }
  74. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  75. return ret;
  76. }
  77. static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry)
  78. {
  79. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  80. unsigned long flags;
  81. int ret = 0;
  82. u64 c;
  83. spin_lock_irqsave(&hw->fifo_lock, flags);
  84. c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  85. if (c & ENTRY_MASK_C) {
  86. if (entry)
  87. *entry = c & ENTRY_MASK_NOSTATE;
  88. fifo_q->fifobar[fifo_q->head & fifo_q->imask] =
  89. (c | ENTRY_MASK) + 1;
  90. fifo_q->head += 1;
  91. ret = 1;
  92. }
  93. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  94. return ret;
  95. }
  96. static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar)
  97. {
  98. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  99. unsigned long flags;
  100. int ret = 0;
  101. u64 c;
  102. spin_lock_irqsave(&hw->fifo_lock, flags);
  103. c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  104. if (c & ENTRY_MASK_C)
  105. ret = 1;
  106. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  107. return ret;
  108. }
  109. static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,
  110. int dir, int id, int len)
  111. {
  112. char *fifobar;
  113. int entry;
  114. if (dir == SENDQ)
  115. fifobar = ccb->ccb_u1.send_fifobar;
  116. else
  117. fifobar = ccb->ccb_u3.recv_fifobar;
  118. entry = mk_entry(id, len);
  119. return fifo_enqueue(hw, fifobar, entry);
  120. }
  121. static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,
  122. int dir, int *id, int *len, void **pkt)
  123. {
  124. char *fifobar, *desc;
  125. int entry = 0, pkt_id = 0;
  126. int ret;
  127. if (dir == SENDQ) {
  128. fifobar = ccb->ccb_u1.send_fifobar;
  129. desc = ccb->ccb_u2.send_desc;
  130. } else {
  131. fifobar = ccb->ccb_u3.recv_fifobar;
  132. desc = ccb->ccb_u4.recv_desc;
  133. }
  134. ret = fifo_dequeue(hw, fifobar, &entry);
  135. if (ret) {
  136. pkt_id = get_entry_id(entry);
  137. if (id)
  138. *id = pkt_id;
  139. if (len)
  140. *len = get_entry_len(entry);
  141. if (pkt)
  142. *pkt = (void *)(desc + desc_mem_sz(pkt_id));
  143. }
  144. return ret;
  145. }
  146. static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb)
  147. {
  148. char *fifobar = ccb->ccb_u3.recv_fifobar;
  149. return fifo_check_recv(hw, fifobar);
  150. }
  151. static inline void doorbell_set(struct ccb *ccb)
  152. {
  153. iowrite8(1, ccb->ccb_u5.db_base);
  154. }
  155. static inline void doorbell_clr(struct ccb *ccb)
  156. {
  157. iowrite8(2, ccb->ccb_u5.db_base);
  158. }
  159. static inline int ctrl_set(int l2sz, int idxmask, int desclim)
  160. {
  161. int active = 0, go = 1;
  162. return l2sz << CTRL_BITPOS_L2SZ |
  163. idxmask << CTRL_BITPOS_FIFOINDEXMASK |
  164. desclim << CTRL_BITPOS_DESCLIMIT |
  165. active << CTRL_BITPOS_A |
  166. go << CTRL_BITPOS_G;
  167. }
  168. static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz)
  169. {
  170. /* for simplicity, use the same parameters for send and recv ctrls */
  171. ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  172. ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  173. }
  174. static inline int fifo_sz(int nr_entry)
  175. {
  176. /* size of a fifo is determined by the number of entries it contains */
  177. return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE;
  178. }
  179. static void fifo_setup(void *base_addr, int nr_entry)
  180. {
  181. struct fifo *fifo_q = base_addr;
  182. int i;
  183. /* set up an empty fifo */
  184. fifo_q->head = 0;
  185. fifo_q->tail = 0;
  186. fifo_q->reset = 0;
  187. fifo_q->nrents = nr_entry;
  188. fifo_q->imask = nr_entry - 1;
  189. fifo_q->merge = ENTRY_MASK_O;
  190. for (i = 0; i < nr_entry; i++)
  191. fifo_q->fifobar[i] = 0;
  192. }
  193. static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data)
  194. {
  195. struct ccb *driver_ccb = &data->driver_ccb;
  196. struct ccb __iomem *device_ccb = data->mapped_ccb;
  197. int retries;
  198. /* complicated dance to tell the hw we are stopping */
  199. doorbell_clr(driver_ccb);
  200. iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),
  201. &device_ccb->send_ctrl);
  202. iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),
  203. &device_ccb->recv_ctrl);
  204. /* give iLO some time to process stop request */
  205. for (retries = MAX_WAIT; retries > 0; retries--) {
  206. doorbell_set(driver_ccb);
  207. udelay(WAIT_TIME);
  208. if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))
  209. &&
  210. !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))
  211. break;
  212. }
  213. if (retries == 0)
  214. dev_err(&pdev->dev, "Closing, but controller still active\n");
  215. /* clear the hw ccb */
  216. memset_io(device_ccb, 0, sizeof(struct ccb));
  217. /* free resources used to back send/recv queues */
  218. pci_free_consistent(pdev, data->dma_size, data->dma_va, data->dma_pa);
  219. }
  220. static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
  221. {
  222. char *dma_va;
  223. dma_addr_t dma_pa;
  224. struct ccb *driver_ccb, *ilo_ccb;
  225. driver_ccb = &data->driver_ccb;
  226. ilo_ccb = &data->ilo_ccb;
  227. data->dma_size = 2 * fifo_sz(NR_QENTRY) +
  228. 2 * desc_mem_sz(NR_QENTRY) +
  229. ILO_START_ALIGN + ILO_CACHE_SZ;
  230. data->dma_va = pci_alloc_consistent(hw->ilo_dev, data->dma_size,
  231. &data->dma_pa);
  232. if (!data->dma_va)
  233. return -ENOMEM;
  234. dma_va = (char *)data->dma_va;
  235. dma_pa = data->dma_pa;
  236. memset(dma_va, 0, data->dma_size);
  237. dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);
  238. dma_pa = roundup(dma_pa, ILO_START_ALIGN);
  239. /*
  240. * Create two ccb's, one with virt addrs, one with phys addrs.
  241. * Copy the phys addr ccb to device shared mem.
  242. */
  243. ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);
  244. ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ);
  245. fifo_setup(dma_va, NR_QENTRY);
  246. driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;
  247. ilo_ccb->ccb_u1.send_fifobar_pa = dma_pa + FIFOHANDLESIZE;
  248. dma_va += fifo_sz(NR_QENTRY);
  249. dma_pa += fifo_sz(NR_QENTRY);
  250. dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);
  251. dma_pa = roundup(dma_pa, ILO_CACHE_SZ);
  252. fifo_setup(dma_va, NR_QENTRY);
  253. driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;
  254. ilo_ccb->ccb_u3.recv_fifobar_pa = dma_pa + FIFOHANDLESIZE;
  255. dma_va += fifo_sz(NR_QENTRY);
  256. dma_pa += fifo_sz(NR_QENTRY);
  257. driver_ccb->ccb_u2.send_desc = dma_va;
  258. ilo_ccb->ccb_u2.send_desc_pa = dma_pa;
  259. dma_pa += desc_mem_sz(NR_QENTRY);
  260. dma_va += desc_mem_sz(NR_QENTRY);
  261. driver_ccb->ccb_u4.recv_desc = dma_va;
  262. ilo_ccb->ccb_u4.recv_desc_pa = dma_pa;
  263. driver_ccb->channel = slot;
  264. ilo_ccb->channel = slot;
  265. driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);
  266. ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */
  267. return 0;
  268. }
  269. static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
  270. {
  271. int pkt_id, pkt_sz;
  272. struct ccb *driver_ccb = &data->driver_ccb;
  273. /* copy the ccb with physical addrs to device memory */
  274. data->mapped_ccb = (struct ccb __iomem *)
  275. (hw->ram_vaddr + (slot * ILOHW_CCB_SZ));
  276. memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb));
  277. /* put packets on the send and receive queues */
  278. pkt_sz = 0;
  279. for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {
  280. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);
  281. doorbell_set(driver_ccb);
  282. }
  283. pkt_sz = desc_mem_sz(1);
  284. for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)
  285. ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz);
  286. /* the ccb is ready to use */
  287. doorbell_clr(driver_ccb);
  288. }
  289. static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data)
  290. {
  291. int pkt_id, i;
  292. struct ccb *driver_ccb = &data->driver_ccb;
  293. /* make sure iLO is really handling requests */
  294. for (i = MAX_WAIT; i > 0; i--) {
  295. if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))
  296. break;
  297. udelay(WAIT_TIME);
  298. }
  299. if (i == 0) {
  300. dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet\n");
  301. return -EBUSY;
  302. }
  303. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);
  304. doorbell_set(driver_ccb);
  305. return 0;
  306. }
  307. static inline int is_channel_reset(struct ccb *ccb)
  308. {
  309. /* check for this particular channel needing a reset */
  310. return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;
  311. }
  312. static inline void set_channel_reset(struct ccb *ccb)
  313. {
  314. /* set a flag indicating this channel needs a reset */
  315. FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;
  316. }
  317. static inline int get_device_outbound(struct ilo_hwinfo *hw)
  318. {
  319. return ioread32(&hw->mmio_vaddr[DB_OUT]);
  320. }
  321. static inline int is_db_reset(int db_out)
  322. {
  323. return db_out & (1 << DB_RESET);
  324. }
  325. static inline int is_device_reset(struct ilo_hwinfo *hw)
  326. {
  327. /* check for global reset condition */
  328. return is_db_reset(get_device_outbound(hw));
  329. }
  330. static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr)
  331. {
  332. iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);
  333. }
  334. static inline void clear_device(struct ilo_hwinfo *hw)
  335. {
  336. /* clear the device (reset bits, pending channel entries) */
  337. clear_pending_db(hw, -1);
  338. }
  339. static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw)
  340. {
  341. iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);
  342. }
  343. static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw)
  344. {
  345. iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,
  346. &hw->mmio_vaddr[DB_IRQ]);
  347. }
  348. static void ilo_set_reset(struct ilo_hwinfo *hw)
  349. {
  350. int slot;
  351. /*
  352. * Mapped memory is zeroed on ilo reset, so set a per ccb flag
  353. * to indicate that this ccb needs to be closed and reopened.
  354. */
  355. for (slot = 0; slot < max_ccb; slot++) {
  356. if (!hw->ccb_alloc[slot])
  357. continue;
  358. set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);
  359. }
  360. }
  361. static ssize_t ilo_read(struct file *fp, char __user *buf,
  362. size_t len, loff_t *off)
  363. {
  364. int err, found, cnt, pkt_id, pkt_len;
  365. struct ccb_data *data = fp->private_data;
  366. struct ccb *driver_ccb = &data->driver_ccb;
  367. struct ilo_hwinfo *hw = data->ilo_hw;
  368. void *pkt;
  369. if (is_channel_reset(driver_ccb)) {
  370. /*
  371. * If the device has been reset, applications
  372. * need to close and reopen all ccbs.
  373. */
  374. return -ENODEV;
  375. }
  376. /*
  377. * This function is to be called when data is expected
  378. * in the channel, and will return an error if no packet is found
  379. * during the loop below. The sleep/retry logic is to allow
  380. * applications to call read() immediately post write(),
  381. * and give iLO some time to process the sent packet.
  382. */
  383. cnt = 20;
  384. do {
  385. /* look for a received packet */
  386. found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,
  387. &pkt_len, &pkt);
  388. if (found)
  389. break;
  390. cnt--;
  391. msleep(100);
  392. } while (!found && cnt);
  393. if (!found)
  394. return -EAGAIN;
  395. /* only copy the length of the received packet */
  396. if (pkt_len < len)
  397. len = pkt_len;
  398. err = copy_to_user(buf, pkt, len);
  399. /* return the received packet to the queue */
  400. ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1));
  401. return err ? -EFAULT : len;
  402. }
  403. static ssize_t ilo_write(struct file *fp, const char __user *buf,
  404. size_t len, loff_t *off)
  405. {
  406. int err, pkt_id, pkt_len;
  407. struct ccb_data *data = fp->private_data;
  408. struct ccb *driver_ccb = &data->driver_ccb;
  409. struct ilo_hwinfo *hw = data->ilo_hw;
  410. void *pkt;
  411. if (is_channel_reset(driver_ccb))
  412. return -ENODEV;
  413. /* get a packet to send the user command */
  414. if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))
  415. return -EBUSY;
  416. /* limit the length to the length of the packet */
  417. if (pkt_len < len)
  418. len = pkt_len;
  419. /* on failure, set the len to 0 to return empty packet to the device */
  420. err = copy_from_user(pkt, buf, len);
  421. if (err)
  422. len = 0;
  423. /* send the packet */
  424. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, len);
  425. doorbell_set(driver_ccb);
  426. return err ? -EFAULT : len;
  427. }
  428. static unsigned int ilo_poll(struct file *fp, poll_table *wait)
  429. {
  430. struct ccb_data *data = fp->private_data;
  431. struct ccb *driver_ccb = &data->driver_ccb;
  432. poll_wait(fp, &data->ccb_waitq, wait);
  433. if (is_channel_reset(driver_ccb))
  434. return POLLERR;
  435. else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
  436. return POLLIN | POLLRDNORM;
  437. return 0;
  438. }
  439. static int ilo_close(struct inode *ip, struct file *fp)
  440. {
  441. int slot;
  442. struct ccb_data *data;
  443. struct ilo_hwinfo *hw;
  444. unsigned long flags;
  445. slot = iminor(ip) % max_ccb;
  446. hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
  447. spin_lock(&hw->open_lock);
  448. if (hw->ccb_alloc[slot]->ccb_cnt == 1) {
  449. data = fp->private_data;
  450. spin_lock_irqsave(&hw->alloc_lock, flags);
  451. hw->ccb_alloc[slot] = NULL;
  452. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  453. ilo_ccb_close(hw->ilo_dev, data);
  454. kfree(data);
  455. } else
  456. hw->ccb_alloc[slot]->ccb_cnt--;
  457. spin_unlock(&hw->open_lock);
  458. return 0;
  459. }
  460. static int ilo_open(struct inode *ip, struct file *fp)
  461. {
  462. int slot, error;
  463. struct ccb_data *data;
  464. struct ilo_hwinfo *hw;
  465. unsigned long flags;
  466. slot = iminor(ip) % max_ccb;
  467. hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
  468. /* new ccb allocation */
  469. data = kzalloc(sizeof(*data), GFP_KERNEL);
  470. if (!data)
  471. return -ENOMEM;
  472. spin_lock(&hw->open_lock);
  473. /* each fd private_data holds sw/hw view of ccb */
  474. if (hw->ccb_alloc[slot] == NULL) {
  475. /* create a channel control block for this minor */
  476. error = ilo_ccb_setup(hw, data, slot);
  477. if (error) {
  478. kfree(data);
  479. goto out;
  480. }
  481. data->ccb_cnt = 1;
  482. data->ccb_excl = fp->f_flags & O_EXCL;
  483. data->ilo_hw = hw;
  484. init_waitqueue_head(&data->ccb_waitq);
  485. /* write the ccb to hw */
  486. spin_lock_irqsave(&hw->alloc_lock, flags);
  487. ilo_ccb_open(hw, data, slot);
  488. hw->ccb_alloc[slot] = data;
  489. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  490. /* make sure the channel is functional */
  491. error = ilo_ccb_verify(hw, data);
  492. if (error) {
  493. spin_lock_irqsave(&hw->alloc_lock, flags);
  494. hw->ccb_alloc[slot] = NULL;
  495. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  496. ilo_ccb_close(hw->ilo_dev, data);
  497. kfree(data);
  498. goto out;
  499. }
  500. } else {
  501. kfree(data);
  502. if (fp->f_flags & O_EXCL || hw->ccb_alloc[slot]->ccb_excl) {
  503. /*
  504. * The channel exists, and either this open
  505. * or a previous open of this channel wants
  506. * exclusive access.
  507. */
  508. error = -EBUSY;
  509. } else {
  510. hw->ccb_alloc[slot]->ccb_cnt++;
  511. error = 0;
  512. }
  513. }
  514. out:
  515. spin_unlock(&hw->open_lock);
  516. if (!error)
  517. fp->private_data = hw->ccb_alloc[slot];
  518. return error;
  519. }
  520. static const struct file_operations ilo_fops = {
  521. .owner = THIS_MODULE,
  522. .read = ilo_read,
  523. .write = ilo_write,
  524. .poll = ilo_poll,
  525. .open = ilo_open,
  526. .release = ilo_close,
  527. .llseek = noop_llseek,
  528. };
  529. static irqreturn_t ilo_isr(int irq, void *data)
  530. {
  531. struct ilo_hwinfo *hw = data;
  532. int pending, i;
  533. spin_lock(&hw->alloc_lock);
  534. /* check for ccbs which have data */
  535. pending = get_device_outbound(hw);
  536. if (!pending) {
  537. spin_unlock(&hw->alloc_lock);
  538. return IRQ_NONE;
  539. }
  540. if (is_db_reset(pending)) {
  541. /* wake up all ccbs if the device was reset */
  542. pending = -1;
  543. ilo_set_reset(hw);
  544. }
  545. for (i = 0; i < max_ccb; i++) {
  546. if (!hw->ccb_alloc[i])
  547. continue;
  548. if (pending & (1 << i))
  549. wake_up_interruptible(&hw->ccb_alloc[i]->ccb_waitq);
  550. }
  551. /* clear the device of the channels that have been handled */
  552. clear_pending_db(hw, pending);
  553. spin_unlock(&hw->alloc_lock);
  554. return IRQ_HANDLED;
  555. }
  556. static void ilo_unmap_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  557. {
  558. pci_iounmap(pdev, hw->db_vaddr);
  559. pci_iounmap(pdev, hw->ram_vaddr);
  560. pci_iounmap(pdev, hw->mmio_vaddr);
  561. }
  562. static int ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  563. {
  564. int error = -ENOMEM;
  565. /* map the memory mapped i/o registers */
  566. hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
  567. if (hw->mmio_vaddr == NULL) {
  568. dev_err(&pdev->dev, "Error mapping mmio\n");
  569. goto out;
  570. }
  571. /* map the adapter shared memory region */
  572. hw->ram_vaddr = pci_iomap(pdev, 2, max_ccb * ILOHW_CCB_SZ);
  573. if (hw->ram_vaddr == NULL) {
  574. dev_err(&pdev->dev, "Error mapping shared mem\n");
  575. goto mmio_free;
  576. }
  577. /* map the doorbell aperture */
  578. hw->db_vaddr = pci_iomap(pdev, 3, max_ccb * ONE_DB_SIZE);
  579. if (hw->db_vaddr == NULL) {
  580. dev_err(&pdev->dev, "Error mapping doorbell\n");
  581. goto ram_free;
  582. }
  583. return 0;
  584. ram_free:
  585. pci_iounmap(pdev, hw->ram_vaddr);
  586. mmio_free:
  587. pci_iounmap(pdev, hw->mmio_vaddr);
  588. out:
  589. return error;
  590. }
  591. static void ilo_remove(struct pci_dev *pdev)
  592. {
  593. int i, minor;
  594. struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
  595. if (!ilo_hw)
  596. return;
  597. clear_device(ilo_hw);
  598. minor = MINOR(ilo_hw->cdev.dev);
  599. for (i = minor; i < minor + max_ccb; i++)
  600. device_destroy(ilo_class, MKDEV(ilo_major, i));
  601. cdev_del(&ilo_hw->cdev);
  602. ilo_disable_interrupts(ilo_hw);
  603. free_irq(pdev->irq, ilo_hw);
  604. ilo_unmap_device(pdev, ilo_hw);
  605. pci_release_regions(pdev);
  606. /*
  607. * pci_disable_device(pdev) used to be here. But this PCI device has
  608. * two functions with interrupt lines connected to a single pin. The
  609. * other one is a USB host controller. So when we disable the PIN here
  610. * e.g. by rmmod hpilo, the controller stops working. It is because
  611. * the interrupt link is disabled in ACPI since it is not refcounted
  612. * yet. See acpi_pci_link_free_irq called from acpi_pci_irq_disable.
  613. */
  614. kfree(ilo_hw);
  615. ilo_hwdev[(minor / max_ccb)] = 0;
  616. }
  617. static int ilo_probe(struct pci_dev *pdev,
  618. const struct pci_device_id *ent)
  619. {
  620. int devnum, minor, start, error = 0;
  621. struct ilo_hwinfo *ilo_hw;
  622. /* Ignore subsystem_device = 0x1979 (set by BIOS) */
  623. if (pdev->subsystem_device == 0x1979)
  624. return 0;
  625. if (max_ccb > MAX_CCB)
  626. max_ccb = MAX_CCB;
  627. else if (max_ccb < MIN_CCB)
  628. max_ccb = MIN_CCB;
  629. /* find a free range for device files */
  630. for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
  631. if (ilo_hwdev[devnum] == 0) {
  632. ilo_hwdev[devnum] = 1;
  633. break;
  634. }
  635. }
  636. if (devnum == MAX_ILO_DEV) {
  637. dev_err(&pdev->dev, "Error finding free device\n");
  638. return -ENODEV;
  639. }
  640. /* track global allocations for this device */
  641. error = -ENOMEM;
  642. ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
  643. if (!ilo_hw)
  644. goto out;
  645. ilo_hw->ilo_dev = pdev;
  646. spin_lock_init(&ilo_hw->alloc_lock);
  647. spin_lock_init(&ilo_hw->fifo_lock);
  648. spin_lock_init(&ilo_hw->open_lock);
  649. error = pci_enable_device(pdev);
  650. if (error)
  651. goto free;
  652. pci_set_master(pdev);
  653. error = pci_request_regions(pdev, ILO_NAME);
  654. if (error)
  655. goto disable;
  656. error = ilo_map_device(pdev, ilo_hw);
  657. if (error)
  658. goto free_regions;
  659. pci_set_drvdata(pdev, ilo_hw);
  660. clear_device(ilo_hw);
  661. error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
  662. if (error)
  663. goto unmap;
  664. ilo_enable_interrupts(ilo_hw);
  665. cdev_init(&ilo_hw->cdev, &ilo_fops);
  666. ilo_hw->cdev.owner = THIS_MODULE;
  667. start = devnum * max_ccb;
  668. error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), max_ccb);
  669. if (error) {
  670. dev_err(&pdev->dev, "Could not add cdev\n");
  671. goto remove_isr;
  672. }
  673. for (minor = 0 ; minor < max_ccb; minor++) {
  674. struct device *dev;
  675. dev = device_create(ilo_class, &pdev->dev,
  676. MKDEV(ilo_major, minor), NULL,
  677. "hpilo!d%dccb%d", devnum, minor);
  678. if (IS_ERR(dev))
  679. dev_err(&pdev->dev, "Could not create files\n");
  680. }
  681. return 0;
  682. remove_isr:
  683. ilo_disable_interrupts(ilo_hw);
  684. free_irq(pdev->irq, ilo_hw);
  685. unmap:
  686. ilo_unmap_device(pdev, ilo_hw);
  687. free_regions:
  688. pci_release_regions(pdev);
  689. disable:
  690. /* pci_disable_device(pdev); see comment in ilo_remove */
  691. free:
  692. kfree(ilo_hw);
  693. out:
  694. ilo_hwdev[devnum] = 0;
  695. return error;
  696. }
  697. static struct pci_device_id ilo_devices[] = {
  698. { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
  699. { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
  700. { }
  701. };
  702. MODULE_DEVICE_TABLE(pci, ilo_devices);
  703. static struct pci_driver ilo_driver = {
  704. .name = ILO_NAME,
  705. .id_table = ilo_devices,
  706. .probe = ilo_probe,
  707. .remove = ilo_remove,
  708. };
  709. static int __init ilo_init(void)
  710. {
  711. int error;
  712. dev_t dev;
  713. ilo_class = class_create(THIS_MODULE, "iLO");
  714. if (IS_ERR(ilo_class)) {
  715. error = PTR_ERR(ilo_class);
  716. goto out;
  717. }
  718. error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
  719. if (error)
  720. goto class_destroy;
  721. ilo_major = MAJOR(dev);
  722. error = pci_register_driver(&ilo_driver);
  723. if (error)
  724. goto chr_remove;
  725. return 0;
  726. chr_remove:
  727. unregister_chrdev_region(dev, MAX_OPEN);
  728. class_destroy:
  729. class_destroy(ilo_class);
  730. out:
  731. return error;
  732. }
  733. static void __exit ilo_exit(void)
  734. {
  735. pci_unregister_driver(&ilo_driver);
  736. unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
  737. class_destroy(ilo_class);
  738. }
  739. MODULE_VERSION("1.4.1");
  740. MODULE_ALIAS(ILO_NAME);
  741. MODULE_DESCRIPTION(ILO_NAME);
  742. MODULE_AUTHOR("David Altobelli <david.altobelli@hpe.com>");
  743. MODULE_LICENSE("GPL v2");
  744. module_param(max_ccb, uint, 0444);
  745. MODULE_PARM_DESC(max_ccb, "Maximum number of HP iLO channels to attach (8-24)(default=16)");
  746. module_init(ilo_init);
  747. module_exit(ilo_exit);