rpa_vscsi.c 9.6 KB

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