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