rpa_vscsi.c 9.9 KB

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