hpilo.c 21 KB

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