hpilo.c 22 KB

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