hpilo.c 22 KB

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