hpilo.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 "hpilo.h"
  29. static struct class *ilo_class;
  30. static unsigned int ilo_major;
  31. static char ilo_hwdev[MAX_ILO_DEV];
  32. static inline int get_entry_id(int entry)
  33. {
  34. return (entry & ENTRY_MASK_DESCRIPTOR) >> ENTRY_BITPOS_DESCRIPTOR;
  35. }
  36. static inline int get_entry_len(int entry)
  37. {
  38. return ((entry & ENTRY_MASK_QWORDS) >> ENTRY_BITPOS_QWORDS) << 3;
  39. }
  40. static inline int mk_entry(int id, int len)
  41. {
  42. int qlen = len & 7 ? (len >> 3) + 1 : len >> 3;
  43. return id << ENTRY_BITPOS_DESCRIPTOR | qlen << ENTRY_BITPOS_QWORDS;
  44. }
  45. static inline int desc_mem_sz(int nr_entry)
  46. {
  47. return nr_entry << L2_QENTRY_SZ;
  48. }
  49. /*
  50. * FIFO queues, shared with hardware.
  51. *
  52. * If a queue has empty slots, an entry is added to the queue tail,
  53. * and that entry is marked as occupied.
  54. * Entries can be dequeued from the head of the list, when the device
  55. * has marked the entry as consumed.
  56. *
  57. * Returns true on successful queue/dequeue, false on failure.
  58. */
  59. static int fifo_enqueue(struct ilo_hwinfo *hw, char *fifobar, int entry)
  60. {
  61. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  62. unsigned long flags;
  63. int ret = 0;
  64. spin_lock_irqsave(&hw->fifo_lock, flags);
  65. if (!(fifo_q->fifobar[(fifo_q->tail + 1) & fifo_q->imask]
  66. & ENTRY_MASK_O)) {
  67. fifo_q->fifobar[fifo_q->tail & fifo_q->imask] |=
  68. (entry & ENTRY_MASK_NOSTATE) | fifo_q->merge;
  69. fifo_q->tail += 1;
  70. ret = 1;
  71. }
  72. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  73. return ret;
  74. }
  75. static int fifo_dequeue(struct ilo_hwinfo *hw, char *fifobar, int *entry)
  76. {
  77. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  78. unsigned long flags;
  79. int ret = 0;
  80. u64 c;
  81. spin_lock_irqsave(&hw->fifo_lock, flags);
  82. c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  83. if (c & ENTRY_MASK_C) {
  84. if (entry)
  85. *entry = c & ENTRY_MASK_NOSTATE;
  86. fifo_q->fifobar[fifo_q->head & fifo_q->imask] =
  87. (c | ENTRY_MASK) + 1;
  88. fifo_q->head += 1;
  89. ret = 1;
  90. }
  91. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  92. return ret;
  93. }
  94. static int fifo_check_recv(struct ilo_hwinfo *hw, char *fifobar)
  95. {
  96. struct fifo *fifo_q = FIFOBARTOHANDLE(fifobar);
  97. unsigned long flags;
  98. int ret = 0;
  99. u64 c;
  100. spin_lock_irqsave(&hw->fifo_lock, flags);
  101. c = fifo_q->fifobar[fifo_q->head & fifo_q->imask];
  102. if (c & ENTRY_MASK_C)
  103. ret = 1;
  104. spin_unlock_irqrestore(&hw->fifo_lock, flags);
  105. return ret;
  106. }
  107. static int ilo_pkt_enqueue(struct ilo_hwinfo *hw, struct ccb *ccb,
  108. int dir, int id, int len)
  109. {
  110. char *fifobar;
  111. int entry;
  112. if (dir == SENDQ)
  113. fifobar = ccb->ccb_u1.send_fifobar;
  114. else
  115. fifobar = ccb->ccb_u3.recv_fifobar;
  116. entry = mk_entry(id, len);
  117. return fifo_enqueue(hw, fifobar, entry);
  118. }
  119. static int ilo_pkt_dequeue(struct ilo_hwinfo *hw, struct ccb *ccb,
  120. int dir, int *id, int *len, void **pkt)
  121. {
  122. char *fifobar, *desc;
  123. int entry = 0, pkt_id = 0;
  124. int ret;
  125. if (dir == SENDQ) {
  126. fifobar = ccb->ccb_u1.send_fifobar;
  127. desc = ccb->ccb_u2.send_desc;
  128. } else {
  129. fifobar = ccb->ccb_u3.recv_fifobar;
  130. desc = ccb->ccb_u4.recv_desc;
  131. }
  132. ret = fifo_dequeue(hw, fifobar, &entry);
  133. if (ret) {
  134. pkt_id = get_entry_id(entry);
  135. if (id)
  136. *id = pkt_id;
  137. if (len)
  138. *len = get_entry_len(entry);
  139. if (pkt)
  140. *pkt = (void *)(desc + desc_mem_sz(pkt_id));
  141. }
  142. return ret;
  143. }
  144. static int ilo_pkt_recv(struct ilo_hwinfo *hw, struct ccb *ccb)
  145. {
  146. char *fifobar = ccb->ccb_u3.recv_fifobar;
  147. return fifo_check_recv(hw, fifobar);
  148. }
  149. static inline void doorbell_set(struct ccb *ccb)
  150. {
  151. iowrite8(1, ccb->ccb_u5.db_base);
  152. }
  153. static inline void doorbell_clr(struct ccb *ccb)
  154. {
  155. iowrite8(2, ccb->ccb_u5.db_base);
  156. }
  157. static inline int ctrl_set(int l2sz, int idxmask, int desclim)
  158. {
  159. int active = 0, go = 1;
  160. return l2sz << CTRL_BITPOS_L2SZ |
  161. idxmask << CTRL_BITPOS_FIFOINDEXMASK |
  162. desclim << CTRL_BITPOS_DESCLIMIT |
  163. active << CTRL_BITPOS_A |
  164. go << CTRL_BITPOS_G;
  165. }
  166. static void ctrl_setup(struct ccb *ccb, int nr_desc, int l2desc_sz)
  167. {
  168. /* for simplicity, use the same parameters for send and recv ctrls */
  169. ccb->send_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  170. ccb->recv_ctrl = ctrl_set(l2desc_sz, nr_desc-1, nr_desc-1);
  171. }
  172. static inline int fifo_sz(int nr_entry)
  173. {
  174. /* size of a fifo is determined by the number of entries it contains */
  175. return (nr_entry * sizeof(u64)) + FIFOHANDLESIZE;
  176. }
  177. static void fifo_setup(void *base_addr, int nr_entry)
  178. {
  179. struct fifo *fifo_q = base_addr;
  180. int i;
  181. /* set up an empty fifo */
  182. fifo_q->head = 0;
  183. fifo_q->tail = 0;
  184. fifo_q->reset = 0;
  185. fifo_q->nrents = nr_entry;
  186. fifo_q->imask = nr_entry - 1;
  187. fifo_q->merge = ENTRY_MASK_O;
  188. for (i = 0; i < nr_entry; i++)
  189. fifo_q->fifobar[i] = 0;
  190. }
  191. static void ilo_ccb_close(struct pci_dev *pdev, struct ccb_data *data)
  192. {
  193. struct ccb *driver_ccb = &data->driver_ccb;
  194. struct ccb __iomem *device_ccb = data->mapped_ccb;
  195. int retries;
  196. /* complicated dance to tell the hw we are stopping */
  197. doorbell_clr(driver_ccb);
  198. iowrite32(ioread32(&device_ccb->send_ctrl) & ~(1 << CTRL_BITPOS_G),
  199. &device_ccb->send_ctrl);
  200. iowrite32(ioread32(&device_ccb->recv_ctrl) & ~(1 << CTRL_BITPOS_G),
  201. &device_ccb->recv_ctrl);
  202. /* give iLO some time to process stop request */
  203. for (retries = MAX_WAIT; retries > 0; retries--) {
  204. doorbell_set(driver_ccb);
  205. udelay(WAIT_TIME);
  206. if (!(ioread32(&device_ccb->send_ctrl) & (1 << CTRL_BITPOS_A))
  207. &&
  208. !(ioread32(&device_ccb->recv_ctrl) & (1 << CTRL_BITPOS_A)))
  209. break;
  210. }
  211. if (retries == 0)
  212. dev_err(&pdev->dev, "Closing, but controller still active\n");
  213. /* clear the hw ccb */
  214. memset_io(device_ccb, 0, sizeof(struct ccb));
  215. /* free resources used to back send/recv queues */
  216. pci_free_consistent(pdev, data->dma_size, data->dma_va, data->dma_pa);
  217. }
  218. static int ilo_ccb_setup(struct ilo_hwinfo *hw, struct ccb_data *data, int slot)
  219. {
  220. char *dma_va, *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 = (char *)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 = (char *)roundup((unsigned long)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 = 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 = (char *)roundup((unsigned long)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 = 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 = 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 = 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 unsigned int 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 POLLERR;
  432. else if (ilo_pkt_recv(data->ilo_hw, driver_ccb))
  433. return POLLIN | POLLRDNORM;
  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. };
  525. static irqreturn_t ilo_isr(int irq, void *data)
  526. {
  527. struct ilo_hwinfo *hw = data;
  528. int pending, i;
  529. spin_lock(&hw->alloc_lock);
  530. /* check for ccbs which have data */
  531. pending = get_device_outbound(hw);
  532. if (!pending) {
  533. spin_unlock(&hw->alloc_lock);
  534. return IRQ_NONE;
  535. }
  536. if (is_db_reset(pending)) {
  537. /* wake up all ccbs if the device was reset */
  538. pending = -1;
  539. ilo_set_reset(hw);
  540. }
  541. for (i = 0; i < MAX_CCB; i++) {
  542. if (!hw->ccb_alloc[i])
  543. continue;
  544. if (pending & (1 << i))
  545. wake_up_interruptible(&hw->ccb_alloc[i]->ccb_waitq);
  546. }
  547. /* clear the device of the channels that have been handled */
  548. clear_pending_db(hw, pending);
  549. spin_unlock(&hw->alloc_lock);
  550. return IRQ_HANDLED;
  551. }
  552. static void ilo_unmap_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  553. {
  554. pci_iounmap(pdev, hw->db_vaddr);
  555. pci_iounmap(pdev, hw->ram_vaddr);
  556. pci_iounmap(pdev, hw->mmio_vaddr);
  557. }
  558. static int __devinit ilo_map_device(struct pci_dev *pdev, struct ilo_hwinfo *hw)
  559. {
  560. int error = -ENOMEM;
  561. /* map the memory mapped i/o registers */
  562. hw->mmio_vaddr = pci_iomap(pdev, 1, 0);
  563. if (hw->mmio_vaddr == NULL) {
  564. dev_err(&pdev->dev, "Error mapping mmio\n");
  565. goto out;
  566. }
  567. /* map the adapter shared memory region */
  568. hw->ram_vaddr = pci_iomap(pdev, 2, MAX_CCB * ILOHW_CCB_SZ);
  569. if (hw->ram_vaddr == NULL) {
  570. dev_err(&pdev->dev, "Error mapping shared mem\n");
  571. goto mmio_free;
  572. }
  573. /* map the doorbell aperture */
  574. hw->db_vaddr = pci_iomap(pdev, 3, MAX_CCB * ONE_DB_SIZE);
  575. if (hw->db_vaddr == NULL) {
  576. dev_err(&pdev->dev, "Error mapping doorbell\n");
  577. goto ram_free;
  578. }
  579. return 0;
  580. ram_free:
  581. pci_iounmap(pdev, hw->ram_vaddr);
  582. mmio_free:
  583. pci_iounmap(pdev, hw->mmio_vaddr);
  584. out:
  585. return error;
  586. }
  587. static void ilo_remove(struct pci_dev *pdev)
  588. {
  589. int i, minor;
  590. struct ilo_hwinfo *ilo_hw = pci_get_drvdata(pdev);
  591. clear_device(ilo_hw);
  592. minor = MINOR(ilo_hw->cdev.dev);
  593. for (i = minor; i < minor + MAX_CCB; i++)
  594. device_destroy(ilo_class, MKDEV(ilo_major, i));
  595. cdev_del(&ilo_hw->cdev);
  596. ilo_disable_interrupts(ilo_hw);
  597. free_irq(pdev->irq, ilo_hw);
  598. ilo_unmap_device(pdev, ilo_hw);
  599. pci_release_regions(pdev);
  600. pci_disable_device(pdev);
  601. kfree(ilo_hw);
  602. ilo_hwdev[(minor / MAX_CCB)] = 0;
  603. }
  604. static int __devinit ilo_probe(struct pci_dev *pdev,
  605. const struct pci_device_id *ent)
  606. {
  607. int devnum, minor, start, error;
  608. struct ilo_hwinfo *ilo_hw;
  609. /* find a free range for device files */
  610. for (devnum = 0; devnum < MAX_ILO_DEV; devnum++) {
  611. if (ilo_hwdev[devnum] == 0) {
  612. ilo_hwdev[devnum] = 1;
  613. break;
  614. }
  615. }
  616. if (devnum == MAX_ILO_DEV) {
  617. dev_err(&pdev->dev, "Error finding free device\n");
  618. return -ENODEV;
  619. }
  620. /* track global allocations for this device */
  621. error = -ENOMEM;
  622. ilo_hw = kzalloc(sizeof(*ilo_hw), GFP_KERNEL);
  623. if (!ilo_hw)
  624. goto out;
  625. ilo_hw->ilo_dev = pdev;
  626. spin_lock_init(&ilo_hw->alloc_lock);
  627. spin_lock_init(&ilo_hw->fifo_lock);
  628. spin_lock_init(&ilo_hw->open_lock);
  629. error = pci_enable_device(pdev);
  630. if (error)
  631. goto free;
  632. pci_set_master(pdev);
  633. error = pci_request_regions(pdev, ILO_NAME);
  634. if (error)
  635. goto disable;
  636. error = ilo_map_device(pdev, ilo_hw);
  637. if (error)
  638. goto free_regions;
  639. pci_set_drvdata(pdev, ilo_hw);
  640. clear_device(ilo_hw);
  641. error = request_irq(pdev->irq, ilo_isr, IRQF_SHARED, "hpilo", ilo_hw);
  642. if (error)
  643. goto unmap;
  644. ilo_enable_interrupts(ilo_hw);
  645. cdev_init(&ilo_hw->cdev, &ilo_fops);
  646. ilo_hw->cdev.owner = THIS_MODULE;
  647. start = devnum * MAX_CCB;
  648. error = cdev_add(&ilo_hw->cdev, MKDEV(ilo_major, start), MAX_CCB);
  649. if (error) {
  650. dev_err(&pdev->dev, "Could not add cdev\n");
  651. goto remove_isr;
  652. }
  653. for (minor = 0 ; minor < MAX_CCB; minor++) {
  654. struct device *dev;
  655. dev = device_create(ilo_class, &pdev->dev,
  656. MKDEV(ilo_major, minor), NULL,
  657. "hpilo!d%dccb%d", devnum, minor);
  658. if (IS_ERR(dev))
  659. dev_err(&pdev->dev, "Could not create files\n");
  660. }
  661. return 0;
  662. remove_isr:
  663. ilo_disable_interrupts(ilo_hw);
  664. free_irq(pdev->irq, ilo_hw);
  665. unmap:
  666. ilo_unmap_device(pdev, ilo_hw);
  667. free_regions:
  668. pci_release_regions(pdev);
  669. disable:
  670. pci_disable_device(pdev);
  671. free:
  672. kfree(ilo_hw);
  673. out:
  674. ilo_hwdev[devnum] = 0;
  675. return error;
  676. }
  677. static struct pci_device_id ilo_devices[] = {
  678. { PCI_DEVICE(PCI_VENDOR_ID_COMPAQ, 0xB204) },
  679. { PCI_DEVICE(PCI_VENDOR_ID_HP, 0x3307) },
  680. { }
  681. };
  682. MODULE_DEVICE_TABLE(pci, ilo_devices);
  683. static struct pci_driver ilo_driver = {
  684. .name = ILO_NAME,
  685. .id_table = ilo_devices,
  686. .probe = ilo_probe,
  687. .remove = __devexit_p(ilo_remove),
  688. };
  689. static int __init ilo_init(void)
  690. {
  691. int error;
  692. dev_t dev;
  693. ilo_class = class_create(THIS_MODULE, "iLO");
  694. if (IS_ERR(ilo_class)) {
  695. error = PTR_ERR(ilo_class);
  696. goto out;
  697. }
  698. error = alloc_chrdev_region(&dev, 0, MAX_OPEN, ILO_NAME);
  699. if (error)
  700. goto class_destroy;
  701. ilo_major = MAJOR(dev);
  702. error = pci_register_driver(&ilo_driver);
  703. if (error)
  704. goto chr_remove;
  705. return 0;
  706. chr_remove:
  707. unregister_chrdev_region(dev, MAX_OPEN);
  708. class_destroy:
  709. class_destroy(ilo_class);
  710. out:
  711. return error;
  712. }
  713. static void __exit ilo_exit(void)
  714. {
  715. pci_unregister_driver(&ilo_driver);
  716. unregister_chrdev_region(MKDEV(ilo_major, 0), MAX_OPEN);
  717. class_destroy(ilo_class);
  718. }
  719. MODULE_VERSION("1.2");
  720. MODULE_ALIAS(ILO_NAME);
  721. MODULE_DESCRIPTION(ILO_NAME);
  722. MODULE_AUTHOR("David Altobelli <david.altobelli@hp.com>");
  723. MODULE_LICENSE("GPL v2");
  724. module_init(ilo_init);
  725. module_exit(ilo_exit);