blob: 84527ebbbe585d367f3f91b2d0ec4dbe0ec72595 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Swen Schillig00bab912008-06-10 18:20:57 +02002 * zfcp device driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Swen Schillig00bab912008-06-10 18:20:57 +02004 * Setup and helper functions to access QDIO.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
Swen Schillig42428f72009-08-18 15:43:18 +02006 * Copyright IBM Corporation 2002, 2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
Christof Schmittecf39d42008-12-25 13:39:53 +01009#define KMSG_COMPONENT "zfcp"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "zfcp_ext.h"
13
Christof Schmitt5d4e2262008-07-02 10:56:34 +020014#define QBUFF_PER_PAGE (PAGE_SIZE / sizeof(struct qdio_buffer))
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Swen Schillig00bab912008-06-10 18:20:57 +020016static int zfcp_qdio_buffers_enqueue(struct qdio_buffer **sbal)
Swen Schilligb4e44592007-07-18 10:55:13 +020017{
18 int pos;
19
20 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos += QBUFF_PER_PAGE) {
Swen Schillig00bab912008-06-10 18:20:57 +020021 sbal[pos] = (struct qdio_buffer *) get_zeroed_page(GFP_KERNEL);
22 if (!sbal[pos])
Swen Schilligb4e44592007-07-18 10:55:13 +020023 return -ENOMEM;
Swen Schilligb4e44592007-07-18 10:55:13 +020024 }
25 for (pos = 0; pos < QDIO_MAX_BUFFERS_PER_Q; pos++)
26 if (pos % QBUFF_PER_PAGE)
Swen Schillig00bab912008-06-10 18:20:57 +020027 sbal[pos] = sbal[pos - 1] + 1;
Swen Schilligb4e44592007-07-18 10:55:13 +020028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029}
30
Swen Schillig44cc76f2008-10-01 12:42:16 +020031static struct qdio_buffer_element *
Swen Schillig00bab912008-06-10 18:20:57 +020032zfcp_qdio_sbale(struct zfcp_qdio_queue *q, int sbal_idx, int sbale_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Swen Schillig00bab912008-06-10 18:20:57 +020034 return &q->sbal[sbal_idx]->element[sbale_idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Volker Sameskefea9d6c2006-08-02 11:05:16 +020037/**
Swen Schillig00bab912008-06-10 18:20:57 +020038 * zfcp_qdio_free - free memory used by request- and resposne queue
Swen Schillig564e1c82009-08-18 15:43:19 +020039 * @qdio: pointer to the zfcp_qdio structure
Volker Sameskefea9d6c2006-08-02 11:05:16 +020040 */
Swen Schillig564e1c82009-08-18 15:43:19 +020041void zfcp_qdio_free(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +020042{
43 struct qdio_buffer **sbal_req, **sbal_resp;
44 int p;
45
Swen Schillig564e1c82009-08-18 15:43:19 +020046 if (qdio->adapter->ccw_device)
47 qdio_free(qdio->adapter->ccw_device);
Swen Schillig00bab912008-06-10 18:20:57 +020048
Swen Schillig564e1c82009-08-18 15:43:19 +020049 sbal_req = qdio->req_q.sbal;
50 sbal_resp = qdio->resp_q.sbal;
Swen Schillig00bab912008-06-10 18:20:57 +020051
52 for (p = 0; p < QDIO_MAX_BUFFERS_PER_Q; p += QBUFF_PER_PAGE) {
53 free_page((unsigned long) sbal_req[p]);
54 free_page((unsigned long) sbal_resp[p]);
55 }
56}
57
Swen Schillig564e1c82009-08-18 15:43:19 +020058static void zfcp_qdio_handler_error(struct zfcp_qdio *qdio, char *id)
Swen Schillig00bab912008-06-10 18:20:57 +020059{
Swen Schillig564e1c82009-08-18 15:43:19 +020060 struct zfcp_adapter *adapter = qdio->adapter;
61
Christof Schmittff3b24f2008-10-01 12:42:15 +020062 dev_warn(&adapter->ccw_device->dev, "A QDIO problem occurred\n");
Swen Schillig00bab912008-06-10 18:20:57 +020063
64 zfcp_erp_adapter_reopen(adapter,
65 ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED |
66 ZFCP_STATUS_COMMON_ERP_FAILED, id, NULL);
67}
68
Christof Schmitt5d4e2262008-07-02 10:56:34 +020069static void zfcp_qdio_zero_sbals(struct qdio_buffer *sbal[], int first, int cnt)
70{
71 int i, sbal_idx;
72
73 for (i = first; i < first + cnt; i++) {
74 sbal_idx = i % QDIO_MAX_BUFFERS_PER_Q;
75 memset(sbal[sbal_idx], 0, sizeof(struct qdio_buffer));
76 }
77}
78
Martin Peschke94506fd2009-03-02 13:08:56 +010079/* this needs to be called prior to updating the queue fill level */
Swen Schillig564e1c82009-08-18 15:43:19 +020080static void zfcp_qdio_account(struct zfcp_qdio *qdio)
Martin Peschke94506fd2009-03-02 13:08:56 +010081{
82 ktime_t now;
83 s64 span;
84 int free, used;
85
Swen Schillig564e1c82009-08-18 15:43:19 +020086 spin_lock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +010087 now = ktime_get();
Swen Schillig564e1c82009-08-18 15:43:19 +020088 span = ktime_us_delta(now, qdio->req_q_time);
89 free = max(0, atomic_read(&qdio->req_q.count));
Martin Peschke94506fd2009-03-02 13:08:56 +010090 used = QDIO_MAX_BUFFERS_PER_Q - free;
Swen Schillig564e1c82009-08-18 15:43:19 +020091 qdio->req_q_util += used * span;
92 qdio->req_q_time = now;
93 spin_unlock(&qdio->stat_lock);
Martin Peschke94506fd2009-03-02 13:08:56 +010094}
95
Jan Glauber779e6e12008-07-17 17:16:48 +020096static void zfcp_qdio_int_req(struct ccw_device *cdev, unsigned int qdio_err,
97 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +020098 unsigned long parm)
99{
Swen Schillig564e1c82009-08-18 15:43:19 +0200100 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
101 struct zfcp_qdio_queue *queue = &qdio->req_q;
Swen Schillig00bab912008-06-10 18:20:57 +0200102
Jan Glauber779e6e12008-07-17 17:16:48 +0200103 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +0200104 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
105 count);
Swen Schillig564e1c82009-08-18 15:43:19 +0200106 zfcp_qdio_handler_error(qdio, "qdireq1");
Swen Schillig00bab912008-06-10 18:20:57 +0200107 return;
108 }
109
110 /* cleanup all SBALs being program-owned now */
111 zfcp_qdio_zero_sbals(queue->sbal, first, count);
112
Swen Schillig564e1c82009-08-18 15:43:19 +0200113 zfcp_qdio_account(qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200114 atomic_add(count, &queue->count);
Swen Schillig564e1c82009-08-18 15:43:19 +0200115 wake_up(&qdio->req_q_wq);
Swen Schillig00bab912008-06-10 18:20:57 +0200116}
117
Swen Schillig564e1c82009-08-18 15:43:19 +0200118static void zfcp_qdio_resp_put_back(struct zfcp_qdio *qdio, int processed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Swen Schillig564e1c82009-08-18 15:43:19 +0200120 struct zfcp_qdio_queue *queue = &qdio->resp_q;
121 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200122 u8 count, start = queue->first;
123 unsigned int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Swen Schillig00bab912008-06-10 18:20:57 +0200125 count = atomic_read(&queue->count) + processed;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jan Glauber779e6e12008-07-17 17:16:48 +0200127 retval = do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Swen Schillig00bab912008-06-10 18:20:57 +0200129 if (unlikely(retval)) {
130 atomic_set(&queue->count, count);
131 /* FIXME: Recover this with an adapter reopen? */
132 } else {
133 queue->first += count;
134 queue->first %= QDIO_MAX_BUFFERS_PER_Q;
135 atomic_set(&queue->count, 0);
136 }
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Jan Glauber779e6e12008-07-17 17:16:48 +0200139static void zfcp_qdio_int_resp(struct ccw_device *cdev, unsigned int qdio_err,
140 int queue_no, int first, int count,
Swen Schillig00bab912008-06-10 18:20:57 +0200141 unsigned long parm)
142{
Swen Schillig564e1c82009-08-18 15:43:19 +0200143 struct zfcp_qdio *qdio = (struct zfcp_qdio *) parm;
Swen Schilligbd63eaf2009-08-18 15:43:13 +0200144 int sbal_idx, sbal_no;
Swen Schillig00bab912008-06-10 18:20:57 +0200145
Jan Glauber779e6e12008-07-17 17:16:48 +0200146 if (unlikely(qdio_err)) {
Swen Schillig57717102009-08-18 15:43:21 +0200147 zfcp_dbf_hba_qdio(qdio->adapter->dbf, qdio_err, first,
148 count);
Swen Schillig564e1c82009-08-18 15:43:19 +0200149 zfcp_qdio_handler_error(qdio, "qdires1");
Swen Schillig00bab912008-06-10 18:20:57 +0200150 return;
151 }
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /*
154 * go through all SBALs from input queue currently
155 * returned by QDIO layer
156 */
Swen Schillig00bab912008-06-10 18:20:57 +0200157 for (sbal_no = 0; sbal_no < count; sbal_no++) {
158 sbal_idx = (first + sbal_no) % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* go through all SBALEs of SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200160 zfcp_fsf_reqid_check(qdio, sbal_idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 /*
164 * put range of SBALs back to response queue
165 * (including SBALs which have already been free before)
166 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200167 zfcp_qdio_resp_put_back(qdio, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/**
Swen Schillig00bab912008-06-10 18:20:57 +0200171 * zfcp_qdio_sbale_req - return ptr to SBALE of req_q for a struct zfcp_fsf_req
Swen Schillig564e1c82009-08-18 15:43:19 +0200172 * @qdio: pointer to struct zfcp_qdio
Swen Schillig42428f72009-08-18 15:43:18 +0200173 * @q_rec: pointer to struct zfcp_queue_rec
Swen Schillig00bab912008-06-10 18:20:57 +0200174 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200176struct qdio_buffer_element *zfcp_qdio_sbale_req(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200177 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Swen Schillig564e1c82009-08-18 15:43:19 +0200179 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
182/**
Swen Schillig00bab912008-06-10 18:20:57 +0200183 * zfcp_qdio_sbale_curr - return curr SBALE on req_q for a struct zfcp_fsf_req
184 * @fsf_req: pointer to struct fsf_req
185 * Returns: pointer to qdio_buffer_element (SBALE) structure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200187struct qdio_buffer_element *zfcp_qdio_sbale_curr(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200188 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Swen Schillig564e1c82009-08-18 15:43:19 +0200190 return zfcp_qdio_sbale(&qdio->req_q, q_req->sbal_last,
Swen Schillig42428f72009-08-18 15:43:18 +0200191 q_req->sbale_curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Swen Schillig564e1c82009-08-18 15:43:19 +0200194static void zfcp_qdio_sbal_limit(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200195 struct zfcp_queue_req *q_req, int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Swen Schillig564e1c82009-08-18 15:43:19 +0200197 int count = atomic_read(&qdio->req_q.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 count = min(count, max_sbals);
Swen Schillig42428f72009-08-18 15:43:18 +0200199 q_req->sbal_limit = (q_req->sbal_first + count - 1)
Swen Schillig00bab912008-06-10 18:20:57 +0200200 % QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Swen Schillig44cc76f2008-10-01 12:42:16 +0200203static struct qdio_buffer_element *
Swen Schillig564e1c82009-08-18 15:43:19 +0200204zfcp_qdio_sbal_chain(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200205 unsigned long sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200207 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 /* set last entry flag in current SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200210 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
212
213 /* don't exceed last allowed SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200214 if (q_req->sbal_last == q_req->sbal_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return NULL;
216
217 /* set chaining flag in first SBALE of current SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200218 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 sbale->flags |= SBAL_FLAGS0_MORE_SBALS;
220
221 /* calculate index of next SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200222 q_req->sbal_last++;
223 q_req->sbal_last %= QDIO_MAX_BUFFERS_PER_Q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 /* keep this requests number of SBALs up-to-date */
Swen Schillig42428f72009-08-18 15:43:18 +0200226 q_req->sbal_number++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 /* start at first SBALE of new SBAL */
Swen Schillig42428f72009-08-18 15:43:18 +0200229 q_req->sbale_curr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231 /* set storage-block type for new SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200232 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 sbale->flags |= sbtype;
234
235 return sbale;
236}
237
Swen Schillig44cc76f2008-10-01 12:42:16 +0200238static struct qdio_buffer_element *
Swen Schillig564e1c82009-08-18 15:43:19 +0200239zfcp_qdio_sbale_next(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req,
Swen Schillig42428f72009-08-18 15:43:18 +0200240 unsigned int sbtype)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
Swen Schillig42428f72009-08-18 15:43:18 +0200242 if (q_req->sbale_curr == ZFCP_LAST_SBALE_PER_SBAL)
Swen Schillig564e1c82009-08-18 15:43:19 +0200243 return zfcp_qdio_sbal_chain(qdio, q_req, sbtype);
Swen Schillig42428f72009-08-18 15:43:18 +0200244 q_req->sbale_curr++;
Swen Schillig564e1c82009-08-18 15:43:19 +0200245 return zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Swen Schillig564e1c82009-08-18 15:43:19 +0200248static void zfcp_qdio_undo_sbals(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200249 struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Swen Schillig564e1c82009-08-18 15:43:19 +0200251 struct qdio_buffer **sbal = qdio->req_q.sbal;
Swen Schillig42428f72009-08-18 15:43:18 +0200252 int first = q_req->sbal_first;
253 int last = q_req->sbal_last;
Swen Schillig00bab912008-06-10 18:20:57 +0200254 int count = (last - first + QDIO_MAX_BUFFERS_PER_Q) %
255 QDIO_MAX_BUFFERS_PER_Q + 1;
256 zfcp_qdio_zero_sbals(sbal, first, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257}
258
Swen Schillig564e1c82009-08-18 15:43:19 +0200259static int zfcp_qdio_fill_sbals(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200260 struct zfcp_queue_req *q_req,
Swen Schillig00bab912008-06-10 18:20:57 +0200261 unsigned int sbtype, void *start_addr,
262 unsigned int total_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200264 struct qdio_buffer_element *sbale;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 unsigned long remaining, length;
266 void *addr;
267
Swen Schillig00bab912008-06-10 18:20:57 +0200268 /* split segment up */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 for (addr = start_addr, remaining = total_length; remaining > 0;
270 addr += length, remaining -= length) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200271 sbale = zfcp_qdio_sbale_next(qdio, q_req, sbtype);
Swen Schillig00bab912008-06-10 18:20:57 +0200272 if (!sbale) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200273 atomic_inc(&qdio->req_q_full);
274 zfcp_qdio_undo_sbals(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -EINVAL;
276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
Swen Schillig00bab912008-06-10 18:20:57 +0200278 /* new piece must not exceed next page boundary */
279 length = min(remaining,
280 (PAGE_SIZE - ((unsigned long)addr &
281 (PAGE_SIZE - 1))));
282 sbale->addr = addr;
283 sbale->length = length;
284 }
285 return 0;
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288/**
289 * zfcp_qdio_sbals_from_sg - fill SBALs from scatter-gather list
290 * @fsf_req: request to be processed
291 * @sbtype: SBALE flags
292 * @sg: scatter-gather list
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * @max_sbals: upper bound for number of SBALs to be used
Swen Schillig00bab912008-06-10 18:20:57 +0200294 * Returns: number of bytes, or error (negativ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200296int zfcp_qdio_sbals_from_sg(struct zfcp_qdio *qdio,
Swen Schillig42428f72009-08-18 15:43:18 +0200297 struct zfcp_queue_req *q_req,
298 unsigned long sbtype, struct scatterlist *sg,
299 int max_sbals)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200301 struct qdio_buffer_element *sbale;
Swen Schillig00bab912008-06-10 18:20:57 +0200302 int retval, bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* figure out last allowed SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200305 zfcp_qdio_sbal_limit(qdio, q_req, max_sbals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Swen Schillig00bab912008-06-10 18:20:57 +0200307 /* set storage-block type for this request */
Swen Schillig564e1c82009-08-18 15:43:19 +0200308 sbale = zfcp_qdio_sbale_req(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 sbale->flags |= sbtype;
310
Swen Schillig00bab912008-06-10 18:20:57 +0200311 for (; sg; sg = sg_next(sg)) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200312 retval = zfcp_qdio_fill_sbals(qdio, q_req, sbtype,
Swen Schillig42428f72009-08-18 15:43:18 +0200313 sg_virt(sg), sg->length);
Swen Schillig00bab912008-06-10 18:20:57 +0200314 if (retval < 0)
315 return retval;
316 bytes += sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
Swen Schillig00bab912008-06-10 18:20:57 +0200318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 /* assume that no other SBALEs are to follow in the same SBAL */
Swen Schillig564e1c82009-08-18 15:43:19 +0200320 sbale = zfcp_qdio_sbale_curr(qdio, q_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 sbale->flags |= SBAL_FLAGS_LAST_ENTRY;
Swen Schillig00bab912008-06-10 18:20:57 +0200322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return bytes;
324}
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326/**
Swen Schillig00bab912008-06-10 18:20:57 +0200327 * zfcp_qdio_send - set PCI flag in first SBALE and send req to QDIO
Swen Schillig564e1c82009-08-18 15:43:19 +0200328 * @qdio: pointer to struct zfcp_qdio
329 * @q_req: pointer to struct zfcp_queue_req
Swen Schillig00bab912008-06-10 18:20:57 +0200330 * Returns: 0 on success, error otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200332int zfcp_qdio_send(struct zfcp_qdio *qdio, struct zfcp_queue_req *q_req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Swen Schillig564e1c82009-08-18 15:43:19 +0200334 struct zfcp_qdio_queue *req_q = &qdio->req_q;
Swen Schillig42428f72009-08-18 15:43:18 +0200335 int first = q_req->sbal_first;
336 int count = q_req->sbal_number;
Christof Schmitt21ddaa52009-03-02 13:09:05 +0100337 int retval;
338 unsigned int qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
Swen Schillig00bab912008-06-10 18:20:57 +0200339
Swen Schillig564e1c82009-08-18 15:43:19 +0200340 zfcp_qdio_account(qdio);
Martin Peschke94506fd2009-03-02 13:08:56 +0100341
Swen Schillig564e1c82009-08-18 15:43:19 +0200342 retval = do_QDIO(qdio->adapter->ccw_device, qdio_flags, 0, first,
343 count);
Swen Schillig00bab912008-06-10 18:20:57 +0200344 if (unlikely(retval)) {
345 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
346 return retval;
347 }
348
349 /* account for transferred buffers */
350 atomic_sub(count, &req_q->count);
351 req_q->first += count;
352 req_q->first %= QDIO_MAX_BUFFERS_PER_Q;
Swen Schillig00bab912008-06-10 18:20:57 +0200353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354}
355
Swen Schillig564e1c82009-08-18 15:43:19 +0200356
357static void zfcp_qdio_setup_init_data(struct qdio_initialize *id,
358 struct zfcp_qdio *qdio)
359{
360
361 id->cdev = qdio->adapter->ccw_device;
362 id->q_format = QDIO_ZFCP_QFMT;
363 memcpy(id->adapter_name, dev_name(&id->cdev->dev), 8);
364 ASCEBC(id->adapter_name, 8);
365 id->qib_param_field_format = 0;
366 id->qib_param_field = NULL;
367 id->input_slib_elements = NULL;
368 id->output_slib_elements = NULL;
369 id->no_input_qs = 1;
370 id->no_output_qs = 1;
371 id->input_handler = zfcp_qdio_int_resp;
372 id->output_handler = zfcp_qdio_int_req;
373 id->int_parm = (unsigned long) qdio;
374 id->flags = QDIO_INBOUND_0COPY_SBALS |
375 QDIO_OUTBOUND_0COPY_SBALS | QDIO_USE_OUTBOUND_PCIS;
376 id->input_sbal_addr_array = (void **) (qdio->resp_q.sbal);
377 id->output_sbal_addr_array = (void **) (qdio->req_q.sbal);
378
379}
Swen Schillig00bab912008-06-10 18:20:57 +0200380/**
Swen Schillig00bab912008-06-10 18:20:57 +0200381 * zfcp_qdio_allocate - allocate queue memory and initialize QDIO data
382 * @adapter: pointer to struct zfcp_adapter
383 * Returns: -ENOMEM on memory allocation error or return value from
384 * qdio_allocate
385 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200386int zfcp_qdio_allocate(struct zfcp_qdio *qdio, struct ccw_device *ccw_dev)
Swen Schillig00bab912008-06-10 18:20:57 +0200387{
Swen Schillig564e1c82009-08-18 15:43:19 +0200388 struct qdio_initialize init_data;
Swen Schillig00bab912008-06-10 18:20:57 +0200389
Swen Schillig564e1c82009-08-18 15:43:19 +0200390 if (zfcp_qdio_buffers_enqueue(qdio->req_q.sbal) ||
391 zfcp_qdio_buffers_enqueue(qdio->resp_q.sbal))
Swen Schillig00bab912008-06-10 18:20:57 +0200392 return -ENOMEM;
393
Swen Schillig564e1c82009-08-18 15:43:19 +0200394 zfcp_qdio_setup_init_data(&init_data, qdio);
Swen Schillig00bab912008-06-10 18:20:57 +0200395
Swen Schillig564e1c82009-08-18 15:43:19 +0200396 return qdio_allocate(&init_data);
Swen Schillig00bab912008-06-10 18:20:57 +0200397}
398
399/**
400 * zfcp_close_qdio - close qdio queues for an adapter
Swen Schillig564e1c82009-08-18 15:43:19 +0200401 * @qdio: pointer to structure zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200402 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200403void zfcp_qdio_close(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200404{
405 struct zfcp_qdio_queue *req_q;
406 int first, count;
407
Swen Schillig564e1c82009-08-18 15:43:19 +0200408 if (!(atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP))
Swen Schillig00bab912008-06-10 18:20:57 +0200409 return;
410
411 /* clear QDIOUP flag, thus do_QDIO is not called during qdio_shutdown */
Swen Schillig564e1c82009-08-18 15:43:19 +0200412 req_q = &qdio->req_q;
413 spin_lock_bh(&qdio->req_q_lock);
414 atomic_clear_mask(ZFCP_STATUS_ADAPTER_QDIOUP, &qdio->adapter->status);
415 spin_unlock_bh(&qdio->req_q_lock);
Swen Schillig00bab912008-06-10 18:20:57 +0200416
Swen Schillig564e1c82009-08-18 15:43:19 +0200417 qdio_shutdown(qdio->adapter->ccw_device,
418 QDIO_FLAG_CLEANUP_USING_CLEAR);
Swen Schillig00bab912008-06-10 18:20:57 +0200419
420 /* cleanup used outbound sbals */
421 count = atomic_read(&req_q->count);
422 if (count < QDIO_MAX_BUFFERS_PER_Q) {
423 first = (req_q->first + count) % QDIO_MAX_BUFFERS_PER_Q;
424 count = QDIO_MAX_BUFFERS_PER_Q - count;
425 zfcp_qdio_zero_sbals(req_q->sbal, first, count);
426 }
427 req_q->first = 0;
428 atomic_set(&req_q->count, 0);
Swen Schillig564e1c82009-08-18 15:43:19 +0200429 qdio->resp_q.first = 0;
430 atomic_set(&qdio->resp_q.count, 0);
Swen Schillig00bab912008-06-10 18:20:57 +0200431}
432
433/**
434 * zfcp_qdio_open - prepare and initialize response queue
Swen Schillig564e1c82009-08-18 15:43:19 +0200435 * @qdio: pointer to struct zfcp_qdio
Swen Schillig00bab912008-06-10 18:20:57 +0200436 * Returns: 0 on success, otherwise -EIO
437 */
Swen Schillig564e1c82009-08-18 15:43:19 +0200438int zfcp_qdio_open(struct zfcp_qdio *qdio)
Swen Schillig00bab912008-06-10 18:20:57 +0200439{
Swen Schillig44cc76f2008-10-01 12:42:16 +0200440 struct qdio_buffer_element *sbale;
Swen Schillig564e1c82009-08-18 15:43:19 +0200441 struct qdio_initialize init_data;
442 struct ccw_device *cdev = qdio->adapter->ccw_device;
Swen Schillig00bab912008-06-10 18:20:57 +0200443 int cc;
444
Swen Schillig564e1c82009-08-18 15:43:19 +0200445 if (atomic_read(&qdio->adapter->status) & ZFCP_STATUS_ADAPTER_QDIOUP)
Swen Schillig00bab912008-06-10 18:20:57 +0200446 return -EIO;
447
Swen Schillig564e1c82009-08-18 15:43:19 +0200448 zfcp_qdio_setup_init_data(&init_data, qdio);
449
450 if (qdio_establish(&init_data))
Christof Schmittff3b24f2008-10-01 12:42:15 +0200451 goto failed_establish;
Swen Schillig00bab912008-06-10 18:20:57 +0200452
Swen Schillig564e1c82009-08-18 15:43:19 +0200453 if (qdio_activate(cdev))
Swen Schillig00bab912008-06-10 18:20:57 +0200454 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200455
456 for (cc = 0; cc < QDIO_MAX_BUFFERS_PER_Q; cc++) {
Swen Schillig564e1c82009-08-18 15:43:19 +0200457 sbale = &(qdio->resp_q.sbal[cc]->element[0]);
Swen Schillig00bab912008-06-10 18:20:57 +0200458 sbale->length = 0;
459 sbale->flags = SBAL_FLAGS_LAST_ENTRY;
460 sbale->addr = NULL;
461 }
462
Swen Schillig564e1c82009-08-18 15:43:19 +0200463 if (do_QDIO(cdev, QDIO_FLAG_SYNC_INPUT, 0, 0,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200464 QDIO_MAX_BUFFERS_PER_Q))
Swen Schillig00bab912008-06-10 18:20:57 +0200465 goto failed_qdio;
Swen Schillig00bab912008-06-10 18:20:57 +0200466
467 /* set index of first avalable SBALS / number of available SBALS */
Swen Schillig564e1c82009-08-18 15:43:19 +0200468 qdio->req_q.first = 0;
469 atomic_set(&qdio->req_q.count, QDIO_MAX_BUFFERS_PER_Q);
Swen Schillig00bab912008-06-10 18:20:57 +0200470
471 return 0;
472
473failed_qdio:
Swen Schillig564e1c82009-08-18 15:43:19 +0200474 qdio_shutdown(cdev, QDIO_FLAG_CLEANUP_USING_CLEAR);
Christof Schmittff3b24f2008-10-01 12:42:15 +0200475failed_establish:
Swen Schillig564e1c82009-08-18 15:43:19 +0200476 dev_err(&cdev->dev,
Christof Schmittff3b24f2008-10-01 12:42:15 +0200477 "Setting up the QDIO connection to the FCP adapter failed\n");
Swen Schillig00bab912008-06-10 18:20:57 +0200478 return -EIO;
479}