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