hpilo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  1. /*
  2. * Driver for HP iLO/iLO2 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, *dma_pa;
  222. struct ccb *driver_ccb, *ilo_ccb;
  223. driver_ccb = &data->driver_ccb;
  224. ilo_ccb = &data->ilo_ccb;
  225. data->dma_size = 2 * fifo_sz(NR_QENTRY) +
  226. 2 * desc_mem_sz(NR_QENTRY) +
  227. ILO_START_ALIGN + ILO_CACHE_SZ;
  228. data->dma_va = pci_alloc_consistent(hw->ilo_dev, data->dma_size,
  229. &data->dma_pa);
  230. if (!data->dma_va)
  231. return -ENOMEM;
  232. dma_va = (char *)data->dma_va;
  233. dma_pa = (char *)data->dma_pa;
  234. memset(dma_va, 0, data->dma_size);
  235. dma_va = (char *)roundup((unsigned long)dma_va, ILO_START_ALIGN);
  236. dma_pa = (char *)roundup((unsigned long)dma_pa, ILO_START_ALIGN);
  237. /*
  238. * Create two ccb's, one with virt addrs, one with phys addrs.
  239. * Copy the phys addr ccb to device shared mem.
  240. */
  241. ctrl_setup(driver_ccb, NR_QENTRY, L2_QENTRY_SZ);
  242. ctrl_setup(ilo_ccb, NR_QENTRY, L2_QENTRY_SZ);
  243. fifo_setup(dma_va, NR_QENTRY);
  244. driver_ccb->ccb_u1.send_fifobar = dma_va + FIFOHANDLESIZE;
  245. ilo_ccb->ccb_u1.send_fifobar = dma_pa + FIFOHANDLESIZE;
  246. dma_va += fifo_sz(NR_QENTRY);
  247. dma_pa += fifo_sz(NR_QENTRY);
  248. dma_va = (char *)roundup((unsigned long)dma_va, ILO_CACHE_SZ);
  249. dma_pa = (char *)roundup((unsigned long)dma_pa, ILO_CACHE_SZ);
  250. fifo_setup(dma_va, NR_QENTRY);
  251. driver_ccb->ccb_u3.recv_fifobar = dma_va + FIFOHANDLESIZE;
  252. ilo_ccb->ccb_u3.recv_fifobar = dma_pa + FIFOHANDLESIZE;
  253. dma_va += fifo_sz(NR_QENTRY);
  254. dma_pa += fifo_sz(NR_QENTRY);
  255. driver_ccb->ccb_u2.send_desc = dma_va;
  256. ilo_ccb->ccb_u2.send_desc = dma_pa;
  257. dma_pa += desc_mem_sz(NR_QENTRY);
  258. dma_va += desc_mem_sz(NR_QENTRY);
  259. driver_ccb->ccb_u4.recv_desc = dma_va;
  260. ilo_ccb->ccb_u4.recv_desc = dma_pa;
  261. driver_ccb->channel = slot;
  262. ilo_ccb->channel = slot;
  263. driver_ccb->ccb_u5.db_base = hw->db_vaddr + (slot << L2_DB_SIZE);
  264. ilo_ccb->ccb_u5.db_base = NULL; /* hw ccb's doorbell is not used */
  265. return 0;
  266. }
  267. static void ilo_ccb_open(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
  268. {
  269. int pkt_id, pkt_sz;
  270. struct ccb *driver_ccb = &data->driver_ccb;
  271. /* copy the ccb with physical addrs to device memory */
  272. data->mapped_ccb = (struct ccb __iomem *)
  273. (hw->ram_vaddr + (slot * ILOHW_CCB_SZ));
  274. memcpy_toio(data->mapped_ccb, &data->ilo_ccb, sizeof(struct ccb));
  275. /* put packets on the send and receive queues */
  276. pkt_sz = 0;
  277. for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++) {
  278. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, pkt_sz);
  279. doorbell_set(driver_ccb);
  280. }
  281. pkt_sz = desc_mem_sz(1);
  282. for (pkt_id = 0; pkt_id < NR_QENTRY; pkt_id++)
  283. ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, pkt_sz);
  284. /* the ccb is ready to use */
  285. doorbell_clr(driver_ccb);
  286. }
  287. static int ilo_ccb_verify(struct ilo_hwinfo *hw, struct ccb_data *data)
  288. {
  289. int pkt_id, i;
  290. struct ccb *driver_ccb = &data->driver_ccb;
  291. /* make sure iLO is really handling requests */
  292. for (i = MAX_WAIT; i > 0; i--) {
  293. if (ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, NULL, NULL))
  294. break;
  295. udelay(WAIT_TIME);
  296. }
  297. if (i == 0) {
  298. dev_err(&hw->ilo_dev->dev, "Open could not dequeue a packet\n");
  299. return -EBUSY;
  300. }
  301. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, 0);
  302. doorbell_set(driver_ccb);
  303. return 0;
  304. }
  305. static inline int is_channel_reset(struct ccb *ccb)
  306. {
  307. /* check for this particular channel needing a reset */
  308. return FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset;
  309. }
  310. static inline void set_channel_reset(struct ccb *ccb)
  311. {
  312. /* set a flag indicating this channel needs a reset */
  313. FIFOBARTOHANDLE(ccb->ccb_u1.send_fifobar)->reset = 1;
  314. }
  315. static inline int get_device_outbound(struct ilo_hwinfo *hw)
  316. {
  317. return ioread32(&hw->mmio_vaddr[DB_OUT]);
  318. }
  319. static inline int is_db_reset(int db_out)
  320. {
  321. return db_out & (1 << DB_RESET);
  322. }
  323. static inline int is_device_reset(struct ilo_hwinfo *hw)
  324. {
  325. /* check for global reset condition */
  326. return is_db_reset(get_device_outbound(hw));
  327. }
  328. static inline void clear_pending_db(struct ilo_hwinfo *hw, int clr)
  329. {
  330. iowrite32(clr, &hw->mmio_vaddr[DB_OUT]);
  331. }
  332. static inline void clear_device(struct ilo_hwinfo *hw)
  333. {
  334. /* clear the device (reset bits, pending channel entries) */
  335. clear_pending_db(hw, -1);
  336. }
  337. static inline void ilo_enable_interrupts(struct ilo_hwinfo *hw)
  338. {
  339. iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) | 1, &hw->mmio_vaddr[DB_IRQ]);
  340. }
  341. static inline void ilo_disable_interrupts(struct ilo_hwinfo *hw)
  342. {
  343. iowrite8(ioread8(&hw->mmio_vaddr[DB_IRQ]) & ~1,
  344. &hw->mmio_vaddr[DB_IRQ]);
  345. }
  346. static void ilo_set_reset(struct ilo_hwinfo *hw)
  347. {
  348. int slot;
  349. /*
  350. * Mapped memory is zeroed on ilo reset, so set a per ccb flag
  351. * to indicate that this ccb needs to be closed and reopened.
  352. */
  353. for (slot = 0; slot < MAX_CCB; slot++) {
  354. if (!hw->ccb_alloc[slot])
  355. continue;
  356. set_channel_reset(&hw->ccb_alloc[slot]->driver_ccb);
  357. }
  358. }
  359. static ssize_t ilo_read(struct file *fp, char __user *buf,
  360. size_t len, loff_t *off)
  361. {
  362. int err, found, cnt, pkt_id, pkt_len;
  363. struct ccb_data *data = fp->private_data;
  364. struct ccb *driver_ccb = &data->driver_ccb;
  365. struct ilo_hwinfo *hw = data->ilo_hw;
  366. void *pkt;
  367. if (is_channel_reset(driver_ccb)) {
  368. /*
  369. * If the device has been reset, applications
  370. * need to close and reopen all ccbs.
  371. */
  372. return -ENODEV;
  373. }
  374. /*
  375. * This function is to be called when data is expected
  376. * in the channel, and will return an error if no packet is found
  377. * during the loop below. The sleep/retry logic is to allow
  378. * applications to call read() immediately post write(),
  379. * and give iLO some time to process the sent packet.
  380. */
  381. cnt = 20;
  382. do {
  383. /* look for a received packet */
  384. found = ilo_pkt_dequeue(hw, driver_ccb, RECVQ, &pkt_id,
  385. &pkt_len, &pkt);
  386. if (found)
  387. break;
  388. cnt--;
  389. msleep(100);
  390. } while (!found && cnt);
  391. if (!found)
  392. return -EAGAIN;
  393. /* only copy the length of the received packet */
  394. if (pkt_len < len)
  395. len = pkt_len;
  396. err = copy_to_user(buf, pkt, len);
  397. /* return the received packet to the queue */
  398. ilo_pkt_enqueue(hw, driver_ccb, RECVQ, pkt_id, desc_mem_sz(1));
  399. return err ? -EFAULT : len;
  400. }
  401. static ssize_t ilo_write(struct file *fp, const char __user *buf,
  402. size_t len, loff_t *off)
  403. {
  404. int err, pkt_id, pkt_len;
  405. struct ccb_data *data = fp->private_data;
  406. struct ccb *driver_ccb = &data->driver_ccb;
  407. struct ilo_hwinfo *hw = data->ilo_hw;
  408. void *pkt;
  409. if (is_channel_reset(driver_ccb))
  410. return -ENODEV;
  411. /* get a packet to send the user command */
  412. if (!ilo_pkt_dequeue(hw, driver_ccb, SENDQ, &pkt_id, &pkt_len, &pkt))
  413. return -EBUSY;
  414. /* limit the length to the length of the packet */
  415. if (pkt_len < len)
  416. len = pkt_len;
  417. /* on failure, set the len to 0 to return empty packet to the device */
  418. err = copy_from_user(pkt, buf, len);
  419. if (err)
  420. len = 0;
  421. /* send the packet */
  422. ilo_pkt_enqueue(hw, driver_ccb, SENDQ, pkt_id, len);
  423. doorbell_set(driver_ccb);
  424. return err ? -EFAULT : len;
  425. }
  426. static unsigned int ilo_poll(struct file *fp, poll_table *wait)
  427. {
  428. struct ccb_data *data = fp->private_data;
  429. struct ccb *driver_ccb = &data->driver_ccb;
  430. poll_wait(fp, &data->ccb_waitq, wait);
  431. if (is_channel_reset(driver_ccb))
  432. return POLLERR;
  433. else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
  434. return POLLIN | POLLRDNORM;
  435. return 0;
  436. }
  437. static int ilo_close(struct inode *ip, struct file *fp)
  438. {
  439. int slot;
  440. struct ccb_data *data;
  441. struct ilo_hwinfo *hw;
  442. unsigned long flags;
  443. slot = iminor(ip) % MAX_CCB;
  444. hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
  445. spin_lock(&hw->open_lock);
  446. if (hw->ccb_alloc[slot]->ccb_cnt == 1) {
  447. data = fp->private_data;
  448. spin_lock_irqsave(&hw->alloc_lock, flags);
  449. hw->ccb_alloc[slot] = NULL;
  450. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  451. ilo_ccb_close(hw->ilo_dev, data);
  452. kfree(data);
  453. } else
  454. hw->ccb_alloc[slot]->ccb_cnt--;
  455. spin_unlock(&hw->open_lock);
  456. return 0;
  457. }
  458. static int ilo_open(struct inode *ip, struct file *fp)
  459. {
  460. int slot, error;
  461. struct ccb_data *data;
  462. struct ilo_hwinfo *hw;
  463. unsigned long flags;
  464. slot = iminor(ip) % MAX_CCB;
  465. hw = container_of(ip->i_cdev, struct ilo_hwinfo, cdev);
  466. /* new ccb allocation */
  467. data = kzalloc(sizeof(*data), GFP_KERNEL);
  468. if (!data)
  469. return -ENOMEM;
  470. spin_lock(&hw->open_lock);
  471. /* each fd private_data holds sw/hw view of ccb */
  472. if (hw->ccb_alloc[slot] == NULL) {
  473. /* create a channel control block for this minor */
  474. error = ilo_ccb_setup(hw, data, slot);
  475. if (error) {
  476. kfree(data);
  477. goto out;
  478. }
  479. data->ccb_cnt = 1;
  480. data->ccb_excl = fp->f_flags & O_EXCL;
  481. data->ilo_hw = hw;
  482. init_waitqueue_head(&data->ccb_waitq);
  483. /* write the ccb to hw */
  484. spin_lock_irqsave(&hw->alloc_lock, flags);
  485. ilo_ccb_open(hw, data, slot);
  486. hw->ccb_alloc[slot] = data;
  487. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  488. /* make sure the channel is functional */
  489. error = ilo_ccb_verify(hw, data);
  490. if (error) {
  491. spin_lock_irqsave(&hw->alloc_lock, flags);
  492. hw->ccb_alloc[slot] = NULL;
  493. spin_unlock_irqrestore(&hw->alloc_lock, flags);
  494. ilo_ccb_close(hw->ilo_dev, data);
  495. kfree(data);
  496. goto out;
  497. }
  498. } else {
  499. kfree(data);
  500. if (fp->f_flags & O_EXCL || hw->ccb_alloc[slot]->ccb_excl) {
  501. /*
  502. * The channel exists, and either this open
  503. * or a previous open of this channel wants
  504. * exclusive access.
  505. */
  506. error = -EBUSY;
  507. } else {
  508. hw->ccb_alloc[slot]->ccb_cnt++;
  509. error = 0;
  510. }
  511. }
  512. out:
  513. spin_unlock(&hw->open_lock);
  514. if (!error)
  515. fp->private_data = hw->ccb_alloc[slot];
  516. return error;
  517. }
  518. static const struct file_operations ilo_fops = {
  519. .owner = THIS_MODULE,
  520. .read = ilo_read,
  521. .write = ilo_write,
  522. .poll = ilo_poll,
  523. .open = ilo_open,
  524. .release = ilo_close,
  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 __devinit ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  560. {
  561. int error = -ENOMEM;
  562. /* map the memory mapped i/o registers */
  563. hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
  564. if (hw->mmio_vaddr == NULL) {
  565. dev_err(&pdev->dev, "Error mapping mmio\n");
  566. goto out;
  567. }
  568. /* map the adapter shared memory region */
  569. hw->ram_vaddr = pci_iomap(pdev, 2, MAX_CCB * ILOHW_CCB_SZ);
  570. if (hw->ram_vaddr == NULL) {
  571. dev_err(&pdev->dev, "Error mapping shared mem\n");
  572. goto mmio_free;
  573. }
  574. /* map the doorbell aperture */
  575. hw->db_vaddr = pci_iomap(pdev, 3, MAX_CCB * ONE_DB_SIZE);
  576. if (hw->db_vaddr == NULL) {
  577. dev_err(&pdev->dev, "Error mapping doorbell\n");
  578. goto ram_free;
  579. }
  580. return 0;
  581. ram_free:
  582. pci_iounmap(pdev, hw->ram_vaddr);
  583. mmio_free:
  584. pci_iounmap(pdev, hw->mmio_vaddr);
  585. out:
  586. return error;
  587. }
  588. static void ilo_remove(struct pci_dev *pdev)
  589. {
  590. int i, minor;
  591. struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
  592. clear_device(ilo_hw);
  593. minor = MINOR(ilo_hw->cdev.dev);
  594. for (i = minor; i < minor + MAX_CCB; i++)
  595. device_destroy(ilo_class, MKDEV(ilo_major, i));
  596. cdev_del(&ilo_hw->cdev);
  597. ilo_disable_interrupts(ilo_hw);
  598. free_irq(pdev->irq, ilo_hw);
  599. ilo_unmap_device(pdev, ilo_hw);
  600. pci_release_regions(pdev);
  601. pci_disable_device(pdev);
  602. kfree(ilo_hw);
  603. ilo_hwdev[(minor / MAX_CCB)] = 0;
  604. }
  605. static int __devinit ilo_probe(struct pci_dev *pdev,
  606. const struct pci_device_id *ent)
  607. {
  608. int devnum, minor, start, error;
  609. struct ilo_hwinfo *ilo_hw;
  610. /* find a free range for device files */
  611. for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
  612. if (ilo_hwdev[devnum] == 0) {
  613. ilo_hwdev[devnum] = 1;
  614. break;
  615. }
  616. }
  617. if (devnum == MAX_ILO_DEV) {
  618. dev_err(&pdev->dev, "Error finding free device\n");
  619. return -ENODEV;
  620. }
  621. /* track global allocations for this device */
  622. error = -ENOMEM;
  623. ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
  624. if (!ilo_hw)
  625. goto out;
  626. ilo_hw->ilo_dev = pdev;
  627. spin_lock_init(&ilo_hw->alloc_lock);
  628. spin_lock_init(&ilo_hw->fifo_lock);
  629. spin_lock_init(&ilo_hw->open_lock);
  630. error = pci_enable_device(pdev);
  631. if (error)
  632. goto free;
  633. pci_set_master(pdev);
  634. error = pci_request_regions(pdev, ILO_NAME);
  635. if (error)
  636. goto disable;
  637. error = ilo_map_device(pdev, ilo_hw);
  638. if (error)
  639. goto free_regions;
  640. pci_set_drvdata(pdev, ilo_hw);
  641. clear_device(ilo_hw);
  642. error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
  643. if (error)
  644. goto unmap;
  645. ilo_enable_interrupts(ilo_hw);
  646. cdev_init(&ilo_hw->cdev, &ilo_fops);
  647. ilo_hw->cdev.owner = THIS_MODULE;
  648. start = devnum * MAX_CCB;
  649. error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), MAX_CCB);
  650. if (error) {
  651. dev_err(&pdev->dev, "Could not add cdev\n");
  652. goto remove_isr;
  653. }
  654. for (minor = 0 ; minor < MAX_CCB; minor++) {
  655. struct device *dev;
  656. dev = device_create(ilo_class, &pdev->dev,
  657. MKDEV(ilo_major, minor), NULL,
  658. "hpilo!d%dccb%d", devnum, minor);
  659. if (IS_ERR(dev))
  660. dev_err(&pdev->dev, "Could not create files\n");
  661. }
  662. return 0;
  663. remove_isr:
  664. ilo_disable_interrupts(ilo_hw);
  665. free_irq(pdev->irq, ilo_hw);
  666. unmap:
  667. ilo_unmap_device(pdev, ilo_hw);
  668. free_regions:
  669. pci_release_regions(pdev);
  670. disable:
  671. pci_disable_device(pdev);
  672. free:
  673. kfree(ilo_hw);
  674. out:
  675. ilo_hwdev[devnum] = 0;
  676. return error;
  677. }
  678. static struct pci_device_id ilo_devices[] = {
  679. { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
  680. { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
  681. { }
  682. };
  683. MODULE_DEVICE_TABLE(pci, ilo_devices);
  684. static struct pci_driver ilo_driver = {
  685. .name = ILO_NAME,
  686. .id_table = ilo_devices,
  687. .probe = ilo_probe,
  688. .remove = __devexit_p(ilo_remove),
  689. };
  690. static int __init ilo_init(void)
  691. {
  692. int error;
  693. dev_t dev;
  694. ilo_class = class_create(THIS_MODULE, "iLO");
  695. if (IS_ERR(ilo_class)) {
  696. error = PTR_ERR(ilo_class);
  697. goto out;
  698. }
  699. error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
  700. if (error)
  701. goto class_destroy;
  702. ilo_major = MAJOR(dev);
  703. error = pci_register_driver(&ilo_driver);
  704. if (error)
  705. goto chr_remove;
  706. return 0;
  707. chr_remove:
  708. unregister_chrdev_region(dev, MAX_OPEN);
  709. class_destroy:
  710. class_destroy(ilo_class);
  711. out:
  712. return error;
  713. }
  714. static void __exit ilo_exit(void)
  715. {
  716. pci_unregister_driver(&ilo_driver);
  717. unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
  718. class_destroy(ilo_class);
  719. }
  720. MODULE_VERSION("1.2");
  721. MODULE_ALIAS(ILO_NAME);
  722. MODULE_DESCRIPTION(ILO_NAME);
  723. MODULE_AUTHOR("David Altobelli <david.altobelli@hp.com>");
  724. MODULE_LICENSE("GPL v2");
  725. module_init(ilo_init);
  726. module_exit(ilo_exit);