rpa_vscsi.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. static char partition_name[97] = "UNKNOWN";
  37. static unsigned int partition_number = -1;
  38. /* ------------------------------------------------------------
  39. * Routines for managing the command/response queue
  40. */
  41. /**
  42. * ibmvscsi_handle_event: - Interrupt handler for crq events
  43. * @irq: number of irq to handle, not used
  44. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  45. * @regs: pt_regs with registers
  46. *
  47. * Disables interrupts and schedules srp_task
  48. * Always returns IRQ_HANDLED
  49. */
  50. static irqreturn_t ibmvscsi_handle_event(int irq,
  51. void *dev_instance,
  52. struct pt_regs *regs)
  53. {
  54. struct ibmvscsi_host_data *hostdata =
  55. (struct ibmvscsi_host_data *)dev_instance;
  56. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  57. tasklet_schedule(&hostdata->srp_task);
  58. return IRQ_HANDLED;
  59. }
  60. /**
  61. * release_crq_queue: - Deallocates data and unregisters CRQ
  62. * @queue: crq_queue to initialize and register
  63. * @host_data: ibmvscsi_host_data of host
  64. *
  65. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  66. * the crq with the hypervisor.
  67. */
  68. void ibmvscsi_release_crq_queue(struct crq_queue *queue,
  69. struct ibmvscsi_host_data *hostdata,
  70. int max_requests)
  71. {
  72. long rc;
  73. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  74. free_irq(vdev->irq, (void *)hostdata);
  75. tasklet_kill(&hostdata->srp_task);
  76. do {
  77. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  78. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  79. dma_unmap_single(hostdata->dev,
  80. queue->msg_token,
  81. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  82. free_page((unsigned long)queue->msgs);
  83. }
  84. /**
  85. * crq_queue_next_crq: - Returns the next entry in message queue
  86. * @queue: crq_queue to use
  87. *
  88. * Returns pointer to next entry in queue, or NULL if there are no new
  89. * entried in the CRQ.
  90. */
  91. static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue)
  92. {
  93. struct viosrp_crq *crq;
  94. unsigned long flags;
  95. spin_lock_irqsave(&queue->lock, flags);
  96. crq = &queue->msgs[queue->cur];
  97. if (crq->valid & 0x80) {
  98. if (++queue->cur == queue->size)
  99. queue->cur = 0;
  100. } else
  101. crq = NULL;
  102. spin_unlock_irqrestore(&queue->lock, flags);
  103. return crq;
  104. }
  105. /**
  106. * ibmvscsi_send_crq: - Send a CRQ
  107. * @hostdata: the adapter
  108. * @word1: the first 64 bits of the data
  109. * @word2: the second 64 bits of the data
  110. */
  111. int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2)
  112. {
  113. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  114. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  115. }
  116. /**
  117. * ibmvscsi_task: - Process srps asynchronously
  118. * @data: ibmvscsi_host_data of host
  119. */
  120. static void ibmvscsi_task(void *data)
  121. {
  122. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  123. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  124. struct viosrp_crq *crq;
  125. int done = 0;
  126. while (!done) {
  127. /* Pull all the valid messages off the CRQ */
  128. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  129. ibmvscsi_handle_crq(crq, hostdata);
  130. crq->valid = 0x00;
  131. }
  132. vio_enable_interrupts(vdev);
  133. if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  134. vio_disable_interrupts(vdev);
  135. ibmvscsi_handle_crq(crq, hostdata);
  136. crq->valid = 0x00;
  137. } else {
  138. done = 1;
  139. }
  140. }
  141. }
  142. static void gather_partition_info(void)
  143. {
  144. struct device_node *rootdn;
  145. char *ppartition_name;
  146. unsigned int *p_number_ptr;
  147. /* Retrieve information about this partition */
  148. rootdn = find_path_device("/");
  149. if (!rootdn) {
  150. return;
  151. }
  152. ppartition_name =
  153. get_property(rootdn, "ibm,partition-name", NULL);
  154. if (ppartition_name)
  155. strncpy(partition_name, ppartition_name,
  156. sizeof(partition_name));
  157. p_number_ptr =
  158. (unsigned int *)get_property(rootdn, "ibm,partition-no",
  159. NULL);
  160. if (p_number_ptr)
  161. partition_number = *p_number_ptr;
  162. }
  163. static void set_adapter_info(struct ibmvscsi_host_data *hostdata)
  164. {
  165. memset(&hostdata->madapter_info, 0x00,
  166. sizeof(hostdata->madapter_info));
  167. printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION);
  168. strcpy(hostdata->madapter_info.srp_version, SRP_VERSION);
  169. strncpy(hostdata->madapter_info.partition_name, partition_name,
  170. sizeof(hostdata->madapter_info.partition_name));
  171. hostdata->madapter_info.partition_number = partition_number;
  172. hostdata->madapter_info.mad_version = 1;
  173. hostdata->madapter_info.os_type = 2;
  174. }
  175. /**
  176. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  177. * @queue: crq_queue to initialize and register
  178. * @hostdata: ibmvscsi_host_data of host
  179. *
  180. * Allocates a page for messages, maps it for dma, and registers
  181. * the crq with the hypervisor.
  182. * Returns zero on success.
  183. */
  184. int ibmvscsi_init_crq_queue(struct crq_queue *queue,
  185. struct ibmvscsi_host_data *hostdata,
  186. int max_requests)
  187. {
  188. int rc;
  189. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  190. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  191. if (!queue->msgs)
  192. goto malloc_failed;
  193. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  194. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  195. queue->size * sizeof(*queue->msgs),
  196. DMA_BIDIRECTIONAL);
  197. if (dma_mapping_error(queue->msg_token))
  198. goto map_failed;
  199. gather_partition_info();
  200. set_adapter_info(hostdata);
  201. rc = plpar_hcall_norets(H_REG_CRQ,
  202. vdev->unit_address,
  203. queue->msg_token, PAGE_SIZE);
  204. if (rc == H_RESOURCE)
  205. /* maybe kexecing and resource is busy. try a reset */
  206. rc = ibmvscsi_reset_crq_queue(queue,
  207. hostdata);
  208. if (rc == 2) {
  209. /* Adapter is good, but other end is not ready */
  210. printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
  211. } else if (rc != 0) {
  212. printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc);
  213. goto reg_crq_failed;
  214. }
  215. if (request_irq(vdev->irq,
  216. ibmvscsi_handle_event,
  217. 0, "ibmvscsi", (void *)hostdata) != 0) {
  218. printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n",
  219. vdev->irq);
  220. goto req_irq_failed;
  221. }
  222. rc = vio_enable_interrupts(vdev);
  223. if (rc != 0) {
  224. printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n",
  225. rc);
  226. goto req_irq_failed;
  227. }
  228. queue->cur = 0;
  229. spin_lock_init(&queue->lock);
  230. tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task,
  231. (unsigned long)hostdata);
  232. return 0;
  233. req_irq_failed:
  234. do {
  235. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  236. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  237. reg_crq_failed:
  238. dma_unmap_single(hostdata->dev,
  239. queue->msg_token,
  240. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  241. map_failed:
  242. free_page((unsigned long)queue->msgs);
  243. malloc_failed:
  244. return -1;
  245. }
  246. /**
  247. * reenable_crq_queue: - reenables a crq after
  248. * @queue: crq_queue to initialize and register
  249. * @hostdata: ibmvscsi_host_data of host
  250. *
  251. */
  252. int ibmvscsi_reenable_crq_queue(struct crq_queue *queue,
  253. struct ibmvscsi_host_data *hostdata)
  254. {
  255. int rc;
  256. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  257. /* Re-enable the CRQ */
  258. do {
  259. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  260. } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  261. if (rc)
  262. printk(KERN_ERR "ibmvscsi: Error %d enabling adapter\n", rc);
  263. return rc;
  264. }
  265. /**
  266. * reset_crq_queue: - resets a crq after a failure
  267. * @queue: crq_queue to initialize and register
  268. * @hostdata: ibmvscsi_host_data of host
  269. *
  270. */
  271. int ibmvscsi_reset_crq_queue(struct crq_queue *queue,
  272. struct ibmvscsi_host_data *hostdata)
  273. {
  274. int rc;
  275. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  276. /* Close the CRQ */
  277. do {
  278. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  279. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  280. /* Clean out the queue */
  281. memset(queue->msgs, 0x00, PAGE_SIZE);
  282. queue->cur = 0;
  283. set_adapter_info(hostdata);
  284. /* And re-open it again */
  285. rc = plpar_hcall_norets(H_REG_CRQ,
  286. vdev->unit_address,
  287. queue->msg_token, PAGE_SIZE);
  288. if (rc == 2) {
  289. /* Adapter is good, but other end is not ready */
  290. printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n");
  291. } else if (rc != 0) {
  292. printk(KERN_WARNING
  293. "ibmvscsi: couldn't register crq--rc 0x%x\n", rc);
  294. }
  295. return rc;
  296. }