rpa_vscsi.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* ------------------------------------------------------------
  2. * rpa_vscsi.c
  3. * (C) Copyright IBM Corporation 1994, 2003
  4. * Authors: Colin DeVilbiss (devilbis@us.ibm.com)
  5. * Santiago Leon (santil@us.ibm.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 as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  20. * USA
  21. *
  22. * ------------------------------------------------------------
  23. * RPA-specific functions of the SCSI host adapter for Virtual I/O devices
  24. *
  25. * This driver allows the Linux SCSI peripheral drivers to directly
  26. * access devices in the hosting partition, either on an iSeries
  27. * hypervisor system or a converged hypervisor system.
  28. */
  29. #include <asm/vio.h>
  30. #include <asm/prom.h>
  31. #include <asm/iommu.h>
  32. #include <asm/hvcall.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/interrupt.h>
  35. #include "ibmvscsi.h"
  36. #include "srp.h"
  37. static char partition_name[97] = "UNKNOWN";
  38. static unsigned int partition_number = -1;
  39. /* ------------------------------------------------------------
  40. * Routines for managing the command/response queue
  41. */
  42. /**
  43. * ibmvscsi_handle_event: - Interrupt handler for crq events
  44. * @irq: number of irq to handle, not used
  45. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  46. * @regs: pt_regs with registers
  47. *
  48. * Disables interrupts and schedules srp_task
  49. * Always returns IRQ_HANDLED
  50. */
  51. static irqreturn_t ibmvscsi_handle_event(int irq,
  52. void *dev_instance,
  53. struct pt_regs *regs)
  54. {
  55. struct ibmvscsi_host_data *hostdata =
  56. (struct ibmvscsi_host_data *)dev_instance;
  57. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  58. tasklet_schedule(&hostdata->srp_task);
  59. return IRQ_HANDLED;
  60. }
  61. /**
  62. * release_crq_queue: - Deallocates data and unregisters CRQ
  63. * @queue: crq_queue to initialize and register
  64. * @host_data: ibmvscsi_host_data of host
  65. *
  66. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  67. * the crq with the hypervisor.
  68. */
  69. void ibmvscsi_release_crq_queue(struct crq_queue *queue,
  70. struct ibmvscsi_host_data *hostdata,
  71. int max_requests)
  72. {
  73. long rc;
  74. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  75. free_irq(vdev->irq, (void *)hostdata);
  76. tasklet_kill(&hostdata->srp_task);
  77. do {
  78. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  79. } while ((rc == H_Busy) || (H_isLongBusy(rc)));
  80. dma_unmap_single(hostdata->dev,
  81. queue->msg_token,
  82. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  83. free_page((unsigned long)queue->msgs);
  84. }
  85. /**
  86. * crq_queue_next_crq: - Returns the next entry in message queue
  87. * @queue: crq_queue to use
  88. *
  89. * Returns pointer to next entry in queue, or NULL if there are no new
  90. * entried in the CRQ.
  91. */
  92. static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
  93. {
  94. struct viosrp_crq *crq;
  95. unsigned long flags;
  96. spin_lock_irqsave(&queue->lock, flags);
  97. crq = &queue->msgs[queue->cur];
  98. if (crq->valid & 0x80) {
  99. if (++queue->cur == queue->size)
  100. queue->cur = 0;
  101. } else
  102. crq = NULL;
  103. spin_unlock_irqrestore(&queue->lock, flags);
  104. return crq;
  105. }
  106. /**
  107. * ibmvscsi_send_crq: - Send a CRQ
  108. * @hostdata: the adapter
  109. * @word1: the first 64 bits of the data
  110. * @word2: the second 64 bits of the data
  111. */
  112. int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2)
  113. {
  114. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  115. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  116. }
  117. /**
  118. * ibmvscsi_task: - Process srps asynchronously
  119. * @data: ibmvscsi_host_data of host
  120. */
  121. static void ibmvscsi_task(void *data)
  122. {
  123. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  124. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  125. struct viosrp_crq *crq;
  126. int done = 0;
  127. while (!done) {
  128. /* Pull all the valid messages off the CRQ */
  129. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  130. ibmvscsi_handle_crq(crq, hostdata);
  131. crq->valid = 0x00;
  132. }
  133. vio_enable_interrupts(vdev);
  134. if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  135. vio_disable_interrupts(vdev);
  136. ibmvscsi_handle_crq(crq, hostdata);
  137. crq->valid = 0x00;
  138. } else {
  139. done = 1;
  140. }
  141. }
  142. }
  143. static void gather_partition_info(void)
  144. {
  145. struct device_node *rootdn;
  146. char *ppartition_name;
  147. unsigned int *p_number_ptr;
  148. /* Retrieve information about this partition */
  149. rootdn = find_path_device("/");
  150. if (!rootdn) {
  151. return;
  152. }
  153. ppartition_name =
  154. get_property(rootdn, "ibm,partition-name", NULL);
  155. if (ppartition_name)
  156. strncpy(partition_name, ppartition_name,
  157. sizeof(partition_name));
  158. p_number_ptr =
  159. (unsigned int *)get_property(rootdn, "ibm,partition-no",
  160. NULL);
  161. if (p_number_ptr)
  162. partition_number = *p_number_ptr;
  163. }
  164. static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
  165. {
  166. memset(&hostdata->madapter_info, 0x00,
  167. sizeof(hostdata->madapter_info));
  168. printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION);
  169. strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
  170. strncpy(hostdata->madapter_info.partition_name, partition_name,
  171. sizeof(hostdata->madapter_info.partition_name));
  172. hostdata->madapter_info.partition_number = partition_number;
  173. hostdata->madapter_info.mad_version = 1;
  174. hostdata->madapter_info.os_type = 2;
  175. }
  176. /**
  177. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  178. * @queue: crq_queue to initialize and register
  179. * @hostdata: ibmvscsi_host_data of host
  180. *
  181. * Allocates a page for messages, maps it for dma, and registers
  182. * the crq with the hypervisor.
  183. * Returns zero on success.
  184. */
  185. int ibmvscsi_init_crq_queue(struct crq_queue *queue,
  186. struct ibmvscsi_host_data *hostdata,
  187. int max_requests)
  188. {
  189. int rc;
  190. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  191. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  192. if (!queue->msgs)
  193. goto malloc_failed;
  194. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  195. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  196. queue->size * sizeof(*queue->msgs),
  197. DMA_BIDIRECTIONAL);
  198. if (dma_mapping_error(queue->msg_token))
  199. goto map_failed;
  200. gather_partition_info();
  201. set_adapter_info(hostdata);
  202. rc = plpar_hcall_norets(H_REG_CRQ,
  203. vdev->unit_address,
  204. queue->msg_token, PAGE_SIZE);
  205. if (rc == 2) {
  206. /* Adapter is good, but other end is not ready */
  207. printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
  208. } else if (rc != 0) {
  209. printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc);
  210. goto reg_crq_failed;
  211. }
  212. if (request_irq(vdev->irq,
  213. ibmvscsi_handle_event,
  214. 0, "ibmvscsi", (void *)hostdata) != 0) {
  215. printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n",
  216. vdev->irq);
  217. goto req_irq_failed;
  218. }
  219. rc = vio_enable_interrupts(vdev);
  220. if (rc != 0) {
  221. printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n",
  222. rc);
  223. goto req_irq_failed;
  224. }
  225. queue->cur = 0;
  226. spin_lock_init(&queue->lock);
  227. tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
  228. (unsigned long)hostdata);
  229. return 0;
  230. req_irq_failed:
  231. do {
  232. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  233. } while ((rc == H_Busy) || (H_isLongBusy(rc)));
  234. reg_crq_failed:
  235. dma_unmap_single(hostdata->dev,
  236. queue->msg_token,
  237. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  238. map_failed:
  239. free_page((unsigned long)queue->msgs);
  240. malloc_failed:
  241. return -1;
  242. }
  243. /**
  244. * reset_crq_queue: - resets a crq after a failure
  245. * @queue: crq_queue to initialize and register
  246. * @hostdata: ibmvscsi_host_data of host
  247. *
  248. */
  249. void ibmvscsi_reset_crq_queue(struct crq_queue *queue,
  250. struct ibmvscsi_host_data *hostdata)
  251. {
  252. int rc;
  253. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  254. /* Close the CRQ */
  255. do {
  256. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  257. } while ((rc == H_Busy) || (H_isLongBusy(rc)));
  258. /* Clean out the queue */
  259. memset(queue->msgs, 0x00, PAGE_SIZE);
  260. queue->cur = 0;
  261. set_adapter_info(hostdata);
  262. /* And re-open it again */
  263. rc = plpar_hcall_norets(H_REG_CRQ,
  264. vdev->unit_address,
  265. queue->msg_token, PAGE_SIZE);
  266. if (rc == 2) {
  267. /* Adapter is good, but other end is not ready */
  268. printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
  269. } else if (rc != 0) {
  270. printk(KERN_WARNING
  271. "ibmvscsi: couldn't register crq--rc 0x%x\n", rc);
  272. }
  273. }