hpilo.c 21 KB

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