hpilo.c 22 KB

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