zfcp_qdio.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * zfcp device driver
  3. *
  4. * Setup and helper functions to access QDIO.
  5. *
  6. * Copyright IBM Corporation 2002, 2010
  7. */
  8. #define KMSG_COMPONENT "zfcp"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/slab.h>
  11. #include "zfcp_ext.h"
  12. #include "zfcp_qdio.h"
  13. #define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
  14. static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
  15. {
  16. int pos;
  17. for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
  18. sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
  19. if (!sbal[pos])
  20. return -ENOMEM;
  21. }
  22. for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
  23. if (pos % QBUFF_PER_PAGE)
  24. sbal[pos] = sbal[pos - 1] + 1;
  25. return 0;
  26. }
  27. static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
  28. {
  29. struct zfcp_adapter *adapter = qdio->adapter;
  30. dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
  31. zfcp_erp_adapter_reopen(adapter,
  32. ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
  33. ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
  34. }
  35. static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
  36. {
  37. int i, sbal_idx;
  38. for (i = first; i < first + cnt; i++) {
  39. sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
  40. memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
  41. }
  42. }
  43. /* this needs to be called prior to updating the queue fill level */
  44. static inline void zfcp_qdio_account(struct zfcp_qdio *qdio)
  45. {
  46. unsigned long long now, span;
  47. int free, used;
  48. spin_lock(&qdio->stat_lock);
  49. now = get_clock_monotonic();
  50. span = (now - qdio->req_q_time) >> 12;
  51. free = atomic_read(&qdio->req_q.count);
  52. used = QDIO_MAX_BUFFERS_PER_Q - free;
  53. qdio->req_q_util += used * span;
  54. qdio->req_q_time = now;
  55. spin_unlock(&qdio->stat_lock);
  56. }
  57. static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
  58. int queue_no, int first, int count,
  59. unsigned long parm)
  60. {
  61. struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
  62. struct zfcp_qdio_queue *queue = &qdio->req_q;
  63. if (unlikely(qdio_err)) {
  64. zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
  65. count);
  66. zfcp_qdio_handler_error(qdio, "qdireq1");
  67. return;
  68. }
  69. /* cleanup all SBALs being program-owned now */
  70. zfcp_qdio_zero_sbals(queue->sbal, first, count);
  71. zfcp_qdio_account(qdio);
  72. atomic_add(count, &queue->count);
  73. wake_up(&qdio->req_q_wq);
  74. }
  75. static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
  76. {
  77. struct zfcp_qdio_queue *queue = &qdio->resp_q;
  78. struct ccw_device *cdev = qdio->adapter->ccw_device;
  79. u8 count, start = queue->first;
  80. unsigned int retval;
  81. count = atomic_read(&queue->count) + processed;
  82. retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
  83. if (unlikely(retval)) {
  84. atomic_set(&queue->count, count);
  85. zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdrpb_1", NULL);
  86. } else {
  87. queue->first += count;
  88. queue->first %= QDIO_MAX_BUFFERS_PER_Q;
  89. atomic_set(&queue->count, 0);
  90. }
  91. }
  92. static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
  93. int queue_no, int first, int count,
  94. unsigned long parm)
  95. {
  96. struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
  97. int sbal_idx, sbal_no;
  98. if (unlikely(qdio_err)) {
  99. zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
  100. count);
  101. zfcp_qdio_handler_error(qdio, "qdires1");
  102. return;
  103. }
  104. /*
  105. * go through all SBALs from input queue currently
  106. * returned by QDIO layer
  107. */
  108. for (sbal_no = 0; sbal_no < count; sbal_no++) {
  109. sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
  110. /* go through all SBALEs of SBAL */
  111. zfcp_fsf_reqid_check(qdio, sbal_idx);
  112. }
  113. /*
  114. * put range of SBALs back to response queue
  115. * (including SBALs which have already been free before)
  116. */
  117. zfcp_qdio_resp_put_back(qdio, count);
  118. }
  119. static struct qdio_buffer_element *
  120. zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  121. {
  122. struct qdio_buffer_element *sbale;
  123. /* set last entry flag in current SBALE of current SBAL */
  124. sbale = zfcp_qdio_sbale_curr(qdio, q_req);
  125. sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
  126. /* don't exceed last allowed SBAL */
  127. if (q_req->sbal_last == q_req->sbal_limit)
  128. return NULL;
  129. /* set chaining flag in first SBALE of current SBAL */
  130. sbale = zfcp_qdio_sbale_req(qdio, q_req);
  131. sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
  132. /* calculate index of next SBAL */
  133. q_req->sbal_last++;
  134. q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
  135. /* keep this requests number of SBALs up-to-date */
  136. q_req->sbal_number++;
  137. BUG_ON(q_req->sbal_number > ZFCP_QDIO_MAX_SBALS_PER_REQ);
  138. /* start at first SBALE of new SBAL */
  139. q_req->sbale_curr = 0;
  140. /* set storage-block type for new SBAL */
  141. sbale = zfcp_qdio_sbale_curr(qdio, q_req);
  142. sbale->flags |= q_req->sbtype;
  143. return sbale;
  144. }
  145. static struct qdio_buffer_element *
  146. zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  147. {
  148. if (q_req->sbale_curr == ZFCP_QDIO_LAST_SBALE_PER_SBAL)
  149. return zfcp_qdio_sbal_chain(qdio, q_req);
  150. q_req->sbale_curr++;
  151. return zfcp_qdio_sbale_curr(qdio, q_req);
  152. }
  153. static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
  154. struct zfcp_qdio_req *q_req)
  155. {
  156. struct qdio_buffer **sbal = qdio->req_q.sbal;
  157. int first = q_req->sbal_first;
  158. int last = q_req->sbal_last;
  159. int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
  160. QDIO_MAX_BUFFERS_PER_Q + 1;
  161. zfcp_qdio_zero_sbals(sbal, first, count);
  162. }
  163. /**
  164. * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
  165. * @qdio: pointer to struct zfcp_qdio
  166. * @q_req: pointer to struct zfcp_qdio_req
  167. * @sg: scatter-gather list
  168. * @max_sbals: upper bound for number of SBALs to be used
  169. * Returns: number of bytes, or error (negativ)
  170. */
  171. int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req,
  172. struct scatterlist *sg)
  173. {
  174. struct qdio_buffer_element *sbale;
  175. int bytes = 0;
  176. /* set storage-block type for this request */
  177. sbale = zfcp_qdio_sbale_req(qdio, q_req);
  178. sbale->flags |= q_req->sbtype;
  179. for (; sg; sg = sg_next(sg)) {
  180. sbale = zfcp_qdio_sbale_next(qdio, q_req);
  181. if (!sbale) {
  182. atomic_inc(&qdio->req_q_full);
  183. zfcp_qdio_undo_sbals(qdio, q_req);
  184. return -EINVAL;
  185. }
  186. sbale->addr = sg_virt(sg);
  187. sbale->length = sg->length;
  188. bytes += sg->length;
  189. }
  190. /* assume that no other SBALEs are to follow in the same SBAL */
  191. sbale = zfcp_qdio_sbale_curr(qdio, q_req);
  192. sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
  193. return bytes;
  194. }
  195. static int zfcp_qdio_sbal_check(struct zfcp_qdio *qdio)
  196. {
  197. struct zfcp_qdio_queue *req_q = &qdio->req_q;
  198. spin_lock_bh(&qdio->req_q_lock);
  199. if (atomic_read(&req_q->count) ||
  200. !(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
  201. return 1;
  202. spin_unlock_bh(&qdio->req_q_lock);
  203. return 0;
  204. }
  205. /**
  206. * zfcp_qdio_sbal_get - get free sbal in request queue, wait if necessary
  207. * @qdio: pointer to struct zfcp_qdio
  208. *
  209. * The req_q_lock must be held by the caller of this function, and
  210. * this function may only be called from process context; it will
  211. * sleep when waiting for a free sbal.
  212. *
  213. * Returns: 0 on success, -EIO if there is no free sbal after waiting.
  214. */
  215. int zfcp_qdio_sbal_get(struct zfcp_qdio *qdio)
  216. {
  217. long ret;
  218. spin_unlock_bh(&qdio->req_q_lock);
  219. ret = wait_event_interruptible_timeout(qdio->req_q_wq,
  220. zfcp_qdio_sbal_check(qdio), 5 * HZ);
  221. if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
  222. return -EIO;
  223. if (ret > 0)
  224. return 0;
  225. if (!ret) {
  226. atomic_inc(&qdio->req_q_full);
  227. /* assume hanging outbound queue, try queue recovery */
  228. zfcp_erp_adapter_reopen(qdio->adapter, 0, "qdsbg_1", NULL);
  229. }
  230. spin_lock_bh(&qdio->req_q_lock);
  231. return -EIO;
  232. }
  233. /**
  234. * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
  235. * @qdio: pointer to struct zfcp_qdio
  236. * @q_req: pointer to struct zfcp_qdio_req
  237. * Returns: 0 on success, error otherwise
  238. */
  239. int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_qdio_req *q_req)
  240. {
  241. struct zfcp_qdio_queue *req_q = &qdio->req_q;
  242. int first = q_req->sbal_first;
  243. int count = q_req->sbal_number;
  244. int retval;
  245. unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
  246. zfcp_qdio_account(qdio);
  247. retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
  248. count);
  249. if (unlikely(retval)) {
  250. zfcp_qdio_zero_sbals(req_q->sbal, first, count);
  251. return retval;
  252. }
  253. /* account for transferred buffers */
  254. atomic_sub(count, &req_q->count);
  255. req_q->first += count;
  256. req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
  257. return 0;
  258. }
  259. static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
  260. struct zfcp_qdio *qdio)
  261. {
  262. id->cdev = qdio->adapter->ccw_device;
  263. id->q_format = QDIO_ZFCP_QFMT;
  264. memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
  265. ASCEBC(id->adapter_name, 8);
  266. id->qib_param_field_format = 0;
  267. id->qib_param_field = NULL;
  268. id->input_slib_elements = NULL;
  269. id->output_slib_elements = NULL;
  270. id->no_input_qs = 1;
  271. id->no_output_qs = 1;
  272. id->input_handler = zfcp_qdio_int_resp;
  273. id->output_handler = zfcp_qdio_int_req;
  274. id->int_parm = (unsigned long) qdio;
  275. id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
  276. id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
  277. }
  278. /**
  279. * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
  280. * @adapter: pointer to struct zfcp_adapter
  281. * Returns: -ENOMEM on memory allocation error or return value from
  282. * qdio_allocate
  283. */
  284. static int zfcp_qdio_allocate(struct zfcp_qdio *qdio)
  285. {
  286. struct qdio_initialize init_data;
  287. if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
  288. zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
  289. return -ENOMEM;
  290. zfcp_qdio_setup_init_data(&init_data, qdio);
  291. return qdio_allocate(&init_data);
  292. }
  293. /**
  294. * zfcp_close_qdio - close qdio queues for an adapter
  295. * @qdio: pointer to structure zfcp_qdio
  296. */
  297. void zfcp_qdio_close(struct zfcp_qdio *qdio)
  298. {
  299. struct zfcp_qdio_queue *req_q;
  300. int first, count;
  301. if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
  302. return;
  303. /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
  304. req_q = &qdio->req_q;
  305. spin_lock_bh(&qdio->req_q_lock);
  306. atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
  307. spin_unlock_bh(&qdio->req_q_lock);
  308. wake_up(&qdio->req_q_wq);
  309. qdio_shutdown(qdio->adapter->ccw_device,
  310. QDIO_FLAG_CLEANUP_USING_CLEAR);
  311. /* cleanup used outbound sbals */
  312. count = atomic_read(&req_q->count);
  313. if (count < QDIO_MAX_BUFFERS_PER_Q) {
  314. first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
  315. count = QDIO_MAX_BUFFERS_PER_Q - count;
  316. zfcp_qdio_zero_sbals(req_q->sbal, first, count);
  317. }
  318. req_q->first = 0;
  319. atomic_set(&req_q->count, 0);
  320. qdio->resp_q.first = 0;
  321. atomic_set(&qdio->resp_q.count, 0);
  322. }
  323. /**
  324. * zfcp_qdio_open - prepare and initialize response queue
  325. * @qdio: pointer to struct zfcp_qdio
  326. * Returns: 0 on success, otherwise -EIO
  327. */
  328. int zfcp_qdio_open(struct zfcp_qdio *qdio)
  329. {
  330. struct qdio_buffer_element *sbale;
  331. struct qdio_initialize init_data;
  332. struct ccw_device *cdev = qdio->adapter->ccw_device;
  333. int cc;
  334. if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
  335. return -EIO;
  336. zfcp_qdio_setup_init_data(&init_data, qdio);
  337. if (qdio_establish(&init_data))
  338. goto failed_establish;
  339. if (qdio_activate(cdev))
  340. goto failed_qdio;
  341. for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
  342. sbale = &(qdio->resp_q.sbal[cc]->element[0]);
  343. sbale->length = 0;
  344. sbale->flags = SBAL_FLAGS_LAST_ENTRY;
  345. sbale->addr = NULL;
  346. }
  347. if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
  348. QDIO_MAX_BUFFERS_PER_Q))
  349. goto failed_qdio;
  350. /* set index of first avalable SBALS / number of available SBALS */
  351. qdio->req_q.first = 0;
  352. atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
  353. return 0;
  354. failed_qdio:
  355. qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
  356. failed_establish:
  357. dev_err(&cdev->dev,
  358. "Setting up the QDIO connection to the FCP adapter failed\n");
  359. return -EIO;
  360. }
  361. void zfcp_qdio_destroy(struct zfcp_qdio *qdio)
  362. {
  363. struct qdio_buffer **sbal_req, **sbal_resp;
  364. int p;
  365. if (!qdio)
  366. return;
  367. if (qdio->adapter->ccw_device)
  368. qdio_free(qdio->adapter->ccw_device);
  369. sbal_req = qdio->req_q.sbal;
  370. sbal_resp = qdio->resp_q.sbal;
  371. for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
  372. free_page((unsigned long) sbal_req[p]);
  373. free_page((unsigned long) sbal_resp[p]);
  374. }
  375. kfree(qdio);
  376. }
  377. int zfcp_qdio_setup(struct zfcp_adapter *adapter)
  378. {
  379. struct zfcp_qdio *qdio;
  380. qdio = kzalloc(sizeof(struct zfcp_qdio), GFP_KERNEL);
  381. if (!qdio)
  382. return -ENOMEM;
  383. qdio->adapter = adapter;
  384. if (zfcp_qdio_allocate(qdio)) {
  385. zfcp_qdio_destroy(qdio);
  386. return -ENOMEM;
  387. }
  388. spin_lock_init(&qdio->req_q_lock);
  389. spin_lock_init(&qdio->stat_lock);
  390. adapter->qdio = qdio;
  391. return 0;
  392. }