root/drivers/s390/net/qeth_core_main.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. qeth_close_dev_handler
  2. qeth_get_cardname
  3. qeth_get_cardname_short
  4. qeth_set_allowed_threads
  5. qeth_threads_running
  6. qeth_clear_working_pool_list
  7. qeth_alloc_buffer_pool
  8. qeth_realloc_buffer_pool
  9. qeth_free_qdio_queue
  10. qeth_alloc_qdio_queue
  11. qeth_cq_init
  12. qeth_alloc_cq
  13. qeth_free_cq
  14. qeth_compute_cq_notification
  15. qeth_cleanup_handled_pending
  16. qeth_qdio_handle_aob
  17. qeth_is_cq
  18. qeth_setup_ccw
  19. __qeth_issue_next_read
  20. qeth_issue_next_read
  21. qeth_enqueue_cmd
  22. qeth_dequeue_cmd
  23. qeth_notify_cmd
  24. qeth_issue_ipa_msg
  25. qeth_check_ipa_data
  26. qeth_clear_ipacmd_list
  27. qeth_check_idx_response
  28. qeth_put_cmd
  29. qeth_release_buffer_cb
  30. qeth_cancel_cmd
  31. qeth_alloc_cmd
  32. qeth_issue_next_read_cb
  33. qeth_set_thread_start_bit
  34. qeth_clear_thread_start_bit
  35. qeth_clear_thread_running_bit
  36. __qeth_do_run_thread
  37. qeth_do_run_thread
  38. qeth_schedule_recovery
  39. qeth_get_problem
  40. qeth_check_irb_error
  41. qeth_irq
  42. qeth_notify_skbs
  43. qeth_tx_complete_buf
  44. qeth_clear_output_buffer
  45. qeth_drain_output_queue
  46. qeth_drain_output_queues
  47. qeth_free_buffer_pool
  48. qeth_clean_channel
  49. qeth_setup_channel
  50. qeth_osa_set_output_queues
  51. qeth_update_from_chp_desc
  52. qeth_init_qdio_info
  53. qeth_set_initial_options
  54. qeth_do_start_thread
  55. qeth_start_kernel_thread
  56. qeth_setup_card
  57. qeth_core_sl_print
  58. qeth_alloc_card
  59. qeth_clear_channel
  60. qeth_halt_channel
  61. qeth_stop_channel
  62. qeth_halt_channels
  63. qeth_clear_channels
  64. qeth_clear_halt_card
  65. qeth_qdio_clear_card
  66. qeth_vm_detect_layer
  67. qeth_enforce_discipline
  68. qeth_set_blkt_defaults
  69. qeth_init_tokens
  70. qeth_init_func_level
  71. qeth_idx_finalize_cmd
  72. qeth_peer_func_level
  73. qeth_mpc_finalize_cmd
  74. qeth_mpc_alloc_cmd
  75. qeth_send_control_data
  76. qeth_read_conf_data_cb
  77. qeth_read_conf_data
  78. qeth_idx_check_activate_response
  79. qeth_idx_activate_read_channel_cb
  80. qeth_idx_activate_write_channel_cb
  81. qeth_idx_setup_activate_cmd
  82. qeth_idx_activate_read_channel
  83. qeth_idx_activate_write_channel
  84. qeth_cm_enable_cb
  85. qeth_cm_enable
  86. qeth_cm_setup_cb
  87. qeth_cm_setup
  88. qeth_update_max_mtu
  89. qeth_get_mtu_outof_framesize
  90. qeth_ulp_enable_cb
  91. qeth_mpc_select_prot_type
  92. qeth_ulp_enable
  93. qeth_ulp_setup_cb
  94. qeth_ulp_setup
  95. qeth_init_qdio_out_buf
  96. qeth_free_output_queue
  97. qeth_alloc_output_queue
  98. qeth_tx_completion_timer
  99. qeth_alloc_qdio_queues
  100. qeth_free_qdio_queues
  101. qeth_create_qib_param_field
  102. qeth_create_qib_param_field_blkt
  103. qeth_qdio_activate
  104. qeth_dm_act
  105. qeth_mpc_initialize
  106. qeth_print_status_message
  107. qeth_initialize_working_pool_list
  108. qeth_find_free_buffer_pool_entry
  109. qeth_init_input_buffer
  110. qeth_init_qdio_queues
  111. qeth_ipa_finalize_cmd
  112. qeth_prepare_ipa_cmd
  113. qeth_ipa_alloc_cmd
  114. qeth_send_ipa_cmd_cb
  115. qeth_send_ipa_cmd
  116. qeth_send_startlan_cb
  117. qeth_send_startlan
  118. qeth_setadpparms_inspect_rc
  119. qeth_query_setadapterparms_cb
  120. qeth_get_adapter_cmd
  121. qeth_query_setadapterparms
  122. qeth_query_ipassists_cb
  123. qeth_query_ipassists
  124. qeth_query_switch_attributes_cb
  125. qeth_query_switch_attributes
  126. qeth_get_diag_cmd
  127. qeth_query_setdiagass_cb
  128. qeth_query_setdiagass
  129. qeth_get_trap_id
  130. qeth_hw_trap_cb
  131. qeth_hw_trap
  132. qeth_check_qdio_errors
  133. qeth_queue_input_buffer
  134. qeth_buffer_reclaim_work
  135. qeth_handle_send_error
  136. qeth_prep_flush_pack_buffer
  137. qeth_switch_to_packing_if_needed
  138. qeth_switch_to_nonpacking_if_needed
  139. qeth_flush_buffers
  140. qeth_flush_queue
  141. qeth_check_outbound_queue
  142. qeth_qdio_start_poll
  143. qeth_configure_cq
  144. qeth_qdio_cq_handler
  145. qeth_qdio_input_handler
  146. qeth_qdio_output_handler
  147. qeth_get_priority_queue
  148. qeth_get_elements_for_frags
  149. qeth_count_elements
  150. qeth_add_hw_header
  151. qeth_iqd_may_bulk
  152. __qeth_fill_buffer
  153. qeth_fill_buffer
  154. __qeth_xmit
  155. qeth_do_send_packet
  156. qeth_fill_tso_ext
  157. qeth_xmit
  158. qeth_setadp_promisc_mode_cb
  159. qeth_setadp_promisc_mode
  160. qeth_setadpparms_change_macaddr_cb
  161. qeth_setadpparms_change_macaddr
  162. qeth_setadpparms_set_access_ctrl_cb
  163. qeth_setadpparms_set_access_ctrl
  164. qeth_set_access_ctrl_online
  165. qeth_tx_timeout
  166. qeth_mdio_read
  167. qeth_snmp_command_cb
  168. qeth_snmp_command
  169. qeth_setadpparms_query_oat_cb
  170. qeth_query_oat_command
  171. qeth_query_card_info_cb
  172. qeth_query_card_info
  173. qeth_vm_request_mac
  174. qeth_determine_capabilities
  175. qeth_qdio_establish_cq
  176. qeth_qdio_establish
  177. qeth_core_free_card
  178. qeth_trace_features
  179. qeth_core_hardsetup_card
  180. qeth_create_skb_frag
  181. qeth_is_last_sbale
  182. qeth_core_get_next_skb
  183. qeth_poll
  184. qeth_iqd_tx_complete
  185. qeth_tx_poll
  186. qeth_setassparms_inspect_rc
  187. qeth_setassparms_get_caps_cb
  188. qeth_setassparms_cb
  189. qeth_get_setassparms_cmd
  190. qeth_send_simple_setassparms_prot
  191. qeth_unregister_dbf_views
  192. qeth_dbf_longtext
  193. qeth_register_dbf_views
  194. qeth_core_load_discipline
  195. qeth_core_free_discipline
  196. qeth_get_dbf_entry
  197. qeth_add_dbf_entry
  198. qeth_clear_dbf_list
  199. qeth_alloc_netdev
  200. qeth_clone_netdev
  201. qeth_core_probe_device
  202. qeth_core_remove_device
  203. qeth_core_set_online
  204. qeth_core_set_offline
  205. qeth_core_shutdown
  206. qeth_suspend
  207. qeth_resume
  208. group_store
  209. qeth_get_card_by_busid
  210. qeth_do_ioctl
  211. qeth_start_csum_cb
  212. qeth_set_csum_off
  213. qeth_set_csum_on
  214. qeth_set_ipa_csum
  215. qeth_start_tso_cb
  216. qeth_set_tso_off
  217. qeth_set_tso_on
  218. qeth_set_ipa_tso
  219. qeth_set_ipa_rx_csum
  220. qeth_enable_hw_features
  221. qeth_set_features
  222. qeth_fix_features
  223. qeth_features_check
  224. qeth_get_stats64
  225. qeth_iqd_select_queue
  226. qeth_open
  227. qeth_stop
  228. qeth_core_init
  229. qeth_core_exit

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  *    Copyright IBM Corp. 2007, 2009
   4  *    Author(s): Utz Bacher <utz.bacher@de.ibm.com>,
   5  *               Frank Pavlic <fpavlic@de.ibm.com>,
   6  *               Thomas Spatzier <tspat@de.ibm.com>,
   7  *               Frank Blaschka <frank.blaschka@de.ibm.com>
   8  */
   9 
  10 #define KMSG_COMPONENT "qeth"
  11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  12 
  13 #include <linux/compat.h>
  14 #include <linux/module.h>
  15 #include <linux/moduleparam.h>
  16 #include <linux/string.h>
  17 #include <linux/errno.h>
  18 #include <linux/kernel.h>
  19 #include <linux/log2.h>
  20 #include <linux/ip.h>
  21 #include <linux/tcp.h>
  22 #include <linux/mii.h>
  23 #include <linux/mm.h>
  24 #include <linux/kthread.h>
  25 #include <linux/slab.h>
  26 #include <linux/if_vlan.h>
  27 #include <linux/netdevice.h>
  28 #include <linux/netdev_features.h>
  29 #include <linux/skbuff.h>
  30 #include <linux/vmalloc.h>
  31 
  32 #include <net/iucv/af_iucv.h>
  33 #include <net/dsfield.h>
  34 
  35 #include <asm/ebcdic.h>
  36 #include <asm/chpid.h>
  37 #include <asm/io.h>
  38 #include <asm/sysinfo.h>
  39 #include <asm/diag.h>
  40 #include <asm/cio.h>
  41 #include <asm/ccwdev.h>
  42 #include <asm/cpcmd.h>
  43 
  44 #include "qeth_core.h"
  45 
  46 struct qeth_dbf_info qeth_dbf[QETH_DBF_INFOS] = {
  47         /* define dbf - Name, Pages, Areas, Maxlen, Level, View, Handle */
  48         /*                   N  P  A    M  L  V                      H  */
  49         [QETH_DBF_SETUP] = {"qeth_setup",
  50                                 8, 1,   8, 5, &debug_hex_ascii_view, NULL},
  51         [QETH_DBF_MSG]   = {"qeth_msg", 8, 1, 11 * sizeof(long), 3,
  52                             &debug_sprintf_view, NULL},
  53         [QETH_DBF_CTRL]  = {"qeth_control",
  54                 8, 1, QETH_DBF_CTRL_LEN, 5, &debug_hex_ascii_view, NULL},
  55 };
  56 EXPORT_SYMBOL_GPL(qeth_dbf);
  57 
  58 struct kmem_cache *qeth_core_header_cache;
  59 EXPORT_SYMBOL_GPL(qeth_core_header_cache);
  60 static struct kmem_cache *qeth_qdio_outbuf_cache;
  61 
  62 static struct device *qeth_core_root_dev;
  63 static struct lock_class_key qdio_out_skb_queue_key;
  64 
  65 static void qeth_issue_next_read_cb(struct qeth_card *card,
  66                                     struct qeth_cmd_buffer *iob,
  67                                     unsigned int data_length);
  68 static void qeth_free_buffer_pool(struct qeth_card *);
  69 static int qeth_qdio_establish(struct qeth_card *);
  70 static void qeth_free_qdio_queues(struct qeth_card *card);
  71 static void qeth_notify_skbs(struct qeth_qdio_out_q *queue,
  72                 struct qeth_qdio_out_buffer *buf,
  73                 enum iucv_tx_notify notification);
  74 static void qeth_tx_complete_buf(struct qeth_qdio_out_buffer *buf, bool error,
  75                                  int budget);
  76 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *, int);
  77 
  78 static void qeth_close_dev_handler(struct work_struct *work)
  79 {
  80         struct qeth_card *card;
  81 
  82         card = container_of(work, struct qeth_card, close_dev_work);
  83         QETH_CARD_TEXT(card, 2, "cldevhdl");
  84         ccwgroup_set_offline(card->gdev);
  85 }
  86 
  87 static const char *qeth_get_cardname(struct qeth_card *card)
  88 {
  89         if (IS_VM_NIC(card)) {
  90                 switch (card->info.type) {
  91                 case QETH_CARD_TYPE_OSD:
  92                         return " Virtual NIC QDIO";
  93                 case QETH_CARD_TYPE_IQD:
  94                         return " Virtual NIC Hiper";
  95                 case QETH_CARD_TYPE_OSM:
  96                         return " Virtual NIC QDIO - OSM";
  97                 case QETH_CARD_TYPE_OSX:
  98                         return " Virtual NIC QDIO - OSX";
  99                 default:
 100                         return " unknown";
 101                 }
 102         } else {
 103                 switch (card->info.type) {
 104                 case QETH_CARD_TYPE_OSD:
 105                         return " OSD Express";
 106                 case QETH_CARD_TYPE_IQD:
 107                         return " HiperSockets";
 108                 case QETH_CARD_TYPE_OSN:
 109                         return " OSN QDIO";
 110                 case QETH_CARD_TYPE_OSM:
 111                         return " OSM QDIO";
 112                 case QETH_CARD_TYPE_OSX:
 113                         return " OSX QDIO";
 114                 default:
 115                         return " unknown";
 116                 }
 117         }
 118         return " n/a";
 119 }
 120 
 121 /* max length to be returned: 14 */
 122 const char *qeth_get_cardname_short(struct qeth_card *card)
 123 {
 124         if (IS_VM_NIC(card)) {
 125                 switch (card->info.type) {
 126                 case QETH_CARD_TYPE_OSD:
 127                         return "Virt.NIC QDIO";
 128                 case QETH_CARD_TYPE_IQD:
 129                         return "Virt.NIC Hiper";
 130                 case QETH_CARD_TYPE_OSM:
 131                         return "Virt.NIC OSM";
 132                 case QETH_CARD_TYPE_OSX:
 133                         return "Virt.NIC OSX";
 134                 default:
 135                         return "unknown";
 136                 }
 137         } else {
 138                 switch (card->info.type) {
 139                 case QETH_CARD_TYPE_OSD:
 140                         switch (card->info.link_type) {
 141                         case QETH_LINK_TYPE_FAST_ETH:
 142                                 return "OSD_100";
 143                         case QETH_LINK_TYPE_HSTR:
 144                                 return "HSTR";
 145                         case QETH_LINK_TYPE_GBIT_ETH:
 146                                 return "OSD_1000";
 147                         case QETH_LINK_TYPE_10GBIT_ETH:
 148                                 return "OSD_10GIG";
 149                         case QETH_LINK_TYPE_25GBIT_ETH:
 150                                 return "OSD_25GIG";
 151                         case QETH_LINK_TYPE_LANE_ETH100:
 152                                 return "OSD_FE_LANE";
 153                         case QETH_LINK_TYPE_LANE_TR:
 154                                 return "OSD_TR_LANE";
 155                         case QETH_LINK_TYPE_LANE_ETH1000:
 156                                 return "OSD_GbE_LANE";
 157                         case QETH_LINK_TYPE_LANE:
 158                                 return "OSD_ATM_LANE";
 159                         default:
 160                                 return "OSD_Express";
 161                         }
 162                 case QETH_CARD_TYPE_IQD:
 163                         return "HiperSockets";
 164                 case QETH_CARD_TYPE_OSN:
 165                         return "OSN";
 166                 case QETH_CARD_TYPE_OSM:
 167                         return "OSM_1000";
 168                 case QETH_CARD_TYPE_OSX:
 169                         return "OSX_10GIG";
 170                 default:
 171                         return "unknown";
 172                 }
 173         }
 174         return "n/a";
 175 }
 176 
 177 void qeth_set_allowed_threads(struct qeth_card *card, unsigned long threads,
 178                          int clear_start_mask)
 179 {
 180         unsigned long flags;
 181 
 182         spin_lock_irqsave(&card->thread_mask_lock, flags);
 183         card->thread_allowed_mask = threads;
 184         if (clear_start_mask)
 185                 card->thread_start_mask &= threads;
 186         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 187         wake_up(&card->wait_q);
 188 }
 189 EXPORT_SYMBOL_GPL(qeth_set_allowed_threads);
 190 
 191 int qeth_threads_running(struct qeth_card *card, unsigned long threads)
 192 {
 193         unsigned long flags;
 194         int rc = 0;
 195 
 196         spin_lock_irqsave(&card->thread_mask_lock, flags);
 197         rc = (card->thread_running_mask & threads);
 198         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 199         return rc;
 200 }
 201 EXPORT_SYMBOL_GPL(qeth_threads_running);
 202 
 203 void qeth_clear_working_pool_list(struct qeth_card *card)
 204 {
 205         struct qeth_buffer_pool_entry *pool_entry, *tmp;
 206 
 207         QETH_CARD_TEXT(card, 5, "clwrklst");
 208         list_for_each_entry_safe(pool_entry, tmp,
 209                             &card->qdio.in_buf_pool.entry_list, list){
 210                         list_del(&pool_entry->list);
 211         }
 212 }
 213 EXPORT_SYMBOL_GPL(qeth_clear_working_pool_list);
 214 
 215 static int qeth_alloc_buffer_pool(struct qeth_card *card)
 216 {
 217         struct qeth_buffer_pool_entry *pool_entry;
 218         void *ptr;
 219         int i, j;
 220 
 221         QETH_CARD_TEXT(card, 5, "alocpool");
 222         for (i = 0; i < card->qdio.init_pool.buf_count; ++i) {
 223                 pool_entry = kzalloc(sizeof(*pool_entry), GFP_KERNEL);
 224                 if (!pool_entry) {
 225                         qeth_free_buffer_pool(card);
 226                         return -ENOMEM;
 227                 }
 228                 for (j = 0; j < QETH_MAX_BUFFER_ELEMENTS(card); ++j) {
 229                         ptr = (void *) __get_free_page(GFP_KERNEL);
 230                         if (!ptr) {
 231                                 while (j > 0)
 232                                         free_page((unsigned long)
 233                                                   pool_entry->elements[--j]);
 234                                 kfree(pool_entry);
 235                                 qeth_free_buffer_pool(card);
 236                                 return -ENOMEM;
 237                         }
 238                         pool_entry->elements[j] = ptr;
 239                 }
 240                 list_add(&pool_entry->init_list,
 241                          &card->qdio.init_pool.entry_list);
 242         }
 243         return 0;
 244 }
 245 
 246 int qeth_realloc_buffer_pool(struct qeth_card *card, int bufcnt)
 247 {
 248         QETH_CARD_TEXT(card, 2, "realcbp");
 249 
 250         if (card->state != CARD_STATE_DOWN)
 251                 return -EPERM;
 252 
 253         /* TODO: steel/add buffers from/to a running card's buffer pool (?) */
 254         qeth_clear_working_pool_list(card);
 255         qeth_free_buffer_pool(card);
 256         card->qdio.in_buf_pool.buf_count = bufcnt;
 257         card->qdio.init_pool.buf_count = bufcnt;
 258         return qeth_alloc_buffer_pool(card);
 259 }
 260 EXPORT_SYMBOL_GPL(qeth_realloc_buffer_pool);
 261 
 262 static void qeth_free_qdio_queue(struct qeth_qdio_q *q)
 263 {
 264         if (!q)
 265                 return;
 266 
 267         qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
 268         kfree(q);
 269 }
 270 
 271 static struct qeth_qdio_q *qeth_alloc_qdio_queue(void)
 272 {
 273         struct qeth_qdio_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
 274         int i;
 275 
 276         if (!q)
 277                 return NULL;
 278 
 279         if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
 280                 kfree(q);
 281                 return NULL;
 282         }
 283 
 284         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; ++i)
 285                 q->bufs[i].buffer = q->qdio_bufs[i];
 286 
 287         QETH_DBF_HEX(SETUP, 2, &q, sizeof(void *));
 288         return q;
 289 }
 290 
 291 static int qeth_cq_init(struct qeth_card *card)
 292 {
 293         int rc;
 294 
 295         if (card->options.cq == QETH_CQ_ENABLED) {
 296                 QETH_CARD_TEXT(card, 2, "cqinit");
 297                 qdio_reset_buffers(card->qdio.c_q->qdio_bufs,
 298                                    QDIO_MAX_BUFFERS_PER_Q);
 299                 card->qdio.c_q->next_buf_to_init = 127;
 300                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT,
 301                              card->qdio.no_in_queues - 1, 0,
 302                              127);
 303                 if (rc) {
 304                         QETH_CARD_TEXT_(card, 2, "1err%d", rc);
 305                         goto out;
 306                 }
 307         }
 308         rc = 0;
 309 out:
 310         return rc;
 311 }
 312 
 313 static int qeth_alloc_cq(struct qeth_card *card)
 314 {
 315         int rc;
 316 
 317         if (card->options.cq == QETH_CQ_ENABLED) {
 318                 int i;
 319                 struct qdio_outbuf_state *outbuf_states;
 320 
 321                 QETH_CARD_TEXT(card, 2, "cqon");
 322                 card->qdio.c_q = qeth_alloc_qdio_queue();
 323                 if (!card->qdio.c_q) {
 324                         rc = -1;
 325                         goto kmsg_out;
 326                 }
 327                 card->qdio.no_in_queues = 2;
 328                 card->qdio.out_bufstates =
 329                         kcalloc(card->qdio.no_out_queues *
 330                                         QDIO_MAX_BUFFERS_PER_Q,
 331                                 sizeof(struct qdio_outbuf_state),
 332                                 GFP_KERNEL);
 333                 outbuf_states = card->qdio.out_bufstates;
 334                 if (outbuf_states == NULL) {
 335                         rc = -1;
 336                         goto free_cq_out;
 337                 }
 338                 for (i = 0; i < card->qdio.no_out_queues; ++i) {
 339                         card->qdio.out_qs[i]->bufstates = outbuf_states;
 340                         outbuf_states += QDIO_MAX_BUFFERS_PER_Q;
 341                 }
 342         } else {
 343                 QETH_CARD_TEXT(card, 2, "nocq");
 344                 card->qdio.c_q = NULL;
 345                 card->qdio.no_in_queues = 1;
 346         }
 347         QETH_CARD_TEXT_(card, 2, "iqc%d", card->qdio.no_in_queues);
 348         rc = 0;
 349 out:
 350         return rc;
 351 free_cq_out:
 352         qeth_free_qdio_queue(card->qdio.c_q);
 353         card->qdio.c_q = NULL;
 354 kmsg_out:
 355         dev_err(&card->gdev->dev, "Failed to create completion queue\n");
 356         goto out;
 357 }
 358 
 359 static void qeth_free_cq(struct qeth_card *card)
 360 {
 361         if (card->qdio.c_q) {
 362                 --card->qdio.no_in_queues;
 363                 qeth_free_qdio_queue(card->qdio.c_q);
 364                 card->qdio.c_q = NULL;
 365         }
 366         kfree(card->qdio.out_bufstates);
 367         card->qdio.out_bufstates = NULL;
 368 }
 369 
 370 static enum iucv_tx_notify qeth_compute_cq_notification(int sbalf15,
 371                                                         int delayed)
 372 {
 373         enum iucv_tx_notify n;
 374 
 375         switch (sbalf15) {
 376         case 0:
 377                 n = delayed ? TX_NOTIFY_DELAYED_OK : TX_NOTIFY_OK;
 378                 break;
 379         case 4:
 380         case 16:
 381         case 17:
 382         case 18:
 383                 n = delayed ? TX_NOTIFY_DELAYED_UNREACHABLE :
 384                         TX_NOTIFY_UNREACHABLE;
 385                 break;
 386         default:
 387                 n = delayed ? TX_NOTIFY_DELAYED_GENERALERROR :
 388                         TX_NOTIFY_GENERALERROR;
 389                 break;
 390         }
 391 
 392         return n;
 393 }
 394 
 395 static void qeth_cleanup_handled_pending(struct qeth_qdio_out_q *q, int bidx,
 396                                          int forced_cleanup)
 397 {
 398         if (q->card->options.cq != QETH_CQ_ENABLED)
 399                 return;
 400 
 401         if (q->bufs[bidx]->next_pending != NULL) {
 402                 struct qeth_qdio_out_buffer *head = q->bufs[bidx];
 403                 struct qeth_qdio_out_buffer *c = q->bufs[bidx]->next_pending;
 404 
 405                 while (c) {
 406                         if (forced_cleanup ||
 407                             atomic_read(&c->state) ==
 408                               QETH_QDIO_BUF_HANDLED_DELAYED) {
 409                                 struct qeth_qdio_out_buffer *f = c;
 410                                 QETH_CARD_TEXT(f->q->card, 5, "fp");
 411                                 QETH_CARD_TEXT_(f->q->card, 5, "%lx", (long) f);
 412                                 /* release here to avoid interleaving between
 413                                    outbound tasklet and inbound tasklet
 414                                    regarding notifications and lifecycle */
 415                                 qeth_tx_complete_buf(c, forced_cleanup, 0);
 416 
 417                                 c = f->next_pending;
 418                                 WARN_ON_ONCE(head->next_pending != f);
 419                                 head->next_pending = c;
 420                                 kmem_cache_free(qeth_qdio_outbuf_cache, f);
 421                         } else {
 422                                 head = c;
 423                                 c = c->next_pending;
 424                         }
 425 
 426                 }
 427         }
 428         if (forced_cleanup && (atomic_read(&(q->bufs[bidx]->state)) ==
 429                                         QETH_QDIO_BUF_HANDLED_DELAYED)) {
 430                 /* for recovery situations */
 431                 qeth_init_qdio_out_buf(q, bidx);
 432                 QETH_CARD_TEXT(q->card, 2, "clprecov");
 433         }
 434 }
 435 
 436 
 437 static void qeth_qdio_handle_aob(struct qeth_card *card,
 438                                  unsigned long phys_aob_addr)
 439 {
 440         struct qaob *aob;
 441         struct qeth_qdio_out_buffer *buffer;
 442         enum iucv_tx_notify notification;
 443         unsigned int i;
 444 
 445         aob = (struct qaob *) phys_to_virt(phys_aob_addr);
 446         QETH_CARD_TEXT(card, 5, "haob");
 447         QETH_CARD_TEXT_(card, 5, "%lx", phys_aob_addr);
 448         buffer = (struct qeth_qdio_out_buffer *) aob->user1;
 449         QETH_CARD_TEXT_(card, 5, "%lx", aob->user1);
 450 
 451         if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
 452                            QETH_QDIO_BUF_IN_CQ) == QETH_QDIO_BUF_PRIMED) {
 453                 notification = TX_NOTIFY_OK;
 454         } else {
 455                 WARN_ON_ONCE(atomic_read(&buffer->state) !=
 456                                                         QETH_QDIO_BUF_PENDING);
 457                 atomic_set(&buffer->state, QETH_QDIO_BUF_IN_CQ);
 458                 notification = TX_NOTIFY_DELAYED_OK;
 459         }
 460 
 461         if (aob->aorc != 0)  {
 462                 QETH_CARD_TEXT_(card, 2, "aorc%02X", aob->aorc);
 463                 notification = qeth_compute_cq_notification(aob->aorc, 1);
 464         }
 465         qeth_notify_skbs(buffer->q, buffer, notification);
 466 
 467         /* Free dangling allocations. The attached skbs are handled by
 468          * qeth_cleanup_handled_pending().
 469          */
 470         for (i = 0;
 471              i < aob->sb_count && i < QETH_MAX_BUFFER_ELEMENTS(card);
 472              i++) {
 473                 if (aob->sba[i] && buffer->is_header[i])
 474                         kmem_cache_free(qeth_core_header_cache,
 475                                         (void *) aob->sba[i]);
 476         }
 477         atomic_set(&buffer->state, QETH_QDIO_BUF_HANDLED_DELAYED);
 478 
 479         qdio_release_aob(aob);
 480 }
 481 
 482 static inline int qeth_is_cq(struct qeth_card *card, unsigned int queue)
 483 {
 484         return card->options.cq == QETH_CQ_ENABLED &&
 485             card->qdio.c_q != NULL &&
 486             queue != 0 &&
 487             queue == card->qdio.no_in_queues - 1;
 488 }
 489 
 490 static void qeth_setup_ccw(struct ccw1 *ccw, u8 cmd_code, u8 flags, u32 len,
 491                            void *data)
 492 {
 493         ccw->cmd_code = cmd_code;
 494         ccw->flags = flags | CCW_FLAG_SLI;
 495         ccw->count = len;
 496         ccw->cda = (__u32) __pa(data);
 497 }
 498 
 499 static int __qeth_issue_next_read(struct qeth_card *card)
 500 {
 501         struct qeth_cmd_buffer *iob = card->read_cmd;
 502         struct qeth_channel *channel = iob->channel;
 503         struct ccw1 *ccw = __ccw_from_cmd(iob);
 504         int rc;
 505 
 506         QETH_CARD_TEXT(card, 5, "issnxrd");
 507         if (channel->state != CH_STATE_UP)
 508                 return -EIO;
 509 
 510         memset(iob->data, 0, iob->length);
 511         qeth_setup_ccw(ccw, CCW_CMD_READ, 0, iob->length, iob->data);
 512         iob->callback = qeth_issue_next_read_cb;
 513         /* keep the cmd alive after completion: */
 514         qeth_get_cmd(iob);
 515 
 516         QETH_CARD_TEXT(card, 6, "noirqpnd");
 517         rc = ccw_device_start(channel->ccwdev, ccw, (addr_t) iob, 0, 0);
 518         if (!rc) {
 519                 channel->active_cmd = iob;
 520         } else {
 521                 QETH_DBF_MESSAGE(2, "error %i on device %x when starting next read ccw!\n",
 522                                  rc, CARD_DEVID(card));
 523                 atomic_set(&channel->irq_pending, 0);
 524                 qeth_put_cmd(iob);
 525                 card->read_or_write_problem = 1;
 526                 qeth_schedule_recovery(card);
 527                 wake_up(&card->wait_q);
 528         }
 529         return rc;
 530 }
 531 
 532 static int qeth_issue_next_read(struct qeth_card *card)
 533 {
 534         int ret;
 535 
 536         spin_lock_irq(get_ccwdev_lock(CARD_RDEV(card)));
 537         ret = __qeth_issue_next_read(card);
 538         spin_unlock_irq(get_ccwdev_lock(CARD_RDEV(card)));
 539 
 540         return ret;
 541 }
 542 
 543 static void qeth_enqueue_cmd(struct qeth_card *card,
 544                              struct qeth_cmd_buffer *iob)
 545 {
 546         spin_lock_irq(&card->lock);
 547         list_add_tail(&iob->list, &card->cmd_waiter_list);
 548         spin_unlock_irq(&card->lock);
 549 }
 550 
 551 static void qeth_dequeue_cmd(struct qeth_card *card,
 552                              struct qeth_cmd_buffer *iob)
 553 {
 554         spin_lock_irq(&card->lock);
 555         list_del(&iob->list);
 556         spin_unlock_irq(&card->lock);
 557 }
 558 
 559 void qeth_notify_cmd(struct qeth_cmd_buffer *iob, int reason)
 560 {
 561         iob->rc = reason;
 562         complete(&iob->done);
 563 }
 564 EXPORT_SYMBOL_GPL(qeth_notify_cmd);
 565 
 566 static void qeth_issue_ipa_msg(struct qeth_ipa_cmd *cmd, int rc,
 567                 struct qeth_card *card)
 568 {
 569         const char *ipa_name;
 570         int com = cmd->hdr.command;
 571         ipa_name = qeth_get_ipa_cmd_name(com);
 572 
 573         if (rc)
 574                 QETH_DBF_MESSAGE(2, "IPA: %s(%#x) for device %x returned %#x \"%s\"\n",
 575                                  ipa_name, com, CARD_DEVID(card), rc,
 576                                  qeth_get_ipa_msg(rc));
 577         else
 578                 QETH_DBF_MESSAGE(5, "IPA: %s(%#x) for device %x succeeded\n",
 579                                  ipa_name, com, CARD_DEVID(card));
 580 }
 581 
 582 static struct qeth_ipa_cmd *qeth_check_ipa_data(struct qeth_card *card,
 583                                                 struct qeth_ipa_cmd *cmd)
 584 {
 585         QETH_CARD_TEXT(card, 5, "chkipad");
 586 
 587         if (IS_IPA_REPLY(cmd)) {
 588                 if (cmd->hdr.command != IPA_CMD_SETCCID &&
 589                     cmd->hdr.command != IPA_CMD_DELCCID &&
 590                     cmd->hdr.command != IPA_CMD_MODCCID &&
 591                     cmd->hdr.command != IPA_CMD_SET_DIAG_ASS)
 592                         qeth_issue_ipa_msg(cmd, cmd->hdr.return_code, card);
 593                 return cmd;
 594         }
 595 
 596         /* handle unsolicited event: */
 597         switch (cmd->hdr.command) {
 598         case IPA_CMD_STOPLAN:
 599                 if (cmd->hdr.return_code == IPA_RC_VEPA_TO_VEB_TRANSITION) {
 600                         dev_err(&card->gdev->dev,
 601                                 "Interface %s is down because the adjacent port is no longer in reflective relay mode\n",
 602                                 QETH_CARD_IFNAME(card));
 603                         schedule_work(&card->close_dev_work);
 604                 } else {
 605                         dev_warn(&card->gdev->dev,
 606                                  "The link for interface %s on CHPID 0x%X failed\n",
 607                                  QETH_CARD_IFNAME(card), card->info.chpid);
 608                         qeth_issue_ipa_msg(cmd, cmd->hdr.return_code, card);
 609                         netif_carrier_off(card->dev);
 610                 }
 611                 return NULL;
 612         case IPA_CMD_STARTLAN:
 613                 dev_info(&card->gdev->dev,
 614                          "The link for %s on CHPID 0x%X has been restored\n",
 615                          QETH_CARD_IFNAME(card), card->info.chpid);
 616                 if (card->info.hwtrap)
 617                         card->info.hwtrap = 2;
 618                 qeth_schedule_recovery(card);
 619                 return NULL;
 620         case IPA_CMD_SETBRIDGEPORT_IQD:
 621         case IPA_CMD_SETBRIDGEPORT_OSA:
 622         case IPA_CMD_ADDRESS_CHANGE_NOTIF:
 623                 if (card->discipline->control_event_handler(card, cmd))
 624                         return cmd;
 625                 return NULL;
 626         case IPA_CMD_MODCCID:
 627                 return cmd;
 628         case IPA_CMD_REGISTER_LOCAL_ADDR:
 629                 QETH_CARD_TEXT(card, 3, "irla");
 630                 return NULL;
 631         case IPA_CMD_UNREGISTER_LOCAL_ADDR:
 632                 QETH_CARD_TEXT(card, 3, "urla");
 633                 return NULL;
 634         default:
 635                 QETH_DBF_MESSAGE(2, "Received data is IPA but not a reply!\n");
 636                 return cmd;
 637         }
 638 }
 639 
 640 void qeth_clear_ipacmd_list(struct qeth_card *card)
 641 {
 642         struct qeth_cmd_buffer *iob;
 643         unsigned long flags;
 644 
 645         QETH_CARD_TEXT(card, 4, "clipalst");
 646 
 647         spin_lock_irqsave(&card->lock, flags);
 648         list_for_each_entry(iob, &card->cmd_waiter_list, list)
 649                 qeth_notify_cmd(iob, -EIO);
 650         spin_unlock_irqrestore(&card->lock, flags);
 651 }
 652 EXPORT_SYMBOL_GPL(qeth_clear_ipacmd_list);
 653 
 654 static int qeth_check_idx_response(struct qeth_card *card,
 655         unsigned char *buffer)
 656 {
 657         QETH_DBF_HEX(CTRL, 2, buffer, QETH_DBF_CTRL_LEN);
 658         if ((buffer[2] & QETH_IDX_TERMINATE_MASK) == QETH_IDX_TERMINATE) {
 659                 QETH_DBF_MESSAGE(2, "received an IDX TERMINATE with cause code %#04x\n",
 660                                  buffer[4]);
 661                 QETH_CARD_TEXT(card, 2, "ckidxres");
 662                 QETH_CARD_TEXT(card, 2, " idxterm");
 663                 QETH_CARD_TEXT_(card, 2, "rc%x", buffer[4]);
 664                 if (buffer[4] == QETH_IDX_TERM_BAD_TRANSPORT ||
 665                     buffer[4] == QETH_IDX_TERM_BAD_TRANSPORT_VM) {
 666                         dev_err(&card->gdev->dev,
 667                                 "The device does not support the configured transport mode\n");
 668                         return -EPROTONOSUPPORT;
 669                 }
 670                 return -EIO;
 671         }
 672         return 0;
 673 }
 674 
 675 void qeth_put_cmd(struct qeth_cmd_buffer *iob)
 676 {
 677         if (refcount_dec_and_test(&iob->ref_count)) {
 678                 kfree(iob->data);
 679                 kfree(iob);
 680         }
 681 }
 682 EXPORT_SYMBOL_GPL(qeth_put_cmd);
 683 
 684 static void qeth_release_buffer_cb(struct qeth_card *card,
 685                                    struct qeth_cmd_buffer *iob,
 686                                    unsigned int data_length)
 687 {
 688         qeth_put_cmd(iob);
 689 }
 690 
 691 static void qeth_cancel_cmd(struct qeth_cmd_buffer *iob, int rc)
 692 {
 693         qeth_notify_cmd(iob, rc);
 694         qeth_put_cmd(iob);
 695 }
 696 
 697 struct qeth_cmd_buffer *qeth_alloc_cmd(struct qeth_channel *channel,
 698                                        unsigned int length, unsigned int ccws,
 699                                        long timeout)
 700 {
 701         struct qeth_cmd_buffer *iob;
 702 
 703         if (length > QETH_BUFSIZE)
 704                 return NULL;
 705 
 706         iob = kzalloc(sizeof(*iob), GFP_KERNEL);
 707         if (!iob)
 708                 return NULL;
 709 
 710         iob->data = kzalloc(ALIGN(length, 8) + ccws * sizeof(struct ccw1),
 711                             GFP_KERNEL | GFP_DMA);
 712         if (!iob->data) {
 713                 kfree(iob);
 714                 return NULL;
 715         }
 716 
 717         init_completion(&iob->done);
 718         spin_lock_init(&iob->lock);
 719         INIT_LIST_HEAD(&iob->list);
 720         refcount_set(&iob->ref_count, 1);
 721         iob->channel = channel;
 722         iob->timeout = timeout;
 723         iob->length = length;
 724         return iob;
 725 }
 726 EXPORT_SYMBOL_GPL(qeth_alloc_cmd);
 727 
 728 static void qeth_issue_next_read_cb(struct qeth_card *card,
 729                                     struct qeth_cmd_buffer *iob,
 730                                     unsigned int data_length)
 731 {
 732         struct qeth_cmd_buffer *request = NULL;
 733         struct qeth_ipa_cmd *cmd = NULL;
 734         struct qeth_reply *reply = NULL;
 735         struct qeth_cmd_buffer *tmp;
 736         unsigned long flags;
 737         int rc = 0;
 738 
 739         QETH_CARD_TEXT(card, 4, "sndctlcb");
 740         rc = qeth_check_idx_response(card, iob->data);
 741         switch (rc) {
 742         case 0:
 743                 break;
 744         case -EIO:
 745                 qeth_schedule_recovery(card);
 746                 /* fall through */
 747         default:
 748                 qeth_clear_ipacmd_list(card);
 749                 goto out;
 750         }
 751 
 752         if (IS_IPA(iob->data)) {
 753                 cmd = (struct qeth_ipa_cmd *) PDU_ENCAPSULATION(iob->data);
 754                 cmd = qeth_check_ipa_data(card, cmd);
 755                 if (!cmd)
 756                         goto out;
 757                 if (IS_OSN(card) && card->osn_info.assist_cb &&
 758                     cmd->hdr.command != IPA_CMD_STARTLAN) {
 759                         card->osn_info.assist_cb(card->dev, cmd);
 760                         goto out;
 761                 }
 762         } else {
 763                 /* non-IPA commands should only flow during initialization */
 764                 if (card->state != CARD_STATE_DOWN)
 765                         goto out;
 766         }
 767 
 768         /* match against pending cmd requests */
 769         spin_lock_irqsave(&card->lock, flags);
 770         list_for_each_entry(tmp, &card->cmd_waiter_list, list) {
 771                 if (!IS_IPA(tmp->data) ||
 772                     __ipa_cmd(tmp)->hdr.seqno == cmd->hdr.seqno) {
 773                         request = tmp;
 774                         /* take the object outside the lock */
 775                         qeth_get_cmd(request);
 776                         break;
 777                 }
 778         }
 779         spin_unlock_irqrestore(&card->lock, flags);
 780 
 781         if (!request)
 782                 goto out;
 783 
 784         reply = &request->reply;
 785         if (!reply->callback) {
 786                 rc = 0;
 787                 goto no_callback;
 788         }
 789 
 790         spin_lock_irqsave(&request->lock, flags);
 791         if (request->rc)
 792                 /* Bail out when the requestor has already left: */
 793                 rc = request->rc;
 794         else
 795                 rc = reply->callback(card, reply, cmd ? (unsigned long)cmd :
 796                                                         (unsigned long)iob);
 797         spin_unlock_irqrestore(&request->lock, flags);
 798 
 799 no_callback:
 800         if (rc <= 0)
 801                 qeth_notify_cmd(request, rc);
 802         qeth_put_cmd(request);
 803 out:
 804         memcpy(&card->seqno.pdu_hdr_ack,
 805                 QETH_PDU_HEADER_SEQ_NO(iob->data),
 806                 QETH_SEQ_NO_LENGTH);
 807         qeth_put_cmd(iob);
 808         __qeth_issue_next_read(card);
 809 }
 810 
 811 static int qeth_set_thread_start_bit(struct qeth_card *card,
 812                 unsigned long thread)
 813 {
 814         unsigned long flags;
 815 
 816         spin_lock_irqsave(&card->thread_mask_lock, flags);
 817         if (!(card->thread_allowed_mask & thread) ||
 818               (card->thread_start_mask & thread)) {
 819                 spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 820                 return -EPERM;
 821         }
 822         card->thread_start_mask |= thread;
 823         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 824         return 0;
 825 }
 826 
 827 void qeth_clear_thread_start_bit(struct qeth_card *card, unsigned long thread)
 828 {
 829         unsigned long flags;
 830 
 831         spin_lock_irqsave(&card->thread_mask_lock, flags);
 832         card->thread_start_mask &= ~thread;
 833         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 834         wake_up(&card->wait_q);
 835 }
 836 EXPORT_SYMBOL_GPL(qeth_clear_thread_start_bit);
 837 
 838 void qeth_clear_thread_running_bit(struct qeth_card *card, unsigned long thread)
 839 {
 840         unsigned long flags;
 841 
 842         spin_lock_irqsave(&card->thread_mask_lock, flags);
 843         card->thread_running_mask &= ~thread;
 844         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 845         wake_up_all(&card->wait_q);
 846 }
 847 EXPORT_SYMBOL_GPL(qeth_clear_thread_running_bit);
 848 
 849 static int __qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
 850 {
 851         unsigned long flags;
 852         int rc = 0;
 853 
 854         spin_lock_irqsave(&card->thread_mask_lock, flags);
 855         if (card->thread_start_mask & thread) {
 856                 if ((card->thread_allowed_mask & thread) &&
 857                     !(card->thread_running_mask & thread)) {
 858                         rc = 1;
 859                         card->thread_start_mask &= ~thread;
 860                         card->thread_running_mask |= thread;
 861                 } else
 862                         rc = -EPERM;
 863         }
 864         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
 865         return rc;
 866 }
 867 
 868 int qeth_do_run_thread(struct qeth_card *card, unsigned long thread)
 869 {
 870         int rc = 0;
 871 
 872         wait_event(card->wait_q,
 873                    (rc = __qeth_do_run_thread(card, thread)) >= 0);
 874         return rc;
 875 }
 876 EXPORT_SYMBOL_GPL(qeth_do_run_thread);
 877 
 878 void qeth_schedule_recovery(struct qeth_card *card)
 879 {
 880         QETH_CARD_TEXT(card, 2, "startrec");
 881         if (qeth_set_thread_start_bit(card, QETH_RECOVER_THREAD) == 0)
 882                 schedule_work(&card->kernel_thread_starter);
 883 }
 884 EXPORT_SYMBOL_GPL(qeth_schedule_recovery);
 885 
 886 static int qeth_get_problem(struct qeth_card *card, struct ccw_device *cdev,
 887                             struct irb *irb)
 888 {
 889         int dstat, cstat;
 890         char *sense;
 891 
 892         sense = (char *) irb->ecw;
 893         cstat = irb->scsw.cmd.cstat;
 894         dstat = irb->scsw.cmd.dstat;
 895 
 896         if (cstat & (SCHN_STAT_CHN_CTRL_CHK | SCHN_STAT_INTF_CTRL_CHK |
 897                      SCHN_STAT_CHN_DATA_CHK | SCHN_STAT_CHAIN_CHECK |
 898                      SCHN_STAT_PROT_CHECK | SCHN_STAT_PROG_CHECK)) {
 899                 QETH_CARD_TEXT(card, 2, "CGENCHK");
 900                 dev_warn(&cdev->dev, "The qeth device driver "
 901                         "failed to recover an error on the device\n");
 902                 QETH_DBF_MESSAGE(2, "check on channel %x with dstat=%#x, cstat=%#x\n",
 903                                  CCW_DEVID(cdev), dstat, cstat);
 904                 print_hex_dump(KERN_WARNING, "qeth: irb ", DUMP_PREFIX_OFFSET,
 905                                 16, 1, irb, 64, 1);
 906                 return -EIO;
 907         }
 908 
 909         if (dstat & DEV_STAT_UNIT_CHECK) {
 910                 if (sense[SENSE_RESETTING_EVENT_BYTE] &
 911                     SENSE_RESETTING_EVENT_FLAG) {
 912                         QETH_CARD_TEXT(card, 2, "REVIND");
 913                         return -EIO;
 914                 }
 915                 if (sense[SENSE_COMMAND_REJECT_BYTE] &
 916                     SENSE_COMMAND_REJECT_FLAG) {
 917                         QETH_CARD_TEXT(card, 2, "CMDREJi");
 918                         return -EIO;
 919                 }
 920                 if ((sense[2] == 0xaf) && (sense[3] == 0xfe)) {
 921                         QETH_CARD_TEXT(card, 2, "AFFE");
 922                         return -EIO;
 923                 }
 924                 if ((!sense[0]) && (!sense[1]) && (!sense[2]) && (!sense[3])) {
 925                         QETH_CARD_TEXT(card, 2, "ZEROSEN");
 926                         return 0;
 927                 }
 928                 QETH_CARD_TEXT(card, 2, "DGENCHK");
 929                         return -EIO;
 930         }
 931         return 0;
 932 }
 933 
 934 static int qeth_check_irb_error(struct qeth_card *card, struct ccw_device *cdev,
 935                                 struct irb *irb)
 936 {
 937         if (!IS_ERR(irb))
 938                 return 0;
 939 
 940         switch (PTR_ERR(irb)) {
 941         case -EIO:
 942                 QETH_DBF_MESSAGE(2, "i/o-error on channel %x\n",
 943                                  CCW_DEVID(cdev));
 944                 QETH_CARD_TEXT(card, 2, "ckirberr");
 945                 QETH_CARD_TEXT_(card, 2, "  rc%d", -EIO);
 946                 return -EIO;
 947         case -ETIMEDOUT:
 948                 dev_warn(&cdev->dev, "A hardware operation timed out"
 949                         " on the device\n");
 950                 QETH_CARD_TEXT(card, 2, "ckirberr");
 951                 QETH_CARD_TEXT_(card, 2, "  rc%d", -ETIMEDOUT);
 952                 return -ETIMEDOUT;
 953         default:
 954                 QETH_DBF_MESSAGE(2, "unknown error %ld on channel %x\n",
 955                                  PTR_ERR(irb), CCW_DEVID(cdev));
 956                 QETH_CARD_TEXT(card, 2, "ckirberr");
 957                 QETH_CARD_TEXT(card, 2, "  rc???");
 958                 return PTR_ERR(irb);
 959         }
 960 }
 961 
 962 static void qeth_irq(struct ccw_device *cdev, unsigned long intparm,
 963                 struct irb *irb)
 964 {
 965         int rc;
 966         int cstat, dstat;
 967         struct qeth_cmd_buffer *iob = NULL;
 968         struct ccwgroup_device *gdev;
 969         struct qeth_channel *channel;
 970         struct qeth_card *card;
 971 
 972         /* while we hold the ccwdev lock, this stays valid: */
 973         gdev = dev_get_drvdata(&cdev->dev);
 974         card = dev_get_drvdata(&gdev->dev);
 975         if (!card)
 976                 return;
 977 
 978         QETH_CARD_TEXT(card, 5, "irq");
 979 
 980         if (card->read.ccwdev == cdev) {
 981                 channel = &card->read;
 982                 QETH_CARD_TEXT(card, 5, "read");
 983         } else if (card->write.ccwdev == cdev) {
 984                 channel = &card->write;
 985                 QETH_CARD_TEXT(card, 5, "write");
 986         } else {
 987                 channel = &card->data;
 988                 QETH_CARD_TEXT(card, 5, "data");
 989         }
 990 
 991         if (intparm == 0) {
 992                 QETH_CARD_TEXT(card, 5, "irqunsol");
 993         } else if ((addr_t)intparm != (addr_t)channel->active_cmd) {
 994                 QETH_CARD_TEXT(card, 5, "irqunexp");
 995 
 996                 dev_err(&cdev->dev,
 997                         "Received IRQ with intparm %lx, expected %px\n",
 998                         intparm, channel->active_cmd);
 999                 if (channel->active_cmd)
1000                         qeth_cancel_cmd(channel->active_cmd, -EIO);
1001         } else {
1002                 iob = (struct qeth_cmd_buffer *) (addr_t)intparm;
1003         }
1004 
1005         channel->active_cmd = NULL;
1006 
1007         rc = qeth_check_irb_error(card, cdev, irb);
1008         if (rc) {
1009                 /* IO was terminated, free its resources. */
1010                 if (iob)
1011                         qeth_cancel_cmd(iob, rc);
1012                 atomic_set(&channel->irq_pending, 0);
1013                 wake_up(&card->wait_q);
1014                 return;
1015         }
1016 
1017         atomic_set(&channel->irq_pending, 0);
1018 
1019         if (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC))
1020                 channel->state = CH_STATE_STOPPED;
1021 
1022         if (irb->scsw.cmd.fctl & (SCSW_FCTL_HALT_FUNC))
1023                 channel->state = CH_STATE_HALTED;
1024 
1025         if (iob && (irb->scsw.cmd.fctl & (SCSW_FCTL_CLEAR_FUNC |
1026                                           SCSW_FCTL_HALT_FUNC))) {
1027                 qeth_cancel_cmd(iob, -ECANCELED);
1028                 iob = NULL;
1029         }
1030 
1031         cstat = irb->scsw.cmd.cstat;
1032         dstat = irb->scsw.cmd.dstat;
1033 
1034         if ((dstat & DEV_STAT_UNIT_EXCEP) ||
1035             (dstat & DEV_STAT_UNIT_CHECK) ||
1036             (cstat)) {
1037                 if (irb->esw.esw0.erw.cons) {
1038                         dev_warn(&channel->ccwdev->dev,
1039                                 "The qeth device driver failed to recover "
1040                                 "an error on the device\n");
1041                         QETH_DBF_MESSAGE(2, "sense data available on channel %x: cstat %#X dstat %#X\n",
1042                                          CCW_DEVID(channel->ccwdev), cstat,
1043                                          dstat);
1044                         print_hex_dump(KERN_WARNING, "qeth: irb ",
1045                                 DUMP_PREFIX_OFFSET, 16, 1, irb, 32, 1);
1046                         print_hex_dump(KERN_WARNING, "qeth: sense data ",
1047                                 DUMP_PREFIX_OFFSET, 16, 1, irb->ecw, 32, 1);
1048                 }
1049 
1050                 rc = qeth_get_problem(card, cdev, irb);
1051                 if (rc) {
1052                         card->read_or_write_problem = 1;
1053                         if (iob)
1054                                 qeth_cancel_cmd(iob, rc);
1055                         qeth_clear_ipacmd_list(card);
1056                         qeth_schedule_recovery(card);
1057                         goto out;
1058                 }
1059         }
1060 
1061         if (iob) {
1062                 /* sanity check: */
1063                 if (irb->scsw.cmd.count > iob->length) {
1064                         qeth_cancel_cmd(iob, -EIO);
1065                         goto out;
1066                 }
1067                 if (iob->callback)
1068                         iob->callback(card, iob,
1069                                       iob->length - irb->scsw.cmd.count);
1070         }
1071 
1072 out:
1073         wake_up(&card->wait_q);
1074         return;
1075 }
1076 
1077 static void qeth_notify_skbs(struct qeth_qdio_out_q *q,
1078                 struct qeth_qdio_out_buffer *buf,
1079                 enum iucv_tx_notify notification)
1080 {
1081         struct sk_buff *skb;
1082 
1083         skb_queue_walk(&buf->skb_list, skb) {
1084                 QETH_CARD_TEXT_(q->card, 5, "skbn%d", notification);
1085                 QETH_CARD_TEXT_(q->card, 5, "%lx", (long) skb);
1086                 if (skb->protocol == htons(ETH_P_AF_IUCV) && skb->sk)
1087                         iucv_sk(skb->sk)->sk_txnotify(skb, notification);
1088         }
1089 }
1090 
1091 static void qeth_tx_complete_buf(struct qeth_qdio_out_buffer *buf, bool error,
1092                                  int budget)
1093 {
1094         struct qeth_qdio_out_q *queue = buf->q;
1095         struct sk_buff *skb;
1096 
1097         /* release may never happen from within CQ tasklet scope */
1098         WARN_ON_ONCE(atomic_read(&buf->state) == QETH_QDIO_BUF_IN_CQ);
1099 
1100         if (atomic_read(&buf->state) == QETH_QDIO_BUF_PENDING)
1101                 qeth_notify_skbs(queue, buf, TX_NOTIFY_GENERALERROR);
1102 
1103         /* Empty buffer? */
1104         if (buf->next_element_to_fill == 0)
1105                 return;
1106 
1107         QETH_TXQ_STAT_INC(queue, bufs);
1108         QETH_TXQ_STAT_ADD(queue, buf_elements, buf->next_element_to_fill);
1109         while ((skb = __skb_dequeue(&buf->skb_list)) != NULL) {
1110                 unsigned int bytes = qdisc_pkt_len(skb);
1111                 bool is_tso = skb_is_gso(skb);
1112                 unsigned int packets;
1113 
1114                 packets = is_tso ? skb_shinfo(skb)->gso_segs : 1;
1115                 if (error) {
1116                         QETH_TXQ_STAT_ADD(queue, tx_errors, packets);
1117                 } else {
1118                         QETH_TXQ_STAT_ADD(queue, tx_packets, packets);
1119                         QETH_TXQ_STAT_ADD(queue, tx_bytes, bytes);
1120                         if (skb->ip_summed == CHECKSUM_PARTIAL)
1121                                 QETH_TXQ_STAT_ADD(queue, skbs_csum, packets);
1122                         if (skb_is_nonlinear(skb))
1123                                 QETH_TXQ_STAT_INC(queue, skbs_sg);
1124                         if (is_tso) {
1125                                 QETH_TXQ_STAT_INC(queue, skbs_tso);
1126                                 QETH_TXQ_STAT_ADD(queue, tso_bytes, bytes);
1127                         }
1128                 }
1129 
1130                 napi_consume_skb(skb, budget);
1131         }
1132 }
1133 
1134 static void qeth_clear_output_buffer(struct qeth_qdio_out_q *queue,
1135                                      struct qeth_qdio_out_buffer *buf,
1136                                      bool error, int budget)
1137 {
1138         int i;
1139 
1140         /* is PCI flag set on buffer? */
1141         if (buf->buffer->element[0].sflags & SBAL_SFLAGS0_PCI_REQ)
1142                 atomic_dec(&queue->set_pci_flags_count);
1143 
1144         qeth_tx_complete_buf(buf, error, budget);
1145 
1146         for (i = 0; i < queue->max_elements; ++i) {
1147                 if (buf->buffer->element[i].addr && buf->is_header[i])
1148                         kmem_cache_free(qeth_core_header_cache,
1149                                 buf->buffer->element[i].addr);
1150                 buf->is_header[i] = 0;
1151         }
1152 
1153         qeth_scrub_qdio_buffer(buf->buffer, queue->max_elements);
1154         buf->next_element_to_fill = 0;
1155         buf->bytes = 0;
1156         atomic_set(&buf->state, QETH_QDIO_BUF_EMPTY);
1157 }
1158 
1159 static void qeth_drain_output_queue(struct qeth_qdio_out_q *q, bool free)
1160 {
1161         int j;
1162 
1163         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
1164                 if (!q->bufs[j])
1165                         continue;
1166                 qeth_cleanup_handled_pending(q, j, 1);
1167                 qeth_clear_output_buffer(q, q->bufs[j], true, 0);
1168                 if (free) {
1169                         kmem_cache_free(qeth_qdio_outbuf_cache, q->bufs[j]);
1170                         q->bufs[j] = NULL;
1171                 }
1172         }
1173 }
1174 
1175 void qeth_drain_output_queues(struct qeth_card *card)
1176 {
1177         int i;
1178 
1179         QETH_CARD_TEXT(card, 2, "clearqdbf");
1180         /* clear outbound buffers to free skbs */
1181         for (i = 0; i < card->qdio.no_out_queues; ++i) {
1182                 if (card->qdio.out_qs[i])
1183                         qeth_drain_output_queue(card->qdio.out_qs[i], false);
1184         }
1185 }
1186 EXPORT_SYMBOL_GPL(qeth_drain_output_queues);
1187 
1188 static void qeth_free_buffer_pool(struct qeth_card *card)
1189 {
1190         struct qeth_buffer_pool_entry *pool_entry, *tmp;
1191         int i = 0;
1192         list_for_each_entry_safe(pool_entry, tmp,
1193                                  &card->qdio.init_pool.entry_list, init_list){
1194                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i)
1195                         free_page((unsigned long)pool_entry->elements[i]);
1196                 list_del(&pool_entry->init_list);
1197                 kfree(pool_entry);
1198         }
1199 }
1200 
1201 static void qeth_clean_channel(struct qeth_channel *channel)
1202 {
1203         struct ccw_device *cdev = channel->ccwdev;
1204 
1205         QETH_DBF_TEXT(SETUP, 2, "freech");
1206 
1207         spin_lock_irq(get_ccwdev_lock(cdev));
1208         cdev->handler = NULL;
1209         spin_unlock_irq(get_ccwdev_lock(cdev));
1210 }
1211 
1212 static void qeth_setup_channel(struct qeth_channel *channel)
1213 {
1214         struct ccw_device *cdev = channel->ccwdev;
1215 
1216         QETH_DBF_TEXT(SETUP, 2, "setupch");
1217 
1218         channel->state = CH_STATE_DOWN;
1219         atomic_set(&channel->irq_pending, 0);
1220 
1221         spin_lock_irq(get_ccwdev_lock(cdev));
1222         cdev->handler = qeth_irq;
1223         spin_unlock_irq(get_ccwdev_lock(cdev));
1224 }
1225 
1226 static int qeth_osa_set_output_queues(struct qeth_card *card, bool single)
1227 {
1228         unsigned int count = single ? 1 : card->dev->num_tx_queues;
1229         int rc;
1230 
1231         rtnl_lock();
1232         rc = netif_set_real_num_tx_queues(card->dev, count);
1233         rtnl_unlock();
1234 
1235         if (rc)
1236                 return rc;
1237 
1238         if (card->qdio.no_out_queues == count)
1239                 return 0;
1240 
1241         if (atomic_read(&card->qdio.state) != QETH_QDIO_UNINITIALIZED)
1242                 qeth_free_qdio_queues(card);
1243 
1244         if (count == 1)
1245                 dev_info(&card->gdev->dev, "Priority Queueing not supported\n");
1246 
1247         card->qdio.no_out_queues = count;
1248         return 0;
1249 }
1250 
1251 static int qeth_update_from_chp_desc(struct qeth_card *card)
1252 {
1253         struct ccw_device *ccwdev;
1254         struct channel_path_desc_fmt0 *chp_dsc;
1255         int rc = 0;
1256 
1257         QETH_CARD_TEXT(card, 2, "chp_desc");
1258 
1259         ccwdev = card->data.ccwdev;
1260         chp_dsc = ccw_device_get_chp_desc(ccwdev, 0);
1261         if (!chp_dsc)
1262                 return -ENOMEM;
1263 
1264         card->info.func_level = 0x4100 + chp_dsc->desc;
1265 
1266         if (IS_OSD(card) || IS_OSX(card))
1267                 /* CHPP field bit 6 == 1 -> single queue */
1268                 rc = qeth_osa_set_output_queues(card, chp_dsc->chpp & 0x02);
1269 
1270         kfree(chp_dsc);
1271         QETH_CARD_TEXT_(card, 2, "nr:%x", card->qdio.no_out_queues);
1272         QETH_CARD_TEXT_(card, 2, "lvl:%02x", card->info.func_level);
1273         return rc;
1274 }
1275 
1276 static void qeth_init_qdio_info(struct qeth_card *card)
1277 {
1278         QETH_CARD_TEXT(card, 4, "intqdinf");
1279         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
1280         card->qdio.do_prio_queueing = QETH_PRIOQ_DEFAULT;
1281         card->qdio.default_out_queue = QETH_DEFAULT_QUEUE;
1282 
1283         /* inbound */
1284         card->qdio.no_in_queues = 1;
1285         card->qdio.in_buf_size = QETH_IN_BUF_SIZE_DEFAULT;
1286         if (IS_IQD(card))
1287                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_HSDEFAULT;
1288         else
1289                 card->qdio.init_pool.buf_count = QETH_IN_BUF_COUNT_DEFAULT;
1290         card->qdio.in_buf_pool.buf_count = card->qdio.init_pool.buf_count;
1291         INIT_LIST_HEAD(&card->qdio.in_buf_pool.entry_list);
1292         INIT_LIST_HEAD(&card->qdio.init_pool.entry_list);
1293 }
1294 
1295 static void qeth_set_initial_options(struct qeth_card *card)
1296 {
1297         card->options.route4.type = NO_ROUTER;
1298         card->options.route6.type = NO_ROUTER;
1299         card->options.rx_sg_cb = QETH_RX_SG_CB;
1300         card->options.isolation = ISOLATION_MODE_NONE;
1301         card->options.cq = QETH_CQ_DISABLED;
1302         card->options.layer = QETH_DISCIPLINE_UNDETERMINED;
1303 }
1304 
1305 static int qeth_do_start_thread(struct qeth_card *card, unsigned long thread)
1306 {
1307         unsigned long flags;
1308         int rc = 0;
1309 
1310         spin_lock_irqsave(&card->thread_mask_lock, flags);
1311         QETH_CARD_TEXT_(card, 4, "  %02x%02x%02x",
1312                         (u8) card->thread_start_mask,
1313                         (u8) card->thread_allowed_mask,
1314                         (u8) card->thread_running_mask);
1315         rc = (card->thread_start_mask & thread);
1316         spin_unlock_irqrestore(&card->thread_mask_lock, flags);
1317         return rc;
1318 }
1319 
1320 static void qeth_start_kernel_thread(struct work_struct *work)
1321 {
1322         struct task_struct *ts;
1323         struct qeth_card *card = container_of(work, struct qeth_card,
1324                                         kernel_thread_starter);
1325         QETH_CARD_TEXT(card , 2, "strthrd");
1326 
1327         if (card->read.state != CH_STATE_UP &&
1328             card->write.state != CH_STATE_UP)
1329                 return;
1330         if (qeth_do_start_thread(card, QETH_RECOVER_THREAD)) {
1331                 ts = kthread_run(card->discipline->recover, (void *)card,
1332                                 "qeth_recover");
1333                 if (IS_ERR(ts)) {
1334                         qeth_clear_thread_start_bit(card, QETH_RECOVER_THREAD);
1335                         qeth_clear_thread_running_bit(card,
1336                                 QETH_RECOVER_THREAD);
1337                 }
1338         }
1339 }
1340 
1341 static void qeth_buffer_reclaim_work(struct work_struct *);
1342 static void qeth_setup_card(struct qeth_card *card)
1343 {
1344         QETH_CARD_TEXT(card, 2, "setupcrd");
1345 
1346         card->info.type = CARD_RDEV(card)->id.driver_info;
1347         card->state = CARD_STATE_DOWN;
1348         spin_lock_init(&card->lock);
1349         spin_lock_init(&card->thread_mask_lock);
1350         mutex_init(&card->conf_mutex);
1351         mutex_init(&card->discipline_mutex);
1352         INIT_WORK(&card->kernel_thread_starter, qeth_start_kernel_thread);
1353         INIT_LIST_HEAD(&card->cmd_waiter_list);
1354         init_waitqueue_head(&card->wait_q);
1355         qeth_set_initial_options(card);
1356         /* IP address takeover */
1357         INIT_LIST_HEAD(&card->ipato.entries);
1358         qeth_init_qdio_info(card);
1359         INIT_DELAYED_WORK(&card->buffer_reclaim_work, qeth_buffer_reclaim_work);
1360         INIT_WORK(&card->close_dev_work, qeth_close_dev_handler);
1361 }
1362 
1363 static void qeth_core_sl_print(struct seq_file *m, struct service_level *slr)
1364 {
1365         struct qeth_card *card = container_of(slr, struct qeth_card,
1366                                         qeth_service_level);
1367         if (card->info.mcl_level[0])
1368                 seq_printf(m, "qeth: %s firmware level %s\n",
1369                         CARD_BUS_ID(card), card->info.mcl_level);
1370 }
1371 
1372 static struct qeth_card *qeth_alloc_card(struct ccwgroup_device *gdev)
1373 {
1374         struct qeth_card *card;
1375 
1376         QETH_DBF_TEXT(SETUP, 2, "alloccrd");
1377         card = kzalloc(sizeof(*card), GFP_KERNEL);
1378         if (!card)
1379                 goto out;
1380         QETH_DBF_HEX(SETUP, 2, &card, sizeof(void *));
1381 
1382         card->gdev = gdev;
1383         dev_set_drvdata(&gdev->dev, card);
1384         CARD_RDEV(card) = gdev->cdev[0];
1385         CARD_WDEV(card) = gdev->cdev[1];
1386         CARD_DDEV(card) = gdev->cdev[2];
1387 
1388         card->event_wq = alloc_ordered_workqueue("%s_event", 0,
1389                                                  dev_name(&gdev->dev));
1390         if (!card->event_wq)
1391                 goto out_wq;
1392 
1393         card->read_cmd = qeth_alloc_cmd(&card->read, QETH_BUFSIZE, 1, 0);
1394         if (!card->read_cmd)
1395                 goto out_read_cmd;
1396 
1397         qeth_setup_channel(&card->read);
1398         qeth_setup_channel(&card->write);
1399         qeth_setup_channel(&card->data);
1400         card->qeth_service_level.seq_print = qeth_core_sl_print;
1401         register_service_level(&card->qeth_service_level);
1402         return card;
1403 
1404 out_read_cmd:
1405         destroy_workqueue(card->event_wq);
1406 out_wq:
1407         dev_set_drvdata(&gdev->dev, NULL);
1408         kfree(card);
1409 out:
1410         return NULL;
1411 }
1412 
1413 static int qeth_clear_channel(struct qeth_card *card,
1414                               struct qeth_channel *channel)
1415 {
1416         int rc;
1417 
1418         QETH_CARD_TEXT(card, 3, "clearch");
1419         spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
1420         rc = ccw_device_clear(channel->ccwdev, (addr_t)channel->active_cmd);
1421         spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
1422 
1423         if (rc)
1424                 return rc;
1425         rc = wait_event_interruptible_timeout(card->wait_q,
1426                         channel->state == CH_STATE_STOPPED, QETH_TIMEOUT);
1427         if (rc == -ERESTARTSYS)
1428                 return rc;
1429         if (channel->state != CH_STATE_STOPPED)
1430                 return -ETIME;
1431         channel->state = CH_STATE_DOWN;
1432         return 0;
1433 }
1434 
1435 static int qeth_halt_channel(struct qeth_card *card,
1436                              struct qeth_channel *channel)
1437 {
1438         int rc;
1439 
1440         QETH_CARD_TEXT(card, 3, "haltch");
1441         spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
1442         rc = ccw_device_halt(channel->ccwdev, (addr_t)channel->active_cmd);
1443         spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
1444 
1445         if (rc)
1446                 return rc;
1447         rc = wait_event_interruptible_timeout(card->wait_q,
1448                         channel->state == CH_STATE_HALTED, QETH_TIMEOUT);
1449         if (rc == -ERESTARTSYS)
1450                 return rc;
1451         if (channel->state != CH_STATE_HALTED)
1452                 return -ETIME;
1453         return 0;
1454 }
1455 
1456 int qeth_stop_channel(struct qeth_channel *channel)
1457 {
1458         struct ccw_device *cdev = channel->ccwdev;
1459         int rc;
1460 
1461         rc = ccw_device_set_offline(cdev);
1462 
1463         spin_lock_irq(get_ccwdev_lock(cdev));
1464         if (channel->active_cmd) {
1465                 dev_err(&cdev->dev, "Stopped channel while cmd %px was still active\n",
1466                         channel->active_cmd);
1467                 channel->active_cmd = NULL;
1468         }
1469         spin_unlock_irq(get_ccwdev_lock(cdev));
1470 
1471         return rc;
1472 }
1473 EXPORT_SYMBOL_GPL(qeth_stop_channel);
1474 
1475 static int qeth_halt_channels(struct qeth_card *card)
1476 {
1477         int rc1 = 0, rc2 = 0, rc3 = 0;
1478 
1479         QETH_CARD_TEXT(card, 3, "haltchs");
1480         rc1 = qeth_halt_channel(card, &card->read);
1481         rc2 = qeth_halt_channel(card, &card->write);
1482         rc3 = qeth_halt_channel(card, &card->data);
1483         if (rc1)
1484                 return rc1;
1485         if (rc2)
1486                 return rc2;
1487         return rc3;
1488 }
1489 
1490 static int qeth_clear_channels(struct qeth_card *card)
1491 {
1492         int rc1 = 0, rc2 = 0, rc3 = 0;
1493 
1494         QETH_CARD_TEXT(card, 3, "clearchs");
1495         rc1 = qeth_clear_channel(card, &card->read);
1496         rc2 = qeth_clear_channel(card, &card->write);
1497         rc3 = qeth_clear_channel(card, &card->data);
1498         if (rc1)
1499                 return rc1;
1500         if (rc2)
1501                 return rc2;
1502         return rc3;
1503 }
1504 
1505 static int qeth_clear_halt_card(struct qeth_card *card, int halt)
1506 {
1507         int rc = 0;
1508 
1509         QETH_CARD_TEXT(card, 3, "clhacrd");
1510 
1511         if (halt)
1512                 rc = qeth_halt_channels(card);
1513         if (rc)
1514                 return rc;
1515         return qeth_clear_channels(card);
1516 }
1517 
1518 int qeth_qdio_clear_card(struct qeth_card *card, int use_halt)
1519 {
1520         int rc = 0;
1521 
1522         QETH_CARD_TEXT(card, 3, "qdioclr");
1523         switch (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ESTABLISHED,
1524                 QETH_QDIO_CLEANING)) {
1525         case QETH_QDIO_ESTABLISHED:
1526                 if (IS_IQD(card))
1527                         rc = qdio_shutdown(CARD_DDEV(card),
1528                                 QDIO_FLAG_CLEANUP_USING_HALT);
1529                 else
1530                         rc = qdio_shutdown(CARD_DDEV(card),
1531                                 QDIO_FLAG_CLEANUP_USING_CLEAR);
1532                 if (rc)
1533                         QETH_CARD_TEXT_(card, 3, "1err%d", rc);
1534                 atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
1535                 break;
1536         case QETH_QDIO_CLEANING:
1537                 return rc;
1538         default:
1539                 break;
1540         }
1541         rc = qeth_clear_halt_card(card, use_halt);
1542         if (rc)
1543                 QETH_CARD_TEXT_(card, 3, "2err%d", rc);
1544         card->state = CARD_STATE_DOWN;
1545         return rc;
1546 }
1547 EXPORT_SYMBOL_GPL(qeth_qdio_clear_card);
1548 
1549 static enum qeth_discipline_id qeth_vm_detect_layer(struct qeth_card *card)
1550 {
1551         enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1552         struct diag26c_vnic_resp *response = NULL;
1553         struct diag26c_vnic_req *request = NULL;
1554         struct ccw_dev_id id;
1555         char userid[80];
1556         int rc = 0;
1557 
1558         QETH_CARD_TEXT(card, 2, "vmlayer");
1559 
1560         cpcmd("QUERY USERID", userid, sizeof(userid), &rc);
1561         if (rc)
1562                 goto out;
1563 
1564         request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
1565         response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
1566         if (!request || !response) {
1567                 rc = -ENOMEM;
1568                 goto out;
1569         }
1570 
1571         ccw_device_get_id(CARD_RDEV(card), &id);
1572         request->resp_buf_len = sizeof(*response);
1573         request->resp_version = DIAG26C_VERSION6_VM65918;
1574         request->req_format = DIAG26C_VNIC_INFO;
1575         ASCEBC(userid, 8);
1576         memcpy(&request->sys_name, userid, 8);
1577         request->devno = id.devno;
1578 
1579         QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1580         rc = diag26c(request, response, DIAG26C_PORT_VNIC);
1581         QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
1582         if (rc)
1583                 goto out;
1584         QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
1585 
1586         if (request->resp_buf_len < sizeof(*response) ||
1587             response->version != request->resp_version) {
1588                 rc = -EIO;
1589                 goto out;
1590         }
1591 
1592         if (response->protocol == VNIC_INFO_PROT_L2)
1593                 disc = QETH_DISCIPLINE_LAYER2;
1594         else if (response->protocol == VNIC_INFO_PROT_L3)
1595                 disc = QETH_DISCIPLINE_LAYER3;
1596 
1597 out:
1598         kfree(response);
1599         kfree(request);
1600         if (rc)
1601                 QETH_CARD_TEXT_(card, 2, "err%x", rc);
1602         return disc;
1603 }
1604 
1605 /* Determine whether the device requires a specific layer discipline */
1606 static enum qeth_discipline_id qeth_enforce_discipline(struct qeth_card *card)
1607 {
1608         enum qeth_discipline_id disc = QETH_DISCIPLINE_UNDETERMINED;
1609 
1610         if (IS_OSM(card) || IS_OSN(card))
1611                 disc = QETH_DISCIPLINE_LAYER2;
1612         else if (IS_VM_NIC(card))
1613                 disc = IS_IQD(card) ? QETH_DISCIPLINE_LAYER3 :
1614                                       qeth_vm_detect_layer(card);
1615 
1616         switch (disc) {
1617         case QETH_DISCIPLINE_LAYER2:
1618                 QETH_CARD_TEXT(card, 3, "force l2");
1619                 break;
1620         case QETH_DISCIPLINE_LAYER3:
1621                 QETH_CARD_TEXT(card, 3, "force l3");
1622                 break;
1623         default:
1624                 QETH_CARD_TEXT(card, 3, "force no");
1625         }
1626 
1627         return disc;
1628 }
1629 
1630 static void qeth_set_blkt_defaults(struct qeth_card *card)
1631 {
1632         QETH_CARD_TEXT(card, 2, "cfgblkt");
1633 
1634         if (card->info.use_v1_blkt) {
1635                 card->info.blkt.time_total = 0;
1636                 card->info.blkt.inter_packet = 0;
1637                 card->info.blkt.inter_packet_jumbo = 0;
1638         } else {
1639                 card->info.blkt.time_total = 250;
1640                 card->info.blkt.inter_packet = 5;
1641                 card->info.blkt.inter_packet_jumbo = 15;
1642         }
1643 }
1644 
1645 static void qeth_init_tokens(struct qeth_card *card)
1646 {
1647         card->token.issuer_rm_w = 0x00010103UL;
1648         card->token.cm_filter_w = 0x00010108UL;
1649         card->token.cm_connection_w = 0x0001010aUL;
1650         card->token.ulp_filter_w = 0x0001010bUL;
1651         card->token.ulp_connection_w = 0x0001010dUL;
1652 }
1653 
1654 static void qeth_init_func_level(struct qeth_card *card)
1655 {
1656         switch (card->info.type) {
1657         case QETH_CARD_TYPE_IQD:
1658                 card->info.func_level = QETH_IDX_FUNC_LEVEL_IQD;
1659                 break;
1660         case QETH_CARD_TYPE_OSD:
1661         case QETH_CARD_TYPE_OSN:
1662                 card->info.func_level = QETH_IDX_FUNC_LEVEL_OSD;
1663                 break;
1664         default:
1665                 break;
1666         }
1667 }
1668 
1669 static void qeth_idx_finalize_cmd(struct qeth_card *card,
1670                                   struct qeth_cmd_buffer *iob)
1671 {
1672         memcpy(QETH_TRANSPORT_HEADER_SEQ_NO(iob->data), &card->seqno.trans_hdr,
1673                QETH_SEQ_NO_LENGTH);
1674         if (iob->channel == &card->write)
1675                 card->seqno.trans_hdr++;
1676 }
1677 
1678 static int qeth_peer_func_level(int level)
1679 {
1680         if ((level & 0xff) == 8)
1681                 return (level & 0xff) + 0x400;
1682         if (((level >> 8) & 3) == 1)
1683                 return (level & 0xff) + 0x200;
1684         return level;
1685 }
1686 
1687 static void qeth_mpc_finalize_cmd(struct qeth_card *card,
1688                                   struct qeth_cmd_buffer *iob)
1689 {
1690         qeth_idx_finalize_cmd(card, iob);
1691 
1692         memcpy(QETH_PDU_HEADER_SEQ_NO(iob->data),
1693                &card->seqno.pdu_hdr, QETH_SEQ_NO_LENGTH);
1694         card->seqno.pdu_hdr++;
1695         memcpy(QETH_PDU_HEADER_ACK_SEQ_NO(iob->data),
1696                &card->seqno.pdu_hdr_ack, QETH_SEQ_NO_LENGTH);
1697 
1698         iob->callback = qeth_release_buffer_cb;
1699 }
1700 
1701 static struct qeth_cmd_buffer *qeth_mpc_alloc_cmd(struct qeth_card *card,
1702                                                   void *data,
1703                                                   unsigned int data_length)
1704 {
1705         struct qeth_cmd_buffer *iob;
1706 
1707         iob = qeth_alloc_cmd(&card->write, data_length, 1, QETH_TIMEOUT);
1708         if (!iob)
1709                 return NULL;
1710 
1711         memcpy(iob->data, data, data_length);
1712         qeth_setup_ccw(__ccw_from_cmd(iob), CCW_CMD_WRITE, 0, data_length,
1713                        iob->data);
1714         iob->finalize = qeth_mpc_finalize_cmd;
1715         return iob;
1716 }
1717 
1718 /**
1719  * qeth_send_control_data() -   send control command to the card
1720  * @card:                       qeth_card structure pointer
1721  * @iob:                        qeth_cmd_buffer pointer
1722  * @reply_cb:                   callback function pointer
1723  * @cb_card:                    pointer to the qeth_card structure
1724  * @cb_reply:                   pointer to the qeth_reply structure
1725  * @cb_cmd:                     pointer to the original iob for non-IPA
1726  *                              commands, or to the qeth_ipa_cmd structure
1727  *                              for the IPA commands.
1728  * @reply_param:                private pointer passed to the callback
1729  *
1730  * Callback function gets called one or more times, with cb_cmd
1731  * pointing to the response returned by the hardware. Callback
1732  * function must return
1733  *   > 0 if more reply blocks are expected,
1734  *     0 if the last or only reply block is received, and
1735  *   < 0 on error.
1736  * Callback function can get the value of the reply_param pointer from the
1737  * field 'param' of the structure qeth_reply.
1738  */
1739 
1740 static int qeth_send_control_data(struct qeth_card *card,
1741                                   struct qeth_cmd_buffer *iob,
1742                                   int (*reply_cb)(struct qeth_card *cb_card,
1743                                                   struct qeth_reply *cb_reply,
1744                                                   unsigned long cb_cmd),
1745                                   void *reply_param)
1746 {
1747         struct qeth_channel *channel = iob->channel;
1748         struct qeth_reply *reply = &iob->reply;
1749         long timeout = iob->timeout;
1750         int rc;
1751 
1752         QETH_CARD_TEXT(card, 2, "sendctl");
1753 
1754         reply->callback = reply_cb;
1755         reply->param = reply_param;
1756 
1757         timeout = wait_event_interruptible_timeout(card->wait_q,
1758                                                    qeth_trylock_channel(channel),
1759                                                    timeout);
1760         if (timeout <= 0) {
1761                 qeth_put_cmd(iob);
1762                 return (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
1763         }
1764 
1765         if (iob->finalize)
1766                 iob->finalize(card, iob);
1767         QETH_DBF_HEX(CTRL, 2, iob->data, min(iob->length, QETH_DBF_CTRL_LEN));
1768 
1769         qeth_enqueue_cmd(card, iob);
1770 
1771         /* This pairs with iob->callback, and keeps the iob alive after IO: */
1772         qeth_get_cmd(iob);
1773 
1774         QETH_CARD_TEXT(card, 6, "noirqpnd");
1775         spin_lock_irq(get_ccwdev_lock(channel->ccwdev));
1776         rc = ccw_device_start_timeout(channel->ccwdev, __ccw_from_cmd(iob),
1777                                       (addr_t) iob, 0, 0, timeout);
1778         if (!rc)
1779                 channel->active_cmd = iob;
1780         spin_unlock_irq(get_ccwdev_lock(channel->ccwdev));
1781         if (rc) {
1782                 QETH_DBF_MESSAGE(2, "qeth_send_control_data on device %x: ccw_device_start rc = %i\n",
1783                                  CARD_DEVID(card), rc);
1784                 QETH_CARD_TEXT_(card, 2, " err%d", rc);
1785                 qeth_dequeue_cmd(card, iob);
1786                 qeth_put_cmd(iob);
1787                 atomic_set(&channel->irq_pending, 0);
1788                 wake_up(&card->wait_q);
1789                 goto out;
1790         }
1791 
1792         timeout = wait_for_completion_interruptible_timeout(&iob->done,
1793                                                             timeout);
1794         if (timeout <= 0)
1795                 rc = (timeout == -ERESTARTSYS) ? -EINTR : -ETIME;
1796 
1797         qeth_dequeue_cmd(card, iob);
1798 
1799         if (reply_cb) {
1800                 /* Wait until the callback for a late reply has completed: */
1801                 spin_lock_irq(&iob->lock);
1802                 if (rc)
1803                         /* Zap any callback that's still pending: */
1804                         iob->rc = rc;
1805                 spin_unlock_irq(&iob->lock);
1806         }
1807 
1808         if (!rc)
1809                 rc = iob->rc;
1810 
1811 out:
1812         qeth_put_cmd(iob);
1813         return rc;
1814 }
1815 
1816 struct qeth_node_desc {
1817         struct node_descriptor nd1;
1818         struct node_descriptor nd2;
1819         struct node_descriptor nd3;
1820 };
1821 
1822 static void qeth_read_conf_data_cb(struct qeth_card *card,
1823                                    struct qeth_cmd_buffer *iob,
1824                                    unsigned int data_length)
1825 {
1826         struct qeth_node_desc *nd = (struct qeth_node_desc *) iob->data;
1827         int rc = 0;
1828         u8 *tag;
1829 
1830         QETH_CARD_TEXT(card, 2, "cfgunit");
1831 
1832         if (data_length < sizeof(*nd)) {
1833                 rc = -EINVAL;
1834                 goto out;
1835         }
1836 
1837         card->info.is_vm_nic = nd->nd1.plant[0] == _ascebc['V'] &&
1838                                nd->nd1.plant[1] == _ascebc['M'];
1839         tag = (u8 *)&nd->nd1.tag;
1840         card->info.chpid = tag[0];
1841         card->info.unit_addr2 = tag[1];
1842 
1843         tag = (u8 *)&nd->nd2.tag;
1844         card->info.cula = tag[1];
1845 
1846         card->info.use_v1_blkt = nd->nd3.model[0] == 0xF0 &&
1847                                  nd->nd3.model[1] == 0xF0 &&
1848                                  nd->nd3.model[2] >= 0xF1 &&
1849                                  nd->nd3.model[2] <= 0xF4;
1850 
1851 out:
1852         qeth_notify_cmd(iob, rc);
1853         qeth_put_cmd(iob);
1854 }
1855 
1856 static int qeth_read_conf_data(struct qeth_card *card)
1857 {
1858         struct qeth_channel *channel = &card->data;
1859         struct qeth_cmd_buffer *iob;
1860         struct ciw *ciw;
1861 
1862         /* scan for RCD command in extended SenseID data */
1863         ciw = ccw_device_get_ciw(channel->ccwdev, CIW_TYPE_RCD);
1864         if (!ciw || ciw->cmd == 0)
1865                 return -EOPNOTSUPP;
1866         if (ciw->count < sizeof(struct qeth_node_desc))
1867                 return -EINVAL;
1868 
1869         iob = qeth_alloc_cmd(channel, ciw->count, 1, QETH_RCD_TIMEOUT);
1870         if (!iob)
1871                 return -ENOMEM;
1872 
1873         iob->callback = qeth_read_conf_data_cb;
1874         qeth_setup_ccw(__ccw_from_cmd(iob), ciw->cmd, 0, iob->length,
1875                        iob->data);
1876 
1877         return qeth_send_control_data(card, iob, NULL, NULL);
1878 }
1879 
1880 static int qeth_idx_check_activate_response(struct qeth_card *card,
1881                                             struct qeth_channel *channel,
1882                                             struct qeth_cmd_buffer *iob)
1883 {
1884         int rc;
1885 
1886         rc = qeth_check_idx_response(card, iob->data);
1887         if (rc)
1888                 return rc;
1889 
1890         if (QETH_IS_IDX_ACT_POS_REPLY(iob->data))
1891                 return 0;
1892 
1893         /* negative reply: */
1894         QETH_CARD_TEXT_(card, 2, "idxneg%c",
1895                         QETH_IDX_ACT_CAUSE_CODE(iob->data));
1896 
1897         switch (QETH_IDX_ACT_CAUSE_CODE(iob->data)) {
1898         case QETH_IDX_ACT_ERR_EXCL:
1899                 dev_err(&channel->ccwdev->dev,
1900                         "The adapter is used exclusively by another host\n");
1901                 return -EBUSY;
1902         case QETH_IDX_ACT_ERR_AUTH:
1903         case QETH_IDX_ACT_ERR_AUTH_USER:
1904                 dev_err(&channel->ccwdev->dev,
1905                         "Setting the device online failed because of insufficient authorization\n");
1906                 return -EPERM;
1907         default:
1908                 QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: negative reply\n",
1909                                  CCW_DEVID(channel->ccwdev));
1910                 return -EIO;
1911         }
1912 }
1913 
1914 static void qeth_idx_activate_read_channel_cb(struct qeth_card *card,
1915                                               struct qeth_cmd_buffer *iob,
1916                                               unsigned int data_length)
1917 {
1918         struct qeth_channel *channel = iob->channel;
1919         u16 peer_level;
1920         int rc;
1921 
1922         QETH_CARD_TEXT(card, 2, "idxrdcb");
1923 
1924         rc = qeth_idx_check_activate_response(card, channel, iob);
1925         if (rc)
1926                 goto out;
1927 
1928         memcpy(&peer_level, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1929         if (peer_level != qeth_peer_func_level(card->info.func_level)) {
1930                 QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: function level mismatch (sent: %#x, received: %#x)\n",
1931                                  CCW_DEVID(channel->ccwdev),
1932                                  card->info.func_level, peer_level);
1933                 rc = -EINVAL;
1934                 goto out;
1935         }
1936 
1937         memcpy(&card->token.issuer_rm_r,
1938                QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1939                QETH_MPC_TOKEN_LENGTH);
1940         memcpy(&card->info.mcl_level[0],
1941                QETH_IDX_REPLY_LEVEL(iob->data), QETH_MCL_LENGTH);
1942 
1943 out:
1944         qeth_notify_cmd(iob, rc);
1945         qeth_put_cmd(iob);
1946 }
1947 
1948 static void qeth_idx_activate_write_channel_cb(struct qeth_card *card,
1949                                                struct qeth_cmd_buffer *iob,
1950                                                unsigned int data_length)
1951 {
1952         struct qeth_channel *channel = iob->channel;
1953         u16 peer_level;
1954         int rc;
1955 
1956         QETH_CARD_TEXT(card, 2, "idxwrcb");
1957 
1958         rc = qeth_idx_check_activate_response(card, channel, iob);
1959         if (rc)
1960                 goto out;
1961 
1962         memcpy(&peer_level, QETH_IDX_ACT_FUNC_LEVEL(iob->data), 2);
1963         if ((peer_level & ~0x0100) !=
1964             qeth_peer_func_level(card->info.func_level)) {
1965                 QETH_DBF_MESSAGE(2, "IDX_ACTIVATE on channel %x: function level mismatch (sent: %#x, received: %#x)\n",
1966                                  CCW_DEVID(channel->ccwdev),
1967                                  card->info.func_level, peer_level);
1968                 rc = -EINVAL;
1969         }
1970 
1971 out:
1972         qeth_notify_cmd(iob, rc);
1973         qeth_put_cmd(iob);
1974 }
1975 
1976 static void qeth_idx_setup_activate_cmd(struct qeth_card *card,
1977                                         struct qeth_cmd_buffer *iob)
1978 {
1979         u16 addr = (card->info.cula << 8) + card->info.unit_addr2;
1980         u8 port = ((u8)card->dev->dev_port) | 0x80;
1981         struct ccw1 *ccw = __ccw_from_cmd(iob);
1982         struct ccw_dev_id dev_id;
1983 
1984         qeth_setup_ccw(&ccw[0], CCW_CMD_WRITE, CCW_FLAG_CC, IDX_ACTIVATE_SIZE,
1985                        iob->data);
1986         qeth_setup_ccw(&ccw[1], CCW_CMD_READ, 0, iob->length, iob->data);
1987         ccw_device_get_id(CARD_DDEV(card), &dev_id);
1988         iob->finalize = qeth_idx_finalize_cmd;
1989 
1990         memcpy(QETH_IDX_ACT_PNO(iob->data), &port, 1);
1991         memcpy(QETH_IDX_ACT_ISSUER_RM_TOKEN(iob->data),
1992                &card->token.issuer_rm_w, QETH_MPC_TOKEN_LENGTH);
1993         memcpy(QETH_IDX_ACT_FUNC_LEVEL(iob->data),
1994                &card->info.func_level, 2);
1995         memcpy(QETH_IDX_ACT_QDIO_DEV_CUA(iob->data), &dev_id.devno, 2);
1996         memcpy(QETH_IDX_ACT_QDIO_DEV_REALADDR(iob->data), &addr, 2);
1997 }
1998 
1999 static int qeth_idx_activate_read_channel(struct qeth_card *card)
2000 {
2001         struct qeth_channel *channel = &card->read;
2002         struct qeth_cmd_buffer *iob;
2003         int rc;
2004 
2005         QETH_CARD_TEXT(card, 2, "idxread");
2006 
2007         iob = qeth_alloc_cmd(channel, QETH_BUFSIZE, 2, QETH_TIMEOUT);
2008         if (!iob)
2009                 return -ENOMEM;
2010 
2011         memcpy(iob->data, IDX_ACTIVATE_READ, IDX_ACTIVATE_SIZE);
2012         qeth_idx_setup_activate_cmd(card, iob);
2013         iob->callback = qeth_idx_activate_read_channel_cb;
2014 
2015         rc = qeth_send_control_data(card, iob, NULL, NULL);
2016         if (rc)
2017                 return rc;
2018 
2019         channel->state = CH_STATE_UP;
2020         return 0;
2021 }
2022 
2023 static int qeth_idx_activate_write_channel(struct qeth_card *card)
2024 {
2025         struct qeth_channel *channel = &card->write;
2026         struct qeth_cmd_buffer *iob;
2027         int rc;
2028 
2029         QETH_CARD_TEXT(card, 2, "idxwrite");
2030 
2031         iob = qeth_alloc_cmd(channel, QETH_BUFSIZE, 2, QETH_TIMEOUT);
2032         if (!iob)
2033                 return -ENOMEM;
2034 
2035         memcpy(iob->data, IDX_ACTIVATE_WRITE, IDX_ACTIVATE_SIZE);
2036         qeth_idx_setup_activate_cmd(card, iob);
2037         iob->callback = qeth_idx_activate_write_channel_cb;
2038 
2039         rc = qeth_send_control_data(card, iob, NULL, NULL);
2040         if (rc)
2041                 return rc;
2042 
2043         channel->state = CH_STATE_UP;
2044         return 0;
2045 }
2046 
2047 static int qeth_cm_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2048                 unsigned long data)
2049 {
2050         struct qeth_cmd_buffer *iob;
2051 
2052         QETH_CARD_TEXT(card, 2, "cmenblcb");
2053 
2054         iob = (struct qeth_cmd_buffer *) data;
2055         memcpy(&card->token.cm_filter_r,
2056                QETH_CM_ENABLE_RESP_FILTER_TOKEN(iob->data),
2057                QETH_MPC_TOKEN_LENGTH);
2058         return 0;
2059 }
2060 
2061 static int qeth_cm_enable(struct qeth_card *card)
2062 {
2063         struct qeth_cmd_buffer *iob;
2064 
2065         QETH_CARD_TEXT(card, 2, "cmenable");
2066 
2067         iob = qeth_mpc_alloc_cmd(card, CM_ENABLE, CM_ENABLE_SIZE);
2068         if (!iob)
2069                 return -ENOMEM;
2070 
2071         memcpy(QETH_CM_ENABLE_ISSUER_RM_TOKEN(iob->data),
2072                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2073         memcpy(QETH_CM_ENABLE_FILTER_TOKEN(iob->data),
2074                &card->token.cm_filter_w, QETH_MPC_TOKEN_LENGTH);
2075 
2076         return qeth_send_control_data(card, iob, qeth_cm_enable_cb, NULL);
2077 }
2078 
2079 static int qeth_cm_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2080                 unsigned long data)
2081 {
2082         struct qeth_cmd_buffer *iob;
2083 
2084         QETH_CARD_TEXT(card, 2, "cmsetpcb");
2085 
2086         iob = (struct qeth_cmd_buffer *) data;
2087         memcpy(&card->token.cm_connection_r,
2088                QETH_CM_SETUP_RESP_DEST_ADDR(iob->data),
2089                QETH_MPC_TOKEN_LENGTH);
2090         return 0;
2091 }
2092 
2093 static int qeth_cm_setup(struct qeth_card *card)
2094 {
2095         struct qeth_cmd_buffer *iob;
2096 
2097         QETH_CARD_TEXT(card, 2, "cmsetup");
2098 
2099         iob = qeth_mpc_alloc_cmd(card, CM_SETUP, CM_SETUP_SIZE);
2100         if (!iob)
2101                 return -ENOMEM;
2102 
2103         memcpy(QETH_CM_SETUP_DEST_ADDR(iob->data),
2104                &card->token.issuer_rm_r, QETH_MPC_TOKEN_LENGTH);
2105         memcpy(QETH_CM_SETUP_CONNECTION_TOKEN(iob->data),
2106                &card->token.cm_connection_w, QETH_MPC_TOKEN_LENGTH);
2107         memcpy(QETH_CM_SETUP_FILTER_TOKEN(iob->data),
2108                &card->token.cm_filter_r, QETH_MPC_TOKEN_LENGTH);
2109         return qeth_send_control_data(card, iob, qeth_cm_setup_cb, NULL);
2110 }
2111 
2112 static int qeth_update_max_mtu(struct qeth_card *card, unsigned int max_mtu)
2113 {
2114         struct net_device *dev = card->dev;
2115         unsigned int new_mtu;
2116 
2117         if (!max_mtu) {
2118                 /* IQD needs accurate max MTU to set up its RX buffers: */
2119                 if (IS_IQD(card))
2120                         return -EINVAL;
2121                 /* tolerate quirky HW: */
2122                 max_mtu = ETH_MAX_MTU;
2123         }
2124 
2125         rtnl_lock();
2126         if (IS_IQD(card)) {
2127                 /* move any device with default MTU to new max MTU: */
2128                 new_mtu = (dev->mtu == dev->max_mtu) ? max_mtu : dev->mtu;
2129 
2130                 /* adjust RX buffer size to new max MTU: */
2131                 card->qdio.in_buf_size = max_mtu + 2 * PAGE_SIZE;
2132                 if (dev->max_mtu && dev->max_mtu != max_mtu)
2133                         qeth_free_qdio_queues(card);
2134         } else {
2135                 if (dev->mtu)
2136                         new_mtu = dev->mtu;
2137                 /* default MTUs for first setup: */
2138                 else if (IS_LAYER2(card))
2139                         new_mtu = ETH_DATA_LEN;
2140                 else
2141                         new_mtu = ETH_DATA_LEN - 8; /* allow for LLC + SNAP */
2142         }
2143 
2144         dev->max_mtu = max_mtu;
2145         dev->mtu = min(new_mtu, max_mtu);
2146         rtnl_unlock();
2147         return 0;
2148 }
2149 
2150 static int qeth_get_mtu_outof_framesize(int framesize)
2151 {
2152         switch (framesize) {
2153         case 0x4000:
2154                 return 8192;
2155         case 0x6000:
2156                 return 16384;
2157         case 0xa000:
2158                 return 32768;
2159         case 0xffff:
2160                 return 57344;
2161         default:
2162                 return 0;
2163         }
2164 }
2165 
2166 static int qeth_ulp_enable_cb(struct qeth_card *card, struct qeth_reply *reply,
2167                 unsigned long data)
2168 {
2169         __u16 mtu, framesize;
2170         __u16 len;
2171         __u8 link_type;
2172         struct qeth_cmd_buffer *iob;
2173 
2174         QETH_CARD_TEXT(card, 2, "ulpenacb");
2175 
2176         iob = (struct qeth_cmd_buffer *) data;
2177         memcpy(&card->token.ulp_filter_r,
2178                QETH_ULP_ENABLE_RESP_FILTER_TOKEN(iob->data),
2179                QETH_MPC_TOKEN_LENGTH);
2180         if (IS_IQD(card)) {
2181                 memcpy(&framesize, QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data), 2);
2182                 mtu = qeth_get_mtu_outof_framesize(framesize);
2183         } else {
2184                 mtu = *(__u16 *)QETH_ULP_ENABLE_RESP_MAX_MTU(iob->data);
2185         }
2186         *(u16 *)reply->param = mtu;
2187 
2188         memcpy(&len, QETH_ULP_ENABLE_RESP_DIFINFO_LEN(iob->data), 2);
2189         if (len >= QETH_MPC_DIFINFO_LEN_INDICATES_LINK_TYPE) {
2190                 memcpy(&link_type,
2191                        QETH_ULP_ENABLE_RESP_LINK_TYPE(iob->data), 1);
2192                 card->info.link_type = link_type;
2193         } else
2194                 card->info.link_type = 0;
2195         QETH_CARD_TEXT_(card, 2, "link%d", card->info.link_type);
2196         return 0;
2197 }
2198 
2199 static u8 qeth_mpc_select_prot_type(struct qeth_card *card)
2200 {
2201         if (IS_OSN(card))
2202                 return QETH_PROT_OSN2;
2203         return IS_LAYER2(card) ? QETH_PROT_LAYER2 : QETH_PROT_TCPIP;
2204 }
2205 
2206 static int qeth_ulp_enable(struct qeth_card *card)
2207 {
2208         u8 prot_type = qeth_mpc_select_prot_type(card);
2209         struct qeth_cmd_buffer *iob;
2210         u16 max_mtu;
2211         int rc;
2212 
2213         QETH_CARD_TEXT(card, 2, "ulpenabl");
2214 
2215         iob = qeth_mpc_alloc_cmd(card, ULP_ENABLE, ULP_ENABLE_SIZE);
2216         if (!iob)
2217                 return -ENOMEM;
2218 
2219         *(QETH_ULP_ENABLE_LINKNUM(iob->data)) = (u8) card->dev->dev_port;
2220         memcpy(QETH_ULP_ENABLE_PROT_TYPE(iob->data), &prot_type, 1);
2221         memcpy(QETH_ULP_ENABLE_DEST_ADDR(iob->data),
2222                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2223         memcpy(QETH_ULP_ENABLE_FILTER_TOKEN(iob->data),
2224                &card->token.ulp_filter_w, QETH_MPC_TOKEN_LENGTH);
2225         rc = qeth_send_control_data(card, iob, qeth_ulp_enable_cb, &max_mtu);
2226         if (rc)
2227                 return rc;
2228         return qeth_update_max_mtu(card, max_mtu);
2229 }
2230 
2231 static int qeth_ulp_setup_cb(struct qeth_card *card, struct qeth_reply *reply,
2232                 unsigned long data)
2233 {
2234         struct qeth_cmd_buffer *iob;
2235 
2236         QETH_CARD_TEXT(card, 2, "ulpstpcb");
2237 
2238         iob = (struct qeth_cmd_buffer *) data;
2239         memcpy(&card->token.ulp_connection_r,
2240                QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2241                QETH_MPC_TOKEN_LENGTH);
2242         if (!strncmp("00S", QETH_ULP_SETUP_RESP_CONNECTION_TOKEN(iob->data),
2243                      3)) {
2244                 QETH_CARD_TEXT(card, 2, "olmlimit");
2245                 dev_err(&card->gdev->dev, "A connection could not be "
2246                         "established because of an OLM limit\n");
2247                 return -EMLINK;
2248         }
2249         return 0;
2250 }
2251 
2252 static int qeth_ulp_setup(struct qeth_card *card)
2253 {
2254         __u16 temp;
2255         struct qeth_cmd_buffer *iob;
2256         struct ccw_dev_id dev_id;
2257 
2258         QETH_CARD_TEXT(card, 2, "ulpsetup");
2259 
2260         iob = qeth_mpc_alloc_cmd(card, ULP_SETUP, ULP_SETUP_SIZE);
2261         if (!iob)
2262                 return -ENOMEM;
2263 
2264         memcpy(QETH_ULP_SETUP_DEST_ADDR(iob->data),
2265                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2266         memcpy(QETH_ULP_SETUP_CONNECTION_TOKEN(iob->data),
2267                &card->token.ulp_connection_w, QETH_MPC_TOKEN_LENGTH);
2268         memcpy(QETH_ULP_SETUP_FILTER_TOKEN(iob->data),
2269                &card->token.ulp_filter_r, QETH_MPC_TOKEN_LENGTH);
2270 
2271         ccw_device_get_id(CARD_DDEV(card), &dev_id);
2272         memcpy(QETH_ULP_SETUP_CUA(iob->data), &dev_id.devno, 2);
2273         temp = (card->info.cula << 8) + card->info.unit_addr2;
2274         memcpy(QETH_ULP_SETUP_REAL_DEVADDR(iob->data), &temp, 2);
2275         return qeth_send_control_data(card, iob, qeth_ulp_setup_cb, NULL);
2276 }
2277 
2278 static int qeth_init_qdio_out_buf(struct qeth_qdio_out_q *q, int bidx)
2279 {
2280         struct qeth_qdio_out_buffer *newbuf;
2281 
2282         newbuf = kmem_cache_zalloc(qeth_qdio_outbuf_cache, GFP_ATOMIC);
2283         if (!newbuf)
2284                 return -ENOMEM;
2285 
2286         newbuf->buffer = q->qdio_bufs[bidx];
2287         skb_queue_head_init(&newbuf->skb_list);
2288         lockdep_set_class(&newbuf->skb_list.lock, &qdio_out_skb_queue_key);
2289         newbuf->q = q;
2290         newbuf->next_pending = q->bufs[bidx];
2291         atomic_set(&newbuf->state, QETH_QDIO_BUF_EMPTY);
2292         q->bufs[bidx] = newbuf;
2293         return 0;
2294 }
2295 
2296 static void qeth_free_output_queue(struct qeth_qdio_out_q *q)
2297 {
2298         if (!q)
2299                 return;
2300 
2301         qeth_drain_output_queue(q, true);
2302         qdio_free_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2303         kfree(q);
2304 }
2305 
2306 static struct qeth_qdio_out_q *qeth_alloc_output_queue(void)
2307 {
2308         struct qeth_qdio_out_q *q = kzalloc(sizeof(*q), GFP_KERNEL);
2309 
2310         if (!q)
2311                 return NULL;
2312 
2313         if (qdio_alloc_buffers(q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q)) {
2314                 kfree(q);
2315                 return NULL;
2316         }
2317         return q;
2318 }
2319 
2320 static void qeth_tx_completion_timer(struct timer_list *timer)
2321 {
2322         struct qeth_qdio_out_q *queue = from_timer(queue, timer, timer);
2323 
2324         napi_schedule(&queue->napi);
2325         QETH_TXQ_STAT_INC(queue, completion_timer);
2326 }
2327 
2328 static int qeth_alloc_qdio_queues(struct qeth_card *card)
2329 {
2330         int i, j;
2331 
2332         QETH_CARD_TEXT(card, 2, "allcqdbf");
2333 
2334         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED,
2335                 QETH_QDIO_ALLOCATED) != QETH_QDIO_UNINITIALIZED)
2336                 return 0;
2337 
2338         QETH_CARD_TEXT(card, 2, "inq");
2339         card->qdio.in_q = qeth_alloc_qdio_queue();
2340         if (!card->qdio.in_q)
2341                 goto out_nomem;
2342 
2343         /* inbound buffer pool */
2344         if (qeth_alloc_buffer_pool(card))
2345                 goto out_freeinq;
2346 
2347         /* outbound */
2348         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2349                 struct qeth_qdio_out_q *queue;
2350 
2351                 queue = qeth_alloc_output_queue();
2352                 if (!queue)
2353                         goto out_freeoutq;
2354                 QETH_CARD_TEXT_(card, 2, "outq %i", i);
2355                 QETH_CARD_HEX(card, 2, &queue, sizeof(void *));
2356                 card->qdio.out_qs[i] = queue;
2357                 queue->card = card;
2358                 queue->queue_no = i;
2359                 timer_setup(&queue->timer, qeth_tx_completion_timer, 0);
2360 
2361                 /* give outbound qeth_qdio_buffers their qdio_buffers */
2362                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2363                         WARN_ON(queue->bufs[j]);
2364                         if (qeth_init_qdio_out_buf(queue, j))
2365                                 goto out_freeoutqbufs;
2366                 }
2367         }
2368 
2369         /* completion */
2370         if (qeth_alloc_cq(card))
2371                 goto out_freeoutq;
2372 
2373         return 0;
2374 
2375 out_freeoutqbufs:
2376         while (j > 0) {
2377                 --j;
2378                 kmem_cache_free(qeth_qdio_outbuf_cache,
2379                                 card->qdio.out_qs[i]->bufs[j]);
2380                 card->qdio.out_qs[i]->bufs[j] = NULL;
2381         }
2382 out_freeoutq:
2383         while (i > 0) {
2384                 qeth_free_output_queue(card->qdio.out_qs[--i]);
2385                 card->qdio.out_qs[i] = NULL;
2386         }
2387         qeth_free_buffer_pool(card);
2388 out_freeinq:
2389         qeth_free_qdio_queue(card->qdio.in_q);
2390         card->qdio.in_q = NULL;
2391 out_nomem:
2392         atomic_set(&card->qdio.state, QETH_QDIO_UNINITIALIZED);
2393         return -ENOMEM;
2394 }
2395 
2396 static void qeth_free_qdio_queues(struct qeth_card *card)
2397 {
2398         int i, j;
2399 
2400         if (atomic_xchg(&card->qdio.state, QETH_QDIO_UNINITIALIZED) ==
2401                 QETH_QDIO_UNINITIALIZED)
2402                 return;
2403 
2404         qeth_free_cq(card);
2405         cancel_delayed_work_sync(&card->buffer_reclaim_work);
2406         for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; ++j) {
2407                 if (card->qdio.in_q->bufs[j].rx_skb)
2408                         dev_kfree_skb_any(card->qdio.in_q->bufs[j].rx_skb);
2409         }
2410         qeth_free_qdio_queue(card->qdio.in_q);
2411         card->qdio.in_q = NULL;
2412         /* inbound buffer pool */
2413         qeth_free_buffer_pool(card);
2414         /* free outbound qdio_qs */
2415         for (i = 0; i < card->qdio.no_out_queues; i++) {
2416                 qeth_free_output_queue(card->qdio.out_qs[i]);
2417                 card->qdio.out_qs[i] = NULL;
2418         }
2419 }
2420 
2421 static void qeth_create_qib_param_field(struct qeth_card *card,
2422                 char *param_field)
2423 {
2424 
2425         param_field[0] = _ascebc['P'];
2426         param_field[1] = _ascebc['C'];
2427         param_field[2] = _ascebc['I'];
2428         param_field[3] = _ascebc['T'];
2429         *((unsigned int *) (&param_field[4])) = QETH_PCI_THRESHOLD_A(card);
2430         *((unsigned int *) (&param_field[8])) = QETH_PCI_THRESHOLD_B(card);
2431         *((unsigned int *) (&param_field[12])) = QETH_PCI_TIMER_VALUE(card);
2432 }
2433 
2434 static void qeth_create_qib_param_field_blkt(struct qeth_card *card,
2435                 char *param_field)
2436 {
2437         param_field[16] = _ascebc['B'];
2438         param_field[17] = _ascebc['L'];
2439         param_field[18] = _ascebc['K'];
2440         param_field[19] = _ascebc['T'];
2441         *((unsigned int *) (&param_field[20])) = card->info.blkt.time_total;
2442         *((unsigned int *) (&param_field[24])) = card->info.blkt.inter_packet;
2443         *((unsigned int *) (&param_field[28])) =
2444                 card->info.blkt.inter_packet_jumbo;
2445 }
2446 
2447 static int qeth_qdio_activate(struct qeth_card *card)
2448 {
2449         QETH_CARD_TEXT(card, 3, "qdioact");
2450         return qdio_activate(CARD_DDEV(card));
2451 }
2452 
2453 static int qeth_dm_act(struct qeth_card *card)
2454 {
2455         struct qeth_cmd_buffer *iob;
2456 
2457         QETH_CARD_TEXT(card, 2, "dmact");
2458 
2459         iob = qeth_mpc_alloc_cmd(card, DM_ACT, DM_ACT_SIZE);
2460         if (!iob)
2461                 return -ENOMEM;
2462 
2463         memcpy(QETH_DM_ACT_DEST_ADDR(iob->data),
2464                &card->token.cm_connection_r, QETH_MPC_TOKEN_LENGTH);
2465         memcpy(QETH_DM_ACT_CONNECTION_TOKEN(iob->data),
2466                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2467         return qeth_send_control_data(card, iob, NULL, NULL);
2468 }
2469 
2470 static int qeth_mpc_initialize(struct qeth_card *card)
2471 {
2472         int rc;
2473 
2474         QETH_CARD_TEXT(card, 2, "mpcinit");
2475 
2476         rc = qeth_issue_next_read(card);
2477         if (rc) {
2478                 QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2479                 return rc;
2480         }
2481         rc = qeth_cm_enable(card);
2482         if (rc) {
2483                 QETH_CARD_TEXT_(card, 2, "2err%d", rc);
2484                 return rc;
2485         }
2486         rc = qeth_cm_setup(card);
2487         if (rc) {
2488                 QETH_CARD_TEXT_(card, 2, "3err%d", rc);
2489                 return rc;
2490         }
2491         rc = qeth_ulp_enable(card);
2492         if (rc) {
2493                 QETH_CARD_TEXT_(card, 2, "4err%d", rc);
2494                 return rc;
2495         }
2496         rc = qeth_ulp_setup(card);
2497         if (rc) {
2498                 QETH_CARD_TEXT_(card, 2, "5err%d", rc);
2499                 return rc;
2500         }
2501         rc = qeth_alloc_qdio_queues(card);
2502         if (rc) {
2503                 QETH_CARD_TEXT_(card, 2, "5err%d", rc);
2504                 return rc;
2505         }
2506         rc = qeth_qdio_establish(card);
2507         if (rc) {
2508                 QETH_CARD_TEXT_(card, 2, "6err%d", rc);
2509                 qeth_free_qdio_queues(card);
2510                 return rc;
2511         }
2512         rc = qeth_qdio_activate(card);
2513         if (rc) {
2514                 QETH_CARD_TEXT_(card, 2, "7err%d", rc);
2515                 return rc;
2516         }
2517         rc = qeth_dm_act(card);
2518         if (rc) {
2519                 QETH_CARD_TEXT_(card, 2, "8err%d", rc);
2520                 return rc;
2521         }
2522 
2523         return 0;
2524 }
2525 
2526 void qeth_print_status_message(struct qeth_card *card)
2527 {
2528         switch (card->info.type) {
2529         case QETH_CARD_TYPE_OSD:
2530         case QETH_CARD_TYPE_OSM:
2531         case QETH_CARD_TYPE_OSX:
2532                 /* VM will use a non-zero first character
2533                  * to indicate a HiperSockets like reporting
2534                  * of the level OSA sets the first character to zero
2535                  * */
2536                 if (!card->info.mcl_level[0]) {
2537                         sprintf(card->info.mcl_level, "%02x%02x",
2538                                 card->info.mcl_level[2],
2539                                 card->info.mcl_level[3]);
2540                         break;
2541                 }
2542                 /* fallthrough */
2543         case QETH_CARD_TYPE_IQD:
2544                 if (IS_VM_NIC(card) || (card->info.mcl_level[0] & 0x80)) {
2545                         card->info.mcl_level[0] = (char) _ebcasc[(__u8)
2546                                 card->info.mcl_level[0]];
2547                         card->info.mcl_level[1] = (char) _ebcasc[(__u8)
2548                                 card->info.mcl_level[1]];
2549                         card->info.mcl_level[2] = (char) _ebcasc[(__u8)
2550                                 card->info.mcl_level[2]];
2551                         card->info.mcl_level[3] = (char) _ebcasc[(__u8)
2552                                 card->info.mcl_level[3]];
2553                         card->info.mcl_level[QETH_MCL_LENGTH] = 0;
2554                 }
2555                 break;
2556         default:
2557                 memset(&card->info.mcl_level[0], 0, QETH_MCL_LENGTH + 1);
2558         }
2559         dev_info(&card->gdev->dev,
2560                  "Device is a%s card%s%s%s\nwith link type %s.\n",
2561                  qeth_get_cardname(card),
2562                  (card->info.mcl_level[0]) ? " (level: " : "",
2563                  (card->info.mcl_level[0]) ? card->info.mcl_level : "",
2564                  (card->info.mcl_level[0]) ? ")" : "",
2565                  qeth_get_cardname_short(card));
2566 }
2567 EXPORT_SYMBOL_GPL(qeth_print_status_message);
2568 
2569 static void qeth_initialize_working_pool_list(struct qeth_card *card)
2570 {
2571         struct qeth_buffer_pool_entry *entry;
2572 
2573         QETH_CARD_TEXT(card, 5, "inwrklst");
2574 
2575         list_for_each_entry(entry,
2576                             &card->qdio.init_pool.entry_list, init_list) {
2577                 qeth_put_buffer_pool_entry(card, entry);
2578         }
2579 }
2580 
2581 static struct qeth_buffer_pool_entry *qeth_find_free_buffer_pool_entry(
2582                                         struct qeth_card *card)
2583 {
2584         struct list_head *plh;
2585         struct qeth_buffer_pool_entry *entry;
2586         int i, free;
2587         struct page *page;
2588 
2589         if (list_empty(&card->qdio.in_buf_pool.entry_list))
2590                 return NULL;
2591 
2592         list_for_each(plh, &card->qdio.in_buf_pool.entry_list) {
2593                 entry = list_entry(plh, struct qeth_buffer_pool_entry, list);
2594                 free = 1;
2595                 for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2596                         if (page_count(virt_to_page(entry->elements[i])) > 1) {
2597                                 free = 0;
2598                                 break;
2599                         }
2600                 }
2601                 if (free) {
2602                         list_del_init(&entry->list);
2603                         return entry;
2604                 }
2605         }
2606 
2607         /* no free buffer in pool so take first one and swap pages */
2608         entry = list_entry(card->qdio.in_buf_pool.entry_list.next,
2609                         struct qeth_buffer_pool_entry, list);
2610         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2611                 if (page_count(virt_to_page(entry->elements[i])) > 1) {
2612                         page = alloc_page(GFP_ATOMIC);
2613                         if (!page) {
2614                                 return NULL;
2615                         } else {
2616                                 free_page((unsigned long)entry->elements[i]);
2617                                 entry->elements[i] = page_address(page);
2618                                 QETH_CARD_STAT_INC(card, rx_sg_alloc_page);
2619                         }
2620                 }
2621         }
2622         list_del_init(&entry->list);
2623         return entry;
2624 }
2625 
2626 static int qeth_init_input_buffer(struct qeth_card *card,
2627                 struct qeth_qdio_buffer *buf)
2628 {
2629         struct qeth_buffer_pool_entry *pool_entry;
2630         int i;
2631 
2632         if ((card->options.cq == QETH_CQ_ENABLED) && (!buf->rx_skb)) {
2633                 buf->rx_skb = netdev_alloc_skb(card->dev,
2634                                                QETH_RX_PULL_LEN + ETH_HLEN);
2635                 if (!buf->rx_skb)
2636                         return -ENOMEM;
2637         }
2638 
2639         pool_entry = qeth_find_free_buffer_pool_entry(card);
2640         if (!pool_entry)
2641                 return -ENOBUFS;
2642 
2643         /*
2644          * since the buffer is accessed only from the input_tasklet
2645          * there shouldn't be a need to synchronize; also, since we use
2646          * the QETH_IN_BUF_REQUEUE_THRESHOLD we should never run  out off
2647          * buffers
2648          */
2649 
2650         buf->pool_entry = pool_entry;
2651         for (i = 0; i < QETH_MAX_BUFFER_ELEMENTS(card); ++i) {
2652                 buf->buffer->element[i].length = PAGE_SIZE;
2653                 buf->buffer->element[i].addr =  pool_entry->elements[i];
2654                 if (i == QETH_MAX_BUFFER_ELEMENTS(card) - 1)
2655                         buf->buffer->element[i].eflags = SBAL_EFLAGS_LAST_ENTRY;
2656                 else
2657                         buf->buffer->element[i].eflags = 0;
2658                 buf->buffer->element[i].sflags = 0;
2659         }
2660         return 0;
2661 }
2662 
2663 int qeth_init_qdio_queues(struct qeth_card *card)
2664 {
2665         unsigned int i;
2666         int rc;
2667 
2668         QETH_CARD_TEXT(card, 2, "initqdqs");
2669 
2670         /* inbound queue */
2671         qdio_reset_buffers(card->qdio.in_q->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2672         memset(&card->rx, 0, sizeof(struct qeth_rx));
2673 
2674         qeth_initialize_working_pool_list(card);
2675         /*give only as many buffers to hardware as we have buffer pool entries*/
2676         for (i = 0; i < card->qdio.in_buf_pool.buf_count - 1; i++) {
2677                 rc = qeth_init_input_buffer(card, &card->qdio.in_q->bufs[i]);
2678                 if (rc)
2679                         return rc;
2680         }
2681 
2682         card->qdio.in_q->next_buf_to_init =
2683                 card->qdio.in_buf_pool.buf_count - 1;
2684         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0, 0,
2685                      card->qdio.in_buf_pool.buf_count - 1);
2686         if (rc) {
2687                 QETH_CARD_TEXT_(card, 2, "1err%d", rc);
2688                 return rc;
2689         }
2690 
2691         /* completion */
2692         rc = qeth_cq_init(card);
2693         if (rc) {
2694                 return rc;
2695         }
2696 
2697         /* outbound queue */
2698         for (i = 0; i < card->qdio.no_out_queues; ++i) {
2699                 struct qeth_qdio_out_q *queue = card->qdio.out_qs[i];
2700 
2701                 qdio_reset_buffers(queue->qdio_bufs, QDIO_MAX_BUFFERS_PER_Q);
2702                 queue->max_elements = QETH_MAX_BUFFER_ELEMENTS(card);
2703                 queue->next_buf_to_fill = 0;
2704                 queue->do_pack = 0;
2705                 queue->prev_hdr = NULL;
2706                 queue->bulk_start = 0;
2707                 atomic_set(&queue->used_buffers, 0);
2708                 atomic_set(&queue->set_pci_flags_count, 0);
2709                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
2710                 netdev_tx_reset_queue(netdev_get_tx_queue(card->dev, i));
2711         }
2712         return 0;
2713 }
2714 EXPORT_SYMBOL_GPL(qeth_init_qdio_queues);
2715 
2716 static void qeth_ipa_finalize_cmd(struct qeth_card *card,
2717                                   struct qeth_cmd_buffer *iob)
2718 {
2719         qeth_mpc_finalize_cmd(card, iob);
2720 
2721         /* override with IPA-specific values: */
2722         __ipa_cmd(iob)->hdr.seqno = card->seqno.ipa++;
2723 }
2724 
2725 void qeth_prepare_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2726                           u16 cmd_length)
2727 {
2728         u8 prot_type = qeth_mpc_select_prot_type(card);
2729         u16 total_length = iob->length;
2730 
2731         qeth_setup_ccw(__ccw_from_cmd(iob), CCW_CMD_WRITE, 0, total_length,
2732                        iob->data);
2733         iob->finalize = qeth_ipa_finalize_cmd;
2734 
2735         memcpy(iob->data, IPA_PDU_HEADER, IPA_PDU_HEADER_SIZE);
2736         memcpy(QETH_IPA_PDU_LEN_TOTAL(iob->data), &total_length, 2);
2737         memcpy(QETH_IPA_CMD_PROT_TYPE(iob->data), &prot_type, 1);
2738         memcpy(QETH_IPA_PDU_LEN_PDU1(iob->data), &cmd_length, 2);
2739         memcpy(QETH_IPA_PDU_LEN_PDU2(iob->data), &cmd_length, 2);
2740         memcpy(QETH_IPA_CMD_DEST_ADDR(iob->data),
2741                &card->token.ulp_connection_r, QETH_MPC_TOKEN_LENGTH);
2742         memcpy(QETH_IPA_PDU_LEN_PDU3(iob->data), &cmd_length, 2);
2743 }
2744 EXPORT_SYMBOL_GPL(qeth_prepare_ipa_cmd);
2745 
2746 struct qeth_cmd_buffer *qeth_ipa_alloc_cmd(struct qeth_card *card,
2747                                            enum qeth_ipa_cmds cmd_code,
2748                                            enum qeth_prot_versions prot,
2749                                            unsigned int data_length)
2750 {
2751         enum qeth_link_types link_type = card->info.link_type;
2752         struct qeth_cmd_buffer *iob;
2753         struct qeth_ipacmd_hdr *hdr;
2754 
2755         data_length += offsetof(struct qeth_ipa_cmd, data);
2756         iob = qeth_alloc_cmd(&card->write, IPA_PDU_HEADER_SIZE + data_length, 1,
2757                              QETH_IPA_TIMEOUT);
2758         if (!iob)
2759                 return NULL;
2760 
2761         qeth_prepare_ipa_cmd(card, iob, data_length);
2762 
2763         hdr = &__ipa_cmd(iob)->hdr;
2764         hdr->command = cmd_code;
2765         hdr->initiator = IPA_CMD_INITIATOR_HOST;
2766         /* hdr->seqno is set by qeth_send_control_data() */
2767         hdr->adapter_type = (link_type == QETH_LINK_TYPE_HSTR) ? 2 : 1;
2768         hdr->rel_adapter_no = (u8) card->dev->dev_port;
2769         hdr->prim_version_no = IS_LAYER2(card) ? 2 : 1;
2770         hdr->param_count = 1;
2771         hdr->prot_version = prot;
2772         return iob;
2773 }
2774 EXPORT_SYMBOL_GPL(qeth_ipa_alloc_cmd);
2775 
2776 static int qeth_send_ipa_cmd_cb(struct qeth_card *card,
2777                                 struct qeth_reply *reply, unsigned long data)
2778 {
2779         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
2780 
2781         return (cmd->hdr.return_code) ? -EIO : 0;
2782 }
2783 
2784 /**
2785  * qeth_send_ipa_cmd() - send an IPA command
2786  *
2787  * See qeth_send_control_data() for explanation of the arguments.
2788  */
2789 
2790 int qeth_send_ipa_cmd(struct qeth_card *card, struct qeth_cmd_buffer *iob,
2791                 int (*reply_cb)(struct qeth_card *, struct qeth_reply*,
2792                         unsigned long),
2793                 void *reply_param)
2794 {
2795         int rc;
2796 
2797         QETH_CARD_TEXT(card, 4, "sendipa");
2798 
2799         if (card->read_or_write_problem) {
2800                 qeth_put_cmd(iob);
2801                 return -EIO;
2802         }
2803 
2804         if (reply_cb == NULL)
2805                 reply_cb = qeth_send_ipa_cmd_cb;
2806         rc = qeth_send_control_data(card, iob, reply_cb, reply_param);
2807         if (rc == -ETIME) {
2808                 qeth_clear_ipacmd_list(card);
2809                 qeth_schedule_recovery(card);
2810         }
2811         return rc;
2812 }
2813 EXPORT_SYMBOL_GPL(qeth_send_ipa_cmd);
2814 
2815 static int qeth_send_startlan_cb(struct qeth_card *card,
2816                                  struct qeth_reply *reply, unsigned long data)
2817 {
2818         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
2819 
2820         if (cmd->hdr.return_code == IPA_RC_LAN_OFFLINE)
2821                 return -ENETDOWN;
2822 
2823         return (cmd->hdr.return_code) ? -EIO : 0;
2824 }
2825 
2826 static int qeth_send_startlan(struct qeth_card *card)
2827 {
2828         struct qeth_cmd_buffer *iob;
2829 
2830         QETH_CARD_TEXT(card, 2, "strtlan");
2831 
2832         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_STARTLAN, QETH_PROT_NONE, 0);
2833         if (!iob)
2834                 return -ENOMEM;
2835         return qeth_send_ipa_cmd(card, iob, qeth_send_startlan_cb, NULL);
2836 }
2837 
2838 static int qeth_setadpparms_inspect_rc(struct qeth_ipa_cmd *cmd)
2839 {
2840         if (!cmd->hdr.return_code)
2841                 cmd->hdr.return_code =
2842                         cmd->data.setadapterparms.hdr.return_code;
2843         return cmd->hdr.return_code;
2844 }
2845 
2846 static int qeth_query_setadapterparms_cb(struct qeth_card *card,
2847                 struct qeth_reply *reply, unsigned long data)
2848 {
2849         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
2850 
2851         QETH_CARD_TEXT(card, 3, "quyadpcb");
2852         if (qeth_setadpparms_inspect_rc(cmd))
2853                 return -EIO;
2854 
2855         if (cmd->data.setadapterparms.data.query_cmds_supp.lan_type & 0x7f) {
2856                 card->info.link_type =
2857                       cmd->data.setadapterparms.data.query_cmds_supp.lan_type;
2858                 QETH_CARD_TEXT_(card, 2, "lnk %d", card->info.link_type);
2859         }
2860         card->options.adp.supported_funcs =
2861                 cmd->data.setadapterparms.data.query_cmds_supp.supported_cmds;
2862         return 0;
2863 }
2864 
2865 static struct qeth_cmd_buffer *qeth_get_adapter_cmd(struct qeth_card *card,
2866                                                     enum qeth_ipa_setadp_cmd adp_cmd,
2867                                                     unsigned int data_length)
2868 {
2869         struct qeth_ipacmd_setadpparms_hdr *hdr;
2870         struct qeth_cmd_buffer *iob;
2871 
2872         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETADAPTERPARMS, QETH_PROT_IPV4,
2873                                  data_length +
2874                                  offsetof(struct qeth_ipacmd_setadpparms,
2875                                           data));
2876         if (!iob)
2877                 return NULL;
2878 
2879         hdr = &__ipa_cmd(iob)->data.setadapterparms.hdr;
2880         hdr->cmdlength = sizeof(*hdr) + data_length;
2881         hdr->command_code = adp_cmd;
2882         hdr->used_total = 1;
2883         hdr->seq_no = 1;
2884         return iob;
2885 }
2886 
2887 static int qeth_query_setadapterparms(struct qeth_card *card)
2888 {
2889         int rc;
2890         struct qeth_cmd_buffer *iob;
2891 
2892         QETH_CARD_TEXT(card, 3, "queryadp");
2893         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_COMMANDS_SUPPORTED,
2894                                    SETADP_DATA_SIZEOF(query_cmds_supp));
2895         if (!iob)
2896                 return -ENOMEM;
2897         rc = qeth_send_ipa_cmd(card, iob, qeth_query_setadapterparms_cb, NULL);
2898         return rc;
2899 }
2900 
2901 static int qeth_query_ipassists_cb(struct qeth_card *card,
2902                 struct qeth_reply *reply, unsigned long data)
2903 {
2904         struct qeth_ipa_cmd *cmd;
2905 
2906         QETH_CARD_TEXT(card, 2, "qipasscb");
2907 
2908         cmd = (struct qeth_ipa_cmd *) data;
2909 
2910         switch (cmd->hdr.return_code) {
2911         case IPA_RC_SUCCESS:
2912                 break;
2913         case IPA_RC_NOTSUPP:
2914         case IPA_RC_L2_UNSUPPORTED_CMD:
2915                 QETH_CARD_TEXT(card, 2, "ipaunsup");
2916                 card->options.ipa4.supported_funcs |= IPA_SETADAPTERPARMS;
2917                 card->options.ipa6.supported_funcs |= IPA_SETADAPTERPARMS;
2918                 return -EOPNOTSUPP;
2919         default:
2920                 QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Unhandled rc=%#x\n",
2921                                  CARD_DEVID(card), cmd->hdr.return_code);
2922                 return -EIO;
2923         }
2924 
2925         if (cmd->hdr.prot_version == QETH_PROT_IPV4) {
2926                 card->options.ipa4.supported_funcs = cmd->hdr.ipa_supported;
2927                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
2928         } else if (cmd->hdr.prot_version == QETH_PROT_IPV6) {
2929                 card->options.ipa6.supported_funcs = cmd->hdr.ipa_supported;
2930                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
2931         } else
2932                 QETH_DBF_MESSAGE(1, "IPA_CMD_QIPASSIST on device %x: Flawed LIC detected\n",
2933                                  CARD_DEVID(card));
2934         return 0;
2935 }
2936 
2937 static int qeth_query_ipassists(struct qeth_card *card,
2938                                 enum qeth_prot_versions prot)
2939 {
2940         int rc;
2941         struct qeth_cmd_buffer *iob;
2942 
2943         QETH_CARD_TEXT_(card, 2, "qipassi%i", prot);
2944         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_QIPASSIST, prot, 0);
2945         if (!iob)
2946                 return -ENOMEM;
2947         rc = qeth_send_ipa_cmd(card, iob, qeth_query_ipassists_cb, NULL);
2948         return rc;
2949 }
2950 
2951 static int qeth_query_switch_attributes_cb(struct qeth_card *card,
2952                                 struct qeth_reply *reply, unsigned long data)
2953 {
2954         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
2955         struct qeth_query_switch_attributes *attrs;
2956         struct qeth_switch_info *sw_info;
2957 
2958         QETH_CARD_TEXT(card, 2, "qswiatcb");
2959         if (qeth_setadpparms_inspect_rc(cmd))
2960                 return -EIO;
2961 
2962         sw_info = (struct qeth_switch_info *)reply->param;
2963         attrs = &cmd->data.setadapterparms.data.query_switch_attributes;
2964         sw_info->capabilities = attrs->capabilities;
2965         sw_info->settings = attrs->settings;
2966         QETH_CARD_TEXT_(card, 2, "%04x%04x", sw_info->capabilities,
2967                         sw_info->settings);
2968         return 0;
2969 }
2970 
2971 int qeth_query_switch_attributes(struct qeth_card *card,
2972                                  struct qeth_switch_info *sw_info)
2973 {
2974         struct qeth_cmd_buffer *iob;
2975 
2976         QETH_CARD_TEXT(card, 2, "qswiattr");
2977         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES))
2978                 return -EOPNOTSUPP;
2979         if (!netif_carrier_ok(card->dev))
2980                 return -ENOMEDIUM;
2981         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_SWITCH_ATTRIBUTES, 0);
2982         if (!iob)
2983                 return -ENOMEM;
2984         return qeth_send_ipa_cmd(card, iob,
2985                                 qeth_query_switch_attributes_cb, sw_info);
2986 }
2987 
2988 struct qeth_cmd_buffer *qeth_get_diag_cmd(struct qeth_card *card,
2989                                           enum qeth_diags_cmds sub_cmd,
2990                                           unsigned int data_length)
2991 {
2992         struct qeth_ipacmd_diagass *cmd;
2993         struct qeth_cmd_buffer *iob;
2994 
2995         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SET_DIAG_ASS, QETH_PROT_NONE,
2996                                  DIAG_HDR_LEN + data_length);
2997         if (!iob)
2998                 return NULL;
2999 
3000         cmd = &__ipa_cmd(iob)->data.diagass;
3001         cmd->subcmd_len = DIAG_SUB_HDR_LEN + data_length;
3002         cmd->subcmd = sub_cmd;
3003         return iob;
3004 }
3005 EXPORT_SYMBOL_GPL(qeth_get_diag_cmd);
3006 
3007 static int qeth_query_setdiagass_cb(struct qeth_card *card,
3008                 struct qeth_reply *reply, unsigned long data)
3009 {
3010         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3011         u16 rc = cmd->hdr.return_code;
3012 
3013         if (rc) {
3014                 QETH_CARD_TEXT_(card, 2, "diagq:%x", rc);
3015                 return -EIO;
3016         }
3017 
3018         card->info.diagass_support = cmd->data.diagass.ext;
3019         return 0;
3020 }
3021 
3022 static int qeth_query_setdiagass(struct qeth_card *card)
3023 {
3024         struct qeth_cmd_buffer *iob;
3025 
3026         QETH_CARD_TEXT(card, 2, "qdiagass");
3027         iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_QUERY, 0);
3028         if (!iob)
3029                 return -ENOMEM;
3030         return qeth_send_ipa_cmd(card, iob, qeth_query_setdiagass_cb, NULL);
3031 }
3032 
3033 static void qeth_get_trap_id(struct qeth_card *card, struct qeth_trap_id *tid)
3034 {
3035         unsigned long info = get_zeroed_page(GFP_KERNEL);
3036         struct sysinfo_2_2_2 *info222 = (struct sysinfo_2_2_2 *)info;
3037         struct sysinfo_3_2_2 *info322 = (struct sysinfo_3_2_2 *)info;
3038         struct ccw_dev_id ccwid;
3039         int level;
3040 
3041         tid->chpid = card->info.chpid;
3042         ccw_device_get_id(CARD_RDEV(card), &ccwid);
3043         tid->ssid = ccwid.ssid;
3044         tid->devno = ccwid.devno;
3045         if (!info)
3046                 return;
3047         level = stsi(NULL, 0, 0, 0);
3048         if ((level >= 2) && (stsi(info222, 2, 2, 2) == 0))
3049                 tid->lparnr = info222->lpar_number;
3050         if ((level >= 3) && (stsi(info322, 3, 2, 2) == 0)) {
3051                 EBCASC(info322->vm[0].name, sizeof(info322->vm[0].name));
3052                 memcpy(tid->vmname, info322->vm[0].name, sizeof(tid->vmname));
3053         }
3054         free_page(info);
3055         return;
3056 }
3057 
3058 static int qeth_hw_trap_cb(struct qeth_card *card,
3059                 struct qeth_reply *reply, unsigned long data)
3060 {
3061         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
3062         u16 rc = cmd->hdr.return_code;
3063 
3064         if (rc) {
3065                 QETH_CARD_TEXT_(card, 2, "trapc:%x", rc);
3066                 return -EIO;
3067         }
3068         return 0;
3069 }
3070 
3071 int qeth_hw_trap(struct qeth_card *card, enum qeth_diags_trap_action action)
3072 {
3073         struct qeth_cmd_buffer *iob;
3074         struct qeth_ipa_cmd *cmd;
3075 
3076         QETH_CARD_TEXT(card, 2, "diagtrap");
3077         iob = qeth_get_diag_cmd(card, QETH_DIAGS_CMD_TRAP, 64);
3078         if (!iob)
3079                 return -ENOMEM;
3080         cmd = __ipa_cmd(iob);
3081         cmd->data.diagass.type = 1;
3082         cmd->data.diagass.action = action;
3083         switch (action) {
3084         case QETH_DIAGS_TRAP_ARM:
3085                 cmd->data.diagass.options = 0x0003;
3086                 cmd->data.diagass.ext = 0x00010000 +
3087                         sizeof(struct qeth_trap_id);
3088                 qeth_get_trap_id(card,
3089                         (struct qeth_trap_id *)cmd->data.diagass.cdata);
3090                 break;
3091         case QETH_DIAGS_TRAP_DISARM:
3092                 cmd->data.diagass.options = 0x0001;
3093                 break;
3094         case QETH_DIAGS_TRAP_CAPTURE:
3095                 break;
3096         }
3097         return qeth_send_ipa_cmd(card, iob, qeth_hw_trap_cb, NULL);
3098 }
3099 EXPORT_SYMBOL_GPL(qeth_hw_trap);
3100 
3101 static int qeth_check_qdio_errors(struct qeth_card *card,
3102                                   struct qdio_buffer *buf,
3103                                   unsigned int qdio_error,
3104                                   const char *dbftext)
3105 {
3106         if (qdio_error) {
3107                 QETH_CARD_TEXT(card, 2, dbftext);
3108                 QETH_CARD_TEXT_(card, 2, " F15=%02X",
3109                                buf->element[15].sflags);
3110                 QETH_CARD_TEXT_(card, 2, " F14=%02X",
3111                                buf->element[14].sflags);
3112                 QETH_CARD_TEXT_(card, 2, " qerr=%X", qdio_error);
3113                 if ((buf->element[15].sflags) == 0x12) {
3114                         QETH_CARD_STAT_INC(card, rx_dropped);
3115                         return 0;
3116                 } else
3117                         return 1;
3118         }
3119         return 0;
3120 }
3121 
3122 static void qeth_queue_input_buffer(struct qeth_card *card, int index)
3123 {
3124         struct qeth_qdio_q *queue = card->qdio.in_q;
3125         struct list_head *lh;
3126         int count;
3127         int i;
3128         int rc;
3129         int newcount = 0;
3130 
3131         count = (index < queue->next_buf_to_init)?
3132                 card->qdio.in_buf_pool.buf_count -
3133                 (queue->next_buf_to_init - index) :
3134                 card->qdio.in_buf_pool.buf_count -
3135                 (queue->next_buf_to_init + QDIO_MAX_BUFFERS_PER_Q - index);
3136         /* only requeue at a certain threshold to avoid SIGAs */
3137         if (count >= QETH_IN_BUF_REQUEUE_THRESHOLD(card)) {
3138                 for (i = queue->next_buf_to_init;
3139                      i < queue->next_buf_to_init + count; ++i) {
3140                         if (qeth_init_input_buffer(card,
3141                                 &queue->bufs[i % QDIO_MAX_BUFFERS_PER_Q])) {
3142                                 break;
3143                         } else {
3144                                 newcount++;
3145                         }
3146                 }
3147 
3148                 if (newcount < count) {
3149                         /* we are in memory shortage so we switch back to
3150                            traditional skb allocation and drop packages */
3151                         atomic_set(&card->force_alloc_skb, 3);
3152                         count = newcount;
3153                 } else {
3154                         atomic_add_unless(&card->force_alloc_skb, -1, 0);
3155                 }
3156 
3157                 if (!count) {
3158                         i = 0;
3159                         list_for_each(lh, &card->qdio.in_buf_pool.entry_list)
3160                                 i++;
3161                         if (i == card->qdio.in_buf_pool.buf_count) {
3162                                 QETH_CARD_TEXT(card, 2, "qsarbw");
3163                                 card->reclaim_index = index;
3164                                 schedule_delayed_work(
3165                                         &card->buffer_reclaim_work,
3166                                         QETH_RECLAIM_WORK_TIME);
3167                         }
3168                         return;
3169                 }
3170 
3171                 /*
3172                  * according to old code it should be avoided to requeue all
3173                  * 128 buffers in order to benefit from PCI avoidance.
3174                  * this function keeps at least one buffer (the buffer at
3175                  * 'index') un-requeued -> this buffer is the first buffer that
3176                  * will be requeued the next time
3177                  */
3178                 rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, 0,
3179                              queue->next_buf_to_init, count);
3180                 if (rc) {
3181                         QETH_CARD_TEXT(card, 2, "qinberr");
3182                 }
3183                 queue->next_buf_to_init = (queue->next_buf_to_init + count) %
3184                                           QDIO_MAX_BUFFERS_PER_Q;
3185         }
3186 }
3187 
3188 static void qeth_buffer_reclaim_work(struct work_struct *work)
3189 {
3190         struct qeth_card *card = container_of(work, struct qeth_card,
3191                 buffer_reclaim_work.work);
3192 
3193         QETH_CARD_TEXT_(card, 2, "brw:%x", card->reclaim_index);
3194         qeth_queue_input_buffer(card, card->reclaim_index);
3195 }
3196 
3197 static void qeth_handle_send_error(struct qeth_card *card,
3198                 struct qeth_qdio_out_buffer *buffer, unsigned int qdio_err)
3199 {
3200         int sbalf15 = buffer->buffer->element[15].sflags;
3201 
3202         QETH_CARD_TEXT(card, 6, "hdsnderr");
3203         qeth_check_qdio_errors(card, buffer->buffer, qdio_err, "qouterr");
3204 
3205         if (!qdio_err)
3206                 return;
3207 
3208         if ((sbalf15 >= 15) && (sbalf15 <= 31))
3209                 return;
3210 
3211         QETH_CARD_TEXT(card, 1, "lnkfail");
3212         QETH_CARD_TEXT_(card, 1, "%04x %02x",
3213                        (u16)qdio_err, (u8)sbalf15);
3214 }
3215 
3216 /**
3217  * qeth_prep_flush_pack_buffer - Prepares flushing of a packing buffer.
3218  * @queue: queue to check for packing buffer
3219  *
3220  * Returns number of buffers that were prepared for flush.
3221  */
3222 static int qeth_prep_flush_pack_buffer(struct qeth_qdio_out_q *queue)
3223 {
3224         struct qeth_qdio_out_buffer *buffer;
3225 
3226         buffer = queue->bufs[queue->next_buf_to_fill];
3227         if ((atomic_read(&buffer->state) == QETH_QDIO_BUF_EMPTY) &&
3228             (buffer->next_element_to_fill > 0)) {
3229                 /* it's a packing buffer */
3230                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3231                 queue->next_buf_to_fill =
3232                         (queue->next_buf_to_fill + 1) % QDIO_MAX_BUFFERS_PER_Q;
3233                 return 1;
3234         }
3235         return 0;
3236 }
3237 
3238 /*
3239  * Switched to packing state if the number of used buffers on a queue
3240  * reaches a certain limit.
3241  */
3242 static void qeth_switch_to_packing_if_needed(struct qeth_qdio_out_q *queue)
3243 {
3244         if (!queue->do_pack) {
3245                 if (atomic_read(&queue->used_buffers)
3246                     >= QETH_HIGH_WATERMARK_PACK){
3247                         /* switch non-PACKING -> PACKING */
3248                         QETH_CARD_TEXT(queue->card, 6, "np->pack");
3249                         QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3250                         queue->do_pack = 1;
3251                 }
3252         }
3253 }
3254 
3255 /*
3256  * Switches from packing to non-packing mode. If there is a packing
3257  * buffer on the queue this buffer will be prepared to be flushed.
3258  * In that case 1 is returned to inform the caller. If no buffer
3259  * has to be flushed, zero is returned.
3260  */
3261 static int qeth_switch_to_nonpacking_if_needed(struct qeth_qdio_out_q *queue)
3262 {
3263         if (queue->do_pack) {
3264                 if (atomic_read(&queue->used_buffers)
3265                     <= QETH_LOW_WATERMARK_PACK) {
3266                         /* switch PACKING -> non-PACKING */
3267                         QETH_CARD_TEXT(queue->card, 6, "pack->np");
3268                         QETH_TXQ_STAT_INC(queue, packing_mode_switch);
3269                         queue->do_pack = 0;
3270                         return qeth_prep_flush_pack_buffer(queue);
3271                 }
3272         }
3273         return 0;
3274 }
3275 
3276 static void qeth_flush_buffers(struct qeth_qdio_out_q *queue, int index,
3277                                int count)
3278 {
3279         struct qeth_card *card = queue->card;
3280         struct qeth_qdio_out_buffer *buf;
3281         int rc;
3282         int i;
3283         unsigned int qdio_flags;
3284 
3285         for (i = index; i < index + count; ++i) {
3286                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3287                 buf = queue->bufs[bidx];
3288                 buf->buffer->element[buf->next_element_to_fill - 1].eflags |=
3289                                 SBAL_EFLAGS_LAST_ENTRY;
3290 
3291                 if (queue->bufstates)
3292                         queue->bufstates[bidx].user = buf;
3293 
3294                 if (IS_IQD(queue->card))
3295                         continue;
3296 
3297                 if (!queue->do_pack) {
3298                         if ((atomic_read(&queue->used_buffers) >=
3299                                 (QETH_HIGH_WATERMARK_PACK -
3300                                  QETH_WATERMARK_PACK_FUZZ)) &&
3301                             !atomic_read(&queue->set_pci_flags_count)) {
3302                                 /* it's likely that we'll go to packing
3303                                  * mode soon */
3304                                 atomic_inc(&queue->set_pci_flags_count);
3305                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3306                         }
3307                 } else {
3308                         if (!atomic_read(&queue->set_pci_flags_count)) {
3309                                 /*
3310                                  * there's no outstanding PCI any more, so we
3311                                  * have to request a PCI to be sure the the PCI
3312                                  * will wake at some time in the future then we
3313                                  * can flush packed buffers that might still be
3314                                  * hanging around, which can happen if no
3315                                  * further send was requested by the stack
3316                                  */
3317                                 atomic_inc(&queue->set_pci_flags_count);
3318                                 buf->buffer->element[0].sflags |= SBAL_SFLAGS0_PCI_REQ;
3319                         }
3320                 }
3321         }
3322 
3323         qdio_flags = QDIO_FLAG_SYNC_OUTPUT;
3324         if (atomic_read(&queue->set_pci_flags_count))
3325                 qdio_flags |= QDIO_FLAG_PCI_OUT;
3326         rc = do_QDIO(CARD_DDEV(queue->card), qdio_flags,
3327                      queue->queue_no, index, count);
3328 
3329         /* Fake the TX completion interrupt: */
3330         if (IS_IQD(card))
3331                 napi_schedule(&queue->napi);
3332 
3333         if (rc) {
3334                 /* ignore temporary SIGA errors without busy condition */
3335                 if (rc == -ENOBUFS)
3336                         return;
3337                 QETH_CARD_TEXT(queue->card, 2, "flushbuf");
3338                 QETH_CARD_TEXT_(queue->card, 2, " q%d", queue->queue_no);
3339                 QETH_CARD_TEXT_(queue->card, 2, " idx%d", index);
3340                 QETH_CARD_TEXT_(queue->card, 2, " c%d", count);
3341                 QETH_CARD_TEXT_(queue->card, 2, " err%d", rc);
3342 
3343                 /* this must not happen under normal circumstances. if it
3344                  * happens something is really wrong -> recover */
3345                 qeth_schedule_recovery(queue->card);
3346                 return;
3347         }
3348 }
3349 
3350 static void qeth_flush_queue(struct qeth_qdio_out_q *queue)
3351 {
3352         qeth_flush_buffers(queue, queue->bulk_start, 1);
3353 
3354         queue->bulk_start = QDIO_BUFNR(queue->bulk_start + 1);
3355         queue->prev_hdr = NULL;
3356 }
3357 
3358 static void qeth_check_outbound_queue(struct qeth_qdio_out_q *queue)
3359 {
3360         int index;
3361         int flush_cnt = 0;
3362         int q_was_packing = 0;
3363 
3364         /*
3365          * check if weed have to switch to non-packing mode or if
3366          * we have to get a pci flag out on the queue
3367          */
3368         if ((atomic_read(&queue->used_buffers) <= QETH_LOW_WATERMARK_PACK) ||
3369             !atomic_read(&queue->set_pci_flags_count)) {
3370                 if (atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH) ==
3371                                 QETH_OUT_Q_UNLOCKED) {
3372                         /*
3373                          * If we get in here, there was no action in
3374                          * do_send_packet. So, we check if there is a
3375                          * packing buffer to be flushed here.
3376                          */
3377                         index = queue->next_buf_to_fill;
3378                         q_was_packing = queue->do_pack;
3379                         /* queue->do_pack may change */
3380                         barrier();
3381                         flush_cnt += qeth_switch_to_nonpacking_if_needed(queue);
3382                         if (!flush_cnt &&
3383                             !atomic_read(&queue->set_pci_flags_count))
3384                                 flush_cnt += qeth_prep_flush_pack_buffer(queue);
3385                         if (q_was_packing)
3386                                 QETH_TXQ_STAT_ADD(queue, bufs_pack, flush_cnt);
3387                         if (flush_cnt)
3388                                 qeth_flush_buffers(queue, index, flush_cnt);
3389                         atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3390                 }
3391         }
3392 }
3393 
3394 static void qeth_qdio_start_poll(struct ccw_device *ccwdev, int queue,
3395                                  unsigned long card_ptr)
3396 {
3397         struct qeth_card *card = (struct qeth_card *)card_ptr;
3398 
3399         if (card->dev->flags & IFF_UP)
3400                 napi_schedule(&card->napi);
3401 }
3402 
3403 int qeth_configure_cq(struct qeth_card *card, enum qeth_cq cq)
3404 {
3405         int rc;
3406 
3407         if (card->options.cq ==  QETH_CQ_NOTAVAILABLE) {
3408                 rc = -1;
3409                 goto out;
3410         } else {
3411                 if (card->options.cq == cq) {
3412                         rc = 0;
3413                         goto out;
3414                 }
3415 
3416                 qeth_free_qdio_queues(card);
3417                 card->options.cq = cq;
3418                 rc = 0;
3419         }
3420 out:
3421         return rc;
3422 
3423 }
3424 EXPORT_SYMBOL_GPL(qeth_configure_cq);
3425 
3426 static void qeth_qdio_cq_handler(struct qeth_card *card, unsigned int qdio_err,
3427                                  unsigned int queue, int first_element,
3428                                  int count)
3429 {
3430         struct qeth_qdio_q *cq = card->qdio.c_q;
3431         int i;
3432         int rc;
3433 
3434         if (!qeth_is_cq(card, queue))
3435                 return;
3436 
3437         QETH_CARD_TEXT_(card, 5, "qcqhe%d", first_element);
3438         QETH_CARD_TEXT_(card, 5, "qcqhc%d", count);
3439         QETH_CARD_TEXT_(card, 5, "qcqherr%d", qdio_err);
3440 
3441         if (qdio_err) {
3442                 netif_tx_stop_all_queues(card->dev);
3443                 qeth_schedule_recovery(card);
3444                 return;
3445         }
3446 
3447         for (i = first_element; i < first_element + count; ++i) {
3448                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3449                 struct qdio_buffer *buffer = cq->qdio_bufs[bidx];
3450                 int e = 0;
3451 
3452                 while ((e < QDIO_MAX_ELEMENTS_PER_BUFFER) &&
3453                        buffer->element[e].addr) {
3454                         unsigned long phys_aob_addr;
3455 
3456                         phys_aob_addr = (unsigned long) buffer->element[e].addr;
3457                         qeth_qdio_handle_aob(card, phys_aob_addr);
3458                         ++e;
3459                 }
3460                 qeth_scrub_qdio_buffer(buffer, QDIO_MAX_ELEMENTS_PER_BUFFER);
3461         }
3462         rc = do_QDIO(CARD_DDEV(card), QDIO_FLAG_SYNC_INPUT, queue,
3463                     card->qdio.c_q->next_buf_to_init,
3464                     count);
3465         if (rc) {
3466                 dev_warn(&card->gdev->dev,
3467                         "QDIO reported an error, rc=%i\n", rc);
3468                 QETH_CARD_TEXT(card, 2, "qcqherr");
3469         }
3470         card->qdio.c_q->next_buf_to_init = (card->qdio.c_q->next_buf_to_init
3471                                    + count) % QDIO_MAX_BUFFERS_PER_Q;
3472 }
3473 
3474 static void qeth_qdio_input_handler(struct ccw_device *ccwdev,
3475                                     unsigned int qdio_err, int queue,
3476                                     int first_elem, int count,
3477                                     unsigned long card_ptr)
3478 {
3479         struct qeth_card *card = (struct qeth_card *)card_ptr;
3480 
3481         QETH_CARD_TEXT_(card, 2, "qihq%d", queue);
3482         QETH_CARD_TEXT_(card, 2, "qiec%d", qdio_err);
3483 
3484         if (qeth_is_cq(card, queue))
3485                 qeth_qdio_cq_handler(card, qdio_err, queue, first_elem, count);
3486         else if (qdio_err)
3487                 qeth_schedule_recovery(card);
3488 }
3489 
3490 static void qeth_qdio_output_handler(struct ccw_device *ccwdev,
3491                                      unsigned int qdio_error, int __queue,
3492                                      int first_element, int count,
3493                                      unsigned long card_ptr)
3494 {
3495         struct qeth_card *card        = (struct qeth_card *) card_ptr;
3496         struct qeth_qdio_out_q *queue = card->qdio.out_qs[__queue];
3497         struct qeth_qdio_out_buffer *buffer;
3498         struct net_device *dev = card->dev;
3499         struct netdev_queue *txq;
3500         int i;
3501 
3502         QETH_CARD_TEXT(card, 6, "qdouhdl");
3503         if (qdio_error & QDIO_ERROR_FATAL) {
3504                 QETH_CARD_TEXT(card, 2, "achkcond");
3505                 netif_tx_stop_all_queues(dev);
3506                 qeth_schedule_recovery(card);
3507                 return;
3508         }
3509 
3510         for (i = first_element; i < (first_element + count); ++i) {
3511                 int bidx = i % QDIO_MAX_BUFFERS_PER_Q;
3512                 buffer = queue->bufs[bidx];
3513                 qeth_handle_send_error(card, buffer, qdio_error);
3514                 qeth_clear_output_buffer(queue, buffer, qdio_error, 0);
3515         }
3516 
3517         atomic_sub(count, &queue->used_buffers);
3518         qeth_check_outbound_queue(queue);
3519 
3520         txq = netdev_get_tx_queue(dev, __queue);
3521         /* xmit may have observed the full-condition, but not yet stopped the
3522          * txq. In which case the code below won't trigger. So before returning,
3523          * xmit will re-check the txq's fill level and wake it up if needed.
3524          */
3525         if (netif_tx_queue_stopped(txq) && !qeth_out_queue_is_full(queue))
3526                 netif_tx_wake_queue(txq);
3527 }
3528 
3529 /**
3530  * Note: Function assumes that we have 4 outbound queues.
3531  */
3532 int qeth_get_priority_queue(struct qeth_card *card, struct sk_buff *skb)
3533 {
3534         struct vlan_ethhdr *veth = vlan_eth_hdr(skb);
3535         u8 tos;
3536 
3537         switch (card->qdio.do_prio_queueing) {
3538         case QETH_PRIO_Q_ING_TOS:
3539         case QETH_PRIO_Q_ING_PREC:
3540                 switch (qeth_get_ip_version(skb)) {
3541                 case 4:
3542                         tos = ipv4_get_dsfield(ip_hdr(skb));
3543                         break;
3544                 case 6:
3545                         tos = ipv6_get_dsfield(ipv6_hdr(skb));
3546                         break;
3547                 default:
3548                         return card->qdio.default_out_queue;
3549                 }
3550                 if (card->qdio.do_prio_queueing == QETH_PRIO_Q_ING_PREC)
3551                         return ~tos >> 6 & 3;
3552                 if (tos & IPTOS_MINCOST)
3553                         return 3;
3554                 if (tos & IPTOS_RELIABILITY)
3555                         return 2;
3556                 if (tos & IPTOS_THROUGHPUT)
3557                         return 1;
3558                 if (tos & IPTOS_LOWDELAY)
3559                         return 0;
3560                 break;
3561         case QETH_PRIO_Q_ING_SKB:
3562                 if (skb->priority > 5)
3563                         return 0;
3564                 return ~skb->priority >> 1 & 3;
3565         case QETH_PRIO_Q_ING_VLAN:
3566                 if (veth->h_vlan_proto == htons(ETH_P_8021Q))
3567                         return ~ntohs(veth->h_vlan_TCI) >>
3568                                (VLAN_PRIO_SHIFT + 1) & 3;
3569                 break;
3570         default:
3571                 break;
3572         }
3573         return card->qdio.default_out_queue;
3574 }
3575 EXPORT_SYMBOL_GPL(qeth_get_priority_queue);
3576 
3577 /**
3578  * qeth_get_elements_for_frags() -      find number of SBALEs for skb frags.
3579  * @skb:                                SKB address
3580  *
3581  * Returns the number of pages, and thus QDIO buffer elements, needed to cover
3582  * fragmented part of the SKB. Returns zero for linear SKB.
3583  */
3584 static int qeth_get_elements_for_frags(struct sk_buff *skb)
3585 {
3586         int cnt, elements = 0;
3587 
3588         for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3589                 skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
3590 
3591                 elements += qeth_get_elements_for_range(
3592                         (addr_t)skb_frag_address(frag),
3593                         (addr_t)skb_frag_address(frag) + skb_frag_size(frag));
3594         }
3595         return elements;
3596 }
3597 
3598 /**
3599  * qeth_count_elements() -      Counts the number of QDIO buffer elements needed
3600  *                              to transmit an skb.
3601  * @skb:                        the skb to operate on.
3602  * @data_offset:                skip this part of the skb's linear data
3603  *
3604  * Returns the number of pages, and thus QDIO buffer elements, needed to map the
3605  * skb's data (both its linear part and paged fragments).
3606  */
3607 unsigned int qeth_count_elements(struct sk_buff *skb, unsigned int data_offset)
3608 {
3609         unsigned int elements = qeth_get_elements_for_frags(skb);
3610         addr_t end = (addr_t)skb->data + skb_headlen(skb);
3611         addr_t start = (addr_t)skb->data + data_offset;
3612 
3613         if (start != end)
3614                 elements += qeth_get_elements_for_range(start, end);
3615         return elements;
3616 }
3617 EXPORT_SYMBOL_GPL(qeth_count_elements);
3618 
3619 #define QETH_HDR_CACHE_OBJ_SIZE         (sizeof(struct qeth_hdr_tso) + \
3620                                          MAX_TCP_HEADER)
3621 
3622 /**
3623  * qeth_add_hw_header() - add a HW header to an skb.
3624  * @skb: skb that the HW header should be added to.
3625  * @hdr: double pointer to a qeth_hdr. When returning with >= 0,
3626  *       it contains a valid pointer to a qeth_hdr.
3627  * @hdr_len: length of the HW header.
3628  * @proto_len: length of protocol headers that need to be in same page as the
3629  *             HW header.
3630  *
3631  * Returns the pushed length. If the header can't be pushed on
3632  * (eg. because it would cross a page boundary), it is allocated from
3633  * the cache instead and 0 is returned.
3634  * The number of needed buffer elements is returned in @elements.
3635  * Error to create the hdr is indicated by returning with < 0.
3636  */
3637 static int qeth_add_hw_header(struct qeth_qdio_out_q *queue,
3638                               struct sk_buff *skb, struct qeth_hdr **hdr,
3639                               unsigned int hdr_len, unsigned int proto_len,
3640                               unsigned int *elements)
3641 {
3642         const unsigned int contiguous = proto_len ? proto_len : 1;
3643         const unsigned int max_elements = queue->max_elements;
3644         unsigned int __elements;
3645         addr_t start, end;
3646         bool push_ok;
3647         int rc;
3648 
3649 check_layout:
3650         start = (addr_t)skb->data - hdr_len;
3651         end = (addr_t)skb->data;
3652 
3653         if (qeth_get_elements_for_range(start, end + contiguous) == 1) {
3654                 /* Push HW header into same page as first protocol header. */
3655                 push_ok = true;
3656                 /* ... but TSO always needs a separate element for headers: */
3657                 if (skb_is_gso(skb))
3658                         __elements = 1 + qeth_count_elements(skb, proto_len);
3659                 else
3660                         __elements = qeth_count_elements(skb, 0);
3661         } else if (!proto_len && PAGE_ALIGNED(skb->data)) {
3662                 /* Push HW header into preceding page, flush with skb->data. */
3663                 push_ok = true;
3664                 __elements = 1 + qeth_count_elements(skb, 0);
3665         } else {
3666                 /* Use header cache, copy protocol headers up. */
3667                 push_ok = false;
3668                 __elements = 1 + qeth_count_elements(skb, proto_len);
3669         }
3670 
3671         /* Compress skb to fit into one IO buffer: */
3672         if (__elements > max_elements) {
3673                 if (!skb_is_nonlinear(skb)) {
3674                         /* Drop it, no easy way of shrinking it further. */
3675                         QETH_DBF_MESSAGE(2, "Dropped an oversized skb (Max Elements=%u / Actual=%u / Length=%u).\n",
3676                                          max_elements, __elements, skb->len);
3677                         return -E2BIG;
3678                 }
3679 
3680                 rc = skb_linearize(skb);
3681                 if (rc) {
3682                         QETH_TXQ_STAT_INC(queue, skbs_linearized_fail);
3683                         return rc;
3684                 }
3685 
3686                 QETH_TXQ_STAT_INC(queue, skbs_linearized);
3687                 /* Linearization changed the layout, re-evaluate: */
3688                 goto check_layout;
3689         }
3690 
3691         *elements = __elements;
3692         /* Add the header: */
3693         if (push_ok) {
3694                 *hdr = skb_push(skb, hdr_len);
3695                 return hdr_len;
3696         }
3697         /* fall back */
3698         if (hdr_len + proto_len > QETH_HDR_CACHE_OBJ_SIZE)
3699                 return -E2BIG;
3700         *hdr = kmem_cache_alloc(qeth_core_header_cache, GFP_ATOMIC);
3701         if (!*hdr)
3702                 return -ENOMEM;
3703         /* Copy protocol headers behind HW header: */
3704         skb_copy_from_linear_data(skb, ((char *)*hdr) + hdr_len, proto_len);
3705         return 0;
3706 }
3707 
3708 static bool qeth_iqd_may_bulk(struct qeth_qdio_out_q *queue,
3709                               struct qeth_qdio_out_buffer *buffer,
3710                               struct sk_buff *curr_skb,
3711                               struct qeth_hdr *curr_hdr)
3712 {
3713         struct qeth_hdr *prev_hdr = queue->prev_hdr;
3714 
3715         if (!prev_hdr)
3716                 return true;
3717 
3718         /* All packets must have the same target: */
3719         if (curr_hdr->hdr.l2.id == QETH_HEADER_TYPE_LAYER2) {
3720                 struct sk_buff *prev_skb = skb_peek(&buffer->skb_list);
3721 
3722                 return ether_addr_equal(eth_hdr(prev_skb)->h_dest,
3723                                         eth_hdr(curr_skb)->h_dest) &&
3724                        qeth_l2_same_vlan(&prev_hdr->hdr.l2, &curr_hdr->hdr.l2);
3725         }
3726 
3727         return qeth_l3_same_next_hop(&prev_hdr->hdr.l3, &curr_hdr->hdr.l3) &&
3728                qeth_l3_iqd_same_vlan(&prev_hdr->hdr.l3, &curr_hdr->hdr.l3);
3729 }
3730 
3731 static unsigned int __qeth_fill_buffer(struct sk_buff *skb,
3732                                        struct qeth_qdio_out_buffer *buf,
3733                                        bool is_first_elem, unsigned int offset)
3734 {
3735         struct qdio_buffer *buffer = buf->buffer;
3736         int element = buf->next_element_to_fill;
3737         int length = skb_headlen(skb) - offset;
3738         char *data = skb->data + offset;
3739         unsigned int elem_length, cnt;
3740 
3741         /* map linear part into buffer element(s) */
3742         while (length > 0) {
3743                 elem_length = min_t(unsigned int, length,
3744                                     PAGE_SIZE - offset_in_page(data));
3745 
3746                 buffer->element[element].addr = data;
3747                 buffer->element[element].length = elem_length;
3748                 length -= elem_length;
3749                 if (is_first_elem) {
3750                         is_first_elem = false;
3751                         if (length || skb_is_nonlinear(skb))
3752                                 /* skb needs additional elements */
3753                                 buffer->element[element].eflags =
3754                                         SBAL_EFLAGS_FIRST_FRAG;
3755                         else
3756                                 buffer->element[element].eflags = 0;
3757                 } else {
3758                         buffer->element[element].eflags =
3759                                 SBAL_EFLAGS_MIDDLE_FRAG;
3760                 }
3761 
3762                 data += elem_length;
3763                 element++;
3764         }
3765 
3766         /* map page frags into buffer element(s) */
3767         for (cnt = 0; cnt < skb_shinfo(skb)->nr_frags; cnt++) {
3768                 skb_frag_t *frag = &skb_shinfo(skb)->frags[cnt];
3769 
3770                 data = skb_frag_address(frag);
3771                 length = skb_frag_size(frag);
3772                 while (length > 0) {
3773                         elem_length = min_t(unsigned int, length,
3774                                             PAGE_SIZE - offset_in_page(data));
3775 
3776                         buffer->element[element].addr = data;
3777                         buffer->element[element].length = elem_length;
3778                         buffer->element[element].eflags =
3779                                 SBAL_EFLAGS_MIDDLE_FRAG;
3780 
3781                         length -= elem_length;
3782                         data += elem_length;
3783                         element++;
3784                 }
3785         }
3786 
3787         if (buffer->element[element - 1].eflags)
3788                 buffer->element[element - 1].eflags = SBAL_EFLAGS_LAST_FRAG;
3789         buf->next_element_to_fill = element;
3790         return element;
3791 }
3792 
3793 /**
3794  * qeth_fill_buffer() - map skb into an output buffer
3795  * @buf:        buffer to transport the skb
3796  * @skb:        skb to map into the buffer
3797  * @hdr:        qeth_hdr for this skb. Either at skb->data, or allocated
3798  *              from qeth_core_header_cache.
3799  * @offset:     when mapping the skb, start at skb->data + offset
3800  * @hd_len:     if > 0, build a dedicated header element of this size
3801  */
3802 static unsigned int qeth_fill_buffer(struct qeth_qdio_out_buffer *buf,
3803                                      struct sk_buff *skb, struct qeth_hdr *hdr,
3804                                      unsigned int offset, unsigned int hd_len)
3805 {
3806         struct qdio_buffer *buffer = buf->buffer;
3807         bool is_first_elem = true;
3808 
3809         __skb_queue_tail(&buf->skb_list, skb);
3810 
3811         /* build dedicated header element */
3812         if (hd_len) {
3813                 int element = buf->next_element_to_fill;
3814                 is_first_elem = false;
3815 
3816                 buffer->element[element].addr = hdr;
3817                 buffer->element[element].length = hd_len;
3818                 buffer->element[element].eflags = SBAL_EFLAGS_FIRST_FRAG;
3819                 /* remember to free cache-allocated qeth_hdr: */
3820                 buf->is_header[element] = ((void *)hdr != skb->data);
3821                 buf->next_element_to_fill++;
3822         }
3823 
3824         return __qeth_fill_buffer(skb, buf, is_first_elem, offset);
3825 }
3826 
3827 static int __qeth_xmit(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3828                        struct sk_buff *skb, unsigned int elements,
3829                        struct qeth_hdr *hdr, unsigned int offset,
3830                        unsigned int hd_len)
3831 {
3832         struct qeth_qdio_out_buffer *buffer = queue->bufs[queue->bulk_start];
3833         unsigned int bytes = qdisc_pkt_len(skb);
3834         unsigned int next_element;
3835         struct netdev_queue *txq;
3836         bool stopped = false;
3837         bool flush;
3838 
3839         txq = netdev_get_tx_queue(card->dev, skb_get_queue_mapping(skb));
3840 
3841         /* Just a sanity check, the wake/stop logic should ensure that we always
3842          * get a free buffer.
3843          */
3844         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
3845                 return -EBUSY;
3846 
3847         if ((buffer->next_element_to_fill + elements > queue->max_elements) ||
3848             !qeth_iqd_may_bulk(queue, buffer, skb, hdr)) {
3849                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3850                 qeth_flush_queue(queue);
3851                 buffer = queue->bufs[queue->bulk_start];
3852 
3853                 /* Sanity-check again: */
3854                 if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY)
3855                         return -EBUSY;
3856         }
3857 
3858         if (buffer->next_element_to_fill == 0 &&
3859             atomic_inc_return(&queue->used_buffers) >= QDIO_MAX_BUFFERS_PER_Q) {
3860                 /* If a TX completion happens right _here_ and misses to wake
3861                  * the txq, then our re-check below will catch the race.
3862                  */
3863                 QETH_TXQ_STAT_INC(queue, stopped);
3864                 netif_tx_stop_queue(txq);
3865                 stopped = true;
3866         }
3867 
3868         next_element = qeth_fill_buffer(buffer, skb, hdr, offset, hd_len);
3869         buffer->bytes += bytes;
3870         queue->prev_hdr = hdr;
3871 
3872         flush = __netdev_tx_sent_queue(txq, bytes,
3873                                        !stopped && netdev_xmit_more());
3874 
3875         if (flush || next_element >= queue->max_elements) {
3876                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3877                 qeth_flush_queue(queue);
3878         }
3879 
3880         if (stopped && !qeth_out_queue_is_full(queue))
3881                 netif_tx_start_queue(txq);
3882         return 0;
3883 }
3884 
3885 int qeth_do_send_packet(struct qeth_card *card, struct qeth_qdio_out_q *queue,
3886                         struct sk_buff *skb, struct qeth_hdr *hdr,
3887                         unsigned int offset, unsigned int hd_len,
3888                         int elements_needed)
3889 {
3890         struct qeth_qdio_out_buffer *buffer;
3891         unsigned int next_element;
3892         struct netdev_queue *txq;
3893         bool stopped = false;
3894         int start_index;
3895         int flush_count = 0;
3896         int do_pack = 0;
3897         int tmp;
3898         int rc = 0;
3899 
3900         /* spin until we get the queue ... */
3901         while (atomic_cmpxchg(&queue->state, QETH_OUT_Q_UNLOCKED,
3902                               QETH_OUT_Q_LOCKED) != QETH_OUT_Q_UNLOCKED);
3903         start_index = queue->next_buf_to_fill;
3904         buffer = queue->bufs[queue->next_buf_to_fill];
3905 
3906         /* Just a sanity check, the wake/stop logic should ensure that we always
3907          * get a free buffer.
3908          */
3909         if (atomic_read(&buffer->state) != QETH_QDIO_BUF_EMPTY) {
3910                 atomic_set(&queue->state, QETH_OUT_Q_UNLOCKED);
3911                 return -EBUSY;
3912         }
3913 
3914         txq = netdev_get_tx_queue(card->dev, skb_get_queue_mapping(skb));
3915 
3916         /* check if we need to switch packing state of this queue */
3917         qeth_switch_to_packing_if_needed(queue);
3918         if (queue->do_pack) {
3919                 do_pack = 1;
3920                 /* does packet fit in current buffer? */
3921                 if (buffer->next_element_to_fill + elements_needed >
3922                     queue->max_elements) {
3923                         /* ... no -> set state PRIMED */
3924                         atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3925                         flush_count++;
3926                         queue->next_buf_to_fill =
3927                                 (queue->next_buf_to_fill + 1) %
3928                                 QDIO_MAX_BUFFERS_PER_Q;
3929                         buffer = queue->bufs[queue->next_buf_to_fill];
3930 
3931                         /* We stepped forward, so sanity-check again: */
3932                         if (atomic_read(&buffer->state) !=
3933                             QETH_QDIO_BUF_EMPTY) {
3934                                 qeth_flush_buffers(queue, start_index,
3935                                                            flush_count);
3936                                 atomic_set(&queue->state,
3937                                                 QETH_OUT_Q_UNLOCKED);
3938                                 rc = -EBUSY;
3939                                 goto out;
3940                         }
3941                 }
3942         }
3943 
3944         if (buffer->next_element_to_fill == 0 &&
3945             atomic_inc_return(&queue->used_buffers) >= QDIO_MAX_BUFFERS_PER_Q) {
3946                 /* If a TX completion happens right _here_ and misses to wake
3947                  * the txq, then our re-check below will catch the race.
3948                  */
3949                 QETH_TXQ_STAT_INC(queue, stopped);
3950                 netif_tx_stop_queue(txq);
3951                 stopped = true;
3952         }
3953 
3954         next_element = qeth_fill_buffer(buffer, skb, hdr, offset, hd_len);
3955 
3956         if (queue->do_pack)
3957                 QETH_TXQ_STAT_INC(queue, skbs_pack);
3958         if (!queue->do_pack || stopped || next_element >= queue->max_elements) {
3959                 flush_count++;
3960                 atomic_set(&buffer->state, QETH_QDIO_BUF_PRIMED);
3961                 queue->next_buf_to_fill = (queue->next_buf_to_fill + 1) %
3962                                           QDIO_MAX_BUFFERS_PER_Q;
3963         }
3964 
3965         if (flush_count)
3966                 qeth_flush_buffers(queue, start_index, flush_count);
3967         else if (!atomic_read(&queue->set_pci_flags_count))
3968                 atomic_xchg(&queue->state, QETH_OUT_Q_LOCKED_FLUSH);
3969         /*
3970          * queue->state will go from LOCKED -> UNLOCKED or from
3971          * LOCKED_FLUSH -> LOCKED if output_handler wanted to 'notify' us
3972          * (switch packing state or flush buffer to get another pci flag out).
3973          * In that case we will enter this loop
3974          */
3975         while (atomic_dec_return(&queue->state)) {
3976                 start_index = queue->next_buf_to_fill;
3977                 /* check if we can go back to non-packing state */
3978                 tmp = qeth_switch_to_nonpacking_if_needed(queue);
3979                 /*
3980                  * check if we need to flush a packing buffer to get a pci
3981                  * flag out on the queue
3982                  */
3983                 if (!tmp && !atomic_read(&queue->set_pci_flags_count))
3984                         tmp = qeth_prep_flush_pack_buffer(queue);
3985                 if (tmp) {
3986                         qeth_flush_buffers(queue, start_index, tmp);
3987                         flush_count += tmp;
3988                 }
3989         }
3990 out:
3991         /* at this point the queue is UNLOCKED again */
3992         if (do_pack)
3993                 QETH_TXQ_STAT_ADD(queue, bufs_pack, flush_count);
3994 
3995         if (stopped && !qeth_out_queue_is_full(queue))
3996                 netif_tx_start_queue(txq);
3997         return rc;
3998 }
3999 EXPORT_SYMBOL_GPL(qeth_do_send_packet);
4000 
4001 static void qeth_fill_tso_ext(struct qeth_hdr_tso *hdr,
4002                               unsigned int payload_len, struct sk_buff *skb,
4003                               unsigned int proto_len)
4004 {
4005         struct qeth_hdr_ext_tso *ext = &hdr->ext;
4006 
4007         ext->hdr_tot_len = sizeof(*ext);
4008         ext->imb_hdr_no = 1;
4009         ext->hdr_type = 1;
4010         ext->hdr_version = 1;
4011         ext->hdr_len = 28;
4012         ext->payload_len = payload_len;
4013         ext->mss = skb_shinfo(skb)->gso_size;
4014         ext->dg_hdr_len = proto_len;
4015 }
4016 
4017 int qeth_xmit(struct qeth_card *card, struct sk_buff *skb,
4018               struct qeth_qdio_out_q *queue, int ipv,
4019               void (*fill_header)(struct qeth_qdio_out_q *queue,
4020                                   struct qeth_hdr *hdr, struct sk_buff *skb,
4021                                   int ipv, unsigned int data_len))
4022 {
4023         unsigned int proto_len, hw_hdr_len;
4024         unsigned int frame_len = skb->len;
4025         bool is_tso = skb_is_gso(skb);
4026         unsigned int data_offset = 0;
4027         struct qeth_hdr *hdr = NULL;
4028         unsigned int hd_len = 0;
4029         unsigned int elements;
4030         int push_len, rc;
4031 
4032         if (is_tso) {
4033                 hw_hdr_len = sizeof(struct qeth_hdr_tso);
4034                 proto_len = skb_transport_offset(skb) + tcp_hdrlen(skb);
4035         } else {
4036                 hw_hdr_len = sizeof(struct qeth_hdr);
4037                 proto_len = (IS_IQD(card) && IS_LAYER2(card)) ? ETH_HLEN : 0;
4038         }
4039 
4040         rc = skb_cow_head(skb, hw_hdr_len);
4041         if (rc)
4042                 return rc;
4043 
4044         push_len = qeth_add_hw_header(queue, skb, &hdr, hw_hdr_len, proto_len,
4045                                       &elements);
4046         if (push_len < 0)
4047                 return push_len;
4048         if (is_tso || !push_len) {
4049                 /* HW header needs its own buffer element. */
4050                 hd_len = hw_hdr_len + proto_len;
4051                 data_offset = push_len + proto_len;
4052         }
4053         memset(hdr, 0, hw_hdr_len);
4054         fill_header(queue, hdr, skb, ipv, frame_len);
4055         if (is_tso)
4056                 qeth_fill_tso_ext((struct qeth_hdr_tso *) hdr,
4057                                   frame_len - proto_len, skb, proto_len);
4058 
4059         if (IS_IQD(card)) {
4060                 rc = __qeth_xmit(card, queue, skb, elements, hdr, data_offset,
4061                                  hd_len);
4062         } else {
4063                 /* TODO: drop skb_orphan() once TX completion is fast enough */
4064                 skb_orphan(skb);
4065                 rc = qeth_do_send_packet(card, queue, skb, hdr, data_offset,
4066                                          hd_len, elements);
4067         }
4068 
4069         if (rc && !push_len)
4070                 kmem_cache_free(qeth_core_header_cache, hdr);
4071 
4072         return rc;
4073 }
4074 EXPORT_SYMBOL_GPL(qeth_xmit);
4075 
4076 static int qeth_setadp_promisc_mode_cb(struct qeth_card *card,
4077                 struct qeth_reply *reply, unsigned long data)
4078 {
4079         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4080         struct qeth_ipacmd_setadpparms *setparms;
4081 
4082         QETH_CARD_TEXT(card, 4, "prmadpcb");
4083 
4084         setparms = &(cmd->data.setadapterparms);
4085         if (qeth_setadpparms_inspect_rc(cmd)) {
4086                 QETH_CARD_TEXT_(card, 4, "prmrc%x", cmd->hdr.return_code);
4087                 setparms->data.mode = SET_PROMISC_MODE_OFF;
4088         }
4089         card->info.promisc_mode = setparms->data.mode;
4090         return (cmd->hdr.return_code) ? -EIO : 0;
4091 }
4092 
4093 void qeth_setadp_promisc_mode(struct qeth_card *card, bool enable)
4094 {
4095         enum qeth_ipa_promisc_modes mode = enable ? SET_PROMISC_MODE_ON :
4096                                                     SET_PROMISC_MODE_OFF;
4097         struct qeth_cmd_buffer *iob;
4098         struct qeth_ipa_cmd *cmd;
4099 
4100         QETH_CARD_TEXT(card, 4, "setprom");
4101         QETH_CARD_TEXT_(card, 4, "mode:%x", mode);
4102 
4103         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_PROMISC_MODE,
4104                                    SETADP_DATA_SIZEOF(mode));
4105         if (!iob)
4106                 return;
4107         cmd = __ipa_cmd(iob);
4108         cmd->data.setadapterparms.data.mode = mode;
4109         qeth_send_ipa_cmd(card, iob, qeth_setadp_promisc_mode_cb, NULL);
4110 }
4111 EXPORT_SYMBOL_GPL(qeth_setadp_promisc_mode);
4112 
4113 static int qeth_setadpparms_change_macaddr_cb(struct qeth_card *card,
4114                 struct qeth_reply *reply, unsigned long data)
4115 {
4116         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4117         struct qeth_ipacmd_setadpparms *adp_cmd;
4118 
4119         QETH_CARD_TEXT(card, 4, "chgmaccb");
4120         if (qeth_setadpparms_inspect_rc(cmd))
4121                 return -EIO;
4122 
4123         adp_cmd = &cmd->data.setadapterparms;
4124         if (!is_valid_ether_addr(adp_cmd->data.change_addr.addr))
4125                 return -EADDRNOTAVAIL;
4126 
4127         if (IS_LAYER2(card) && IS_OSD(card) && !IS_VM_NIC(card) &&
4128             !(adp_cmd->hdr.flags & QETH_SETADP_FLAGS_VIRTUAL_MAC))
4129                 return -EADDRNOTAVAIL;
4130 
4131         ether_addr_copy(card->dev->dev_addr, adp_cmd->data.change_addr.addr);
4132         return 0;
4133 }
4134 
4135 int qeth_setadpparms_change_macaddr(struct qeth_card *card)
4136 {
4137         int rc;
4138         struct qeth_cmd_buffer *iob;
4139         struct qeth_ipa_cmd *cmd;
4140 
4141         QETH_CARD_TEXT(card, 4, "chgmac");
4142 
4143         iob = qeth_get_adapter_cmd(card, IPA_SETADP_ALTER_MAC_ADDRESS,
4144                                    SETADP_DATA_SIZEOF(change_addr));
4145         if (!iob)
4146                 return -ENOMEM;
4147         cmd = __ipa_cmd(iob);
4148         cmd->data.setadapterparms.data.change_addr.cmd = CHANGE_ADDR_READ_MAC;
4149         cmd->data.setadapterparms.data.change_addr.addr_size = ETH_ALEN;
4150         ether_addr_copy(cmd->data.setadapterparms.data.change_addr.addr,
4151                         card->dev->dev_addr);
4152         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_change_macaddr_cb,
4153                                NULL);
4154         return rc;
4155 }
4156 EXPORT_SYMBOL_GPL(qeth_setadpparms_change_macaddr);
4157 
4158 static int qeth_setadpparms_set_access_ctrl_cb(struct qeth_card *card,
4159                 struct qeth_reply *reply, unsigned long data)
4160 {
4161         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4162         struct qeth_set_access_ctrl *access_ctrl_req;
4163         int fallback = *(int *)reply->param;
4164 
4165         QETH_CARD_TEXT(card, 4, "setaccb");
4166         if (cmd->hdr.return_code)
4167                 return -EIO;
4168         qeth_setadpparms_inspect_rc(cmd);
4169 
4170         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4171         QETH_CARD_TEXT_(card, 2, "rc=%d",
4172                         cmd->data.setadapterparms.hdr.return_code);
4173         if (cmd->data.setadapterparms.hdr.return_code !=
4174                                                 SET_ACCESS_CTRL_RC_SUCCESS)
4175                 QETH_DBF_MESSAGE(3, "ERR:SET_ACCESS_CTRL(%#x) on device %x: %#x\n",
4176                                  access_ctrl_req->subcmd_code, CARD_DEVID(card),
4177                                  cmd->data.setadapterparms.hdr.return_code);
4178         switch (cmd->data.setadapterparms.hdr.return_code) {
4179         case SET_ACCESS_CTRL_RC_SUCCESS:
4180                 if (card->options.isolation == ISOLATION_MODE_NONE) {
4181                         dev_info(&card->gdev->dev,
4182                             "QDIO data connection isolation is deactivated\n");
4183                 } else {
4184                         dev_info(&card->gdev->dev,
4185                             "QDIO data connection isolation is activated\n");
4186                 }
4187                 break;
4188         case SET_ACCESS_CTRL_RC_ALREADY_NOT_ISOLATED:
4189                 QETH_DBF_MESSAGE(2, "QDIO data connection isolation on device %x already deactivated\n",
4190                                  CARD_DEVID(card));
4191                 if (fallback)
4192                         card->options.isolation = card->options.prev_isolation;
4193                 break;
4194         case SET_ACCESS_CTRL_RC_ALREADY_ISOLATED:
4195                 QETH_DBF_MESSAGE(2, "QDIO data connection isolation on device %x already activated\n",
4196                                  CARD_DEVID(card));
4197                 if (fallback)
4198                         card->options.isolation = card->options.prev_isolation;
4199                 break;
4200         case SET_ACCESS_CTRL_RC_NOT_SUPPORTED:
4201                 dev_err(&card->gdev->dev, "Adapter does not "
4202                         "support QDIO data connection isolation\n");
4203                 break;
4204         case SET_ACCESS_CTRL_RC_NONE_SHARED_ADAPTER:
4205                 dev_err(&card->gdev->dev,
4206                         "Adapter is dedicated. "
4207                         "QDIO data connection isolation not supported\n");
4208                 if (fallback)
4209                         card->options.isolation = card->options.prev_isolation;
4210                 break;
4211         case SET_ACCESS_CTRL_RC_ACTIVE_CHECKSUM_OFF:
4212                 dev_err(&card->gdev->dev,
4213                         "TSO does not permit QDIO data connection isolation\n");
4214                 if (fallback)
4215                         card->options.isolation = card->options.prev_isolation;
4216                 break;
4217         case SET_ACCESS_CTRL_RC_REFLREL_UNSUPPORTED:
4218                 dev_err(&card->gdev->dev, "The adjacent switch port does not "
4219                         "support reflective relay mode\n");
4220                 if (fallback)
4221                         card->options.isolation = card->options.prev_isolation;
4222                 break;
4223         case SET_ACCESS_CTRL_RC_REFLREL_FAILED:
4224                 dev_err(&card->gdev->dev, "The reflective relay mode cannot be "
4225                                         "enabled at the adjacent switch port");
4226                 if (fallback)
4227                         card->options.isolation = card->options.prev_isolation;
4228                 break;
4229         case SET_ACCESS_CTRL_RC_REFLREL_DEACT_FAILED:
4230                 dev_warn(&card->gdev->dev, "Turning off reflective relay mode "
4231                                         "at the adjacent switch failed\n");
4232                 break;
4233         default:
4234                 /* this should never happen */
4235                 if (fallback)
4236                         card->options.isolation = card->options.prev_isolation;
4237                 break;
4238         }
4239         return (cmd->hdr.return_code) ? -EIO : 0;
4240 }
4241 
4242 static int qeth_setadpparms_set_access_ctrl(struct qeth_card *card,
4243                 enum qeth_ipa_isolation_modes isolation, int fallback)
4244 {
4245         int rc;
4246         struct qeth_cmd_buffer *iob;
4247         struct qeth_ipa_cmd *cmd;
4248         struct qeth_set_access_ctrl *access_ctrl_req;
4249 
4250         QETH_CARD_TEXT(card, 4, "setacctl");
4251 
4252         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_ACCESS_CONTROL,
4253                                    SETADP_DATA_SIZEOF(set_access_ctrl));
4254         if (!iob)
4255                 return -ENOMEM;
4256         cmd = __ipa_cmd(iob);
4257         access_ctrl_req = &cmd->data.setadapterparms.data.set_access_ctrl;
4258         access_ctrl_req->subcmd_code = isolation;
4259 
4260         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_set_access_ctrl_cb,
4261                                &fallback);
4262         QETH_CARD_TEXT_(card, 2, "rc=%d", rc);
4263         return rc;
4264 }
4265 
4266 int qeth_set_access_ctrl_online(struct qeth_card *card, int fallback)
4267 {
4268         int rc = 0;
4269 
4270         QETH_CARD_TEXT(card, 4, "setactlo");
4271 
4272         if ((IS_OSD(card) || IS_OSX(card)) &&
4273             qeth_adp_supported(card, IPA_SETADP_SET_ACCESS_CONTROL)) {
4274                 rc = qeth_setadpparms_set_access_ctrl(card,
4275                         card->options.isolation, fallback);
4276                 if (rc) {
4277                         QETH_DBF_MESSAGE(3, "IPA(SET_ACCESS_CTRL(%d) on device %x: sent failed\n",
4278                                          rc, CARD_DEVID(card));
4279                         rc = -EOPNOTSUPP;
4280                 }
4281         } else if (card->options.isolation != ISOLATION_MODE_NONE) {
4282                 card->options.isolation = ISOLATION_MODE_NONE;
4283 
4284                 dev_err(&card->gdev->dev, "Adapter does not "
4285                         "support QDIO data connection isolation\n");
4286                 rc = -EOPNOTSUPP;
4287         }
4288         return rc;
4289 }
4290 EXPORT_SYMBOL_GPL(qeth_set_access_ctrl_online);
4291 
4292 void qeth_tx_timeout(struct net_device *dev)
4293 {
4294         struct qeth_card *card;
4295 
4296         card = dev->ml_priv;
4297         QETH_CARD_TEXT(card, 4, "txtimeo");
4298         qeth_schedule_recovery(card);
4299 }
4300 EXPORT_SYMBOL_GPL(qeth_tx_timeout);
4301 
4302 static int qeth_mdio_read(struct net_device *dev, int phy_id, int regnum)
4303 {
4304         struct qeth_card *card = dev->ml_priv;
4305         int rc = 0;
4306 
4307         switch (regnum) {
4308         case MII_BMCR: /* Basic mode control register */
4309                 rc = BMCR_FULLDPLX;
4310                 if ((card->info.link_type != QETH_LINK_TYPE_GBIT_ETH) &&
4311                     (card->info.link_type != QETH_LINK_TYPE_OSN) &&
4312                     (card->info.link_type != QETH_LINK_TYPE_10GBIT_ETH) &&
4313                     (card->info.link_type != QETH_LINK_TYPE_25GBIT_ETH))
4314                         rc |= BMCR_SPEED100;
4315                 break;
4316         case MII_BMSR: /* Basic mode status register */
4317                 rc = BMSR_ERCAP | BMSR_ANEGCOMPLETE | BMSR_LSTATUS |
4318                      BMSR_10HALF | BMSR_10FULL | BMSR_100HALF | BMSR_100FULL |
4319                      BMSR_100BASE4;
4320                 break;
4321         case MII_PHYSID1: /* PHYS ID 1 */
4322                 rc = (dev->dev_addr[0] << 16) | (dev->dev_addr[1] << 8) |
4323                      dev->dev_addr[2];
4324                 rc = (rc >> 5) & 0xFFFF;
4325                 break;
4326         case MII_PHYSID2: /* PHYS ID 2 */
4327                 rc = (dev->dev_addr[2] << 10) & 0xFFFF;
4328                 break;
4329         case MII_ADVERTISE: /* Advertisement control reg */
4330                 rc = ADVERTISE_ALL;
4331                 break;
4332         case MII_LPA: /* Link partner ability reg */
4333                 rc = LPA_10HALF | LPA_10FULL | LPA_100HALF | LPA_100FULL |
4334                      LPA_100BASE4 | LPA_LPACK;
4335                 break;
4336         case MII_EXPANSION: /* Expansion register */
4337                 break;
4338         case MII_DCOUNTER: /* disconnect counter */
4339                 break;
4340         case MII_FCSCOUNTER: /* false carrier counter */
4341                 break;
4342         case MII_NWAYTEST: /* N-way auto-neg test register */
4343                 break;
4344         case MII_RERRCOUNTER: /* rx error counter */
4345                 rc = card->stats.rx_errors;
4346                 break;
4347         case MII_SREVISION: /* silicon revision */
4348                 break;
4349         case MII_RESV1: /* reserved 1 */
4350                 break;
4351         case MII_LBRERROR: /* loopback, rx, bypass error */
4352                 break;
4353         case MII_PHYADDR: /* physical address */
4354                 break;
4355         case MII_RESV2: /* reserved 2 */
4356                 break;
4357         case MII_TPISTATUS: /* TPI status for 10mbps */
4358                 break;
4359         case MII_NCONFIG: /* network interface config */
4360                 break;
4361         default:
4362                 break;
4363         }
4364         return rc;
4365 }
4366 
4367 static int qeth_snmp_command_cb(struct qeth_card *card,
4368                                 struct qeth_reply *reply, unsigned long data)
4369 {
4370         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
4371         struct qeth_arp_query_info *qinfo = reply->param;
4372         struct qeth_ipacmd_setadpparms *adp_cmd;
4373         unsigned int data_len;
4374         void *snmp_data;
4375 
4376         QETH_CARD_TEXT(card, 3, "snpcmdcb");
4377 
4378         if (cmd->hdr.return_code) {
4379                 QETH_CARD_TEXT_(card, 4, "scer1%x", cmd->hdr.return_code);
4380                 return -EIO;
4381         }
4382         if (cmd->data.setadapterparms.hdr.return_code) {
4383                 cmd->hdr.return_code =
4384                         cmd->data.setadapterparms.hdr.return_code;
4385                 QETH_CARD_TEXT_(card, 4, "scer2%x", cmd->hdr.return_code);
4386                 return -EIO;
4387         }
4388 
4389         adp_cmd = &cmd->data.setadapterparms;
4390         data_len = adp_cmd->hdr.cmdlength - sizeof(adp_cmd->hdr);
4391         if (adp_cmd->hdr.seq_no == 1) {
4392                 snmp_data = &adp_cmd->data.snmp;
4393         } else {
4394                 snmp_data = &adp_cmd->data.snmp.request;
4395                 data_len -= offsetof(struct qeth_snmp_cmd, request);
4396         }
4397 
4398         /* check if there is enough room in userspace */
4399         if ((qinfo->udata_len - qinfo->udata_offset) < data_len) {
4400                 QETH_CARD_TEXT_(card, 4, "scer3%i", -ENOSPC);
4401                 return -ENOSPC;
4402         }
4403         QETH_CARD_TEXT_(card, 4, "snore%i",
4404                         cmd->data.setadapterparms.hdr.used_total);
4405         QETH_CARD_TEXT_(card, 4, "sseqn%i",
4406                         cmd->data.setadapterparms.hdr.seq_no);
4407         /*copy entries to user buffer*/
4408         memcpy(qinfo->udata + qinfo->udata_offset, snmp_data, data_len);
4409         qinfo->udata_offset += data_len;
4410 
4411         if (cmd->data.setadapterparms.hdr.seq_no <
4412             cmd->data.setadapterparms.hdr.used_total)
4413                 return 1;
4414         return 0;
4415 }
4416 
4417 static int qeth_snmp_command(struct qeth_card *card, char __user *udata)
4418 {
4419         struct qeth_snmp_ureq __user *ureq;
4420         struct qeth_cmd_buffer *iob;
4421         unsigned int req_len;
4422         struct qeth_arp_query_info qinfo = {0, };
4423         int rc = 0;
4424 
4425         QETH_CARD_TEXT(card, 3, "snmpcmd");
4426 
4427         if (IS_VM_NIC(card))
4428                 return -EOPNOTSUPP;
4429 
4430         if ((!qeth_adp_supported(card, IPA_SETADP_SET_SNMP_CONTROL)) &&
4431             IS_LAYER3(card))
4432                 return -EOPNOTSUPP;
4433 
4434         ureq = (struct qeth_snmp_ureq __user *) udata;
4435         if (get_user(qinfo.udata_len, &ureq->hdr.data_len) ||
4436             get_user(req_len, &ureq->hdr.req_len))
4437                 return -EFAULT;
4438 
4439         /* Sanitize user input, to avoid overflows in iob size calculation: */
4440         if (req_len > QETH_BUFSIZE)
4441                 return -EINVAL;
4442 
4443         iob = qeth_get_adapter_cmd(card, IPA_SETADP_SET_SNMP_CONTROL, req_len);
4444         if (!iob)
4445                 return -ENOMEM;
4446 
4447         if (copy_from_user(&__ipa_cmd(iob)->data.setadapterparms.data.snmp,
4448                            &ureq->cmd, req_len)) {
4449                 qeth_put_cmd(iob);
4450                 return -EFAULT;
4451         }
4452 
4453         qinfo.udata = kzalloc(qinfo.udata_len, GFP_KERNEL);
4454         if (!qinfo.udata) {
4455                 qeth_put_cmd(iob);
4456                 return -ENOMEM;
4457         }
4458         qinfo.udata_offset = sizeof(struct qeth_snmp_ureq_hdr);
4459 
4460         rc = qeth_send_ipa_cmd(card, iob, qeth_snmp_command_cb, &qinfo);
4461         if (rc)
4462                 QETH_DBF_MESSAGE(2, "SNMP command failed on device %x: (%#x)\n",
4463                                  CARD_DEVID(card), rc);
4464         else {
4465                 if (copy_to_user(udata, qinfo.udata, qinfo.udata_len))
4466                         rc = -EFAULT;
4467         }
4468 
4469         kfree(qinfo.udata);
4470         return rc;
4471 }
4472 
4473 static int qeth_setadpparms_query_oat_cb(struct qeth_card *card,
4474                 struct qeth_reply *reply, unsigned long data)
4475 {
4476         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4477         struct qeth_qoat_priv *priv;
4478         char *resdata;
4479         int resdatalen;
4480 
4481         QETH_CARD_TEXT(card, 3, "qoatcb");
4482         if (qeth_setadpparms_inspect_rc(cmd))
4483                 return -EIO;
4484 
4485         priv = (struct qeth_qoat_priv *)reply->param;
4486         resdatalen = cmd->data.setadapterparms.hdr.cmdlength;
4487         resdata = (char *)data + 28;
4488 
4489         if (resdatalen > (priv->buffer_len - priv->response_len))
4490                 return -ENOSPC;
4491 
4492         memcpy((priv->buffer + priv->response_len), resdata,
4493                 resdatalen);
4494         priv->response_len += resdatalen;
4495 
4496         if (cmd->data.setadapterparms.hdr.seq_no <
4497             cmd->data.setadapterparms.hdr.used_total)
4498                 return 1;
4499         return 0;
4500 }
4501 
4502 static int qeth_query_oat_command(struct qeth_card *card, char __user *udata)
4503 {
4504         int rc = 0;
4505         struct qeth_cmd_buffer *iob;
4506         struct qeth_ipa_cmd *cmd;
4507         struct qeth_query_oat *oat_req;
4508         struct qeth_query_oat_data oat_data;
4509         struct qeth_qoat_priv priv;
4510         void __user *tmp;
4511 
4512         QETH_CARD_TEXT(card, 3, "qoatcmd");
4513 
4514         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_OAT)) {
4515                 rc = -EOPNOTSUPP;
4516                 goto out;
4517         }
4518 
4519         if (copy_from_user(&oat_data, udata,
4520             sizeof(struct qeth_query_oat_data))) {
4521                         rc = -EFAULT;
4522                         goto out;
4523         }
4524 
4525         priv.buffer_len = oat_data.buffer_len;
4526         priv.response_len = 0;
4527         priv.buffer = vzalloc(oat_data.buffer_len);
4528         if (!priv.buffer) {
4529                 rc = -ENOMEM;
4530                 goto out;
4531         }
4532 
4533         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_OAT,
4534                                    SETADP_DATA_SIZEOF(query_oat));
4535         if (!iob) {
4536                 rc = -ENOMEM;
4537                 goto out_free;
4538         }
4539         cmd = __ipa_cmd(iob);
4540         oat_req = &cmd->data.setadapterparms.data.query_oat;
4541         oat_req->subcmd_code = oat_data.command;
4542 
4543         rc = qeth_send_ipa_cmd(card, iob, qeth_setadpparms_query_oat_cb,
4544                                &priv);
4545         if (!rc) {
4546                 if (is_compat_task())
4547                         tmp = compat_ptr(oat_data.ptr);
4548                 else
4549                         tmp = (void __user *)(unsigned long)oat_data.ptr;
4550 
4551                 if (copy_to_user(tmp, priv.buffer,
4552                     priv.response_len)) {
4553                         rc = -EFAULT;
4554                         goto out_free;
4555                 }
4556 
4557                 oat_data.response_len = priv.response_len;
4558 
4559                 if (copy_to_user(udata, &oat_data,
4560                     sizeof(struct qeth_query_oat_data)))
4561                         rc = -EFAULT;
4562         }
4563 
4564 out_free:
4565         vfree(priv.buffer);
4566 out:
4567         return rc;
4568 }
4569 
4570 static int qeth_query_card_info_cb(struct qeth_card *card,
4571                                    struct qeth_reply *reply, unsigned long data)
4572 {
4573         struct carrier_info *carrier_info = (struct carrier_info *)reply->param;
4574         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *)data;
4575         struct qeth_query_card_info *card_info;
4576 
4577         QETH_CARD_TEXT(card, 2, "qcrdincb");
4578         if (qeth_setadpparms_inspect_rc(cmd))
4579                 return -EIO;
4580 
4581         card_info = &cmd->data.setadapterparms.data.card_info;
4582         carrier_info->card_type = card_info->card_type;
4583         carrier_info->port_mode = card_info->port_mode;
4584         carrier_info->port_speed = card_info->port_speed;
4585         return 0;
4586 }
4587 
4588 int qeth_query_card_info(struct qeth_card *card,
4589                          struct carrier_info *carrier_info)
4590 {
4591         struct qeth_cmd_buffer *iob;
4592 
4593         QETH_CARD_TEXT(card, 2, "qcrdinfo");
4594         if (!qeth_adp_supported(card, IPA_SETADP_QUERY_CARD_INFO))
4595                 return -EOPNOTSUPP;
4596         iob = qeth_get_adapter_cmd(card, IPA_SETADP_QUERY_CARD_INFO, 0);
4597         if (!iob)
4598                 return -ENOMEM;
4599         return qeth_send_ipa_cmd(card, iob, qeth_query_card_info_cb,
4600                                         (void *)carrier_info);
4601 }
4602 
4603 /**
4604  * qeth_vm_request_mac() - Request a hypervisor-managed MAC address
4605  * @card: pointer to a qeth_card
4606  *
4607  * Returns
4608  *      0, if a MAC address has been set for the card's netdevice
4609  *      a return code, for various error conditions
4610  */
4611 int qeth_vm_request_mac(struct qeth_card *card)
4612 {
4613         struct diag26c_mac_resp *response;
4614         struct diag26c_mac_req *request;
4615         struct ccw_dev_id id;
4616         int rc;
4617 
4618         QETH_CARD_TEXT(card, 2, "vmreqmac");
4619 
4620         request = kzalloc(sizeof(*request), GFP_KERNEL | GFP_DMA);
4621         response = kzalloc(sizeof(*response), GFP_KERNEL | GFP_DMA);
4622         if (!request || !response) {
4623                 rc = -ENOMEM;
4624                 goto out;
4625         }
4626 
4627         ccw_device_get_id(CARD_DDEV(card), &id);
4628         request->resp_buf_len = sizeof(*response);
4629         request->resp_version = DIAG26C_VERSION2;
4630         request->op_code = DIAG26C_GET_MAC;
4631         request->devno = id.devno;
4632 
4633         QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4634         rc = diag26c(request, response, DIAG26C_MAC_SERVICES);
4635         QETH_DBF_HEX(CTRL, 2, request, sizeof(*request));
4636         if (rc)
4637                 goto out;
4638         QETH_DBF_HEX(CTRL, 2, response, sizeof(*response));
4639 
4640         if (request->resp_buf_len < sizeof(*response) ||
4641             response->version != request->resp_version) {
4642                 rc = -EIO;
4643                 QETH_CARD_TEXT(card, 2, "badresp");
4644                 QETH_CARD_HEX(card, 2, &request->resp_buf_len,
4645                               sizeof(request->resp_buf_len));
4646         } else if (!is_valid_ether_addr(response->mac)) {
4647                 rc = -EINVAL;
4648                 QETH_CARD_TEXT(card, 2, "badmac");
4649                 QETH_CARD_HEX(card, 2, response->mac, ETH_ALEN);
4650         } else {
4651                 ether_addr_copy(card->dev->dev_addr, response->mac);
4652         }
4653 
4654 out:
4655         kfree(response);
4656         kfree(request);
4657         return rc;
4658 }
4659 EXPORT_SYMBOL_GPL(qeth_vm_request_mac);
4660 
4661 static void qeth_determine_capabilities(struct qeth_card *card)
4662 {
4663         struct qeth_channel *channel = &card->data;
4664         struct ccw_device *ddev = channel->ccwdev;
4665         int rc;
4666         int ddev_offline = 0;
4667 
4668         QETH_CARD_TEXT(card, 2, "detcapab");
4669         if (!ddev->online) {
4670                 ddev_offline = 1;
4671                 rc = ccw_device_set_online(ddev);
4672                 if (rc) {
4673                         QETH_CARD_TEXT_(card, 2, "3err%d", rc);
4674                         goto out;
4675                 }
4676         }
4677 
4678         rc = qeth_read_conf_data(card);
4679         if (rc) {
4680                 QETH_DBF_MESSAGE(2, "qeth_read_conf_data on device %x returned %i\n",
4681                                  CARD_DEVID(card), rc);
4682                 QETH_CARD_TEXT_(card, 2, "5err%d", rc);
4683                 goto out_offline;
4684         }
4685 
4686         rc = qdio_get_ssqd_desc(ddev, &card->ssqd);
4687         if (rc)
4688                 QETH_CARD_TEXT_(card, 2, "6err%d", rc);
4689 
4690         QETH_CARD_TEXT_(card, 2, "qfmt%d", card->ssqd.qfmt);
4691         QETH_CARD_TEXT_(card, 2, "ac1:%02x", card->ssqd.qdioac1);
4692         QETH_CARD_TEXT_(card, 2, "ac2:%04x", card->ssqd.qdioac2);
4693         QETH_CARD_TEXT_(card, 2, "ac3:%04x", card->ssqd.qdioac3);
4694         QETH_CARD_TEXT_(card, 2, "icnt%d", card->ssqd.icnt);
4695         if (!((card->ssqd.qfmt != QDIO_IQDIO_QFMT) ||
4696             ((card->ssqd.qdioac1 & CHSC_AC1_INITIATE_INPUTQ) == 0) ||
4697             ((card->ssqd.qdioac3 & CHSC_AC3_FORMAT2_CQ_AVAILABLE) == 0))) {
4698                 dev_info(&card->gdev->dev,
4699                         "Completion Queueing supported\n");
4700         } else {
4701                 card->options.cq = QETH_CQ_NOTAVAILABLE;
4702         }
4703 
4704 
4705 out_offline:
4706         if (ddev_offline == 1)
4707                 qeth_stop_channel(channel);
4708 out:
4709         return;
4710 }
4711 
4712 static void qeth_qdio_establish_cq(struct qeth_card *card,
4713                                    struct qdio_buffer **in_sbal_ptrs,
4714                                    void (**queue_start_poll)
4715                                         (struct ccw_device *, int,
4716                                          unsigned long))
4717 {
4718         int i;
4719 
4720         if (card->options.cq == QETH_CQ_ENABLED) {
4721                 int offset = QDIO_MAX_BUFFERS_PER_Q *
4722                              (card->qdio.no_in_queues - 1);
4723 
4724                 for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++)
4725                         in_sbal_ptrs[offset + i] =
4726                                 card->qdio.c_q->bufs[i].buffer;
4727 
4728                 queue_start_poll[card->qdio.no_in_queues - 1] = NULL;
4729         }
4730 }
4731 
4732 static int qeth_qdio_establish(struct qeth_card *card)
4733 {
4734         struct qdio_initialize init_data;
4735         char *qib_param_field;
4736         struct qdio_buffer **in_sbal_ptrs;
4737         void (**queue_start_poll) (struct ccw_device *, int, unsigned long);
4738         struct qdio_buffer **out_sbal_ptrs;
4739         int i, j, k;
4740         int rc = 0;
4741 
4742         QETH_CARD_TEXT(card, 2, "qdioest");
4743 
4744         qib_param_field = kzalloc(FIELD_SIZEOF(struct qib, parm), GFP_KERNEL);
4745         if (!qib_param_field) {
4746                 rc =  -ENOMEM;
4747                 goto out_free_nothing;
4748         }
4749 
4750         qeth_create_qib_param_field(card, qib_param_field);
4751         qeth_create_qib_param_field_blkt(card, qib_param_field);
4752 
4753         in_sbal_ptrs = kcalloc(card->qdio.no_in_queues * QDIO_MAX_BUFFERS_PER_Q,
4754                                sizeof(void *),
4755                                GFP_KERNEL);
4756         if (!in_sbal_ptrs) {
4757                 rc = -ENOMEM;
4758                 goto out_free_qib_param;
4759         }
4760 
4761         for (i = 0; i < QDIO_MAX_BUFFERS_PER_Q; i++)
4762                 in_sbal_ptrs[i] = card->qdio.in_q->bufs[i].buffer;
4763 
4764         queue_start_poll = kcalloc(card->qdio.no_in_queues, sizeof(void *),
4765                                    GFP_KERNEL);
4766         if (!queue_start_poll) {
4767                 rc = -ENOMEM;
4768                 goto out_free_in_sbals;
4769         }
4770         for (i = 0; i < card->qdio.no_in_queues; ++i)
4771                 queue_start_poll[i] = qeth_qdio_start_poll;
4772 
4773         qeth_qdio_establish_cq(card, in_sbal_ptrs, queue_start_poll);
4774 
4775         out_sbal_ptrs =
4776                 kcalloc(card->qdio.no_out_queues * QDIO_MAX_BUFFERS_PER_Q,
4777                         sizeof(void *),
4778                         GFP_KERNEL);
4779         if (!out_sbal_ptrs) {
4780                 rc = -ENOMEM;
4781                 goto out_free_queue_start_poll;
4782         }
4783 
4784         for (i = 0, k = 0; i < card->qdio.no_out_queues; ++i)
4785                 for (j = 0; j < QDIO_MAX_BUFFERS_PER_Q; j++, k++)
4786                         out_sbal_ptrs[k] =
4787                                 card->qdio.out_qs[i]->bufs[j]->buffer;
4788 
4789         memset(&init_data, 0, sizeof(struct qdio_initialize));
4790         init_data.cdev                   = CARD_DDEV(card);
4791         init_data.q_format               = IS_IQD(card) ? QDIO_IQDIO_QFMT :
4792                                                           QDIO_QETH_QFMT;
4793         init_data.qib_param_field_format = 0;
4794         init_data.qib_param_field        = qib_param_field;
4795         init_data.no_input_qs            = card->qdio.no_in_queues;
4796         init_data.no_output_qs           = card->qdio.no_out_queues;
4797         init_data.input_handler          = qeth_qdio_input_handler;
4798         init_data.output_handler         = qeth_qdio_output_handler;
4799         init_data.queue_start_poll_array = queue_start_poll;
4800         init_data.int_parm               = (unsigned long) card;
4801         init_data.input_sbal_addr_array  = in_sbal_ptrs;
4802         init_data.output_sbal_addr_array = out_sbal_ptrs;
4803         init_data.output_sbal_state_array = card->qdio.out_bufstates;
4804         init_data.scan_threshold         = IS_IQD(card) ? 0 : 32;
4805 
4806         if (atomic_cmpxchg(&card->qdio.state, QETH_QDIO_ALLOCATED,
4807                 QETH_QDIO_ESTABLISHED) == QETH_QDIO_ALLOCATED) {
4808                 rc = qdio_allocate(&init_data);
4809                 if (rc) {
4810                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4811                         goto out;
4812                 }
4813                 rc = qdio_establish(&init_data);
4814                 if (rc) {
4815                         atomic_set(&card->qdio.state, QETH_QDIO_ALLOCATED);
4816                         qdio_free(CARD_DDEV(card));
4817                 }
4818         }
4819 
4820         switch (card->options.cq) {
4821         case QETH_CQ_ENABLED:
4822                 dev_info(&card->gdev->dev, "Completion Queue support enabled");
4823                 break;
4824         case QETH_CQ_DISABLED:
4825                 dev_info(&card->gdev->dev, "Completion Queue support disabled");
4826                 break;
4827         default:
4828                 break;
4829         }
4830 out:
4831         kfree(out_sbal_ptrs);
4832 out_free_queue_start_poll:
4833         kfree(queue_start_poll);
4834 out_free_in_sbals:
4835         kfree(in_sbal_ptrs);
4836 out_free_qib_param:
4837         kfree(qib_param_field);
4838 out_free_nothing:
4839         return rc;
4840 }
4841 
4842 static void qeth_core_free_card(struct qeth_card *card)
4843 {
4844         QETH_CARD_TEXT(card, 2, "freecrd");
4845         qeth_clean_channel(&card->read);
4846         qeth_clean_channel(&card->write);
4847         qeth_clean_channel(&card->data);
4848         qeth_put_cmd(card->read_cmd);
4849         destroy_workqueue(card->event_wq);
4850         qeth_free_qdio_queues(card);
4851         unregister_service_level(&card->qeth_service_level);
4852         dev_set_drvdata(&card->gdev->dev, NULL);
4853         kfree(card);
4854 }
4855 
4856 void qeth_trace_features(struct qeth_card *card)
4857 {
4858         QETH_CARD_TEXT(card, 2, "features");
4859         QETH_CARD_HEX(card, 2, &card->options.ipa4, sizeof(card->options.ipa4));
4860         QETH_CARD_HEX(card, 2, &card->options.ipa6, sizeof(card->options.ipa6));
4861         QETH_CARD_HEX(card, 2, &card->options.adp, sizeof(card->options.adp));
4862         QETH_CARD_HEX(card, 2, &card->info.diagass_support,
4863                       sizeof(card->info.diagass_support));
4864 }
4865 EXPORT_SYMBOL_GPL(qeth_trace_features);
4866 
4867 static struct ccw_device_id qeth_ids[] = {
4868         {CCW_DEVICE_DEVTYPE(0x1731, 0x01, 0x1732, 0x01),
4869                                         .driver_info = QETH_CARD_TYPE_OSD},
4870         {CCW_DEVICE_DEVTYPE(0x1731, 0x05, 0x1732, 0x05),
4871                                         .driver_info = QETH_CARD_TYPE_IQD},
4872         {CCW_DEVICE_DEVTYPE(0x1731, 0x06, 0x1732, 0x06),
4873                                         .driver_info = QETH_CARD_TYPE_OSN},
4874         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x03),
4875                                         .driver_info = QETH_CARD_TYPE_OSM},
4876         {CCW_DEVICE_DEVTYPE(0x1731, 0x02, 0x1732, 0x02),
4877                                         .driver_info = QETH_CARD_TYPE_OSX},
4878         {},
4879 };
4880 MODULE_DEVICE_TABLE(ccw, qeth_ids);
4881 
4882 static struct ccw_driver qeth_ccw_driver = {
4883         .driver = {
4884                 .owner = THIS_MODULE,
4885                 .name = "qeth",
4886         },
4887         .ids = qeth_ids,
4888         .probe = ccwgroup_probe_ccwdev,
4889         .remove = ccwgroup_remove_ccwdev,
4890 };
4891 
4892 int qeth_core_hardsetup_card(struct qeth_card *card, bool *carrier_ok)
4893 {
4894         int retries = 3;
4895         int rc;
4896 
4897         QETH_CARD_TEXT(card, 2, "hrdsetup");
4898         atomic_set(&card->force_alloc_skb, 0);
4899         rc = qeth_update_from_chp_desc(card);
4900         if (rc)
4901                 return rc;
4902 retry:
4903         if (retries < 3)
4904                 QETH_DBF_MESSAGE(2, "Retrying to do IDX activates on device %x.\n",
4905                                  CARD_DEVID(card));
4906         rc = qeth_qdio_clear_card(card, !IS_IQD(card));
4907         qeth_stop_channel(&card->data);
4908         qeth_stop_channel(&card->write);
4909         qeth_stop_channel(&card->read);
4910         qdio_free(CARD_DDEV(card));
4911         rc = ccw_device_set_online(CARD_RDEV(card));
4912         if (rc)
4913                 goto retriable;
4914         rc = ccw_device_set_online(CARD_WDEV(card));
4915         if (rc)
4916                 goto retriable;
4917         rc = ccw_device_set_online(CARD_DDEV(card));
4918         if (rc)
4919                 goto retriable;
4920 retriable:
4921         if (rc == -ERESTARTSYS) {
4922                 QETH_CARD_TEXT(card, 2, "break1");
4923                 return rc;
4924         } else if (rc) {
4925                 QETH_CARD_TEXT_(card, 2, "1err%d", rc);
4926                 if (--retries < 0)
4927                         goto out;
4928                 else
4929                         goto retry;
4930         }
4931         qeth_determine_capabilities(card);
4932         qeth_init_tokens(card);
4933         qeth_init_func_level(card);
4934 
4935         rc = qeth_idx_activate_read_channel(card);
4936         if (rc == -EINTR) {
4937                 QETH_CARD_TEXT(card, 2, "break2");
4938                 return rc;
4939         } else if (rc) {
4940                 QETH_CARD_TEXT_(card, 2, "3err%d", rc);
4941                 if (--retries < 0)
4942                         goto out;
4943                 else
4944                         goto retry;
4945         }
4946 
4947         rc = qeth_idx_activate_write_channel(card);
4948         if (rc == -EINTR) {
4949                 QETH_CARD_TEXT(card, 2, "break3");
4950                 return rc;
4951         } else if (rc) {
4952                 QETH_CARD_TEXT_(card, 2, "4err%d", rc);
4953                 if (--retries < 0)
4954                         goto out;
4955                 else
4956                         goto retry;
4957         }
4958         card->read_or_write_problem = 0;
4959         rc = qeth_mpc_initialize(card);
4960         if (rc) {
4961                 QETH_CARD_TEXT_(card, 2, "5err%d", rc);
4962                 goto out;
4963         }
4964 
4965         rc = qeth_send_startlan(card);
4966         if (rc) {
4967                 QETH_CARD_TEXT_(card, 2, "6err%d", rc);
4968                 if (rc == -ENETDOWN) {
4969                         dev_warn(&card->gdev->dev, "The LAN is offline\n");
4970                         *carrier_ok = false;
4971                 } else {
4972                         goto out;
4973                 }
4974         } else {
4975                 *carrier_ok = true;
4976         }
4977 
4978         card->options.ipa4.supported_funcs = 0;
4979         card->options.ipa6.supported_funcs = 0;
4980         card->options.adp.supported_funcs = 0;
4981         card->options.sbp.supported_funcs = 0;
4982         card->info.diagass_support = 0;
4983         rc = qeth_query_ipassists(card, QETH_PROT_IPV4);
4984         if (rc == -ENOMEM)
4985                 goto out;
4986         if (qeth_is_supported(card, IPA_IPV6)) {
4987                 rc = qeth_query_ipassists(card, QETH_PROT_IPV6);
4988                 if (rc == -ENOMEM)
4989                         goto out;
4990         }
4991         if (qeth_is_supported(card, IPA_SETADAPTERPARMS)) {
4992                 rc = qeth_query_setadapterparms(card);
4993                 if (rc < 0) {
4994                         QETH_CARD_TEXT_(card, 2, "7err%d", rc);
4995                         goto out;
4996                 }
4997         }
4998         if (qeth_adp_supported(card, IPA_SETADP_SET_DIAG_ASSIST)) {
4999                 rc = qeth_query_setdiagass(card);
5000                 if (rc)
5001                         QETH_CARD_TEXT_(card, 2, "8err%d", rc);
5002         }
5003         return 0;
5004 out:
5005         dev_warn(&card->gdev->dev, "The qeth device driver failed to recover "
5006                 "an error on the device\n");
5007         QETH_DBF_MESSAGE(2, "Initialization for device %x failed in hardsetup! rc=%d\n",
5008                          CARD_DEVID(card), rc);
5009         return rc;
5010 }
5011 EXPORT_SYMBOL_GPL(qeth_core_hardsetup_card);
5012 
5013 static void qeth_create_skb_frag(struct qdio_buffer_element *element,
5014                                  struct sk_buff *skb, int offset, int data_len)
5015 {
5016         struct page *page = virt_to_page(element->addr);
5017         unsigned int next_frag;
5018 
5019         /* first fill the linear space */
5020         if (!skb->len) {
5021                 unsigned int linear = min(data_len, skb_tailroom(skb));
5022 
5023                 skb_put_data(skb, element->addr + offset, linear);
5024                 data_len -= linear;
5025                 if (!data_len)
5026                         return;
5027                 offset += linear;
5028                 /* fall through to add page frag for remaining data */
5029         }
5030 
5031         next_frag = skb_shinfo(skb)->nr_frags;
5032         get_page(page);
5033         skb_add_rx_frag(skb, next_frag, page, offset, data_len, data_len);
5034 }
5035 
5036 static inline int qeth_is_last_sbale(struct qdio_buffer_element *sbale)
5037 {
5038         return (sbale->eflags & SBAL_EFLAGS_LAST_ENTRY);
5039 }
5040 
5041 struct sk_buff *qeth_core_get_next_skb(struct qeth_card *card,
5042                 struct qeth_qdio_buffer *qethbuffer,
5043                 struct qdio_buffer_element **__element, int *__offset,
5044                 struct qeth_hdr **hdr)
5045 {
5046         struct qdio_buffer_element *element = *__element;
5047         struct qdio_buffer *buffer = qethbuffer->buffer;
5048         int offset = *__offset;
5049         struct sk_buff *skb;
5050         int skb_len = 0;
5051         void *data_ptr;
5052         int data_len;
5053         int headroom = 0;
5054         int use_rx_sg = 0;
5055 
5056         /* qeth_hdr must not cross element boundaries */
5057         while (element->length < offset + sizeof(struct qeth_hdr)) {
5058                 if (qeth_is_last_sbale(element))
5059                         return NULL;
5060                 element++;
5061                 offset = 0;
5062         }
5063         *hdr = element->addr + offset;
5064 
5065         offset += sizeof(struct qeth_hdr);
5066         switch ((*hdr)->hdr.l2.id) {
5067         case QETH_HEADER_TYPE_LAYER2:
5068                 skb_len = (*hdr)->hdr.l2.pkt_length;
5069                 break;
5070         case QETH_HEADER_TYPE_LAYER3:
5071                 skb_len = (*hdr)->hdr.l3.length;
5072                 headroom = ETH_HLEN;
5073                 break;
5074         case QETH_HEADER_TYPE_OSN:
5075                 skb_len = (*hdr)->hdr.osn.pdu_length;
5076                 headroom = sizeof(struct qeth_hdr);
5077                 break;
5078         default:
5079                 break;
5080         }
5081 
5082         if (!skb_len)
5083                 return NULL;
5084 
5085         if (((skb_len >= card->options.rx_sg_cb) &&
5086              !IS_OSN(card) &&
5087              (!atomic_read(&card->force_alloc_skb))) ||
5088             (card->options.cq == QETH_CQ_ENABLED))
5089                 use_rx_sg = 1;
5090 
5091         if (use_rx_sg && qethbuffer->rx_skb) {
5092                 /* QETH_CQ_ENABLED only: */
5093                 skb = qethbuffer->rx_skb;
5094                 qethbuffer->rx_skb = NULL;
5095         } else {
5096                 unsigned int linear = (use_rx_sg) ? QETH_RX_PULL_LEN : skb_len;
5097 
5098                 skb = napi_alloc_skb(&card->napi, linear + headroom);
5099         }
5100         if (!skb)
5101                 goto no_mem;
5102         if (headroom)
5103                 skb_reserve(skb, headroom);
5104 
5105         data_ptr = element->addr + offset;
5106         while (skb_len) {
5107                 data_len = min(skb_len, (int)(element->length - offset));
5108                 if (data_len) {
5109                         if (use_rx_sg)
5110                                 qeth_create_skb_frag(element, skb, offset,
5111                                                      data_len);
5112                         else
5113                                 skb_put_data(skb, data_ptr, data_len);
5114                 }
5115                 skb_len -= data_len;
5116                 if (skb_len) {
5117                         if (qeth_is_last_sbale(element)) {
5118                                 QETH_CARD_TEXT(card, 4, "unexeob");
5119                                 QETH_CARD_HEX(card, 2, buffer, sizeof(void *));
5120                                 dev_kfree_skb_any(skb);
5121                                 QETH_CARD_STAT_INC(card, rx_errors);
5122                                 return NULL;
5123                         }
5124                         element++;
5125                         offset = 0;
5126                         data_ptr = element->addr;
5127                 } else {
5128                         offset += data_len;
5129                 }
5130         }
5131         *__element = element;
5132         *__offset = offset;
5133         if (use_rx_sg) {
5134                 QETH_CARD_STAT_INC(card, rx_sg_skbs);
5135                 QETH_CARD_STAT_ADD(card, rx_sg_frags,
5136                                    skb_shinfo(skb)->nr_frags);
5137         }
5138         return skb;
5139 no_mem:
5140         if (net_ratelimit()) {
5141                 QETH_CARD_TEXT(card, 2, "noskbmem");
5142         }
5143         QETH_CARD_STAT_INC(card, rx_dropped);
5144         return NULL;
5145 }
5146 EXPORT_SYMBOL_GPL(qeth_core_get_next_skb);
5147 
5148 int qeth_poll(struct napi_struct *napi, int budget)
5149 {
5150         struct qeth_card *card = container_of(napi, struct qeth_card, napi);
5151         int work_done = 0;
5152         struct qeth_qdio_buffer *buffer;
5153         int done;
5154         int new_budget = budget;
5155 
5156         while (1) {
5157                 if (!card->rx.b_count) {
5158                         card->rx.qdio_err = 0;
5159                         card->rx.b_count = qdio_get_next_buffers(
5160                                 card->data.ccwdev, 0, &card->rx.b_index,
5161                                 &card->rx.qdio_err);
5162                         if (card->rx.b_count <= 0) {
5163                                 card->rx.b_count = 0;
5164                                 break;
5165                         }
5166                         card->rx.b_element =
5167                                 &card->qdio.in_q->bufs[card->rx.b_index]
5168                                 .buffer->element[0];
5169                         card->rx.e_offset = 0;
5170                 }
5171 
5172                 while (card->rx.b_count) {
5173                         buffer = &card->qdio.in_q->bufs[card->rx.b_index];
5174                         if (!(card->rx.qdio_err &&
5175                             qeth_check_qdio_errors(card, buffer->buffer,
5176                             card->rx.qdio_err, "qinerr")))
5177                                 work_done +=
5178                                         card->discipline->process_rx_buffer(
5179                                                 card, new_budget, &done);
5180                         else
5181                                 done = 1;
5182 
5183                         if (done) {
5184                                 QETH_CARD_STAT_INC(card, rx_bufs);
5185                                 qeth_put_buffer_pool_entry(card,
5186                                         buffer->pool_entry);
5187                                 qeth_queue_input_buffer(card, card->rx.b_index);
5188                                 card->rx.b_count--;
5189                                 if (card->rx.b_count) {
5190                                         card->rx.b_index =
5191                                                 (card->rx.b_index + 1) %
5192                                                 QDIO_MAX_BUFFERS_PER_Q;
5193                                         card->rx.b_element =
5194                                                 &card->qdio.in_q
5195                                                 ->bufs[card->rx.b_index]
5196                                                 .buffer->element[0];
5197                                         card->rx.e_offset = 0;
5198                                 }
5199                         }
5200 
5201                         if (work_done >= budget)
5202                                 goto out;
5203                         else
5204                                 new_budget = budget - work_done;
5205                 }
5206         }
5207 
5208         napi_complete_done(napi, work_done);
5209         if (qdio_start_irq(card->data.ccwdev, 0))
5210                 napi_schedule(&card->napi);
5211 out:
5212         return work_done;
5213 }
5214 EXPORT_SYMBOL_GPL(qeth_poll);
5215 
5216 static void qeth_iqd_tx_complete(struct qeth_qdio_out_q *queue,
5217                                  unsigned int bidx, bool error, int budget)
5218 {
5219         struct qeth_qdio_out_buffer *buffer = queue->bufs[bidx];
5220         u8 sflags = buffer->buffer->element[15].sflags;
5221         struct qeth_card *card = queue->card;
5222 
5223         if (queue->bufstates && (queue->bufstates[bidx].flags &
5224                                  QDIO_OUTBUF_STATE_FLAG_PENDING)) {
5225                 WARN_ON_ONCE(card->options.cq != QETH_CQ_ENABLED);
5226 
5227                 if (atomic_cmpxchg(&buffer->state, QETH_QDIO_BUF_PRIMED,
5228                                                    QETH_QDIO_BUF_PENDING) ==
5229                     QETH_QDIO_BUF_PRIMED)
5230                         qeth_notify_skbs(queue, buffer, TX_NOTIFY_PENDING);
5231 
5232                 QETH_CARD_TEXT_(card, 5, "pel%u", bidx);
5233 
5234                 /* prepare the queue slot for re-use: */
5235                 qeth_scrub_qdio_buffer(buffer->buffer, queue->max_elements);
5236                 if (qeth_init_qdio_out_buf(queue, bidx)) {
5237                         QETH_CARD_TEXT(card, 2, "outofbuf");
5238                         qeth_schedule_recovery(card);
5239                 }
5240 
5241                 return;
5242         }
5243 
5244         if (card->options.cq == QETH_CQ_ENABLED)
5245                 qeth_notify_skbs(queue, buffer,
5246                                  qeth_compute_cq_notification(sflags, 0));
5247         qeth_clear_output_buffer(queue, buffer, error, budget);
5248 }
5249 
5250 static int qeth_tx_poll(struct napi_struct *napi, int budget)
5251 {
5252         struct qeth_qdio_out_q *queue = qeth_napi_to_out_queue(napi);
5253         unsigned int queue_no = queue->queue_no;
5254         struct qeth_card *card = queue->card;
5255         struct net_device *dev = card->dev;
5256         unsigned int work_done = 0;
5257         struct netdev_queue *txq;
5258 
5259         txq = netdev_get_tx_queue(dev, qeth_iqd_translate_txq(dev, queue_no));
5260 
5261         while (1) {
5262                 unsigned int start, error, i;
5263                 unsigned int packets = 0;
5264                 unsigned int bytes = 0;
5265                 int completed;
5266 
5267                 if (qeth_out_queue_is_empty(queue)) {
5268                         napi_complete(napi);
5269                         return 0;
5270                 }
5271 
5272                 /* Give the CPU a breather: */
5273                 if (work_done >= QDIO_MAX_BUFFERS_PER_Q) {
5274                         QETH_TXQ_STAT_INC(queue, completion_yield);
5275                         if (napi_complete_done(napi, 0))
5276                                 napi_schedule(napi);
5277                         return 0;
5278                 }
5279 
5280                 completed = qdio_inspect_queue(CARD_DDEV(card), queue_no, false,
5281                                                &start, &error);
5282                 if (completed <= 0) {
5283                         /* Ensure we see TX completion for pending work: */
5284                         if (napi_complete_done(napi, 0))
5285                                 qeth_tx_arm_timer(queue);
5286                         return 0;
5287                 }
5288 
5289                 for (i = start; i < start + completed; i++) {
5290                         struct qeth_qdio_out_buffer *buffer;
5291                         unsigned int bidx = QDIO_BUFNR(i);
5292 
5293                         buffer = queue->bufs[bidx];
5294                         packets += skb_queue_len(&buffer->skb_list);
5295                         bytes += buffer->bytes;
5296 
5297                         qeth_handle_send_error(card, buffer, error);
5298                         qeth_iqd_tx_complete(queue, bidx, error, budget);
5299                         qeth_cleanup_handled_pending(queue, bidx, false);
5300                 }
5301 
5302                 netdev_tx_completed_queue(txq, packets, bytes);
5303                 atomic_sub(completed, &queue->used_buffers);
5304                 work_done += completed;
5305 
5306                 /* xmit may have observed the full-condition, but not yet
5307                  * stopped the txq. In which case the code below won't trigger.
5308                  * So before returning, xmit will re-check the txq's fill level
5309                  * and wake it up if needed.
5310                  */
5311                 if (netif_tx_queue_stopped(txq) &&
5312                     !qeth_out_queue_is_full(queue))
5313                         netif_tx_wake_queue(txq);
5314         }
5315 }
5316 
5317 static int qeth_setassparms_inspect_rc(struct qeth_ipa_cmd *cmd)
5318 {
5319         if (!cmd->hdr.return_code)
5320                 cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5321         return cmd->hdr.return_code;
5322 }
5323 
5324 static int qeth_setassparms_get_caps_cb(struct qeth_card *card,
5325                                         struct qeth_reply *reply,
5326                                         unsigned long data)
5327 {
5328         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
5329         struct qeth_ipa_caps *caps = reply->param;
5330 
5331         if (qeth_setassparms_inspect_rc(cmd))
5332                 return -EIO;
5333 
5334         caps->supported = cmd->data.setassparms.data.caps.supported;
5335         caps->enabled = cmd->data.setassparms.data.caps.enabled;
5336         return 0;
5337 }
5338 
5339 int qeth_setassparms_cb(struct qeth_card *card,
5340                         struct qeth_reply *reply, unsigned long data)
5341 {
5342         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
5343 
5344         QETH_CARD_TEXT(card, 4, "defadpcb");
5345 
5346         if (cmd->hdr.return_code)
5347                 return -EIO;
5348 
5349         cmd->hdr.return_code = cmd->data.setassparms.hdr.return_code;
5350         if (cmd->hdr.prot_version == QETH_PROT_IPV4)
5351                 card->options.ipa4.enabled_funcs = cmd->hdr.ipa_enabled;
5352         if (cmd->hdr.prot_version == QETH_PROT_IPV6)
5353                 card->options.ipa6.enabled_funcs = cmd->hdr.ipa_enabled;
5354         return 0;
5355 }
5356 EXPORT_SYMBOL_GPL(qeth_setassparms_cb);
5357 
5358 struct qeth_cmd_buffer *qeth_get_setassparms_cmd(struct qeth_card *card,
5359                                                  enum qeth_ipa_funcs ipa_func,
5360                                                  u16 cmd_code,
5361                                                  unsigned int data_length,
5362                                                  enum qeth_prot_versions prot)
5363 {
5364         struct qeth_ipacmd_setassparms *setassparms;
5365         struct qeth_ipacmd_setassparms_hdr *hdr;
5366         struct qeth_cmd_buffer *iob;
5367 
5368         QETH_CARD_TEXT(card, 4, "getasscm");
5369         iob = qeth_ipa_alloc_cmd(card, IPA_CMD_SETASSPARMS, prot,
5370                                  data_length +
5371                                  offsetof(struct qeth_ipacmd_setassparms,
5372                                           data));
5373         if (!iob)
5374                 return NULL;
5375 
5376         setassparms = &__ipa_cmd(iob)->data.setassparms;
5377         setassparms->assist_no = ipa_func;
5378 
5379         hdr = &setassparms->hdr;
5380         hdr->length = sizeof(*hdr) + data_length;
5381         hdr->command_code = cmd_code;
5382         return iob;
5383 }
5384 EXPORT_SYMBOL_GPL(qeth_get_setassparms_cmd);
5385 
5386 int qeth_send_simple_setassparms_prot(struct qeth_card *card,
5387                                       enum qeth_ipa_funcs ipa_func,
5388                                       u16 cmd_code, u32 *data,
5389                                       enum qeth_prot_versions prot)
5390 {
5391         unsigned int length = data ? SETASS_DATA_SIZEOF(flags_32bit) : 0;
5392         struct qeth_cmd_buffer *iob;
5393 
5394         QETH_CARD_TEXT_(card, 4, "simassp%i", prot);
5395         iob = qeth_get_setassparms_cmd(card, ipa_func, cmd_code, length, prot);
5396         if (!iob)
5397                 return -ENOMEM;
5398 
5399         if (data)
5400                 __ipa_cmd(iob)->data.setassparms.data.flags_32bit = *data;
5401         return qeth_send_ipa_cmd(card, iob, qeth_setassparms_cb, NULL);
5402 }
5403 EXPORT_SYMBOL_GPL(qeth_send_simple_setassparms_prot);
5404 
5405 static void qeth_unregister_dbf_views(void)
5406 {
5407         int x;
5408         for (x = 0; x < QETH_DBF_INFOS; x++) {
5409                 debug_unregister(qeth_dbf[x].id);
5410                 qeth_dbf[x].id = NULL;
5411         }
5412 }
5413 
5414 void qeth_dbf_longtext(debug_info_t *id, int level, char *fmt, ...)
5415 {
5416         char dbf_txt_buf[32];
5417         va_list args;
5418 
5419         if (!debug_level_enabled(id, level))
5420                 return;
5421         va_start(args, fmt);
5422         vsnprintf(dbf_txt_buf, sizeof(dbf_txt_buf), fmt, args);
5423         va_end(args);
5424         debug_text_event(id, level, dbf_txt_buf);
5425 }
5426 EXPORT_SYMBOL_GPL(qeth_dbf_longtext);
5427 
5428 static int qeth_register_dbf_views(void)
5429 {
5430         int ret;
5431         int x;
5432 
5433         for (x = 0; x < QETH_DBF_INFOS; x++) {
5434                 /* register the areas */
5435                 qeth_dbf[x].id = debug_register(qeth_dbf[x].name,
5436                                                 qeth_dbf[x].pages,
5437                                                 qeth_dbf[x].areas,
5438                                                 qeth_dbf[x].len);
5439                 if (qeth_dbf[x].id == NULL) {
5440                         qeth_unregister_dbf_views();
5441                         return -ENOMEM;
5442                 }
5443 
5444                 /* register a view */
5445                 ret = debug_register_view(qeth_dbf[x].id, qeth_dbf[x].view);
5446                 if (ret) {
5447                         qeth_unregister_dbf_views();
5448                         return ret;
5449                 }
5450 
5451                 /* set a passing level */
5452                 debug_set_level(qeth_dbf[x].id, qeth_dbf[x].level);
5453         }
5454 
5455         return 0;
5456 }
5457 
5458 static DEFINE_MUTEX(qeth_mod_mutex);    /* for synchronized module loading */
5459 
5460 int qeth_core_load_discipline(struct qeth_card *card,
5461                 enum qeth_discipline_id discipline)
5462 {
5463         mutex_lock(&qeth_mod_mutex);
5464         switch (discipline) {
5465         case QETH_DISCIPLINE_LAYER3:
5466                 card->discipline = try_then_request_module(
5467                         symbol_get(qeth_l3_discipline), "qeth_l3");
5468                 break;
5469         case QETH_DISCIPLINE_LAYER2:
5470                 card->discipline = try_then_request_module(
5471                         symbol_get(qeth_l2_discipline), "qeth_l2");
5472                 break;
5473         default:
5474                 break;
5475         }
5476         mutex_unlock(&qeth_mod_mutex);
5477 
5478         if (!card->discipline) {
5479                 dev_err(&card->gdev->dev, "There is no kernel module to "
5480                         "support discipline %d\n", discipline);
5481                 return -EINVAL;
5482         }
5483 
5484         card->options.layer = discipline;
5485         return 0;
5486 }
5487 
5488 void qeth_core_free_discipline(struct qeth_card *card)
5489 {
5490         if (IS_LAYER2(card))
5491                 symbol_put(qeth_l2_discipline);
5492         else
5493                 symbol_put(qeth_l3_discipline);
5494         card->options.layer = QETH_DISCIPLINE_UNDETERMINED;
5495         card->discipline = NULL;
5496 }
5497 
5498 const struct device_type qeth_generic_devtype = {
5499         .name = "qeth_generic",
5500         .groups = qeth_generic_attr_groups,
5501 };
5502 EXPORT_SYMBOL_GPL(qeth_generic_devtype);
5503 
5504 static const struct device_type qeth_osn_devtype = {
5505         .name = "qeth_osn",
5506         .groups = qeth_osn_attr_groups,
5507 };
5508 
5509 #define DBF_NAME_LEN    20
5510 
5511 struct qeth_dbf_entry {
5512         char dbf_name[DBF_NAME_LEN];
5513         debug_info_t *dbf_info;
5514         struct list_head dbf_list;
5515 };
5516 
5517 static LIST_HEAD(qeth_dbf_list);
5518 static DEFINE_MUTEX(qeth_dbf_list_mutex);
5519 
5520 static debug_info_t *qeth_get_dbf_entry(char *name)
5521 {
5522         struct qeth_dbf_entry *entry;
5523         debug_info_t *rc = NULL;
5524 
5525         mutex_lock(&qeth_dbf_list_mutex);
5526         list_for_each_entry(entry, &qeth_dbf_list, dbf_list) {
5527                 if (strcmp(entry->dbf_name, name) == 0) {
5528                         rc = entry->dbf_info;
5529                         break;
5530                 }
5531         }
5532         mutex_unlock(&qeth_dbf_list_mutex);
5533         return rc;
5534 }
5535 
5536 static int qeth_add_dbf_entry(struct qeth_card *card, char *name)
5537 {
5538         struct qeth_dbf_entry *new_entry;
5539 
5540         card->debug = debug_register(name, 2, 1, 8);
5541         if (!card->debug) {
5542                 QETH_DBF_TEXT_(SETUP, 2, "%s", "qcdbf");
5543                 goto err;
5544         }
5545         if (debug_register_view(card->debug, &debug_hex_ascii_view))
5546                 goto err_dbg;
5547         new_entry = kzalloc(sizeof(struct qeth_dbf_entry), GFP_KERNEL);
5548         if (!new_entry)
5549                 goto err_dbg;
5550         strncpy(new_entry->dbf_name, name, DBF_NAME_LEN);
5551         new_entry->dbf_info = card->debug;
5552         mutex_lock(&qeth_dbf_list_mutex);
5553         list_add(&new_entry->dbf_list, &qeth_dbf_list);
5554         mutex_unlock(&qeth_dbf_list_mutex);
5555 
5556         return 0;
5557 
5558 err_dbg:
5559         debug_unregister(card->debug);
5560 err:
5561         return -ENOMEM;
5562 }
5563 
5564 static void qeth_clear_dbf_list(void)
5565 {
5566         struct qeth_dbf_entry *entry, *tmp;
5567 
5568         mutex_lock(&qeth_dbf_list_mutex);
5569         list_for_each_entry_safe(entry, tmp, &qeth_dbf_list, dbf_list) {
5570                 list_del(&entry->dbf_list);
5571                 debug_unregister(entry->dbf_info);
5572                 kfree(entry);
5573         }
5574         mutex_unlock(&qeth_dbf_list_mutex);
5575 }
5576 
5577 static struct net_device *qeth_alloc_netdev(struct qeth_card *card)
5578 {
5579         struct net_device *dev;
5580 
5581         switch (card->info.type) {
5582         case QETH_CARD_TYPE_IQD:
5583                 dev = alloc_netdev_mqs(0, "hsi%d", NET_NAME_UNKNOWN,
5584                                        ether_setup, QETH_MAX_QUEUES, 1);
5585                 break;
5586         case QETH_CARD_TYPE_OSM:
5587                 dev = alloc_etherdev(0);
5588                 break;
5589         case QETH_CARD_TYPE_OSN:
5590                 dev = alloc_netdev(0, "osn%d", NET_NAME_UNKNOWN, ether_setup);
5591                 break;
5592         default:
5593                 dev = alloc_etherdev_mqs(0, QETH_MAX_QUEUES, 1);
5594         }
5595 
5596         if (!dev)
5597                 return NULL;
5598 
5599         dev->ml_priv = card;
5600         dev->watchdog_timeo = QETH_TX_TIMEOUT;
5601         dev->min_mtu = IS_OSN(card) ? 64 : 576;
5602          /* initialized when device first goes online: */
5603         dev->max_mtu = 0;
5604         dev->mtu = 0;
5605         SET_NETDEV_DEV(dev, &card->gdev->dev);
5606         netif_carrier_off(dev);
5607 
5608         if (IS_OSN(card)) {
5609                 dev->ethtool_ops = &qeth_osn_ethtool_ops;
5610         } else {
5611                 dev->ethtool_ops = &qeth_ethtool_ops;
5612                 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
5613                 dev->hw_features |= NETIF_F_SG;
5614                 dev->vlan_features |= NETIF_F_SG;
5615                 if (IS_IQD(card)) {
5616                         dev->features |= NETIF_F_SG;
5617                         if (netif_set_real_num_tx_queues(dev,
5618                                                          QETH_IQD_MIN_TXQ)) {
5619                                 free_netdev(dev);
5620                                 return NULL;
5621                         }
5622                 }
5623         }
5624 
5625         return dev;
5626 }
5627 
5628 struct net_device *qeth_clone_netdev(struct net_device *orig)
5629 {
5630         struct net_device *clone = qeth_alloc_netdev(orig->ml_priv);
5631 
5632         if (!clone)
5633                 return NULL;
5634 
5635         clone->dev_port = orig->dev_port;
5636         return clone;
5637 }
5638 
5639 static int qeth_core_probe_device(struct ccwgroup_device *gdev)
5640 {
5641         struct qeth_card *card;
5642         struct device *dev;
5643         int rc;
5644         enum qeth_discipline_id enforced_disc;
5645         char dbf_name[DBF_NAME_LEN];
5646 
5647         QETH_DBF_TEXT(SETUP, 2, "probedev");
5648 
5649         dev = &gdev->dev;
5650         if (!get_device(dev))
5651                 return -ENODEV;
5652 
5653         QETH_DBF_TEXT_(SETUP, 2, "%s", dev_name(&gdev->dev));
5654 
5655         card = qeth_alloc_card(gdev);
5656         if (!card) {
5657                 QETH_DBF_TEXT_(SETUP, 2, "1err%d", -ENOMEM);
5658                 rc = -ENOMEM;
5659                 goto err_dev;
5660         }
5661 
5662         snprintf(dbf_name, sizeof(dbf_name), "qeth_card_%s",
5663                 dev_name(&gdev->dev));
5664         card->debug = qeth_get_dbf_entry(dbf_name);
5665         if (!card->debug) {
5666                 rc = qeth_add_dbf_entry(card, dbf_name);
5667                 if (rc)
5668                         goto err_card;
5669         }
5670 
5671         qeth_setup_card(card);
5672         card->dev = qeth_alloc_netdev(card);
5673         if (!card->dev) {
5674                 rc = -ENOMEM;
5675                 goto err_card;
5676         }
5677 
5678         card->qdio.no_out_queues = card->dev->num_tx_queues;
5679         rc = qeth_update_from_chp_desc(card);
5680         if (rc)
5681                 goto err_chp_desc;
5682         qeth_determine_capabilities(card);
5683         qeth_set_blkt_defaults(card);
5684 
5685         enforced_disc = qeth_enforce_discipline(card);
5686         switch (enforced_disc) {
5687         case QETH_DISCIPLINE_UNDETERMINED:
5688                 gdev->dev.type = &qeth_generic_devtype;
5689                 break;
5690         default:
5691                 card->info.layer_enforced = true;
5692                 rc = qeth_core_load_discipline(card, enforced_disc);
5693                 if (rc)
5694                         goto err_load;
5695 
5696                 gdev->dev.type = IS_OSN(card) ? &qeth_osn_devtype :
5697                                                 card->discipline->devtype;
5698                 rc = card->discipline->setup(card->gdev);
5699                 if (rc)
5700                         goto err_disc;
5701                 break;
5702         }
5703 
5704         return 0;
5705 
5706 err_disc:
5707         qeth_core_free_discipline(card);
5708 err_load:
5709 err_chp_desc:
5710         free_netdev(card->dev);
5711 err_card:
5712         qeth_core_free_card(card);
5713 err_dev:
5714         put_device(dev);
5715         return rc;
5716 }
5717 
5718 static void qeth_core_remove_device(struct ccwgroup_device *gdev)
5719 {
5720         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5721 
5722         QETH_CARD_TEXT(card, 2, "removedv");
5723 
5724         if (card->discipline) {
5725                 card->discipline->remove(gdev);
5726                 qeth_core_free_discipline(card);
5727         }
5728 
5729         free_netdev(card->dev);
5730         qeth_core_free_card(card);
5731         put_device(&gdev->dev);
5732 }
5733 
5734 static int qeth_core_set_online(struct ccwgroup_device *gdev)
5735 {
5736         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5737         int rc = 0;
5738         enum qeth_discipline_id def_discipline;
5739 
5740         if (!card->discipline) {
5741                 def_discipline = IS_IQD(card) ? QETH_DISCIPLINE_LAYER3 :
5742                                                 QETH_DISCIPLINE_LAYER2;
5743                 rc = qeth_core_load_discipline(card, def_discipline);
5744                 if (rc)
5745                         goto err;
5746                 rc = card->discipline->setup(card->gdev);
5747                 if (rc) {
5748                         qeth_core_free_discipline(card);
5749                         goto err;
5750                 }
5751         }
5752         rc = card->discipline->set_online(gdev);
5753 err:
5754         return rc;
5755 }
5756 
5757 static int qeth_core_set_offline(struct ccwgroup_device *gdev)
5758 {
5759         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5760         return card->discipline->set_offline(gdev);
5761 }
5762 
5763 static void qeth_core_shutdown(struct ccwgroup_device *gdev)
5764 {
5765         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5766         qeth_set_allowed_threads(card, 0, 1);
5767         if ((gdev->state == CCWGROUP_ONLINE) && card->info.hwtrap)
5768                 qeth_hw_trap(card, QETH_DIAGS_TRAP_DISARM);
5769         qeth_qdio_clear_card(card, 0);
5770         qeth_drain_output_queues(card);
5771         qdio_free(CARD_DDEV(card));
5772 }
5773 
5774 static int qeth_suspend(struct ccwgroup_device *gdev)
5775 {
5776         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5777 
5778         qeth_set_allowed_threads(card, 0, 1);
5779         wait_event(card->wait_q, qeth_threads_running(card, 0xffffffff) == 0);
5780         if (gdev->state == CCWGROUP_OFFLINE)
5781                 return 0;
5782 
5783         card->discipline->set_offline(gdev);
5784         return 0;
5785 }
5786 
5787 static int qeth_resume(struct ccwgroup_device *gdev)
5788 {
5789         struct qeth_card *card = dev_get_drvdata(&gdev->dev);
5790         int rc;
5791 
5792         rc = card->discipline->set_online(gdev);
5793 
5794         qeth_set_allowed_threads(card, 0xffffffff, 0);
5795         if (rc)
5796                 dev_warn(&card->gdev->dev, "The qeth device driver failed to recover an error on the device\n");
5797         return rc;
5798 }
5799 
5800 static ssize_t group_store(struct device_driver *ddrv, const char *buf,
5801                            size_t count)
5802 {
5803         int err;
5804 
5805         err = ccwgroup_create_dev(qeth_core_root_dev, to_ccwgroupdrv(ddrv), 3,
5806                                   buf);
5807 
5808         return err ? err : count;
5809 }
5810 static DRIVER_ATTR_WO(group);
5811 
5812 static struct attribute *qeth_drv_attrs[] = {
5813         &driver_attr_group.attr,
5814         NULL,
5815 };
5816 static struct attribute_group qeth_drv_attr_group = {
5817         .attrs = qeth_drv_attrs,
5818 };
5819 static const struct attribute_group *qeth_drv_attr_groups[] = {
5820         &qeth_drv_attr_group,
5821         NULL,
5822 };
5823 
5824 static struct ccwgroup_driver qeth_core_ccwgroup_driver = {
5825         .driver = {
5826                 .groups = qeth_drv_attr_groups,
5827                 .owner = THIS_MODULE,
5828                 .name = "qeth",
5829         },
5830         .ccw_driver = &qeth_ccw_driver,
5831         .setup = qeth_core_probe_device,
5832         .remove = qeth_core_remove_device,
5833         .set_online = qeth_core_set_online,
5834         .set_offline = qeth_core_set_offline,
5835         .shutdown = qeth_core_shutdown,
5836         .prepare = NULL,
5837         .complete = NULL,
5838         .freeze = qeth_suspend,
5839         .thaw = qeth_resume,
5840         .restore = qeth_resume,
5841 };
5842 
5843 struct qeth_card *qeth_get_card_by_busid(char *bus_id)
5844 {
5845         struct ccwgroup_device *gdev;
5846         struct qeth_card *card;
5847 
5848         gdev = get_ccwgroupdev_by_busid(&qeth_core_ccwgroup_driver, bus_id);
5849         if (!gdev)
5850                 return NULL;
5851 
5852         card = dev_get_drvdata(&gdev->dev);
5853         put_device(&gdev->dev);
5854         return card;
5855 }
5856 EXPORT_SYMBOL_GPL(qeth_get_card_by_busid);
5857 
5858 int qeth_do_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
5859 {
5860         struct qeth_card *card = dev->ml_priv;
5861         struct mii_ioctl_data *mii_data;
5862         int rc = 0;
5863 
5864         if (!card)
5865                 return -ENODEV;
5866 
5867         switch (cmd) {
5868         case SIOC_QETH_ADP_SET_SNMP_CONTROL:
5869                 rc = qeth_snmp_command(card, rq->ifr_ifru.ifru_data);
5870                 break;
5871         case SIOC_QETH_GET_CARD_TYPE:
5872                 if ((IS_OSD(card) || IS_OSM(card) || IS_OSX(card)) &&
5873                     !IS_VM_NIC(card))
5874                         return 1;
5875                 return 0;
5876         case SIOCGMIIPHY:
5877                 mii_data = if_mii(rq);
5878                 mii_data->phy_id = 0;
5879                 break;
5880         case SIOCGMIIREG:
5881                 mii_data = if_mii(rq);
5882                 if (mii_data->phy_id != 0)
5883                         rc = -EINVAL;
5884                 else
5885                         mii_data->val_out = qeth_mdio_read(dev,
5886                                 mii_data->phy_id, mii_data->reg_num);
5887                 break;
5888         case SIOC_QETH_QUERY_OAT:
5889                 rc = qeth_query_oat_command(card, rq->ifr_ifru.ifru_data);
5890                 break;
5891         default:
5892                 if (card->discipline->do_ioctl)
5893                         rc = card->discipline->do_ioctl(dev, rq, cmd);
5894                 else
5895                         rc = -EOPNOTSUPP;
5896         }
5897         if (rc)
5898                 QETH_CARD_TEXT_(card, 2, "ioce%x", rc);
5899         return rc;
5900 }
5901 EXPORT_SYMBOL_GPL(qeth_do_ioctl);
5902 
5903 static int qeth_start_csum_cb(struct qeth_card *card, struct qeth_reply *reply,
5904                               unsigned long data)
5905 {
5906         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
5907         u32 *features = reply->param;
5908 
5909         if (qeth_setassparms_inspect_rc(cmd))
5910                 return -EIO;
5911 
5912         *features = cmd->data.setassparms.data.flags_32bit;
5913         return 0;
5914 }
5915 
5916 static int qeth_set_csum_off(struct qeth_card *card, enum qeth_ipa_funcs cstype,
5917                              enum qeth_prot_versions prot)
5918 {
5919         return qeth_send_simple_setassparms_prot(card, cstype, IPA_CMD_ASS_STOP,
5920                                                  NULL, prot);
5921 }
5922 
5923 static int qeth_set_csum_on(struct qeth_card *card, enum qeth_ipa_funcs cstype,
5924                             enum qeth_prot_versions prot)
5925 {
5926         u32 required_features = QETH_IPA_CHECKSUM_UDP | QETH_IPA_CHECKSUM_TCP;
5927         struct qeth_cmd_buffer *iob;
5928         struct qeth_ipa_caps caps;
5929         u32 features;
5930         int rc;
5931 
5932         /* some L3 HW requires combined L3+L4 csum offload: */
5933         if (IS_LAYER3(card) && prot == QETH_PROT_IPV4 &&
5934             cstype == IPA_OUTBOUND_CHECKSUM)
5935                 required_features |= QETH_IPA_CHECKSUM_IP_HDR;
5936 
5937         iob = qeth_get_setassparms_cmd(card, cstype, IPA_CMD_ASS_START, 0,
5938                                        prot);
5939         if (!iob)
5940                 return -ENOMEM;
5941 
5942         rc = qeth_send_ipa_cmd(card, iob, qeth_start_csum_cb, &features);
5943         if (rc)
5944                 return rc;
5945 
5946         if ((required_features & features) != required_features) {
5947                 qeth_set_csum_off(card, cstype, prot);
5948                 return -EOPNOTSUPP;
5949         }
5950 
5951         iob = qeth_get_setassparms_cmd(card, cstype, IPA_CMD_ASS_ENABLE,
5952                                        SETASS_DATA_SIZEOF(flags_32bit),
5953                                        prot);
5954         if (!iob) {
5955                 qeth_set_csum_off(card, cstype, prot);
5956                 return -ENOMEM;
5957         }
5958 
5959         if (features & QETH_IPA_CHECKSUM_LP2LP)
5960                 required_features |= QETH_IPA_CHECKSUM_LP2LP;
5961         __ipa_cmd(iob)->data.setassparms.data.flags_32bit = required_features;
5962         rc = qeth_send_ipa_cmd(card, iob, qeth_setassparms_get_caps_cb, &caps);
5963         if (rc) {
5964                 qeth_set_csum_off(card, cstype, prot);
5965                 return rc;
5966         }
5967 
5968         if (!qeth_ipa_caps_supported(&caps, required_features) ||
5969             !qeth_ipa_caps_enabled(&caps, required_features)) {
5970                 qeth_set_csum_off(card, cstype, prot);
5971                 return -EOPNOTSUPP;
5972         }
5973 
5974         dev_info(&card->gdev->dev, "HW Checksumming (%sbound IPv%d) enabled\n",
5975                  cstype == IPA_INBOUND_CHECKSUM ? "in" : "out", prot);
5976         if (!qeth_ipa_caps_enabled(&caps, QETH_IPA_CHECKSUM_LP2LP) &&
5977             cstype == IPA_OUTBOUND_CHECKSUM)
5978                 dev_warn(&card->gdev->dev,
5979                          "Hardware checksumming is performed only if %s and its peer use different OSA Express 3 ports\n",
5980                          QETH_CARD_IFNAME(card));
5981         return 0;
5982 }
5983 
5984 static int qeth_set_ipa_csum(struct qeth_card *card, bool on, int cstype,
5985                              enum qeth_prot_versions prot)
5986 {
5987         return on ? qeth_set_csum_on(card, cstype, prot) :
5988                     qeth_set_csum_off(card, cstype, prot);
5989 }
5990 
5991 static int qeth_start_tso_cb(struct qeth_card *card, struct qeth_reply *reply,
5992                              unsigned long data)
5993 {
5994         struct qeth_ipa_cmd *cmd = (struct qeth_ipa_cmd *) data;
5995         struct qeth_tso_start_data *tso_data = reply->param;
5996 
5997         if (qeth_setassparms_inspect_rc(cmd))
5998                 return -EIO;
5999 
6000         tso_data->mss = cmd->data.setassparms.data.tso.mss;
6001         tso_data->supported = cmd->data.setassparms.data.tso.supported;
6002         return 0;
6003 }
6004 
6005 static int qeth_set_tso_off(struct qeth_card *card,
6006                             enum qeth_prot_versions prot)
6007 {
6008         return qeth_send_simple_setassparms_prot(card, IPA_OUTBOUND_TSO,
6009                                                  IPA_CMD_ASS_STOP, NULL, prot);
6010 }
6011 
6012 static int qeth_set_tso_on(struct qeth_card *card,
6013                            enum qeth_prot_versions prot)
6014 {
6015         struct qeth_tso_start_data tso_data;
6016         struct qeth_cmd_buffer *iob;
6017         struct qeth_ipa_caps caps;
6018         int rc;
6019 
6020         iob = qeth_get_setassparms_cmd(card, IPA_OUTBOUND_TSO,
6021                                        IPA_CMD_ASS_START, 0, prot);
6022         if (!iob)
6023                 return -ENOMEM;
6024 
6025         rc = qeth_send_ipa_cmd(card, iob, qeth_start_tso_cb, &tso_data);
6026         if (rc)
6027                 return rc;
6028 
6029         if (!tso_data.mss || !(tso_data.supported & QETH_IPA_LARGE_SEND_TCP)) {
6030                 qeth_set_tso_off(card, prot);
6031                 return -EOPNOTSUPP;
6032         }
6033 
6034         iob = qeth_get_setassparms_cmd(card, IPA_OUTBOUND_TSO,
6035                                        IPA_CMD_ASS_ENABLE,
6036                                        SETASS_DATA_SIZEOF(caps), prot);
6037         if (!iob) {
6038                 qeth_set_tso_off(card, prot);
6039                 return -ENOMEM;
6040         }
6041 
6042         /* enable TSO capability */
6043         __ipa_cmd(iob)->data.setassparms.data.caps.enabled =
6044                 QETH_IPA_LARGE_SEND_TCP;
6045         rc = qeth_send_ipa_cmd(card, iob, qeth_setassparms_get_caps_cb, &caps);
6046         if (rc) {
6047                 qeth_set_tso_off(card, prot);
6048                 return rc;
6049         }
6050 
6051         if (!qeth_ipa_caps_supported(&caps, QETH_IPA_LARGE_SEND_TCP) ||
6052             !qeth_ipa_caps_enabled(&caps, QETH_IPA_LARGE_SEND_TCP)) {
6053                 qeth_set_tso_off(card, prot);
6054                 return -EOPNOTSUPP;
6055         }
6056 
6057         dev_info(&card->gdev->dev, "TSOv%u enabled (MSS: %u)\n", prot,
6058                  tso_data.mss);
6059         return 0;
6060 }
6061 
6062 static int qeth_set_ipa_tso(struct qeth_card *card, bool on,
6063                             enum qeth_prot_versions prot)
6064 {
6065         return on ? qeth_set_tso_on(card, prot) : qeth_set_tso_off(card, prot);
6066 }
6067 
6068 static int qeth_set_ipa_rx_csum(struct qeth_card *card, bool on)
6069 {
6070         int rc_ipv4 = (on) ? -EOPNOTSUPP : 0;
6071         int rc_ipv6;
6072 
6073         if (qeth_is_supported(card, IPA_INBOUND_CHECKSUM))
6074                 rc_ipv4 = qeth_set_ipa_csum(card, on, IPA_INBOUND_CHECKSUM,
6075                                             QETH_PROT_IPV4);
6076         if (!qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6))
6077                 /* no/one Offload Assist available, so the rc is trivial */
6078                 return rc_ipv4;
6079 
6080         rc_ipv6 = qeth_set_ipa_csum(card, on, IPA_INBOUND_CHECKSUM,
6081                                     QETH_PROT_IPV6);
6082 
6083         if (on)
6084                 /* enable: success if any Assist is active */
6085                 return (rc_ipv6) ? rc_ipv4 : 0;
6086 
6087         /* disable: failure if any Assist is still active */
6088         return (rc_ipv6) ? rc_ipv6 : rc_ipv4;
6089 }
6090 
6091 /**
6092  * qeth_enable_hw_features() - (Re-)Enable HW functions for device features
6093  * @dev:        a net_device
6094  */
6095 void qeth_enable_hw_features(struct net_device *dev)
6096 {
6097         struct qeth_card *card = dev->ml_priv;
6098         netdev_features_t features;
6099 
6100         features = dev->features;
6101         /* force-off any feature that might need an IPA sequence.
6102          * netdev_update_features() will restart them.
6103          */
6104         dev->features &= ~dev->hw_features;
6105         /* toggle VLAN filter, so that VIDs are re-programmed: */
6106         if (IS_LAYER2(card) && IS_VM_NIC(card)) {
6107                 dev->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
6108                 dev->wanted_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
6109         }
6110         netdev_update_features(dev);
6111         if (features != dev->features)
6112                 dev_warn(&card->gdev->dev,
6113                          "Device recovery failed to restore all offload features\n");
6114 }
6115 EXPORT_SYMBOL_GPL(qeth_enable_hw_features);
6116 
6117 int qeth_set_features(struct net_device *dev, netdev_features_t features)
6118 {
6119         struct qeth_card *card = dev->ml_priv;
6120         netdev_features_t changed = dev->features ^ features;
6121         int rc = 0;
6122 
6123         QETH_CARD_TEXT(card, 2, "setfeat");
6124         QETH_CARD_HEX(card, 2, &features, sizeof(features));
6125 
6126         if ((changed & NETIF_F_IP_CSUM)) {
6127                 rc = qeth_set_ipa_csum(card, features & NETIF_F_IP_CSUM,
6128                                        IPA_OUTBOUND_CHECKSUM, QETH_PROT_IPV4);
6129                 if (rc)
6130                         changed ^= NETIF_F_IP_CSUM;
6131         }
6132         if (changed & NETIF_F_IPV6_CSUM) {
6133                 rc = qeth_set_ipa_csum(card, features & NETIF_F_IPV6_CSUM,
6134                                        IPA_OUTBOUND_CHECKSUM, QETH_PROT_IPV6);
6135                 if (rc)
6136                         changed ^= NETIF_F_IPV6_CSUM;
6137         }
6138         if (changed & NETIF_F_RXCSUM) {
6139                 rc = qeth_set_ipa_rx_csum(card, features & NETIF_F_RXCSUM);
6140                 if (rc)
6141                         changed ^= NETIF_F_RXCSUM;
6142         }
6143         if (changed & NETIF_F_TSO) {
6144                 rc = qeth_set_ipa_tso(card, features & NETIF_F_TSO,
6145                                       QETH_PROT_IPV4);
6146                 if (rc)
6147                         changed ^= NETIF_F_TSO;
6148         }
6149         if (changed & NETIF_F_TSO6) {
6150                 rc = qeth_set_ipa_tso(card, features & NETIF_F_TSO6,
6151                                       QETH_PROT_IPV6);
6152                 if (rc)
6153                         changed ^= NETIF_F_TSO6;
6154         }
6155 
6156         /* everything changed successfully? */
6157         if ((dev->features ^ features) == changed)
6158                 return 0;
6159         /* something went wrong. save changed features and return error */
6160         dev->features ^= changed;
6161         return -EIO;
6162 }
6163 EXPORT_SYMBOL_GPL(qeth_set_features);
6164 
6165 netdev_features_t qeth_fix_features(struct net_device *dev,
6166                                     netdev_features_t features)
6167 {
6168         struct qeth_card *card = dev->ml_priv;
6169 
6170         QETH_CARD_TEXT(card, 2, "fixfeat");
6171         if (!qeth_is_supported(card, IPA_OUTBOUND_CHECKSUM))
6172                 features &= ~NETIF_F_IP_CSUM;
6173         if (!qeth_is_supported6(card, IPA_OUTBOUND_CHECKSUM_V6))
6174                 features &= ~NETIF_F_IPV6_CSUM;
6175         if (!qeth_is_supported(card, IPA_INBOUND_CHECKSUM) &&
6176             !qeth_is_supported6(card, IPA_INBOUND_CHECKSUM_V6))
6177                 features &= ~NETIF_F_RXCSUM;
6178         if (!qeth_is_supported(card, IPA_OUTBOUND_TSO))
6179                 features &= ~NETIF_F_TSO;
6180         if (!qeth_is_supported6(card, IPA_OUTBOUND_TSO))
6181                 features &= ~NETIF_F_TSO6;
6182 
6183         QETH_CARD_HEX(card, 2, &features, sizeof(features));
6184         return features;
6185 }
6186 EXPORT_SYMBOL_GPL(qeth_fix_features);
6187 
6188 netdev_features_t qeth_features_check(struct sk_buff *skb,
6189                                       struct net_device *dev,
6190                                       netdev_features_t features)
6191 {
6192         /* GSO segmentation builds skbs with
6193          *      a (small) linear part for the headers, and
6194          *      page frags for the data.
6195          * Compared to a linear skb, the header-only part consumes an
6196          * additional buffer element. This reduces buffer utilization, and
6197          * hurts throughput. So compress small segments into one element.
6198          */
6199         if (netif_needs_gso(skb, features)) {
6200                 /* match skb_segment(): */
6201                 unsigned int doffset = skb->data - skb_mac_header(skb);
6202                 unsigned int hsize = skb_shinfo(skb)->gso_size;
6203                 unsigned int hroom = skb_headroom(skb);
6204 
6205                 /* linearize only if resulting skb allocations are order-0: */
6206                 if (SKB_DATA_ALIGN(hroom + doffset + hsize) <= SKB_MAX_HEAD(0))
6207                         features &= ~NETIF_F_SG;
6208         }
6209 
6210         return vlan_features_check(skb, features);
6211 }
6212 EXPORT_SYMBOL_GPL(qeth_features_check);
6213 
6214 void qeth_get_stats64(struct net_device *dev, struct rtnl_link_stats64 *stats)
6215 {
6216         struct qeth_card *card = dev->ml_priv;
6217         struct qeth_qdio_out_q *queue;
6218         unsigned int i;
6219 
6220         QETH_CARD_TEXT(card, 5, "getstat");
6221 
6222         stats->rx_packets = card->stats.rx_packets;
6223         stats->rx_bytes = card->stats.rx_bytes;
6224         stats->rx_errors = card->stats.rx_errors;
6225         stats->rx_dropped = card->stats.rx_dropped;
6226         stats->multicast = card->stats.rx_multicast;
6227 
6228         for (i = 0; i < card->qdio.no_out_queues; i++) {
6229                 queue = card->qdio.out_qs[i];
6230 
6231                 stats->tx_packets += queue->stats.tx_packets;
6232                 stats->tx_bytes += queue->stats.tx_bytes;
6233                 stats->tx_errors += queue->stats.tx_errors;
6234                 stats->tx_dropped += queue->stats.tx_dropped;
6235         }
6236 }
6237 EXPORT_SYMBOL_GPL(qeth_get_stats64);
6238 
6239 u16 qeth_iqd_select_queue(struct net_device *dev, struct sk_buff *skb,
6240                           u8 cast_type, struct net_device *sb_dev)
6241 {
6242         if (cast_type != RTN_UNICAST)
6243                 return QETH_IQD_MCAST_TXQ;
6244         return QETH_IQD_MIN_UCAST_TXQ;
6245 }
6246 EXPORT_SYMBOL_GPL(qeth_iqd_select_queue);
6247 
6248 int qeth_open(struct net_device *dev)
6249 {
6250         struct qeth_card *card = dev->ml_priv;
6251 
6252         QETH_CARD_TEXT(card, 4, "qethopen");
6253 
6254         if (qdio_stop_irq(CARD_DDEV(card), 0) < 0)
6255                 return -EIO;
6256 
6257         card->data.state = CH_STATE_UP;
6258         netif_tx_start_all_queues(dev);
6259 
6260         napi_enable(&card->napi);
6261         local_bh_disable();
6262         napi_schedule(&card->napi);
6263         if (IS_IQD(card)) {
6264                 struct qeth_qdio_out_q *queue;
6265                 unsigned int i;
6266 
6267                 qeth_for_each_output_queue(card, queue, i) {
6268                         netif_tx_napi_add(dev, &queue->napi, qeth_tx_poll,
6269                                           QETH_NAPI_WEIGHT);
6270                         napi_enable(&queue->napi);
6271                         napi_schedule(&queue->napi);
6272                 }
6273         }
6274         /* kick-start the NAPI softirq: */
6275         local_bh_enable();
6276         return 0;
6277 }
6278 EXPORT_SYMBOL_GPL(qeth_open);
6279 
6280 int qeth_stop(struct net_device *dev)
6281 {
6282         struct qeth_card *card = dev->ml_priv;
6283 
6284         QETH_CARD_TEXT(card, 4, "qethstop");
6285         if (IS_IQD(card)) {
6286                 struct qeth_qdio_out_q *queue;
6287                 unsigned int i;
6288 
6289                 /* Quiesce the NAPI instances: */
6290                 qeth_for_each_output_queue(card, queue, i) {
6291                         napi_disable(&queue->napi);
6292                         del_timer_sync(&queue->timer);
6293                 }
6294 
6295                 /* Stop .ndo_start_xmit, might still access queue->napi. */
6296                 netif_tx_disable(dev);
6297 
6298                 /* Queues may get re-allocated, so remove the NAPIs here. */
6299                 qeth_for_each_output_queue(card, queue, i)
6300                         netif_napi_del(&queue->napi);
6301         } else {
6302                 netif_tx_disable(dev);
6303         }
6304 
6305         napi_disable(&card->napi);
6306         return 0;
6307 }
6308 EXPORT_SYMBOL_GPL(qeth_stop);
6309 
6310 static int __init qeth_core_init(void)
6311 {
6312         int rc;
6313 
6314         pr_info("loading core functions\n");
6315 
6316         rc = qeth_register_dbf_views();
6317         if (rc)
6318                 goto dbf_err;
6319         qeth_core_root_dev = root_device_register("qeth");
6320         rc = PTR_ERR_OR_ZERO(qeth_core_root_dev);
6321         if (rc)
6322                 goto register_err;
6323         qeth_core_header_cache =
6324                 kmem_cache_create("qeth_hdr", QETH_HDR_CACHE_OBJ_SIZE,
6325                                   roundup_pow_of_two(QETH_HDR_CACHE_OBJ_SIZE),
6326                                   0, NULL);
6327         if (!qeth_core_header_cache) {
6328                 rc = -ENOMEM;
6329                 goto slab_err;
6330         }
6331         qeth_qdio_outbuf_cache = kmem_cache_create("qeth_buf",
6332                         sizeof(struct qeth_qdio_out_buffer), 0, 0, NULL);
6333         if (!qeth_qdio_outbuf_cache) {
6334                 rc = -ENOMEM;
6335                 goto cqslab_err;
6336         }
6337         rc = ccw_driver_register(&qeth_ccw_driver);
6338         if (rc)
6339                 goto ccw_err;
6340         rc = ccwgroup_driver_register(&qeth_core_ccwgroup_driver);
6341         if (rc)
6342                 goto ccwgroup_err;
6343 
6344         return 0;
6345 
6346 ccwgroup_err:
6347         ccw_driver_unregister(&qeth_ccw_driver);
6348 ccw_err:
6349         kmem_cache_destroy(qeth_qdio_outbuf_cache);
6350 cqslab_err:
6351         kmem_cache_destroy(qeth_core_header_cache);
6352 slab_err:
6353         root_device_unregister(qeth_core_root_dev);
6354 register_err:
6355         qeth_unregister_dbf_views();
6356 dbf_err:
6357         pr_err("Initializing the qeth device driver failed\n");
6358         return rc;
6359 }
6360 
6361 static void __exit qeth_core_exit(void)
6362 {
6363         qeth_clear_dbf_list();
6364         ccwgroup_driver_unregister(&qeth_core_ccwgroup_driver);
6365         ccw_driver_unregister(&qeth_ccw_driver);
6366         kmem_cache_destroy(qeth_qdio_outbuf_cache);
6367         kmem_cache_destroy(qeth_core_header_cache);
6368         root_device_unregister(qeth_core_root_dev);
6369         qeth_unregister_dbf_views();
6370         pr_info("core functions removed\n");
6371 }
6372 
6373 module_init(qeth_core_init);
6374 module_exit(qeth_core_exit);
6375 MODULE_AUTHOR("Frank Blaschka <frank.blaschka@de.ibm.com>");
6376 MODULE_DESCRIPTION("qeth core functions");
6377 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */