rpa_vscsi.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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/delay.h>
  34. #include <linux/dma-mapping.h>
  35. #include <linux/gfp.h>
  36. #include <linux/interrupt.h>
  37. #include "ibmvscsi.h"
  38. static char partition_name[97] = "UNKNOWN";
  39. static unsigned int partition_number = -1;
  40. /* ------------------------------------------------------------
  41. * Routines for managing the command/response queue
  42. */
  43. /**
  44. * rpavscsi_handle_event: - Interrupt handler for crq events
  45. * @irq: number of irq to handle, not used
  46. * @dev_instance: ibmvscsi_host_data of host that received interrupt
  47. *
  48. * Disables interrupts and schedules srp_task
  49. * Always returns IRQ_HANDLED
  50. */
  51. static irqreturn_t rpavscsi_handle_event(int irq, void *dev_instance)
  52. {
  53. struct ibmvscsi_host_data *hostdata =
  54. (struct ibmvscsi_host_data *)dev_instance;
  55. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  56. tasklet_schedule(&hostdata->srp_task);
  57. return IRQ_HANDLED;
  58. }
  59. /**
  60. * release_crq_queue: - Deallocates data and unregisters CRQ
  61. * @queue: crq_queue to initialize and register
  62. * @host_data: ibmvscsi_host_data of host
  63. *
  64. * Frees irq, deallocates a page for messages, unmaps dma, and unregisters
  65. * the crq with the hypervisor.
  66. */
  67. static void rpavscsi_release_crq_queue(struct crq_queue *queue,
  68. struct ibmvscsi_host_data *hostdata,
  69. int max_requests)
  70. {
  71. long rc = 0;
  72. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  73. free_irq(vdev->irq, (void *)hostdata);
  74. tasklet_kill(&hostdata->srp_task);
  75. do {
  76. if (rc)
  77. msleep(100);
  78. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  79. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(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. * rpavscsi_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. static int rpavscsi_send_crq(struct ibmvscsi_host_data *hostdata,
  113. u64 word1, u64 word2)
  114. {
  115. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  116. return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2);
  117. }
  118. /**
  119. * rpavscsi_task: - Process srps asynchronously
  120. * @data: ibmvscsi_host_data of host
  121. */
  122. static void rpavscsi_task(void *data)
  123. {
  124. struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data;
  125. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  126. struct viosrp_crq *crq;
  127. int done = 0;
  128. while (!done) {
  129. /* Pull all the valid messages off the CRQ */
  130. while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  131. ibmvscsi_handle_crq(crq, hostdata);
  132. crq->valid = 0x00;
  133. }
  134. vio_enable_interrupts(vdev);
  135. if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) {
  136. vio_disable_interrupts(vdev);
  137. ibmvscsi_handle_crq(crq, hostdata);
  138. crq->valid = 0x00;
  139. } else {
  140. done = 1;
  141. }
  142. }
  143. }
  144. static void gather_partition_info(void)
  145. {
  146. struct device_node *rootdn;
  147. const char *ppartition_name;
  148. const unsigned int *p_number_ptr;
  149. /* Retrieve information about this partition */
  150. rootdn = of_find_node_by_path("/");
  151. if (!rootdn) {
  152. return;
  153. }
  154. ppartition_name = of_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 = of_get_property(rootdn, "ibm,partition-no", NULL);
  159. if (p_number_ptr)
  160. partition_number = *p_number_ptr;
  161. of_node_put(rootdn);
  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. dev_info(hostdata->dev, "SRP_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. * reset_crq_queue: - resets a crq after a failure
  177. * @queue: crq_queue to initialize and register
  178. * @hostdata: ibmvscsi_host_data of host
  179. *
  180. */
  181. static int rpavscsi_reset_crq_queue(struct crq_queue *queue,
  182. struct ibmvscsi_host_data *hostdata)
  183. {
  184. int rc = 0;
  185. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  186. /* Close the CRQ */
  187. do {
  188. if (rc)
  189. msleep(100);
  190. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  191. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  192. /* Clean out the queue */
  193. memset(queue->msgs, 0x00, PAGE_SIZE);
  194. queue->cur = 0;
  195. set_adapter_info(hostdata);
  196. /* And re-open it again */
  197. rc = plpar_hcall_norets(H_REG_CRQ,
  198. vdev->unit_address,
  199. queue->msg_token, PAGE_SIZE);
  200. if (rc == 2) {
  201. /* Adapter is good, but other end is not ready */
  202. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  203. } else if (rc != 0) {
  204. dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc);
  205. }
  206. return rc;
  207. }
  208. /**
  209. * initialize_crq_queue: - Initializes and registers CRQ with hypervisor
  210. * @queue: crq_queue to initialize and register
  211. * @hostdata: ibmvscsi_host_data of host
  212. *
  213. * Allocates a page for messages, maps it for dma, and registers
  214. * the crq with the hypervisor.
  215. * Returns zero on success.
  216. */
  217. static int rpavscsi_init_crq_queue(struct crq_queue *queue,
  218. struct ibmvscsi_host_data *hostdata,
  219. int max_requests)
  220. {
  221. int rc;
  222. int retrc;
  223. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  224. queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  225. if (!queue->msgs)
  226. goto malloc_failed;
  227. queue->size = PAGE_SIZE / sizeof(*queue->msgs);
  228. queue->msg_token = dma_map_single(hostdata->dev, queue->msgs,
  229. queue->size * sizeof(*queue->msgs),
  230. DMA_BIDIRECTIONAL);
  231. if (dma_mapping_error(hostdata->dev, queue->msg_token))
  232. goto map_failed;
  233. gather_partition_info();
  234. set_adapter_info(hostdata);
  235. retrc = rc = plpar_hcall_norets(H_REG_CRQ,
  236. vdev->unit_address,
  237. queue->msg_token, PAGE_SIZE);
  238. if (rc == H_RESOURCE)
  239. /* maybe kexecing and resource is busy. try a reset */
  240. rc = rpavscsi_reset_crq_queue(queue,
  241. hostdata);
  242. if (rc == 2) {
  243. /* Adapter is good, but other end is not ready */
  244. dev_warn(hostdata->dev, "Partner adapter not ready\n");
  245. retrc = 0;
  246. } else if (rc != 0) {
  247. dev_warn(hostdata->dev, "Error %d opening adapter\n", rc);
  248. goto reg_crq_failed;
  249. }
  250. queue->cur = 0;
  251. spin_lock_init(&queue->lock);
  252. tasklet_init(&hostdata->srp_task, (void *)rpavscsi_task,
  253. (unsigned long)hostdata);
  254. if (request_irq(vdev->irq,
  255. rpavscsi_handle_event,
  256. 0, "ibmvscsi", (void *)hostdata) != 0) {
  257. dev_err(hostdata->dev, "couldn't register irq 0x%x\n",
  258. vdev->irq);
  259. goto req_irq_failed;
  260. }
  261. rc = vio_enable_interrupts(vdev);
  262. if (rc != 0) {
  263. dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc);
  264. goto req_irq_failed;
  265. }
  266. return retrc;
  267. req_irq_failed:
  268. tasklet_kill(&hostdata->srp_task);
  269. rc = 0;
  270. do {
  271. if (rc)
  272. msleep(100);
  273. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  274. } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  275. reg_crq_failed:
  276. dma_unmap_single(hostdata->dev,
  277. queue->msg_token,
  278. queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL);
  279. map_failed:
  280. free_page((unsigned long)queue->msgs);
  281. malloc_failed:
  282. return -1;
  283. }
  284. /**
  285. * reenable_crq_queue: - reenables a crq after
  286. * @queue: crq_queue to initialize and register
  287. * @hostdata: ibmvscsi_host_data of host
  288. *
  289. */
  290. static int rpavscsi_reenable_crq_queue(struct crq_queue *queue,
  291. struct ibmvscsi_host_data *hostdata)
  292. {
  293. int rc = 0;
  294. struct vio_dev *vdev = to_vio_dev(hostdata->dev);
  295. /* Re-enable the CRQ */
  296. do {
  297. if (rc)
  298. msleep(100);
  299. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  300. } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc)));
  301. if (rc)
  302. dev_err(hostdata->dev, "Error %d enabling adapter\n", rc);
  303. return rc;
  304. }
  305. /**
  306. * rpavscsi_resume: - resume after suspend
  307. * @hostdata: ibmvscsi_host_data of host
  308. *
  309. */
  310. static int rpavscsi_resume(struct ibmvscsi_host_data *hostdata)
  311. {
  312. vio_disable_interrupts(to_vio_dev(hostdata->dev));
  313. tasklet_schedule(&hostdata->srp_task);
  314. return 0;
  315. }
  316. struct ibmvscsi_ops rpavscsi_ops = {
  317. .init_crq_queue = rpavscsi_init_crq_queue,
  318. .release_crq_queue = rpavscsi_release_crq_queue,
  319. .reset_crq_queue = rpavscsi_reset_crq_queue,
  320. .reenable_crq_queue = rpavscsi_reenable_crq_queue,
  321. .send_crq = rpavscsi_send_crq,
  322. .resume = rpavscsi_resume,
  323. };